plumtree/
message.rs

1//! Application and protocol messages.
2use std::fmt;
3
4use System;
5
6/// Application message.
7pub struct Message<T: System> {
8    /// The identifier of the message.
9    pub id: T::MessageId,
10
11    /// The payload of the message
12    pub payload: T::MessagePayload,
13}
14impl<T: System> Message<T> {
15    /// Makes a new `Message` instance.
16    ///
17    /// This is equivalent to `Message { id, payload }`.
18    pub fn new(id: T::MessageId, payload: T::MessagePayload) -> Self {
19        Message { id, payload }
20    }
21}
22impl<T: System> Clone for Message<T> {
23    fn clone(&self) -> Self {
24        Message {
25            id: self.id.clone(),
26            payload: self.payload.clone(),
27        }
28    }
29}
30impl<T: System> fmt::Debug for Message<T>
31where
32    T::MessageId: fmt::Debug,
33    T::MessagePayload: fmt::Debug,
34{
35    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36        write!(
37            f,
38            "Message {{ id: {:?}, payload: {:?} }}",
39            self.id, self.payload
40        )
41    }
42}
43impl<T: System> PartialEq for Message<T>
44where
45    T::MessageId: PartialEq,
46    T::MessagePayload: PartialEq,
47{
48    fn eq(&self, other: &Self) -> bool {
49        self.id.eq(&other.id) && self.payload.eq(&other.payload)
50    }
51}
52impl<T: System> Eq for Message<T>
53where
54    T::MessageId: Eq,
55    T::MessagePayload: Eq,
56{
57}
58
59/// Messages defined by the Plumtree algorithm.
60///
61/// Those are used for inter-node communications.
62#[allow(missing_docs)]
63pub enum ProtocolMessage<T: System> {
64    Gossip(GossipMessage<T>),
65    Ihave(IhaveMessage<T>),
66    Graft(GraftMessage<T>),
67    Prune(PruneMessage<T>),
68}
69impl<T: System> ProtocolMessage<T> {
70    /// Returns the sender of the message.
71    pub fn sender(&self) -> &T::NodeId {
72        match self {
73            ProtocolMessage::Gossip(m) => &m.sender,
74            ProtocolMessage::Ihave(m) => &m.sender,
75            ProtocolMessage::Graft(m) => &m.sender,
76            ProtocolMessage::Prune(m) => &m.sender,
77        }
78    }
79}
80impl<T: System> Clone for ProtocolMessage<T> {
81    fn clone(&self) -> Self {
82        match self {
83            ProtocolMessage::Gossip(m) => m.clone().into(),
84            ProtocolMessage::Ihave(m) => m.clone().into(),
85            ProtocolMessage::Graft(m) => m.clone().into(),
86            ProtocolMessage::Prune(m) => m.clone().into(),
87        }
88    }
89}
90impl<T: System> fmt::Debug for ProtocolMessage<T>
91where
92    T::NodeId: fmt::Debug,
93    T::MessageId: fmt::Debug,
94    T::MessagePayload: fmt::Debug,
95{
96    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
97        match self {
98            ProtocolMessage::Gossip(m) => write!(f, "Gossip({:?})", m),
99            ProtocolMessage::Ihave(m) => write!(f, "Ihave({:?})", m),
100            ProtocolMessage::Graft(m) => write!(f, "Graft({:?})", m),
101            ProtocolMessage::Prune(m) => write!(f, "Prune({:?})", m),
102        }
103    }
104}
105impl<T: System> From<GossipMessage<T>> for ProtocolMessage<T> {
106    fn from(f: GossipMessage<T>) -> Self {
107        ProtocolMessage::Gossip(f)
108    }
109}
110impl<T: System> From<IhaveMessage<T>> for ProtocolMessage<T> {
111    fn from(f: IhaveMessage<T>) -> Self {
112        ProtocolMessage::Ihave(f)
113    }
114}
115impl<T: System> From<GraftMessage<T>> for ProtocolMessage<T> {
116    fn from(f: GraftMessage<T>) -> Self {
117        ProtocolMessage::Graft(f)
118    }
119}
120impl<T: System> From<PruneMessage<T>> for ProtocolMessage<T> {
121    fn from(f: PruneMessage<T>) -> Self {
122        ProtocolMessage::Prune(f)
123    }
124}
125
126/// `GOSSIP` message.
127pub struct GossipMessage<T: System> {
128    /// The sender of the message.
129    pub sender: T::NodeId,
130
131    /// The message to be diffused.
132    pub message: Message<T>,
133
134    /// The hop count of the message.
135    pub round: u16,
136}
137impl<T: System> GossipMessage<T> {
138    pub(crate) fn new(sender: &T::NodeId, message: Message<T>, round: u16) -> Self {
139        GossipMessage {
140            sender: sender.clone(),
141            message,
142            round,
143        }
144    }
145}
146impl<T: System> Clone for GossipMessage<T> {
147    fn clone(&self) -> Self {
148        GossipMessage {
149            sender: self.sender.clone(),
150            message: self.message.clone(),
151            round: self.round,
152        }
153    }
154}
155impl<T: System> fmt::Debug for GossipMessage<T>
156where
157    T::NodeId: fmt::Debug,
158    T::MessageId: fmt::Debug,
159    T::MessagePayload: fmt::Debug,
160{
161    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
162        write!(
163            f,
164            "GossipMessage {{ sender: {:?}, message: {:?}, round: {:?} }}",
165            self.sender, self.message, self.round
166        )
167    }
168}
169
170/// `IHAVE` message.
171pub struct IhaveMessage<T: System> {
172    /// The sender of the message.
173    pub sender: T::NodeId,
174
175    /// The identifier of the message that the sender has keeping.
176    pub message_id: T::MessageId,
177
178    /// The hop count of the message.
179    pub round: u16,
180
181    /// Indicates whether this is a real-time message or a buffered message.
182    ///
183    /// The latter is used for synchronizing messages when new neighbors are joined.
184    pub realtime: bool,
185}
186impl<T: System> IhaveMessage<T> {
187    pub(crate) fn new(
188        sender: &T::NodeId,
189        message_id: T::MessageId,
190        round: u16,
191        realtime: bool,
192    ) -> Self {
193        IhaveMessage {
194            sender: sender.clone(),
195            message_id,
196            round,
197            realtime,
198        }
199    }
200}
201impl<T: System> Clone for IhaveMessage<T> {
202    fn clone(&self) -> Self {
203        IhaveMessage {
204            sender: self.sender.clone(),
205            message_id: self.message_id.clone(),
206            round: self.round,
207            realtime: self.realtime,
208        }
209    }
210}
211impl<T: System> fmt::Debug for IhaveMessage<T>
212where
213    T::NodeId: fmt::Debug,
214    T::MessageId: fmt::Debug,
215{
216    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
217        write!(
218            f,
219            "IhaveMessage {{ sender: {:?}, message_id: {:?}, round: {:?}, realtime: {:?} }}",
220            self.sender, self.message_id, self.round, self.realtime
221        )
222    }
223}
224
225/// `GRAFT` message.
226pub struct GraftMessage<T: System> {
227    /// The sender of the message.
228    pub sender: T::NodeId,
229
230    /// The identifier of the message requested by the sender.
231    pub message_id: Option<T::MessageId>,
232
233    /// The hop count of the message.
234    pub round: u16,
235}
236impl<T: System> GraftMessage<T> {
237    pub(crate) fn new(sender: &T::NodeId, message_id: Option<T::MessageId>, round: u16) -> Self {
238        GraftMessage {
239            sender: sender.clone(),
240            message_id,
241            round,
242        }
243    }
244}
245impl<T: System> Clone for GraftMessage<T> {
246    fn clone(&self) -> Self {
247        GraftMessage {
248            sender: self.sender.clone(),
249            message_id: self.message_id.clone(),
250            round: self.round,
251        }
252    }
253}
254impl<T: System> fmt::Debug for GraftMessage<T>
255where
256    T::NodeId: fmt::Debug,
257    T::MessageId: fmt::Debug,
258{
259    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
260        write!(
261            f,
262            "GraftMessage {{ sender: {:?}, message_id: {:?}, round: {:?} }}",
263            self.sender, self.message_id, self.round
264        )
265    }
266}
267
268/// `PRUNE` message.
269pub struct PruneMessage<T: System> {
270    /// The sender of the message.
271    pub sender: T::NodeId,
272}
273impl<T: System> PruneMessage<T> {
274    pub(crate) fn new(sender: &T::NodeId) -> Self {
275        PruneMessage {
276            sender: sender.clone(),
277        }
278    }
279}
280impl<T: System> Clone for PruneMessage<T> {
281    fn clone(&self) -> Self {
282        PruneMessage {
283            sender: self.sender.clone(),
284        }
285    }
286}
287impl<T: System> fmt::Debug for PruneMessage<T>
288where
289    T::NodeId: fmt::Debug,
290{
291    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
292        write!(f, "PruneMessage {{ sender: {:?} }}", self.sender)
293    }
294}