# piyushxcoder/dhaar-torrent architecture

> Architecture diagram of the GitHub repository piyushxcoder/dhaar-torrent, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/piyushxcoder/dhaar-torrent
- Repository: https://github.com/piyushxcoder/dhaar-torrent

## Overview

Dhaar Torrent is a Rust BitTorrent client library and CLI. Its main workflow parses torrent metadata, discovers peers through HTTP trackers, coordinates inbound and outbound peer connections, requests and verifies pieces, persists resumable data, and extracts the completed torrent into its real file layout. The `Download` API wires these actors together and publishes live progress and transfer statistics. The graph preserves documented capabilities even where implementation excerpts were unavailable; unsampled subsystem links are omitted when their wiring cannot be established.

Main entry points: [`src/lib.rs`](https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/lib.rs) (Download API), [`src/peer_manager/mod.rs`](https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/peer_manager/mod.rs) (Peer manager), [`src/peer_connection/mod.rs`](https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/peer_connection/mod.rs) (Peer connection), [`src/piece_manager/mod.rs`](https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/piece_manager/mod.rs) (Piece manager)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entry and orchestration"]
  node_cli["CLI entry<br/>[main.rs]"]
  node_config["Configuration<br/>[mod.rs]"]
  node_download["Download API<br/>[lib.rs]"]
  node_status["Status feed<br/>[status.rs]"]
end

subgraph group_metadata["Torrent metadata"]
  node_parser["Torrent parser<br/>[parser.rs]"]
  node_metadata_model["Torrent metadata<br/>[metadata.rs]"]
  node_bencode["Bencode codec<br/>[lib.rs]"]
end

subgraph group_discovery["Peer discovery"]
  node_explorer["Peer explorer<br/>[mod.rs]"]
  node_tracker_manager["Tracker manager<br/>[tracker_manager.rs]"]
  node_tracker_client["Tracker clients<br/>[tracker_client.rs]"]
end

subgraph group_transfer["Peer transfer"]
  node_peer_manager["Peer manager<br/>[mod.rs]"]
  node_selection["Peer selection"]
  node_peer_connection["Peer connection<br/>[mod.rs]"]
  node_wire["Wire codec<br/>[mod.rs]"]
end

subgraph group_data["Pieces and storage"]
  node_piece_manager["Piece manager<br/>[mod.rs]"]
  node_store[("Resumable disk store<br/>[mod.rs]")]
end

node_user(("CLI or library user"))
node_tracker_service(("HTTP trackers"))
node_remote_peers(("Remote peers"))
node_filesystem(("Download filesystem"))

node_user -->|"runs"| node_cli
node_user -->|"creates download"| node_download
node_cli -->|"loads settings"| node_config
node_cli -->|"starts download"| node_download
node_download -->|"parses torrent"| node_parser
node_parser -->|"decodes"| node_bencode
node_parser -->|"builds metadata"| node_metadata_model
node_download -->|"configures tracker source"| node_tracker_manager
node_download -->|"starts actor"| node_explorer
node_tracker_manager -->|"announces"| node_tracker_client
node_tracker_client -->|"requests peers"| node_tracker_service
node_tracker_manager -->|"sends discovered peers"| node_explorer
node_explorer -->|"dispatches peers"| node_peer_manager
node_download -->|"starts actor"| node_peer_manager
node_peer_manager -->|"selects peers"| node_selection
node_peer_manager -->|"opens connections"| node_peer_connection
node_peer_manager -->|"accepts and dials"| node_remote_peers
node_peer_connection -->|"encodes and decodes"| node_wire
node_peer_connection -->|"exchanges blocks"| node_remote_peers
node_download -->|"starts actor"| node_piece_manager
node_peer_connection -->|"submits blocks"| node_piece_manager
node_piece_manager -->|"reads and writes"| node_store
node_store -->|"persists and extracts"| node_filesystem
node_piece_manager -->|"publishes progress"| node_status
node_download -->|"samples statistics"| node_status
node_user -->|"subscribes"| node_status

click node_cli "https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/main.rs"
click node_config "https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/config/mod.rs"
click node_download "https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/lib.rs"
click node_status "https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/status.rs"
click node_parser "https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/torrent_parser/parser.rs"
click node_metadata_model "https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/torrent_parser/metadata.rs"
click node_bencode "https://github.com/piyushxcoder/dhaar-torrent/blob/master/crates/bencode-dhaar/src/lib.rs"
click node_explorer "https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/peer_explorer/mod.rs"
click node_tracker_manager "https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/peer_explorer/tracker/tracker_manager.rs"
click node_tracker_client "https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/peer_explorer/tracker/tracker_client.rs"
click node_peer_manager "https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/peer_manager/mod.rs"
click node_selection "https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/peer_manager/peer_selection_strategy.rs"
click node_peer_connection "https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/peer_connection/mod.rs"
click node_wire "https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/wire_protocol/codec/mod.rs"
click node_piece_manager "https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/piece_manager/mod.rs"
click node_store "https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/store/mod.rs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_config,node_download,node_status,node_user toneBlue
class node_parser,node_metadata_model,node_bencode toneAmber
class node_explorer,node_tracker_manager,node_tracker_client toneMint
class node_peer_manager,node_selection,node_peer_connection,node_wire toneRose
class node_piece_manager,node_store,node_tracker_service,node_remote_peers,node_filesystem toneIndigo
```

## Components

### Entry and orchestration

- [CLI entry](https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/main.rs): `src/main.rs`
- [Configuration](https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/config/mod.rs): `src/config/mod.rs`
- [Download API](https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/lib.rs): `src/lib.rs`
- [Status feed](https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/status.rs): `src/status.rs`

### Torrent metadata

- [Torrent parser](https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/torrent_parser/parser.rs): `src/torrent_parser/parser.rs`
- [Torrent metadata](https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/torrent_parser/metadata.rs): `src/torrent_parser/metadata.rs`
- [Bencode codec](https://github.com/piyushxcoder/dhaar-torrent/blob/master/crates/bencode-dhaar/src/lib.rs): `crates/bencode-dhaar/src/lib.rs`

### Peer discovery

- [Peer explorer](https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/peer_explorer/mod.rs): `src/peer_explorer/mod.rs`
- [Tracker manager](https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/peer_explorer/tracker/tracker_manager.rs): `src/peer_explorer/tracker/tracker_manager.rs`
- [Tracker clients](https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/peer_explorer/tracker/tracker_client.rs): `src/peer_explorer/tracker/tracker_client.rs`

### Peer transfer

- [Peer manager](https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/peer_manager/mod.rs): `src/peer_manager/mod.rs`
- [Peer selection](https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/peer_manager/peer_selection_strategy.rs): `src/peer_manager/peer_selection_strategy.rs`
- [Peer connection](https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/peer_connection/mod.rs): `src/peer_connection/mod.rs`
- [Wire codec](https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/wire_protocol/codec/mod.rs): `src/wire_protocol/codec/mod.rs`

### Pieces and storage

- [Piece manager](https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/piece_manager/mod.rs): `src/piece_manager/mod.rs`
- [Resumable disk store](https://github.com/piyushxcoder/dhaar-torrent/blob/master/src/store/mod.rs): `src/store/mod.rs`

### Other components

- **CLI or library user**
- **HTTP trackers**
- **Remote peers**
- **Download filesystem**

## Connections

- CLI or library user → CLI entry: runs
- CLI or library user → Download API: creates download
- CLI entry → Configuration: loads settings
- CLI entry → Download API: starts download
- Download API → Torrent parser: parses torrent
- Torrent parser → Bencode codec: decodes
- Torrent parser → Torrent metadata: builds metadata
- Download API → Tracker manager: configures tracker source
- Download API → Peer explorer: starts actor
- Tracker manager → Tracker clients: announces
- Tracker clients → HTTP trackers: requests peers
- Tracker manager → Peer explorer: sends discovered peers
- Peer explorer → Peer manager: dispatches peers
- Download API → Peer manager: starts actor
- Peer manager → Peer selection: selects peers
- Peer manager → Peer connection: opens connections
- Peer manager → Remote peers: accepts and dials
- Peer connection → Wire codec: encodes and decodes
- Peer connection → Remote peers: exchanges blocks
- Download API → Piece manager: starts actor
- Peer connection → Piece manager: submits blocks
- Piece manager → Resumable disk store: reads and writes
- Resumable disk store → Download filesystem: persists and extracts
- Piece manager → Status feed: publishes progress
- Download API → Status feed: samples statistics
- CLI or library user → Status feed: subscribes
