This project simulates the BitTorrent peer-to-peer file-sharing protocol using MPI (Message Passing Interface) and Pthreads. The implementation focuses on decentralized file distribution, swarm management, and efficient segment downloading across multiple clients.
-
Tracker: Manages swarm information for files and coordinates client interactions.
-
Clients: Act as peers or seeds depending on their file segments.
-
Multi-threading: Uses Pthreads for concurrent download and upload operations.
-
Efficiency: Balances segment downloads across peers to avoid overloading a single source.
-
Simulation: Simulates file transfers using hash-based segments instead of actual files.
-
MPI: OpenMPI or MPICH (tested with OpenMPI 4.1.2).
-
C++ Compiler: Supports C++11 or higher (e.g.,
g++orclang). -
Pthreads: Standard threading library (included with most systems).
Compile the project using the provided Makefile:
make build # Generates the executable `BitTorrent`
Each client (process rank > 0) requires an input file named in<R>.txt, where R is the client's MPI rank, stored in the same folder as the executable.
Example structure (in1.txt):
2
file1 3
3fcfb9d1242fdcec64aee2bfe3526691
6dd6078d720fc86c885f7f87faf0d32a
b56195fb830b234e8e35acaabc399400
file2 2
0f2ab6f4eab22e6b061f99daa48dd74e
f70fee606c4e57add77b3773fed6622b
1
file3
Start the simulation with MPI:
mpirun -np <N> ./BitTorrent
-
<N>must be ≥ 3 (1 tracker + ≥2 clients). -
Ensure input files (
in1.txt,in2.txt, etc.) are in the working directory.
After completing downloads, clients generate output files named client<R>_<FILENAME> (e.g., client2_file1), containing the ordered hash segments of the downloaded file.
- Tracker (Rank 0):
-
Maintains swarm data (seeds/peers per file).
-
Responds to client requests for swarm updates.
-
Coordinates client initialization and shutdown.
- Clients (Rank > 0):
-
Download Thread: Requests missing segments from peers/seeds and updates the tracker.
-
Upload Thread: Responds to segment requests from other clients.
-
Supports role transitions (leecher → peer → seed).
- Initialization:
-
Clients send owned file metadata (filenames, segment hashes) to the tracker.
-
Tracker builds initial swarm lists and signals clients to start downloading.
- Download Phase:
-
Clients request swarm lists for desired files.
-
Segments are downloaded in a round-robin fashion from available peers/seeds.
-
Swarm lists are refreshed every 10 segments to account for new peers.
- Completion:
-
Clients become seeds for downloaded files.
-
Tracker terminates all processes once all downloads are complete.
-
Balanced Downloads: Clients prioritize less busy peers/seeds to avoid bottlenecks.
-
Dynamic Swarms: Regular updates ensure fresh peer lists for optimal resource usage.
-
Parallelism: Multi-threaded design maximizes upload/download throughput.
Contributions to improve this project or extend its capabilities are welcome. Please submit pull requests with detailed descriptions of changes or improvements.
This project is licensed under the MIT License - see the LICENSE file for details.