# muxy03/progetto-finale architecture

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

- Interactive diagram: https://gitdiagram.com/muxy03/progetto-finale
- Repository: https://github.com/muxy03/progetto-finale

## Overview

##### Project Type and Purpose
- This is a distributed, concurrent client-server/archive system:
- Clients (two C programs: “type A” and “type B”) send requests over TCP.
- A Python server accepts socket connections and forwards each line (prefixed by its length) into a set of POSIX FIFOs.
- An “archivio” C program reads from those FIFOs, spawns threads (writers, readers, and their “capo” supervisors), manages a linked list buffer, and writes/archives the data.

##### File Structure
- Top-level files:
- client1.c, client2.c         ← two client implementations
- server.py                    ← central TCP acceptor & FIFO writer
- archivio.c                   ← archive manager: thread pool + linked list
- progetto.c / progetto.h      ← shared definitions, data structures (Buffer, GArg, SLArg, CArg, Node)
- makefile                     ← build rules
- file1, file2, file3          ← output archives or test inputs
- Key IPC/Concurrency artifacts:
- POSIX sockets (HOST, PORT in progetto.h)
- Named FIFOs for “type A” vs “type B” channels
- pthreads (writers, readers, capo threads)
- Mutexes for list/head/tot synchronization
- Signal handler for graceful shutdown (writing “2049” into FIFOs)

##### README for Architecture Insights
- Server protocol: prefix messages by length (ASCII), clients choose “0” or “1” to select FIFO A or B.
- Archivio uses five custom structs (Buffer, GArg, SLArg, CArg, Node) to manage its thread-pool pipeline.
- A global linked list holds pending records; two globals (head, tot) track the queue.
- Signal handler safely prints/logs and flips a “fine” flag under mutex.

a. Main Components
1. Clients
- Client A (C)
- Client B (C)
2. Server (Python)
3. Archivio (C)
- Signal Handler
- Dispatcher (“Gestore”) threads
- Scrittore threads + Capo-Scrittore
- Lettore threads + Capo-Lettore
- Linked-List buffer + Mutex (ht_mutex)
4. Archives (file1, file2, file3)
b. Relationships & Data Flows
1. Clients → TCP Socket → Server
2. Server → writes “length” + “line” messages → Named FIFO A or FIFO B
3. Archivio:
– FIFO reader (“Gestore”) threads read FIFO A/B → enqueue Node into linked list
– Scrittore threads dequeue, write to file archives
– Lettore threads may perform read/validation
– Capo threads supervise worker threads, handle termination signal
4. Signal Handler injects termination messages (“2049”) into FIFOs and sets `fine=1`
c. Architectural Patterns & Design Principles
- Producer–Consumer pipeline via FIFOs + thread queue
- Thread-pool pattern (workers + “capo” supervisors)
- Shared linked-list protected by mutex (ht_mutex)
- Graceful shutdown via signal + sentinel message
d. Key Technologies
- POSIX sockets (TCP)
- Named FIFOs (mkfifo)
- pthreads + mutexes
- C data structures (Buffer, Node)
- Python for socket acceptor

##### Essential Elements
- Annotations on key data structures (e.g., Buffer struct, Node struct).

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Clients Layer
    subgraph "Clients"
        direction TB
        ClientA["Client A (C)"]:::client
        ClientB["Client B (C)"]:::client
    end

    %% Network to Server
    ClientA -->|"TCP connect"| Server["Server (Python)"]:::server
    ClientB -->|"TCP connect"| Server

    %% Server writes to FIFOs
    subgraph "IPC Channels"
        direction TB
        FIFO_A["FIFO A"]:::ipc
        FIFO_B["FIFO B"]:::ipc
    end
    Server -->|"write length+line\n(select 0) "| FIFO_A
    Server -->|"write length+line\n(select 1) "| FIFO_B

    %% Archivio Layer
    subgraph "Archivio (C)" 
        direction TB
        SignalHandler["Signal Handler"]:::concurrency
        Gestore["Gestore Threads\n(FIFO Readers)"]:::concurrency
        LinkedList["Linked-List Buffer\n(head, tot)"]:::concurrency
        Scrittore["Scrittore Threads\n(Workers)"]:::concurrency
        Lettore["Lettore Threads\n(Workers)"]:::concurrency
        CapoScr["Capo-Scrittore Supervisor"]:::concurrency
        CapoLet["Capo-Lettore Supervisor"]:::concurrency
    end

    %% Data Flows within Archivio
    FIFO_A -->|"enqueue node"| Gestore
    FIFO_B -->|"enqueue node"| Gestore
    Gestore -->|"enqueue node"| LinkedList
    LinkedList -->|"dequeue"| Scrittore
    LinkedList -->|"dequeue"| Lettore
    Scrittore -->|"write archive"| Storage
    Lettore -->|"validate/read"| Storage
    SignalHandler -.->|"send 2049 sentinel"| FIFO_A
    SignalHandler -.->|"send 2049 sentinel"| FIFO_B
    SignalHandler -.->|"set fine=1"| LinkedList
    Scrittore --> CapoScr
    Lettore --> CapoLet

    %% Storage Layer
    subgraph "Archive Storage"
        direction TB
        file1["file1"]:::storage
        file2["file2"]:::storage
        file3["file3"]:::storage
    end
    Storage --- file1
    Storage --- file2
    Storage --- file3

    %% Documentation and Build
    README["Project README"]:::storage
    MakefileNode["Makefile"]:::storage

    %% Click Events
    click ClientA "https://github.com/muxy03/progetto-finale/blob/main/client1.c"
    click ClientB "https://github.com/muxy03/progetto-finale/blob/main/client2.c"
    click Server "https://github.com/muxy03/progetto-finale/blob/main/server.py"
    click Gestore "https://github.com/muxy03/progetto-finale/blob/main/archivio.c"
    click SignalHandler "https://github.com/muxy03/progetto-finale/blob/main/archivio.c"
    click LinkedList "https://github.com/muxy03/progetto-finale/blob/main/progetto.c"
    click MakefileNode "https://github.com/muxy03/progetto-finale/tree/main/makefile"
    click file1 "https://github.com/muxy03/progetto-finale/tree/main/file1"
    click file2 "https://github.com/muxy03/progetto-finale/tree/main/file2"
    click file3 "https://github.com/muxy03/progetto-finale/tree/main/file3"
    click README "https://github.com/muxy03/progetto-finale/blob/main/README.md"

    %% Styles
    classDef client fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef server fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef ipc fill:#90EE90,stroke:#000,stroke-width:1px
    classDef concurrency fill:#FFA500,stroke:#000,stroke-width:1px
    classDef storage fill:#D3D3D3,stroke:#000,stroke-width:1px
```
