1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//! Primitives for representing packets used to communicate with the tracker server

use serde::{Deserialize, Serialize};
use std::convert::TryFrom;

#[derive(Serialize, Deserialize, Default, Debug, PartialEq)]
pub struct ConnectionRequest {
    pub identity_number: u32,
    pub username: String,
    pub port: u16,
    pub ip: [u8; 4],
}

impl Clone for ConnectionRequest {
    fn clone(&self) -> Self {
        ConnectionRequest {
            identity_number: self.identity_number,
            username: self.username.clone(),
            port: self.port,
            ip: self.ip,
        }
    }
}

#[derive(Serialize, Deserialize, Default, Debug, PartialEq, Clone)]
pub struct TrackerPacket {
    pub identity_number: u32,
    pub username: String,
    pub peer_username: String,
    pub req: bool,
    pub packet_type: u8,
    pub port: u16,
    pub ip: [u8; 4],
    pub connections: Vec<ConnectionRequest>,
}

impl TryFrom<TrackerPacket> for Vec<u8> {
    type Error = &'static str;

    fn try_from(packet: TrackerPacket) -> Result<Self, Self::Error> {
        match serde_json::to_string(&packet) {
            Ok(json) => Ok(json.into_bytes()),
            Err(_) => Err("Error converting to json"),
        }
    }
}

impl TryFrom<Vec<u8>> for TrackerPacket {
    type Error = &'static str;

    fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
        match String::from_utf8(bytes) {
            Ok(json) => match serde_json::from_str(&json) {
                Ok(data) => Ok(data),
                Err(_) => Err("Unable to parse json"),
            },
            Err(_) => Err("Unable to parse utf8"),
        }
    }
}

#[cfg(test)]
mod tests {

    use crate::tracker::{ConnectionRequest, TrackerPacket};
    use std::convert::TryFrom;
    #[test]
    fn tracker_test() {
        let connection = ConnectionRequest {
            identity_number: 32,
            username: String::from("someone"),
            port: 4200,
            ip: [42, 32, 22, 12],
        };

        let packet = TrackerPacket {
            identity_number: 42,
            peer_username: "another".to_string(),
            connections: vec![connection],
            username: "test".to_string(),
            req: true,
            packet_type: 10_u8,
            port: 1234,
            ip: [1, 2, 3, 4],
        };

        let original_packet = packet.clone();

        let parsed_packet: Vec<u8> = TryFrom::try_from(packet).unwrap();
        let unparsed_packet: TrackerPacket = TryFrom::try_from(parsed_packet).unwrap();

        assert_eq!(unparsed_packet, original_packet);
    }
}