# tavolop3/chat architecture

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

- Interactive diagram: https://gitdiagram.com/tavolop3/chat
- Repository: https://github.com/tavolop3/chat

## Overview

- Client (client.c)
- Server (server.c)
- Dynamic Array Service (darray from lib/darray.h) used within the server to store and manage user connections.
- Epoll-based multiplexing mechanism
- Threading modules (epoll threads handling connections and a main thread accepting new connections)

##### Core architecture (focus on client and server, without tests)
- The Client:
- As one separate entity that initiates a connection to the server.
- That the client sends messages and receives broadcast messages from the server.
- The Server:
- A component that uses epoll to monitor multiple file descriptors for incoming communications.
- How the server accepts connections on a primary (main) thread and delegates communication to worker threads (handle threads).
- Use of epoll as a central element where all threads (or at least a thread pool) are monitoring for activity.
- Role of the dynamic array (darray) for storing information about connected clients and facilitating message broadcast.

##### Incorporate specific technical elements
- “epoll” as an external mechanism or subsystem that ties into the server’s threading model.
- “threading” by showing multiple threads interacting with the epoll instance.
- Optionally, include annotations like “non-blocking I/O” or “efficient handling of high load,” which are central design principles for this project.

##### Summarize the dynamic
- The Client sends a message over a TCP socket to the Server.
- The Server’s main thread accepts the connection and registers the client's socket with the epoll instance.
- A pool of worker threads, each waiting on the epoll, picks up the incoming message events.
- Upon receiving a message, the server uses the darray-managed list of connections to determine targets and broadcasts the message to every connected client.
- If a client sends a special command ("/u <username>"), the change is processed by the server, possibly updating its state in the darray.

## Diagram (Mermaid)

```mermaid
flowchart TD
    client_node("Client (client.c)"):::clientStyle
    server_node("Server (server.c)"):::serverStyle

    client_node -->|"TCP_Socket"| server_node

    subgraph "Server Internals"
        main_thread("Main Thread (Connection Acceptor)"):::threadStyle
        epoll_node("Epoll Module (epoll)"):::networkStyle
        worker_thread("Worker Threads"):::threadStyle
        darray_node("Dynamic Array Service (darray)"):::serviceStyle
    end

    server_node -->|"Delegates_Connections"| main_thread
    main_thread -->|"Registers_with_epoll"| epoll_node
    epoll_node -->|"Dispatches_Events"| worker_thread
    worker_thread -->|"Uses_darray"| darray_node
    worker_thread -->|"TCP_Socket_(Broadcast)"| client_node

    click client_node "https://github.com/tavolop3/chat/blob/main/client.c"
    click server_node "https://github.com/tavolop3/chat/blob/main/server.c"
    click main_thread "https://github.com/tavolop3/chat/blob/main/server.c"
    click epoll_node "https://github.com/tavolop3/chat/blob/main/server.c"
    click worker_thread "https://github.com/tavolop3/chat/blob/main/server.c"
    click darray_node "https://github.com/tavolop3/chat/blob/main/lib/darray.h"

    classDef clientStyle fill:#cfe2f3,stroke:#0b5394,stroke-width:2px;
    classDef serverStyle fill:#f4cccc,stroke:#b45f06,stroke-width:2px;
    classDef threadStyle fill:#d9ead3,stroke:#38761d,stroke-width:2px;
    classDef networkStyle fill:#fff2cc,stroke:#bf9000,stroke-width:2px;
    classDef serviceStyle fill:#ead1dc,stroke:#833c7c,stroke-width:2px;
```
