# eliasson/pieces architecture

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

- Interactive diagram: https://gitdiagram.com/eliasson/pieces
- Repository: https://github.com/eliasson/pieces

## Overview

pieces is an experimental asyncio BitTorrent download client. A user supplies a single-file .torrent through the CLI; metadata is parsed, the client periodically announces to a tracker, queues returned peers, and coordinates peer connections that request blocks. The piece manager assembles and verifies pieces before writing them to disk and the client stops when complete. The README documents this workflow and the message/stream design; unsampled source limits detail about some runtime calls.

Main entry points: [`pieces/protocol.py`](https://github.com/eliasson/pieces/blob/master/pieces/protocol.py) (Peer connection), [`pieces/client.py`](https://github.com/eliasson/pieces/blob/master/pieces/client.py) (Download coordinator), [`pieces/tracker.py`](https://github.com/eliasson/pieces/blob/master/pieces/tracker.py) (Tracker announce), [`pieces/cli.py`](https://github.com/eliasson/pieces/blob/master/pieces/cli.py) (Command-line entry)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entry and metadata"]
  node_cli["Command-line entry<br/>[cli.py]"]
  node_torrent["Torrent metadata<br/>[torrent.py]"]
end

subgraph group_coordination["Download coordination"]
  node_client["Download coordinator<br/>[client.py]"]
  node_tracker["Tracker announce<br/>[tracker.py]"]
  node_peer_queue["Available-peer queue<br/>[client.py]"]
  node_piece_manager["Piece scheduling and assembly<br/>[client.py]"]
  node_output[("Downloaded file")]
end

subgraph group_wire["BitTorrent protocol"]
  node_peer_connection["Peer connection<br/>[protocol.py]"]
  node_peer_stream["Peer message stream<br/>[protocol.py]"]
  node_peer_messages["Peer message codecs<br/>[protocol.py]"]
end

subgraph group_encoding["Metadata encoding"]
  node_bencoding["Bencode codec<br/>[bencoding.py]"]
end

node_user(("User"))
node_tracker_service(("Tracker"))
node_remote_peers(("Remote peers"))

node_user -->|"starts download"| node_cli
node_cli -->|"loads metadata"| node_torrent
node_cli -->|"starts client"| node_client
node_torrent -->|"decodes metadata"| node_bencoding
node_tracker -->|"decodes response"| node_bencoding
node_client -->|"announces periodically"| node_tracker
node_tracker -->|"requests peers"| node_tracker_service
node_tracker_service -.->|"returns peers"| node_tracker
node_client -->|"enqueues peers"| node_peer_queue
node_client -->|"coordinates pieces"| node_piece_manager
node_client -->|"starts workers"| node_peer_connection
node_peer_connection -->|"consumes peers"| node_peer_queue
node_peer_connection -->|"connects to"| node_remote_peers
node_peer_connection -->|"reads messages"| node_peer_stream
node_peer_connection -->|"encodes messages"| node_peer_messages
node_peer_connection -->|"requests blocks"| node_piece_manager
node_piece_manager -->|"writes verified pieces"| node_output

click node_cli "https://github.com/eliasson/pieces/blob/master/pieces/cli.py"
click node_torrent "https://github.com/eliasson/pieces/blob/master/pieces/torrent.py"
click node_client "https://github.com/eliasson/pieces/blob/master/pieces/client.py"
click node_tracker "https://github.com/eliasson/pieces/blob/master/pieces/tracker.py"
click node_peer_queue "https://github.com/eliasson/pieces/blob/master/pieces/client.py"
click node_piece_manager "https://github.com/eliasson/pieces/blob/master/pieces/client.py"
click node_peer_connection "https://github.com/eliasson/pieces/blob/master/pieces/protocol.py"
click node_peer_stream "https://github.com/eliasson/pieces/blob/master/pieces/protocol.py"
click node_peer_messages "https://github.com/eliasson/pieces/blob/master/pieces/protocol.py"
click node_bencoding "https://github.com/eliasson/pieces/blob/master/pieces/bencoding.py"

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_torrent,node_user toneBlue
class node_client,node_tracker,node_peer_queue,node_piece_manager,node_output toneAmber
class node_peer_connection,node_peer_stream,node_peer_messages toneMint
class node_bencoding toneRose
class node_tracker_service,node_remote_peers toneIndigo
```

## Components

### Entry and metadata

- [Command-line entry](https://github.com/eliasson/pieces/blob/master/pieces/cli.py): `pieces/cli.py`
- [Torrent metadata](https://github.com/eliasson/pieces/blob/master/pieces/torrent.py): `pieces/torrent.py`

### Download coordination

- [Download coordinator](https://github.com/eliasson/pieces/blob/master/pieces/client.py): `pieces/client.py`
- [Tracker announce](https://github.com/eliasson/pieces/blob/master/pieces/tracker.py): `pieces/tracker.py`
- [Available-peer queue](https://github.com/eliasson/pieces/blob/master/pieces/client.py): `pieces/client.py`
- [Piece scheduling and assembly](https://github.com/eliasson/pieces/blob/master/pieces/client.py): `pieces/client.py`
- **Downloaded file**

### BitTorrent protocol

- [Peer connection](https://github.com/eliasson/pieces/blob/master/pieces/protocol.py): `pieces/protocol.py`
- [Peer message stream](https://github.com/eliasson/pieces/blob/master/pieces/protocol.py): `pieces/protocol.py`
- [Peer message codecs](https://github.com/eliasson/pieces/blob/master/pieces/protocol.py): `pieces/protocol.py`

### Metadata encoding

- [Bencode codec](https://github.com/eliasson/pieces/blob/master/pieces/bencoding.py): `pieces/bencoding.py`

### Other components

- **User**
- **Tracker**
- **Remote peers**

## Connections

- User → Command-line entry: starts download (evidence: `README.md`)
- Command-line entry → Torrent metadata: loads metadata (evidence: `pieces/cli.py`)
- Command-line entry → Download coordinator: starts client (evidence: `pieces/cli.py`)
- Torrent metadata → Bencode codec: decodes metadata (evidence: `pieces/torrent.py`)
- Tracker announce → Bencode codec: decodes response (evidence: `pieces/tracker.py`)
- Download coordinator → Tracker announce: announces periodically (evidence: `pieces/client.py`)
- Tracker announce → Tracker: requests peers (evidence: `pieces/tracker.py`)
- Tracker → Tracker announce: returns peers
- Download coordinator → Available-peer queue: enqueues peers (evidence: `pieces/client.py`)
- Download coordinator → Piece scheduling and assembly: coordinates pieces (evidence: `pieces/client.py`)
- Download coordinator → Peer connection: starts workers (evidence: `README.md`)
- Peer connection → Available-peer queue: consumes peers (evidence: `pieces/protocol.py`)
- Peer connection → Remote peers: connects to (evidence: `pieces/protocol.py`)
- Peer connection → Peer message stream: reads messages (evidence: `pieces/protocol.py`)
- Peer connection → Peer message codecs: encodes messages (evidence: `pieces/protocol.py`)
- Peer connection → Piece scheduling and assembly: requests blocks (evidence: `README.md`)
- Piece scheduling and assembly → Downloaded file: writes verified pieces (evidence: `pieces/client.py`)
