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
use super::version_vector::VersionVector;

/**
 * Struct for the message sent over the network.
 */
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Message {
    ///Sender id
    pub id: usize,
    ///Message payload
    pub payload: Vec<u8>,
    ///Message version vector
    pub version_vector: VersionVector,
}

impl Message {
    /**
     * Builds a new Message.
     *
     * # Arguments
     *
     * `id` - Sender id
     *
     * `payload` - Serialized message payload
     *
     * `version_vector` - Message version vector
     */
    pub fn new(id: usize, payload: Vec<u8>, version_vector: VersionVector) -> Self {
        Self {
            id,
            payload,
            version_vector,
        }
    }
}

/**
 * Enum of the messages sent/received in the streams between peers.
 * */
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum StreamMsg {
    ///Handshake
    HND { index: usize },
    ///Peer message
    MSG { msg: Vec<u8>, peer_id: usize },
    ///Terminate connection
    CLOSE,
}

/**
 * Enum for the messages that will be sent/received in the channels between
 * the main middleware, stream reader and client
 */
pub enum ClientPeerMiddleware {
    ///Message sent by the Client to broadcast
    CLIENT {
        msg_id: usize,
        payload: Vec<u8>,
        version_vector: VersionVector,
    },
    ///Message received from a peer
    PEER { peer_id: usize, message: Message },
    ///Indicates that the Middleware has finished the starting up
    SETUP,
    ///Connection end
    END,
}

/**
 * Enum that will be sent by the Middleware to the Client.
 * */
pub enum MiddlewareClient {
    ///Delivered message with its sender id, payload and version vector
    DELIVER {
        sender_id: usize,
        message: Message,
        version_vector: VersionVector,
    },
    ///Stable message with its sender id, message id and version vector
    STABLE {
        sender_id: usize,
        message_id: usize,
        version_vector: VersionVector,
    },
    ///Setup variation
    SETUP,
}