# dung-h/liketorrent_242 architecture

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

- Interactive diagram: https://gitdiagram.com/dung-h/liketorrent_242
- Repository: https://github.com/dung-h/liketorrent_242

## Overview

##### Project type and purpose
- This is a peer‑to‑peer file‑sharing application (“LikeTorrent”) with two main subsystems: a Flask‑based HTTP tracker and a Python/Tkinter‑based client.
- It implements the BitTorrent protocol (announce/scrape endpoints, piece exchange, seeding, leeching) and provides GUIs for both tracker and client.

##### File structure
- Top‑level:
- requirements.txt, LICENSE, README.md, torrent.db (SQLite)
- src/ui.py (Client GUI)
- src/tracker_ui.py (Tracker GUI)
- src/peer/… (Peer modules)
- src/tracker/tracker.py (Tracker service)
- Patterns:
- Separation between UI and core logic
- MVC‑style: UI modules call into core (“client.py”, “peer.py”, “tracker.py”)
- Persistence: SQLite database (“torrent.db”) handled by tracker

##### Key insights from README
- Tracker exposes /announce and /scrape endpoints via Flask
- Client implements multi‑threaded piece downloads/uploads, SHA‑1 hashing, rarest‑first selection, optimistic unchoking
- GUIs built with Tkinter, use matplotlib for graphs

##### Main components and interactions
a. Components
1. Tracker subsystem
- Tracker UI (tracker_ui.py)
- HTTP Tracker service (tracker.py)
- SQLite database (torrent.db)
2. Peer (Client) subsystem
- Client UI (ui.py)
- Core modules in src/peer:
- client.py (orchestrator)
- config.py (settings)
- metainfo.py (parse/.torrent)
- torrent_maker.py (create .torrent)
- piece_manager.py (file I/O, piece hashing)
- peer.py (socket handling, message framing)
3. External interactions
- HTTP calls (requests) between client and tracker
- TCP connections (peer‑to‑peer) between clients
4. Third‑party libraries
- Flask, flask‑cors, requests, bencodepy, tkinter, matplotlib, Pillow

b. Relationships & data flows
- Client UI → invokes client.py to:
- Load/create torrents via metainfo and torrent_maker
- Start/stop downloads (spawns worker threads)
- client.py ↔ tracker.py (HTTP announce/scrape)
- client.py ↔ peer.py + piece_manager.py (TCP piece exchange)
- tracker.py ↔ SQLite DB (peer/torrent registration)
- Tracker UI polls tracker.py for real‑time stats (internal API calls)

c. Architectural patterns & principles
- Modular separation: UI vs. core logic vs. data persistence
- Multi‑threaded concurrency in client for parallel piece downloads
- MVC‑inspired layering: GUI (View) ↔ client logic (Controller) ↔ file system/DB (Model)
- Strategy pattern: rarest‑first piece selection, optimistic unchoking

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Tracker Subsystem
    subgraph "Tracker Subsystem"
        direction TB
        TrackerUI["Tracker UI"]:::ui
        TrackerService["HTTP Tracker Service\n(announce/scrape)"]:::core
        DB["SQLite DB"]:::db
    end

    %% Peer Subsystem
    subgraph "Peer Subsystem"
        direction TB
        ClientUI["Client UI"]:::ui
        ClientOrch["Client Orchestrator"]:::core
        Config["Peer Configuration"]:::core
        Metainfo["Metainfo Parser"]:::core
        TorrentMaker["Torrent Maker"]:::core
        PieceManager["Piece Manager"]:::core
        PeerNet["Peer Networking"]:::core
    end

    %% External Networks
    HTTPNet["HTTP Network"]:::net
    P2PNet["P2P Network"]:::net

    %% Connections
    TrackerUI -->|"internal API calls"| TrackerService
    TrackerService -->|"reads/writes"| DB
    TrackerService -->|"exposes endpoints"| HTTPNet

    ClientUI -->|"invokes"| ClientOrch
    ClientOrch -->|"uses settings"| Config
    ClientOrch -->|"parses/generates .torrent"| Metainfo
    ClientOrch -->|"parses/generates .torrent"| TorrentMaker
    ClientOrch -->|"manages pieces"| PieceManager
    ClientOrch -->|"HTTP announce/scrape"| HTTPNet
    ClientOrch -->|"TCP peer messaging"| P2PNet
    PeerNet -->|"delivers messages to"| PieceManager

    %% Click Events
    click TrackerUI "https://github.com/dung-h/liketorrent_242/blob/main/src/tracker_ui.py"
    click TrackerService "https://github.com/dung-h/liketorrent_242/blob/main/src/tracker/tracker.py"
    click DB "https://github.com/dung-h/liketorrent_242/blob/main/torrent.db"
    click ClientUI "https://github.com/dung-h/liketorrent_242/blob/main/src/ui.py"
    click ClientOrch "https://github.com/dung-h/liketorrent_242/blob/main/src/peer/client.py"
    click Config "https://github.com/dung-h/liketorrent_242/blob/main/src/peer/config.py"
    click Metainfo "https://github.com/dung-h/liketorrent_242/blob/main/src/peer/metainfo.py"
    click TorrentMaker "https://github.com/dung-h/liketorrent_242/blob/main/src/peer/torrent_maker.py"
    click PieceManager "https://github.com/dung-h/liketorrent_242/blob/main/src/peer/piece_manager.py"
    click PeerNet "https://github.com/dung-h/liketorrent_242/blob/main/src/peer/peer.py"

    %% Styles
    classDef ui fill:#add8e6,stroke:#000
    classDef core fill:#90ee90,stroke:#000
    classDef db fill:#ffa500,stroke:#000
    classDef net fill:#d3d3d3,stroke:#000
```
