g_rust/protocol/
hmessage.rs

1use crate::protocol::hdirection::HDirection;
2use crate::protocol::hpacket::HPacket;
3
4#[derive(Debug, Clone, Eq, PartialEq)]
5pub struct HMessage {
6    packet: HPacket,
7    index: i32,
8    direction: HDirection,
9    pub blocked: bool
10}
11
12impl HMessage {
13    pub fn from_string(s: String) -> Self {
14        let parts: Vec<&str> = s.split("\t").collect();
15
16        Self {
17            packet: HPacket::from_string(parts[3..].join("\t")),
18            index: parts[1].parse::<i32>().unwrap(),
19            direction: parts[2].parse::<HDirection>().unwrap(),
20            blocked: parts[0] == "1"
21        }
22    }
23
24    pub fn from_message(message: Self) -> Self {
25        Self {
26            packet: HPacket::from_packet(message.packet),
27            index: message.index,
28            direction: message.direction,
29            blocked: message.blocked
30        }
31    }
32
33    pub fn from_packet_dir_index(packet: HPacket, direction: HDirection, index: i32) -> Self {
34        Self {
35            packet,
36            direction,
37            index,
38            blocked: false
39        }
40    }
41
42    pub fn get_packet(&mut self) -> &mut HPacket {
43        &mut self.packet
44    }
45
46    pub fn get_index(&mut self) -> i32 {
47        self.index
48    }
49
50    pub fn get_destination(&mut self) -> HDirection {
51        self.direction.clone()
52    }
53
54    pub fn is_corrupted(&mut self) -> bool {
55        self.packet.is_corrupted()
56    }
57
58    pub fn stringify(&mut self) -> String {
59        (if self.blocked { "1" } else { "0" }).to_string() + "\t" + self.index.to_string().as_str() + "\t" + self.direction.to_string().as_str() + "\t" + self.packet.stringify().as_str()
60    }
61}