# niranjanblank/bittorrrent_client architecture

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

- Interactive diagram: https://gitdiagram.com/niranjanblank/bittorrrent_client
- Repository: https://github.com/niranjanblank/bittorrrent_client

## Overview

##### Main components
- Main Program Entry (main.cpp) – the startup point that ties all other components together.
- Torrent File Parsing – represented by the TorrentParser module along with the bencode decoding utilities.
- Peer Discovery – handled by the PeerDiscovery component, which finds and connects to peers.
- Handshake Management – represented by the HandshakeHandler which manages the initiation of peer connections.
- Peer Communication – managed by the PeerMessageHandler that handles ongoing message exchanges between peers.
- Download Management – through the PieceDownloader for piece handling and download orchestration.
- Networking Layer – managed by SocketManager and additional networking code (as seen in networking.hpp and httplib.h) for handling low-level socket communications.
- Utility Modules – includes sha1.hpp for hashing, common.hpp for shared constants/utilities, utils.hpp for helper functions, and structures.hpp for data definitions.
- External Libraries – note the inclusion of nlohmann’s JSON library for potential configuration or data processing needs.

##### Relationships and interactions
- The main entry point (main.cpp) initializes the overall client, calling into the TorrentParser to process .torrent files and extract tracker and file metadata.
- The TorrentParser then triggers the Tracker Communication to engage with trackers and gather peer lists.
- PeerDiscovery uses this data to establish network connections with available peers.
- Once a connection is attempted, the HandshakeHandler manages the initial handshake process.
- After establishing a successful handshake, the PeerMessageHandler takes over to exchange protocol-specific commands.
- The PieceDownloader coordinates the actual downloading process, interacting with peers concurrently (leveraging multi-threading) and utilizing SocketManager to manage the network sockets.
- Each downloaded piece’s integrity is confirmed using SHA-1 hashing (via sha1.hpp), and utility functions help maintain proper logging and error handling throughout.

##### Architectural patterns and design principles
- Modular design: The separation into discrete header files and subdirectories (like TorrentParser and its bencode submodule) indicates a clear modular and layered architecture.
- Single Responsibility: Each component (e.g., piece downloading, handshake management) handles one aspect of the BitTorrent protocol, which simplifies maintenance and testing.
- Concurrency: Given the mention of multi-threading, ensure that you note the threading model or concurrency handling between components (e.g., multiple threads managing peer connections).

## Diagram (Mermaid)

```mermaid
flowchart TD
    A["Main Program Entry (main.cpp)"]:::entry

    subgraph "Torrent Parsing Layer"
        TP["TorrentParser (Torrent File Parsing)"]:::parsing
        BE["Bencode Decoder"]:::parsing
        TC["Tracker Communication"]:::parsing
    end

    PD["Peer Discovery"]:::peer

    subgraph "Peer Communication"
        HH["Handshake Handler"]:::peer
        PM["Peer Message Handler"]:::peer
    end

    subgraph "Networking Layer"
        SM["Socket Manager"]:::networking
        NW["Networking (networking.hpp)"]:::networking
        HT["HTTP Library (httplib.h)"]:::networking
    end

    PDwn["Piece Downloader"]:::download

    subgraph "Utility Modules"
        UCommon["Common (common.hpp)"]:::utility
        USha1["SHA-1 (sha1.hpp)"]:::utility
        UUtils["Utils (utils.hpp)"]:::utility
        UStruct["Structures (structures.hpp)"]:::utility
    end

    EL["External Libraries (nlohmann/json)"]:::external

    %% Connections
    A -->|"initializes"| TP
    TP -->|"decodes"| BE
    TP -->|"triggers"| TC
    TC -->|"notifies"| PD
    PD -->|"initiates"| HH
    PD -->|"initiates"| PM
    HH -->|"establishes"| SM
    PM -->|"communicates"| SM
    SM -->|"facilitates"| PDwn
    PDwn -->|"verifies"| USha1
    TP ---|"uses"| EL

    %% Click Events
    click A "https://github.com/niranjanblank/bittorrrent_client/blob/main/src/main.cpp"
    click TP "https://github.com/niranjanblank/bittorrrent_client/blob/main/src/lib/TorrentParser/TorrentParser.hpp"
    click BE "https://github.com/niranjanblank/bittorrrent_client/blob/main/src/lib/bencode/bencode.hpp"
    click TC "https://github.com/niranjanblank/bittorrrent_client/blob/main/src/lib/TorrentParser/TorrentParser.hpp"
    click PD "https://github.com/niranjanblank/bittorrrent_client/blob/main/src/lib/PeerDiscovery.hpp"
    click HH "https://github.com/niranjanblank/bittorrrent_client/blob/main/src/lib/HandshakeHandler.hpp"
    click PM "https://github.com/niranjanblank/bittorrrent_client/blob/main/src/lib/PeerMessageHandler.hpp"
    click SM "https://github.com/niranjanblank/bittorrrent_client/blob/main/src/lib/SocketManager.hpp"
    click NW "https://github.com/niranjanblank/bittorrrent_client/blob/main/src/lib/networking.hpp"
    click HT "https://github.com/niranjanblank/bittorrrent_client/blob/main/src/lib/httplib.h"
    click PDwn "https://github.com/niranjanblank/bittorrrent_client/blob/main/src/lib/PieceDownloader.hpp"
    click UCommon "https://github.com/niranjanblank/bittorrrent_client/blob/main/src/common.hpp"
    click USha1 "https://github.com/niranjanblank/bittorrrent_client/blob/main/src/lib/sha1.hpp"
    click UUtils "https://github.com/niranjanblank/bittorrrent_client/blob/main/src/lib/utils.hpp"
    click UStruct "https://github.com/niranjanblank/bittorrrent_client/blob/main/src/lib/structures.hpp"
    click EL "https://github.com/niranjanblank/bittorrrent_client/blob/main/src/lib/nlohmann/json.hpp"

    %% Styles
    classDef entry fill:#f4a261,stroke:#333,stroke-width:2px;
    classDef parsing fill:#2a9d8f,stroke:#333,stroke-width:2px;
    classDef peer fill:#e9c46a,stroke:#333,stroke-width:2px;
    classDef networking fill:#264653,stroke:#fff,stroke-width:2px;
    classDef download fill:#e76f51,stroke:#333,stroke-width:2px;
    classDef utility fill:#6a4c93,stroke:#fff,stroke-width:2px;
    classDef external fill:#a8dadc,stroke:#333,stroke-width:2px;
```
