# torchman005/simple-chat-room architecture

> Architecture diagram of the GitHub repository torchman005/simple-chat-room, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/torchman005/simple-chat-room
- Repository: https://github.com/torchman005/simple-chat-room

## Overview

##### Project type and purpose
- This is a classic client-server, socket-based chat application written in Java SE.
- It consists of two Maven modules:
- ChatServer – the server that accepts socket connections, manages client threads, routes messages.
- ChatClient – the desktop client (JavaFX UI) that connects to the server, sends/receives messages and files.
- A shared domain module (`chatcommon`) holds the Message, User, MessageType, UserType classes used by both.

##### High-level architecture analysis
- Top-level modules:
- chatcommon (shared DTOs)
- chatserver (server application)
- chatclient (client application)
- Key patterns:
- MVC-style layering in the client (UI ↔ service ↔ networking)
- Multi-threaded server: one thread per client, managed by a thread manager
- Simple in-memory data structures for groups and online-user lists (no persistence layer)

##### Main components
A. Client side
1. UI Layer (JavaFX):
– LoginUI, ChatUI, PrivateChatDialog, GroupChatDialog, FileSendDialog, etc.
2. Service Layer:
– ClientMessageService (package: service)
– FileClientService, ToUserFunction, Group utility
– ClientConnServerThreadsManage – coordinates client networking threads
3. Networking Layer:
– ClientConnectServerThread – maintains socket I/O with ChatServer
4. Resources:
– config.properties (server IP, port)
– styles.css, images for JavaFX theming

B. Shared Domain (chatcommon)
– Message, MessageType
– User, UserType
- These classes define the payload and metadata exchanged over sockets

C. Server side
1. Entry Point:
– ChatServer main class (socket listener on port 2323)
2. Thread Management:
– ServerConnectClientThread – one per connected client
– ClientThreadsManage – registry of live client threads
3. Business Logic:
– Groups – manages group-chat memberships
– OnlineUsers – tracks logged-in users
– SendNewsToAllService – broadcasts system messages
4. Admin UI:
– ChatFrame – simple JavaFX window showing server events/logs
5. Optional persistence (configured via JDBC if enabled)

##### Relationships and data flows
- Client UI → Service Layer → ClientConnectServerThread → Socket → Server’s ChatServer listener
- ServerChatServer accepts connection → spawns ServerConnectClientThread → registers with ClientThreadsManage
- Incoming Message DTOs flow:
- ServerConnectClientThread → dispatch logic → appropriate thread(s) → back to ClientConnectServerThread → socket out → ClientReceive
- File transfers piggyback on the same socket channel with file-specific MessageType
- Shared DTOs travel over the wire (no separate REST/API layer)

##### Architectural patterns & principles
- Layered architecture (presentation → business/service → networking)
- Single-responsibility classes (each service class handles one function)
- Thread-per-connection model on server
- Use of configuration file for decoupling environment settings
- Shared domain module prevents duplication

##### Technologies & frameworks
- JavaSE 17
- Java Sockets (java.net.Socket/ServerSocket)
- JavaFX for desktop UI
- Maven for build & dependency management
- JUnit for unit tests
- (Optional) JDBC/MySQL if persistence is added

- Arrowheads for data-flow direction
- Configuration externalized (config.properties)
- Call out JavaFX, Sockets, Maven

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client Side
    subgraph "Client (JavaFX Socket Client)"
        direction TB
        ChatMain["ChatMain"]:::client
        subgraph "UI Layer"
            direction TB
            LoginUI["LoginUI"]:::client
            ChatUI["ChatUI"]:::client
            PrivateChatDialog["PrivateChatDialog"]:::client
            GroupChatDialog["GroupChatDialog"]:::client
            FileSendDialog["FileSendDialog"]:::client
            CreateGroupDialog["CreateGroupDialog"]:::client
            BackgroundSelectDialog["BackgroundSelectDialog"]:::client
            RegisterUI["RegisterUI"]:::client
        end
        subgraph "Service Layer"
            direction TB
            ClientMessageService["ClientMessageService"]:::client
            FileClientService["FileClientService"]:::client
            GroupUtil["Group"]:::client
            ToUserFunction["ToUserFunction"]:::client
            ClientConnServerThreadsManage["ClientConnServerThreadsManage"]:::client
        end
        subgraph "Networking Layer"
            direction TB
            ClientConnectServerThread["ClientConnectServerThread"]:::client
        end
        ConfigClient["config.properties"]:::cylinder
        Stylesheet["styles.css"]:::cylinder
        Images["images/"]:::cylinder
    end

    %% Shared Domain
    subgraph "Shared Domain (chatcommon)"
        direction TB
        Message["Message"]:::shared
        MessageType["MessageType"]:::shared
        User["User"]:::shared
        UserType["UserType"]:::shared
    end

    %% Server Side
    subgraph "Server (Socket Chat Server)"
        direction TB
        ChatServerEntry["ChatServer"]:::server
        subgraph "Thread Management"
            direction TB
            ServerConnectClientThread["ServerConnectClientThread"]:::server
            ClientThreadsManage["ClientThreadsManage"]:::server
        end
        subgraph "Business Logic"
            direction TB
            Groups["Groups"]:::server
            OnlineUsers["OnlineUsers"]:::server
            SendNewsToAllService["SendNewsToAllService"]:::server
        end
        ChatFrame["ChatFrame"]:::server
        ConfigServer["config.properties"]:::cylinder
        UtilityServer["Utility"]:::server
    end

    %% Data Flows
    LoginUI -->|"user action"| ClientMessageService
    ChatUI -->|"send/receive"| ClientMessageService
    ClientMessageService -->|"prepare Message DTO"| ClientConnectServerThread
    FileClientService -->|"prepare File DTO"| ClientConnectServerThread
    ClientConnectServerThread -->|"socket send Message DTO"| ChatServerEntry
    ChatServerEntry -->|"spawn thread"| ServerConnectClientThread
    ServerConnectClientThread -->|"register"| ClientThreadsManage
    ServerConnectClientThread -->|"dispatch Message"| Groups
    ServerConnectClientThread -->|"dispatch Message"| OnlineUsers
    Groups -->|"update group"| ServerConnectClientThread
    OnlineUsers -->|"update users"| ServerConnectClientThread
    ServerConnectClientThread -->|"socket send DTO"| ClientConnectServerThread
    ClientConnectServerThread -->|"deliver Message"| ChatUI

    %% File flow
    FileSendDialog -->|"file request"| FileClientService
    FileClientService -->|"file DTO"| ClientConnectServerThread
    ClientConnectServerThread -->|"socket send File DTO"| ServerConnectClientThread
    ServerConnectClientThread -->|"socket send File DTO"| ClientConnectServerThread
    ClientConnectServerThread -->|"deliver File DTO"| FileSendDialog

    %% Shared DTO flows
    ClientConnectServerThread -->|"uses DTO classes"| Message
    ServerConnectClientThread -->|"uses DTO classes"| MessageType

    %% Click events
    click LoginUI "https://github.com/torchman005/simple-chat-room/blob/main/ChatClient/src/main/java/com/jinyu/ui/LoginUI.java"
    click ChatUI "https://github.com/torchman005/simple-chat-room/blob/main/ChatClient/src/main/java/com/jinyu/ui/ChatUI.java"
    click PrivateChatDialog "https://github.com/torchman005/simple-chat-room/blob/main/ChatClient/src/main/java/com/jinyu/ui/PrivateChatDialog.java"
    click GroupChatDialog "https://github.com/torchman005/simple-chat-room/blob/main/ChatClient/src/main/java/com/jinyu/ui/GroupChatDialog.java"
    click FileSendDialog "https://github.com/torchman005/simple-chat-room/blob/main/ChatClient/src/main/java/com/jinyu/ui/FileSendDialog.java"
    click CreateGroupDialog "https://github.com/torchman005/simple-chat-room/blob/main/ChatClient/src/main/java/com/jinyu/ui/CreateGroupDialog.java"
    click BackgroundSelectDialog "https://github.com/torchman005/simple-chat-room/blob/main/ChatClient/src/main/java/com/jinyu/ui/BackgroundSelectDialog.java"
    click RegisterUI "https://github.com/torchman005/simple-chat-room/blob/main/ChatClient/src/main/java/com/jinyu/ui/RegisterUI.java"
    click ClientMessageService "https://github.com/torchman005/simple-chat-room/blob/main/ChatClient/src/main/java/com/jinyu/chatclient/service/ClientMessageService.java"
    click FileClientService "https://github.com/torchman005/simple-chat-room/blob/main/ChatClient/src/main/java/com/jinyu/chatclient/service/FileClientService.java"
    click GroupUtil "https://github.com/torchman005/simple-chat-room/blob/main/ChatClient/src/main/java/com/jinyu/chatclient/service/Group.java"
    click ToUserFunction "https://github.com/torchman005/simple-chat-room/blob/main/ChatClient/src/main/java/com/jinyu/chatclient/service/ToUserFunction.java"
    click ClientConnServerThreadsManage "https://github.com/torchman005/simple-chat-room/blob/main/ChatClient/src/main/java/com/jinyu/chatclient/service/ClientConnServerThreadsManage.java"
    click ClientConnectServerThread "https://github.com/torchman005/simple-chat-room/blob/main/ChatClient/src/main/java/com/jinyu/chatclient/service/ClientConnectServerThread.java"
    click ChatMain "https://github.com/torchman005/simple-chat-room/blob/main/ChatClient/src/main/java/com/jinyu/main/ChatMain.java"
    click ConfigClient "https://github.com/torchman005/simple-chat-room/blob/main/ChatClient/src/main/resources/config.properties"
    click Stylesheet "https://github.com/torchman005/simple-chat-room/blob/main/ChatClient/src/main/resources/styles.css"
    click Images "https://github.com/torchman005/simple-chat-room/tree/main/ChatClient/src/main/resources/images"
    click Message "https://github.com/torchman005/simple-chat-room/blob/main/src/main/java/com/jinyu/chatcommon/Message.java"
    click MessageType "https://github.com/torchman005/simple-chat-room/blob/main/src/main/java/com/jinyu/chatcommon/MessageType.java"
    click User "https://github.com/torchman005/simple-chat-room/blob/main/src/main/java/com/jinyu/chatcommon/User.java"
    click UserType "https://github.com/torchman005/simple-chat-room/blob/main/src/main/java/com/jinyu/chatcommon/UserType.java"
    click ChatServerEntry "https://github.com/torchman005/simple-chat-room/blob/main/ChatServer/src/main/java/com/jinyu/chatserver/service/ChatServer.java"
    click ServerConnectClientThread "https://github.com/torchman005/simple-chat-room/blob/main/ChatServer/src/main/java/com/jinyu/chatserver/service/ServerConnectClientThread.java"
    click ClientThreadsManage "https://github.com/torchman005/simple-chat-room/blob/main/ChatServer/src/main/java/com/jinyu/chatserver/service/ClientThreadsManage.java"
    click Groups "https://github.com/torchman005/simple-chat-room/blob/main/ChatServer/src/main/java/com/jinyu/chatserver/service/Groups.java"
    click OnlineUsers "https://github.com/torchman005/simple-chat-room/blob/main/ChatServer/src/main/java/com/jinyu/chatserver/service/OnlineUsers.java"
    click SendNewsToAllService "https://github.com/torchman005/simple-chat-room/blob/main/ChatServer/src/main/java/com/jinyu/chatserver/service/SendNewsToAllService.java"
    click ChatFrame "https://github.com/torchman005/simple-chat-room/blob/main/ChatServer/src/main/java/com/jinyu/chatframe/ChatFrame.java"
    click ConfigServer "https://github.com/torchman005/simple-chat-room/blob/main/ChatServer/src/main/resources/config.properties"
    click UtilityServer "https://github.com/torchman005/simple-chat-room/blob/main/ChatServer/src/main/java/com/jinyu/utils/Utility.java"

    %% Styles
    classDef client fill:#ccffcc,stroke:#333,stroke-width:1px
    classDef shared fill:#eeeeee,stroke:#333,stroke-width:1px
    classDef server fill:#cce5ff,stroke:#333,stroke-width:1px
    classDef cylinder fill:#f2e6ff,stroke:#333,stroke-width:1px,shape:cylinder
    class LoginUI,ChatUI,PrivateChatDialog,GroupChatDialog,FileSendDialog,CreateGroupDialog,BackgroundSelectDialog,RegisterUI,ClientMessageService,FileClientService,GroupUtil,ToUserFunction,ClientConnServerThreadsManage,ClientConnectServerThread,ChatMain client
    class Message,MessageType,User,UserType shared
    class ChatServerEntry,ServerConnectClientThread,ClientThreadsManage,Groups,OnlineUsers,SendNewsToAllService,ChatFrame,UtilityServer server
    class ConfigClient,Stylesheet,Images,ConfigServer cylinder
```
