# lunar-huang/a-distributed-flight-information-system architecture

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

- Interactive diagram: https://gitdiagram.com/lunar-huang/a-distributed-flight-information-system
- Repository: https://github.com/lunar-huang/a-distributed-flight-information-system

## Overview

##### Main Components
- Server (C-based):
- Core UDP server (server.c) which listens for incoming client requests and spawns new threads per request.
- Flight Service – implemented in flight_service.c and flight_service.h – handling queries, reservations, and business logic.
- Callback Handler – manages client callback registrations to push updates (updates on seat availability).
- Data Storage – modules in data_storage.c (and related SQL scripts) that interface with the MariaDB database.
- Thread Pool – implemented via thread_pool.c to support concurrent request handling.
- Marshalling/Unmarshalling – these modules (marshalling.c and unmarshalling.c) transform data between C-struct representations and byte streams.
- Database Connect and SQL integration – see database_connect.c, database_insert.sql and flight_system.sql.

- Client (Java-based):
- Client GUI and logic – with files Client.java, Flight.java, Message.java, and UserInterface.java which capture the client-side interface.
- Callback mechanism interface – enabling the server to notify the client of changes.

- Communication Infrastructure:
- The message structure encoding (including message type, request id, data_length, and flight data) which ties client and server communications.
- UDP socket communication with directional flows.

- External/Supporting Components:
- Database (MariaDB) hosting the flights table and supporting SQL operations.
- Configuration for fault tolerance that selects between “at-least-once” and “at-most-once” semantics.
- Build scripts and Makefile in your server folder for compiling and linking.

##### Relationships and Data Flow
- The client initiates requests via its GUI. These requests are constructed into the defined message structure (with marshalling handled by Java code on the client side) and sent over UDP to the server.
- The Server receives these messages through its socket interface and spawns a new thread (or uses a thread from the thread pool) to handle each incoming request.
- Inside the server, the request is processed by the flight service and possibly routed through modules:
- For queries and reservation requests, the service interacts with the database via data_storage and database_connect modules.
- Marshalling/unmarshalling layers transform data between network byte streams and internal C structures.
- Callback functionality: when seat or baggage availability changes, the callback_handler is invoked to communicate updates back to registered clients.
- The server then sends a reply (remarshalled into a byte message) back to the client over UDP.
- The database component is central to storing flight details and is updated based on operations (e.g., making seat reservations).

##### Architectural Patterns & Design Principles
- Modular Design: Each functional aspect (communication, flight service logic, data persistence, thread management, marshalling) is implemented in its own module.
- Client-Server Pattern: With clear separation of concerns between the Java-based client (presentation, request generation, callback reception) and C-based server (business logic and data processing).
- Concurrency: The use of multi-threading and thread pools ensures the server can handle multiple simultaneous client connections.
- Fault Tolerance: The design provides alternative request handling semantics (at-least-once vs. at-most-once) to ensure resiliency.
- Network Communication Focus: Using UDP sockets and a clear message protocol, with marshalling routines to ensure data consistency.

##### Additional Details to Emphasize
- Notation for fault tolerance choices (e.g., indicate on the server block that it supports at-least-once and at-most-once processing strategies).
- Multi-threading aspect in the server by depicting a “Thread Pool” sub-component.
- Mention the significance of the marshalling/unmarshalling modules that convert messages between byte streams and application-level data.
- That the system uses UDP sockets for network communication and illustrate how the client's requests are handled concurrently by the server.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Client Layer
    subgraph "Client Application (Java)"
        C1["Client Main Logic"]:::client
        C2["Flight Data"]:::client
        C3["Message Formatter"]:::client
        C4["Graphical User Interface"]:::client
        C5["Client-Side Marshalling"]:::client
    end

    %% Server Layer
    subgraph "Server Application (C)"
        S1["UDP Listener"]:::server
        S5["Thread Pool Executor"]:::server
        S2["Flight Service"]:::server
        S3["Callback Handler"]:::server
        S4["Data Storage"]:::server
        S4a["Database Insert SQL"]:::server
        S4b["Flight System SQL"]:::server
        S6["Marshalling Module"]:::server
        S7["Unmarshalling Module"]:::server
        S8["Database Connectivity"]:::server
        S9["Communication Definitions"]:::server
        Note["FaultToler:AtLeastOnce/AtMostOnce"]:::serverNote
    end

    %% Database Layer
    subgraph "Database (MariaDB)"
        DB["Flights Table"]:::db
    end

    %% Client Internal Connections
    C4 -->|"initiates"| C1
    C2 -->|"providesData"| C1
    C3 -->|"formatsMessage"| C5
    C5 -->|"sendsMarshalledMsg"| C1

    %% Client to Server Communication
    C1 -->|"sendRequest(UDP)"| S1

    %% Server Internal Processing
    S1 -->|"spawnsThread"| S5
    S1 -->|"delegatesTo"| S2
    S1 -->|"marshalData"| S6
    S1 -->|"unmarshalData"| S7
    S1 -->|"protocolDefinedBy"| S9
    S2 -->|"processesReqs"| S4
    S4 -->|"executes"| S8
    S4 -->|"usesScript"| S4a
    S4 -->|"usesScript"| S4b
    S8 -->|"queriesUpdates"| DB
    S2 -- "callbackUpdate"--> S3
    S3 -- "sendsAsyncUpdate"--> C4
    S1 -->|"sendsResponse(UDP)"| C1

    %% Click Events for Server Components
    click S1 "https://github.com/lunar-huang/a-distributed-flight-information-system/blob/main/src/server_c/server.c"
    click S2 "https://github.com/lunar-huang/a-distributed-flight-information-system/blob/main/src/server_c/flight_service.c"
    click S3 "https://github.com/lunar-huang/a-distributed-flight-information-system/blob/main/src/server_c/callback_handler.c"
    click S4 "https://github.com/lunar-huang/a-distributed-flight-information-system/blob/main/src/server_c/data_storage.c"
    click S4a "https://github.com/lunar-huang/a-distributed-flight-information-system/blob/main/src/server_c/database_insert.sql"
    click S4b "https://github.com/lunar-huang/a-distributed-flight-information-system/blob/main/src/server_c/flight_system.sql"
    click S5 "https://github.com/lunar-huang/a-distributed-flight-information-system/blob/main/src/server_c/thread_pool.c"
    click S6 "https://github.com/lunar-huang/a-distributed-flight-information-system/blob/main/src/server_c/marshalling.c"
    click S7 "https://github.com/lunar-huang/a-distributed-flight-information-system/blob/main/src/server_c/unmarshalling.c"
    click S8 "https://github.com/lunar-huang/a-distributed-flight-information-system/blob/main/src/server_c/database_connect.c"
    click S9 "https://github.com/lunar-huang/a-distributed-flight-information-system/blob/main/src/server_c/communication.h"

    %% Click Events for Client Components
    click C1 "https://github.com/lunar-huang/a-distributed-flight-information-system/blob/main/src/src/Client.java"
    click C2 "https://github.com/lunar-huang/a-distributed-flight-information-system/blob/main/src/src/Flight.java"
    click C3 "https://github.com/lunar-huang/a-distributed-flight-information-system/blob/main/src/src/Message.java"
    click C4 "https://github.com/lunar-huang/a-distributed-flight-information-system/blob/main/src/src/UserInterface.java"
    click C5 "https://github.com/lunar-huang/a-distributed-flight-information-system/blob/main/src/src/Marshalling.java"

    %% Styles
    classDef client fill:#ccffcc,stroke:#006600,stroke-width:2px;
    classDef server fill:#cce5ff,stroke:#003399,stroke-width:2px;
    classDef db fill:#ffe6cc,stroke:#993300,stroke-width:2px;
    classDef serverNote fill:#ffcccc,stroke:#cc0000,stroke-width:2px;
```
