# aitorblanco03/distribuidos-p1-sockets architecture

> Architecture diagram of the GitHub repository aitorblanco03/distribuidos-p1-sockets, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/aitorblanco03/distribuidos-p1-sockets
- Repository: https://github.com/aitorblanco03/distribuidos-p1-sockets

## Overview

##### Project type and purpose
- This is a Java-based, console chat application using TCP sockets.
- Purpose: real-time multi-user chat within a LAN, supporting broadcast messaging, per-user blocking, safe disconnect, and thread-based concurrency.

##### File structure
- Top-level:
- pom.xml, build.xml – Maven and Ant build definitions
- src/main/java/es/ubu/lsi
– client: ChatClient.java / ChatClientImpl.java
– server: ChatServer.java / ChatServerImpl.java
– common: ChatMessage.java, MessageType.java
- src/test/java: AppTest.java
- doc/: Generated Javadoc site
- No database directory; it’s pure in-memory, socket-based.
- Concurrency artifacts: ChatServerImpl uses threads, read/write locks.

##### README for additional insights
- Three core modules: Server, Client, Message model.
- Server handles incoming connections, broadcasting, block lists, thread safety.
- Client connects to server, spawns threads for input/output, processes commands.
- Messages carry sender, payload, type (broadcast, ban, logout).

##### Components
a. Main components
1. ChatClient instances (multiple)
- Subcomponents:
- User Input Handler (stdin → ChatMessage)
- Network I/O Handler (socket read/write)
- Command Processor (ban/unban/logout commands)
2. TCP Network (LAN)
3. ChatServer
- Subcomponents:
- Connection Listener (accepts new socket connections)
- ClientHandler Threads (one per client)
- Broadcast Service (forwards messages to all handlers)
- Block Manager (tracks banned/unbanned users)
- ReadWrite Locks (protect shared state)
4. Common Model (shared library)
- ChatMessage, MessageType

b. Relationships & interactions
- Clients open TCP socket to Server → Connection Listener spawns ClientHandler
- ClientHandler receives ChatMessage, passes it to Broadcast Service
- Broadcast Service iterates through active handlers, sending to each unless blocked
- ClientHandler writes message bytes back over socket → Client Network I/O receives and displays

c. Architectural patterns & design principles
- Client-Server pattern over TCP sockets
- Thread-per-connection concurrency model
- Read/Write locks for safe concurrent access to shared lists (clients & block lists)
- Message-based communication using serializable ChatMessage objects

d. Relevant technologies
- Java SE 17+ (java.net.Socket, java.net.ServerSocket, java.util.concurrent)
- Maven & Ant for build/dependency management
- Javadoc for API documentation

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client Runtime" 
        direction TB
        CLI_IF["ChatClient Interface"]:::client
        CLI_IMPL["ChatClientImpl"]:::client
        UI_HANDLER["User Input Handler"]:::client
        NET_IO["Network I/O Handler"]:::client
        CMD_PROC["Command Processor"]:::client
    end

    %% Network Boundary
    TCP["TCP Network (LAN)"]:::network

    %% Server Layer
    subgraph "Server Runtime"
        direction TB
        SV_IF["ChatServer Interface"]:::server
        SV_IMPL["ChatServerImpl"]:::server
        LISTENER["Connection Listener"]:::server
        HANDLER["ClientHandler Thread"]:::server
        BROADCAST["Broadcast Service"]:::server
        BLOCKMGR["Block Manager"]:::server
        LOCKS["Read/Write Locks"]:::server
    end

    %% Common Model
    subgraph "Shared Model Library"
        direction TB
        CM["ChatMessage"]:::model
        MT["MessageType"]:::model
    end

    %% Project Files
    subgraph "Project Files"
        direction TB
        POM["pom.xml"]:::file
        ANT["build.xml"]:::file
        TEST["AppTest.java"]:::file
        DIR_CLI["client/"]:::file
        DIR_SRV["server/"]:::file
        DIR_CMN["common/"]:::file
    end

    %% Relationships
    CLI_IMPL -->|implements| CLI_IF
    SV_IMPL -->|implements| SV_IF
    CLI_IMPL -->|uses| UI_HANDLER
    CLI_IMPL -->|uses| NET_IO
    CLI_IMPL -->|uses| CMD_PROC
    UI_HANDLER -->|creates ChatMessage| CM
    CMD_PROC -->|reads MessageType| MT

    CLI_IMPL -->|"opens socket"| TCP
    TCP -->|accepts connection| LISTENER
    LISTENER -->|spawns| HANDLER
    HANDLER -->|forwards ChatMessage| BROADCAST
    BROADCAST -->|iterates handlers| HANDLER
    BROADCAST -->|checks bans via| BLOCKMGR
    BLOCKMGR -->|protected by| LOCKS
    BROADCAST -->|"sends bytes over TCP"| TCP
    TCP -->|delivers to| NET_IO
    NET_IO -->|displays to user| UI_HANDLER

    %% Click Events
    click CLI_IF "https://github.com/aitorblanco03/distribuidos-p1-sockets/blob/main/src/main/java/es/ubu/lsi/client/ChatClient.java"
    click CLI_IMPL "https://github.com/aitorblanco03/distribuidos-p1-sockets/blob/main/src/main/java/es/ubu/lsi/client/ChatClientImpl.java"
    click SV_IF "https://github.com/aitorblanco03/distribuidos-p1-sockets/blob/main/src/main/java/es/ubu/lsi/server/ChatServer.java"
    click SV_IMPL "https://github.com/aitorblanco03/distribuidos-p1-sockets/blob/main/src/main/java/es/ubu/lsi/server/ChatServerImpl.java"
    click CM "https://github.com/aitorblanco03/distribuidos-p1-sockets/blob/main/src/main/java/es/ubu/lsi/common/ChatMessage.java"
    click MT "https://github.com/aitorblanco03/distribuidos-p1-sockets/blob/main/src/main/java/es/ubu/lsi/common/MessageType.java"
    click DIR_CLI "https://github.com/aitorblanco03/distribuidos-p1-sockets/tree/main/src/main/java/es/ubu/lsi/client/"
    click DIR_SRV "https://github.com/aitorblanco03/distribuidos-p1-sockets/tree/main/src/main/java/es/ubu/lsi/server/"
    click DIR_CMN "https://github.com/aitorblanco03/distribuidos-p1-sockets/tree/main/src/main/java/es/ubu/lsi/common/"
    click TEST "https://github.com/aitorblanco03/distribuidos-p1-sockets/blob/main/src/test/java/es/ubu/lsi/AppTest.java"
    click POM "https://github.com/aitorblanco03/distribuidos-p1-sockets/blob/main/pom.xml"
    click ANT "https://github.com/aitorblanco03/distribuidos-p1-sockets/blob/main/build.xml"

    %% Styles
    classDef client fill:#e0f7d4,stroke:#2d7a2d,color:#2d7a2d;
    classDef server fill:#d0e7ff,stroke:#215a8f,color:#215a8f;
    classDef model fill:#ffe5b4,stroke:#cc8400,color:#cc8400;
    classDef network fill:#f0f0f0,stroke:#999999,color:#666666,stroke-dasharray: 5 5;
    classDef file fill:#f9f9f9,stroke:#cccccc,color:#333333,stroke-dasharray: 2 2;
```
