# tranlam2004/peer-to-peer-sharing architecture

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

- Interactive diagram: https://gitdiagram.com/tranlam2004/peer-to-peer-sharing
- Repository: https://github.com/tranlam2004/peer-to-peer-sharing

## Overview

##### Main Components
- Tracker: Acts as a central coordinator, managing communication between the Seeder and the Peers.
- Seeder: Responsible for uploading (sharing) files and serving them to Peers.
- Peers: Nodes that request and download files from the Seeder (and potentially other Peers).
- CLI Interface: Facilitates all user commands and interactions (e.g., file creation, upload, download, and info display).
- Supporting Modules: Components such as PeerConnection, Downloader, Upload, Torrent, and utility modules that encapsulate the core functionalities and communication logic.
- External Dependencies: Although not in the file tree, the README mentions tools like Python, Docker, Pip, and MySQL. (Even if MySQL is not explicitly represented in the file list, include it as an external component if it is used for metadata management).

##### Architectural Relationships and Interactions
- The CLI serves as the entry point for user commands, triggering actions like torrent file creation (using Torrent.py), uploading (handled by Upload.py), or downloading files (via Downloader.py).
- The Tracker sits in the center of the file-sharing network, managing discovery and coordination between the Seeder and Peers. It receives status or update messages (possibly via modules such as sendMessage.py) and informs nodes about available resources.
- PeerConnection handles the communication protocols among peers, ensuring proper data exchange.
- The Seeder shares files across the network in response to download requests received either directly from Peers or via Tracker-mediated discovery.
- Peers initiate downloads by querying the Tracker to get lists or metadata of available torrents and then connecting to the Seeder to retrieve file data.

##### Architectural Patterns and Design Principles
- P2P Architecture with a central coordination element (Tracker) which simplifies discovery in a LAN-based network.
- Modular design: Each file (e.g., Downloader.py, Upload.py, Torrent.py) encapsulates specific functionality, improving separation of concerns.
- CLI-driven control ensures that each role (Tracker, Seeder, Peer) can be started or managed individually on different devices as outlined in the README.
- Extensibility: Modules are decoupled, so enhancements (such as adding error handling, improved peer selection, or additional metadata handling) can be implemented without major changes in the overall structure.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Interface Layer
    subgraph "User Interface Layer"
        CLI["CLI Interface"]:::cli
        TMG["Torrent Management"]:::module
        UTIL["Supporting Utilities"]:::module
    end

    %% Coordinator Layer
    subgraph "Coordinator Layer"
        TRACKER["Tracker Service"]:::tracker
        MSG["Message Dispatch"]:::module
    end

    %% Seeder Node
    subgraph "Seeder Node"
        SEED["Seeder Service"]:::seeder
        UP["Uploader Module"]:::module
        FILES["File Structure Management"]:::module
        PC1["Peer Communication"]:::module
    end

    %% Peer Node
    subgraph "Peer Node"
        PEER["Peer Service"]:::peer
        DL["Downloader Module"]:::module
        PC2["Peer Communication"]:::module
    end

    %% External Dependencies
    subgraph "External Dependencies"
        MYSQL[(MySQL Database)]:::external
        DOCKER[(Docker)]:::external
        PYTHON[(Python)]:::external
    end

    %% Connections from User Layer
    CLI -->|"torrent/create"| TMG
    CLI -->|"upload"| UP
    CLI -->|"download"| DL
    CLI -->|"info_display"| MSG

    %% Torrent Management and Utilities interactions
    TMG -->|"generate_torrent"| TRACKER
    UTIL --- TMG
    UTIL --- UP
    UTIL --- DL
    UTIL --- MSG

    %% Tracker interactions
    MSG -->|"update_registry"| TRACKER
    TRACKER -->|"announce_peer"| SEED
    TRACKER -->|"peer_lookup"| PEER
    TRACKER -->|"metadata_sync"| MYSQL

    %% Seeder Node interactions
    UP -->|"share_file"| PC1
    PC1 -->|"connects_to"| SEED

    %% Peer Node interactions
    DL -->|"request_file"| PC2
    PC2 -->|"establish_conn"| PEER

    %% Click Events
    click CLI "https://github.com/tranlam2004/peer-to-peer-sharing/blob/main/Cli.py"
    click TMG "https://github.com/tranlam2004/peer-to-peer-sharing/blob/main/Torrent.py"
    click UP "https://github.com/tranlam2004/peer-to-peer-sharing/blob/main/Upload.py"
    click DL "https://github.com/tranlam2004/peer-to-peer-sharing/blob/main/Downloader.py"
    click PC1 "https://github.com/tranlam2004/peer-to-peer-sharing/blob/main/PeerConnection.py"
    click PC2 "https://github.com/tranlam2004/peer-to-peer-sharing/blob/main/PeerConnection.py"
    click MSG "https://github.com/tranlam2004/peer-to-peer-sharing/blob/main/sendMessage.py"
    click UTIL "https://github.com/tranlam2004/peer-to-peer-sharing/blob/main/utils.py"
    click FILES "https://github.com/tranlam2004/peer-to-peer-sharing/blob/main/FileStructure.py"

    %% Styles
    classDef cli fill:#add8e6,stroke:#000,stroke-width:2px;
    classDef tracker fill:#ffa500,stroke:#000,stroke-width:2px;
    classDef seeder fill:#90ee90,stroke:#000,stroke-width:2px;
    classDef peer fill:#dda0dd,stroke:#000,stroke-width:2px;
    classDef module fill:#f0e68c,stroke:#000,stroke-width:2px;
    classDef external fill:#ff7f7f,stroke:#000,stroke-width:2px;
```
