|
| 1 | +# P2P Transfer Protocol (Peerile) |
| 2 | +## What's this? |
| 3 | +This is a command-line tool for transferring files between two machines on the same network, without a central server. The tool can function as both sender and receiver. |
| 4 | + |
| 5 | +## Architecture |
| 6 | +### Project Structure |
| 7 | +The project is organized into 4 modules: |
| 8 | +- `protocol.rs`: Communication protocol definition, where all protocol commands are declared and defined for "peer-to-peer" communication. |
| 9 | +- `listener.rs`: File reception management, it listens for incoming connections and handle them. |
| 10 | +- `sender.rs`: File sending management, it tries to connect to a listener in order to send a file. |
| 11 | +- `main.rs`: Entry point & command-line interface declaration |
| 12 | + |
| 13 | +### Communication Protocol |
| 14 | +The protocol uses four commands: |
| 15 | +1. **HELLO filename size**: The sender proposes a file to the receiver |
| 16 | + - `filename`: name of the file to transfer |
| 17 | + - `size`: file size in bytes |
| 18 | +2. **ACK**: The receiver accepts the proposed file |
| 19 | +3. **NACK**: The receiver refuses the proposed file |
| 20 | +4. **SEND size**: The sender starts sending the file |
| 21 | + - `size`: must match the size announced in HELLO |
| 22 | + |
| 23 | +All commands end with a newline character (`\n`) which serves as a delimiter for the program. |
| 24 | + |
| 25 | +## How it works? |
| 26 | +### Receiver Side (listener) |
| 27 | +1. The receiver listens on a specified port (or by default 9000) |
| 28 | +2. For each incoming connection, a new thread is created |
| 29 | +3. The receiver waits for a HELLO command indicating a filename and its size |
| 30 | +4. If the file doesn't already exist, it sends ACK, otherwise NACK |
| 31 | +5. It waits for the SEND command with the corresponding size |
| 32 | +6. It receives the file data and writes it to the output directory |
| 33 | + |
| 34 | +### Sender Side |
| 35 | +1. The sender connects to the receiver's IP address and port |
| 36 | +2. It sends a HELLO command with the file name and size |
| 37 | +3. It waits for the receiver's response (ACK or NACK) |
| 38 | +4. If ACK is received, it sends the SEND command followed by the file data |
| 39 | +5. If NACK is received, the transfer is cancelled |
| 40 | + |
| 41 | +## Usage |
| 42 | +### Receiving a File |
| 43 | +> Command examples use `peerile`, but you can use `cargo run --` instead. (the `--` is important!) |
| 44 | +```bash |
| 45 | +peerile listen --port 9000 --output ./shared |
| 46 | +``` |
| 47 | +- `--port | -p`: Port on which the program will listen for incoming file transfers |
| 48 | +- `--output | -o`: destination for received files |
| 49 | + |
| 50 | +### Sending a File |
| 51 | + |
| 52 | +```bash |
| 53 | +peerile send --file document.pdf --to 192.168.1.100 --port 9000 |
| 54 | +``` |
| 55 | + |
| 56 | +- `--file | -f`: Path to the file to send |
| 57 | +- `--to | -t`: Receiver's IP address |
| 58 | +- `--port | -p`: Port on which the receiver is listening |
| 59 | + |
0 commit comments