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
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//! Messages used for inter-node communication.
use TimeToLive;

/// Messages used for inter-node communication.
///
/// [HyParView]: http://asc.di.fct.unl.pt/~jleitao/pdf/dsn07-leitao.pdf
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ProtocolMessage<T> {
    /// `JOIN` message.
    Join(JoinMessage<T>),

    /// `FORWARD_JOIN` message.
    ForwardJoin(ForwardJoinMessage<T>),

    /// `NEIGHBOR` message.
    Neighbor(NeighborMessage<T>),

    /// `SHUFFLE` message.
    Shuffle(ShuffleMessage<T>),

    /// `SHUFFLE_REPLY` message.
    ShuffleReply(ShuffleReplyMessage<T>),

    /// `DISCONNECT` messsage.
    Disconnect(DisconnectMessage<T>),
}
impl<T> ProtocolMessage<T> {
    /// Returns the node ID of the sender of the message.
    pub fn sender(&self) -> &T {
        match self {
            ProtocolMessage::Join(m) => &m.sender,
            ProtocolMessage::ForwardJoin(m) => &m.sender,
            ProtocolMessage::Neighbor(m) => &m.sender,
            ProtocolMessage::Shuffle(m) => &m.sender,
            ProtocolMessage::ShuffleReply(m) => &m.sender,
            ProtocolMessage::Disconnect(m) => &m.sender,
        }
    }
}
impl<T: Clone> ProtocolMessage<T> {
    pub(crate) fn join(sender: &T) -> Self {
        ProtocolMessage::Join(JoinMessage {
            sender: sender.clone(),
        })
    }

    pub(crate) fn forward_join(sender: &T, new_node: T, ttl: TimeToLive) -> Self {
        ProtocolMessage::ForwardJoin(ForwardJoinMessage {
            sender: sender.clone(),
            new_node,
            ttl,
        })
    }

    pub(crate) fn neighbor(sender: &T, high_priority: bool) -> Self {
        ProtocolMessage::Neighbor(NeighborMessage {
            sender: sender.clone(),
            high_priority,
        })
    }

    pub(crate) fn shuffle(sender: &T, origin: T, nodes: Vec<T>, ttl: TimeToLive) -> Self {
        ProtocolMessage::Shuffle(ShuffleMessage {
            sender: sender.clone(),
            origin,
            nodes,
            ttl,
        })
    }

    pub(crate) fn shuffle_reply(sender: &T, nodes: Vec<T>) -> Self {
        ProtocolMessage::ShuffleReply(ShuffleReplyMessage {
            sender: sender.clone(),
            nodes,
        })
    }

    pub(crate) fn disconnect(sender: &T) -> Self {
        ProtocolMessage::Disconnect(DisconnectMessage {
            sender: sender.clone(),
        })
    }
}

/// `JOIN` message.
///
/// This is sent by new nodes for joining a HyParView cluster.
/// The receiver is the contact node of the cluster.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct JoinMessage<T> {
    /// The node ID of the message sender.
    ///
    /// It is also a new node that wishes to join the cluster.
    pub sender: T,
}

/// `FORWARD_JOIN` message.
///
/// This is used for disseminating a `JOIN` request to the members of the cluster to
/// which the contact node belongs.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ForwardJoinMessage<T> {
    /// The node ID of the message sender.
    pub sender: T,

    /// The ID of the new joining node.
    pub new_node: T,

    /// TTL of the message.
    pub ttl: TimeToLive,
}

/// `NEIGHBOR` message.
///
/// This is used for refilling active view shrunk by node disconnections.
///
/// In this crate, it is also used for notifing to new node that
/// HyParView level connection has been established
/// (in that case the value of `high_priority` always be set to `true`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NeighborMessage<T> {
    /// The node ID of the message sender.
    pub sender: T,

    /// Whether the priority of the sender is high or low.
    pub high_priority: bool,
}

/// `SHUFFLE` message.
///
/// This and `SHUFFLE_REPLY` messages are used for shuffling passive views of two nodes.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ShuffleMessage<T> {
    /// The node ID of the message sender.
    pub sender: T,

    /// The ID of the origin node that emitted the shuffle request.
    pub origin: T,

    /// The nodes selected by `origin` for shuffling.
    pub nodes: Vec<T>,

    /// TTL of the message.
    pub ttl: TimeToLive,
}

/// `SHUFFLE_REPLY` message.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ShuffleReplyMessage<T> {
    /// The node ID of the message sender.
    pub sender: T,

    /// The nodes selected by `sender` as the reply of the associated `Shuffle` message.
    pub nodes: Vec<T>,
}

/// `DISCONNECT` message.
///
/// This is sent by a node for removing the sender from the active view of the receiver.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DisconnectMessage<T> {
    /// The node ID of the message sender.
    pub sender: T,
}