# mr-1504/p2p-file-sharing-system architecture

> Architecture diagram of the GitHub repository mr-1504/p2p-file-sharing-system, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-07-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mr-1504/p2p-file-sharing-system
- Repository: https://github.com/mr-1504/p2p-file-sharing-system

## Overview

##### Project Type and Purpose
- This is a Java-based desktop P2P file-sharing application with a central tracker. It uses JavaFX for the GUI and implements a hybrid client-server / peer-to-peer architecture:
- The Tracker is a TCP/UDP server that maintains peer & file metadata.
- Each Peer runs both client (to talk to the Tracker & download chunks) and server (to serve file chunks) roles.

##### File‐Structure Analysis
- Top‐level:
- lib/ – bundled JavaFX jars
- resources/ – config.properties, CSS, i18n resource bundles
- src/main/java/ – MVC-style packages:
– controller/ (P2PController)
– view/ (P2PView)
– model/ (PeerModel, TrackerModel, FileInfor, ClientState, etc.)
– utils/ (ConfigLoader, EnvConf, GetLocalIP, RequestInfor, Log, etc.)
– main/ (Main.java entry point)
- No web frontend: pure desktop.
- Configuration via .env or resources/config.properties.
- Launcher Main.java can fork a Tracker thread or start peers only.

##### README Insights
- Two core components: Peer and Tracker.
- Communication:
- Peer–Tracker over TCP (requests, registrations) + UDP (keep-alive).
- Peer–Peer over TCP (chunk transfer).
- File broken into chunks for parallel multi-source download.
- GUI provides search, share, download, real-time logging.

##### Detail Requirements
- Every essential subcomponent (TrackerModel logic, Peer server socket, ConfigLoader).
- Data store in Tracker (in‐memory file index) and note that it does not store file contents.
- How a user action (e.g., “Search file”) propagates from UI down to network and back.

- The dual-role nature of each Peer,
- The centralized role of the Tracker,
- The MVC structure of the desktop app,
- The chunked, multi-source transfer mechanism,
- And the protocols (TCP/UDP) that tie it all together.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Tracker Server
    subgraph "Tracker Server"
        direction TB
        TM["TrackerModel"]:::tracker
        TCP_T["TCP Listener\n(port 5001)"]:::tracker
        UDP_T["UDP Ping Listener\n(port 5002)"]:::tracker
        Store["In-Memory File Index"]:::tracker
        TM -->|"updates index"| Store
        TM -->|"listens"| TCP_T
        TM -->|-. UDP Ping/Pong .| UDP_T
    end

    %% Peer Application
    subgraph "Peer Application"
        direction TB
        Main["Main"]:::entry

        subgraph "MVC Layers"
            direction TB
            subgraph "View Layer"
                V["P2PView"]:::view
            end
            subgraph "Controller Layer"
                C["P2PController"]:::controller
            end
            subgraph "Model Layer"
                PM["PeerModel"]:::model
                FB["FileBase"]:::model
                FI["FileInfor"]:::model
                CS["ClientState"]:::model
                PI["PeerInfor"]:::model
            end
        end

        subgraph "Network Components"
            direction TB
            NC["Tracker RPC Client\n(TCP 5001)"]:::network
            SC["Chunk Server\n(TCP 6001)"]:::network
            HC["Heartbeat UDP\n(5002)"]:::network
        end

        subgraph "Utils & Config"
            direction TB
            CL["ConfigLoader"]:::util
            EC["EnvConf"]:::util
            IP["GetLocalIP"]:::util
            RI["RequestInfor"]:::util
            LG["Log"]:::util
            LT["LogTag"]:::util
            AP["AppPaths"]:::util
            IN["Infor"]:::util
        end

        Main -->|initialize| CL
        Main -->|initialize| EC
        Main -->|launch UI| V
        V -->|user action| C
        C -->|invokes| PM
        PM -->|register/search| NC
        PM -->|serve chunks| SC
        PM -->|send heartbeat| HC
        NC -->|TCP Register/Search| TM
        UDP_T -.->|UDP Ping/Pong| HC
        %% Peer-to-peer chunk download
        PM -->|requests chunk list| TM
        TM -->|metadata| PM
        PM -->|"parallel TCP\nchunk download"| PM

    end

    %% Configuration & Resources
    subgraph "Resources"
        direction TB
        CP["config.properties"]:::resource
        L1["labels_en.properties"]:::resource
        L2["labels_vi.properties"]:::resource
        M1["messages_en.properties"]:::resource
        M2["messages_vi.properties"]:::resource
        LANG["lang.properties"]:::resource
        CSS["style.css"]:::resource
    end

    %% Click Events
    click TM "https://github.com/mr-1504/p2p-file-sharing-system/blob/main/src/main/java/model/TrackerModel.java"
    click Main "https://github.com/mr-1504/p2p-file-sharing-system/blob/main/src/main/java/main/Main.java"
    click V "https://github.com/mr-1504/p2p-file-sharing-system/blob/main/src/main/java/view/P2PView.java"
    click C "https://github.com/mr-1504/p2p-file-sharing-system/blob/main/src/main/java/controller/P2PController.java"
    click PM "https://github.com/mr-1504/p2p-file-sharing-system/blob/main/src/main/java/model/PeerModel.java"
    click FB "https://github.com/mr-1504/p2p-file-sharing-system/blob/main/src/main/java/model/FileBase.java"
    click FI "https://github.com/mr-1504/p2p-file-sharing-system/blob/main/src/main/java/model/FileInfor.java"
    click CS "https://github.com/mr-1504/p2p-file-sharing-system/blob/main/src/main/java/model/ClientState.java"
    click PI "https://github.com/mr-1504/p2p-file-sharing-system/blob/main/src/main/java/model/PeerInfor.java"
    click CL "https://github.com/mr-1504/p2p-file-sharing-system/blob/main/src/main/java/utils/ConfigLoader.java"
    click EC "https://github.com/mr-1504/p2p-file-sharing-system/blob/main/src/main/java/utils/EnvConf.java"
    click IP "https://github.com/mr-1504/p2p-file-sharing-system/blob/main/src/main/java/utils/GetLocalIP.java"
    click RI "https://github.com/mr-1504/p2p-file-sharing-system/blob/main/src/main/java/utils/RequestInfor.java"
    click LG "https://github.com/mr-1504/p2p-file-sharing-system/blob/main/src/main/java/utils/Log.java"
    click LT "https://github.com/mr-1504/p2p-file-sharing-system/blob/main/src/main/java/utils/LogTag.java"
    click AP "https://github.com/mr-1504/p2p-file-sharing-system/blob/main/src/main/java/utils/AppPaths.java"
    click IN "https://github.com/mr-1504/p2p-file-sharing-system/blob/main/src/main/java/utils/Infor.java"
    click CP "https://github.com/mr-1504/p2p-file-sharing-system/blob/main/resources/config.properties"
    click L1 "https://github.com/mr-1504/p2p-file-sharing-system/blob/main/resources/lan/labels/labels_en.properties"
    click L2 "https://github.com/mr-1504/p2p-file-sharing-system/blob/main/resources/lan/labels/labels_vi.properties"
    click M1 "https://github.com/mr-1504/p2p-file-sharing-system/blob/main/resources/lan/messages/messages_en.properties"
    click M2 "https://github.com/mr-1504/p2p-file-sharing-system/blob/main/resources/lan/messages/messages_vi.properties"
    click LANG "https://github.com/mr-1504/p2p-file-sharing-system/blob/main/resources/lan/lang.properties"
    click CSS "https://github.com/mr-1504/p2p-file-sharing-system/blob/main/resources/style.css"

    %% Styles
    classDef tracker fill:#D0E6FF,stroke:#1E90FF,color:#000;
    classDef entry fill:#E8E8E8,stroke:#888,color:#000;
    classDef view fill:#FFF5BA,stroke:#FFD700,color:#000;
    classDef controller fill:#FFE4B5,stroke:#FFA500,color:#000;
    classDef model fill:#DFFFE0,stroke:#32CD32,color:#000;
    classDef network fill:#E8F5FF,stroke:#87CEEB,color:#000;
    classDef util fill:#F0F0F0,stroke:#AAA,color:#000;
    classDef resource fill:#F9F9F9,stroke:#CCC,color:#000;
```
