# sua0649/os-project2025 architecture

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

- Interactive diagram: https://gitdiagram.com/sua0649/os-project2025
- Repository: https://github.com/sua0649/os-project2025

## Overview

##### Project type and purpose
- This is a C-based, multi-threaded client-server chat application (“NERV Terminal”) that allows multiple clients to connect to a centralized server, exchange broadcast and private messages, and optionally render a terminal or GTK GUI.
- The README confirms use of POSIX sockets, Pthreads for concurrency, mutexes/condition variables for synchronization, and an optional NCurses/GTK UI.

##### File structure
- Top-level:
- client.c / client.h: core client logic (connect, send/receive broadcast/private)
- client_p2p.c / client_p2p.h: peer-to-peer client variant
- server.c / server.h: central server, connection acceptor, thread‐pool manager
- Utils/cJSON.c & .h: JSON encode/decode utility used by both client and server
- makefile: build recipes for client, client_p2p, server
- Executables (client.exe, client_p2p.exe, server.exe)
- Patterns:
- Client‐Server separation
- Thread‐pool on the server (producer–consumer queue with condition variables)
- Shared JSON library for message serialization

##### Incorporate README architecture hints

a. Main components:
- Client Application (client & client_p2p modules)
- Server Application (listener, dispatcher, thread-pool)
- Shared Utils Library (cJSON)
- Underlying OS Services (POSIX Sockets, Pthread)
- Optional UI Layer (NCurses/GTK)
- Data Store (in-memory message queue guarded by mutex/condvar)
b. Relationships & interactions:
- Clients ⇄ Server via TCP sockets
- Server listener enqueues incoming messages into a synchronized queue
- Thread‐pool workers dequeue messages, parse JSON, route to all or target client socket(s)
- JSON library invoked at both ends for serialization/deserialization
- UI Layer hooks into client send/receive routines
c. Architectural patterns & design principles:
- Client–Server architecture
- Thread‐pool (fixed worker threads) implementing producer–consumer pattern
- Modular separation: networking, business logic, UI, utilities
- Use of mutexes and condition variables for safe queue access
d. Key technologies/frameworks:
- C language
- POSIX Socket API
- Pthread Library (pthread_mutex_t, pthread_cond_t)
- cJSON (third-party JSON parser)
- Optional UI: NCurses or GTK

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Underlying OS Services"
        POSIX["POSIX Sockets"]:::os
        PTHREAD["Pthread Library"]:::os
    end

    subgraph "Shared Utility Library"
        CJSON_C["cJSON.c"]:::util
        CJSON_H["cJSON.h"]:::util
    end

    subgraph "Client Application"
        direction TB
        ClientC["client.c"]:::client
        ClientH["client.h"]:::client
        PeerC["client_p2p.c"]:::client
        PeerH["client_p2p.h"]:::client
        UI["UI Layer (NCurses/GTK)"]:::ui
    end

    subgraph "Server Application"
        direction TB
        Listener["Server Listener (server.c)"]:::server
        ServerH["server.h"]:::server
        Queue["Message Queue"]:::queue
        Workers["Thread Pool Workers"]:::server
        Dispatcher["Dispatcher Logic"]:::server
    end

    subgraph "Build & Executables"
        direction TB
        Make["makefile"]:::build
        ExecClient["client.exe"]:::exec
        ExecPeer["client_p2p.exe"]:::exec
        ExecServer["server.exe"]:::exec
    end

    POSIX --> Listener
    PTHREAD --> Listener
    POSIX --> ClientC
    PTHREAD --> ClientC

    ClientC -->|"JSON over TCP"| Listener
    PeerC -->|"JSON over TCP"| Listener
    Listener -->|"enqueue"| Queue
    Queue -->|"dequeue"| Workers
    Workers --> Dispatcher
    Dispatcher -->|"send JSON over TCP"| ClientC
    Dispatcher -->|"send JSON over TCP"| PeerC

    ClientC --> UI
    PeerC --> UI

    ClientC --> CJSON_C
    ClientC --> CJSON_H
    PeerC --> CJSON_C
    PeerC --> CJSON_H
    Listener --> CJSON_C
    Listener --> CJSON_H
    Dispatcher --> CJSON_C
    Dispatcher --> CJSON_H

    Make --> ExecClient
    Make --> ExecPeer
    Make --> ExecServer
    Make --> ClientC
    Make --> PeerC
    Make --> Listener
    Make --> CJSON_C

    click ClientC "https://github.com/sua0649/os-project2025/blob/main/client.c"
    click ClientH "https://github.com/sua0649/os-project2025/blob/main/client.h"
    click PeerC "https://github.com/sua0649/os-project2025/blob/main/client_p2p.c"
    click PeerH "https://github.com/sua0649/os-project2025/blob/main/client_p2p.h"
    click Listener "https://github.com/sua0649/os-project2025/blob/main/server.c"
    click ServerH "https://github.com/sua0649/os-project2025/blob/main/server.h"
    click CJSON_C "https://github.com/sua0649/os-project2025/blob/main/Utils/cJSON.c"
    click CJSON_H "https://github.com/sua0649/os-project2025/blob/main/Utils/cJSON.h"
    click Make "https://github.com/sua0649/os-project2025/tree/main/makefile"

    classDef client fill:#AEDFF7,stroke:#036B79;
    classDef server fill:#FFD5D5,stroke:#B22222;
    classDef util fill:#FFF5BA,stroke:#E1AD01;
    classDef os fill:#D3D3D3,stroke:#808080;
    classDef queue fill:#C8E6C9,stroke:#388E3C,stroke-dasharray: 5 5;
    classDef ui fill:#E1BEE7,stroke:#8E24AA;
    classDef build fill:#E0E0E0,stroke:#616161;
    classDef exec fill:#FFECB3,stroke:#FFA000;
```
