A Java-based educational simulation of the BitTorrent protocol, demonstrating peer-to-peer file distribution concepts through practical implementation.
This project implements a distributed network simulation that models core BitTorrent concepts including:
- Peer discovery and tracking
- Segmented file transfers
- Bandwidth management
- Network topology
- Progress monitoring
.
├── src/ # Source code
│ ├── base/ # Core network components
│ │ └── torrent/ # BitTorrent protocol implementation
│ ├── model/ # Data models
│ ├── simulation/ # Simulation runtime
│ ├── test/ # Test suites
│ └── util/ # Utility classes
├── student/ # Student implementation directory
└── solution/ # Reference implementation
NetworkDevice.java: Base class for all network-capable devicesRouter.java: Network routing and connection managementComputer.java: Extended network node with storage/bandwidth constraintsHub.java: Network hub implementationSwitch.java: Network switch implementation
TorrentClient.java: Peer node for file transfer operationsTorrentServer.java: Seeding node with peer coordinationTorrentTracker.java: Centralized peer discovery service
TorrentFile.java: File representation with piece managementPiece.java: Individual file segment unitPeerStatus.java: Transfer metrics and statisticsSwarmInfo.java: Peer group state tracking
public static final int PIECE_SIZE = 262144; // 256 KB
public static final long FILE_SIZE = 10485760L; // 10 MB
public static final int SIMULATION_CYCLES = 20;
public static final long SLEEP_TIME = 1000; // 1 secondCreating a torrent client:
TorrentClient client = new TorrentClient(
"CLIENT1", // Device ID
"192.168.1.10", // IP address
"NYC", // Location
1000, // Bandwidth (Mbps)
50.0, // Max upload speed
100.0, // Max download speed
10000000L // Storage capacity (bytes)
);Initializing a file transfer:
TorrentFile file = new TorrentFile(
"hash123", // Info hash
"test.mp4", // Filename
1048576L, // File size (bytes)
262144 // Piece size (bytes)
);
client.initializeDownload(file);Running the simulation:
Simulation simulation = new Simulation();
simulation.runSimulation();- IPv4 address validation and management
- Bandwidth and storage constraints
- Router-based network topology
- Connection tracking
- Device discovery
- Piece-based file segmentation
- Peer discovery and tracking
- Progress monitoring
- Transfer speed calculations
- Swarm management
- Multiple simultaneous transfers
Comprehensive test suite covering:
- Network device functionality
- Torrent protocol operations
- Data model integrity
- Edge cases and error conditions
- Java JDK 21 or higher
- JUnit 5.7.0+ for testing
- Clone the repository
- Navigate to the project directory
- Run the tests:
./gradlew testormvn test - Run the simulation:
./gradlew runormvn exec:java
The simulation intentionally excludes:
- Actual file I/O
- Network protocol headers
- Socket communications
- Cryptographic verification
- Request pipelining
- Advanced peer selection
This implementation prioritizes:
- Code clarity and readability
- Demonstrable protocol concepts
- Testability
- Modular design
- Clear separation of concerns