This repository has been archived by the owner on Jun 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
mod.rs
69 lines (61 loc) · 2.58 KB
/
mod.rs
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
// Copyright 2021 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under the MIT license <LICENSE-MIT
// https://opensource.org/licenses/MIT> or the Modified BSD license <LICENSE-BSD
// https://opensource.org/licenses/BSD-3-Clause>, at your option. This file may not be copied,
// modified, or distributed except according to those terms. Please review the Licences for the
// specific language governing permissions and limitations relating to use of the SAFE Network
// Software.
mod node_cmd;
use crate::{MessageType, WireMsg};
use bytes::Bytes;
pub use node_cmd::{
NodeCmd, NodeCmdError, NodeCmdMessage, NodeDataError, NodeDataQueryResponse, NodeEvent,
NodeQuery, NodeQueryResponse, NodeRewardQuery, NodeSystemCmd, NodeSystemQuery,
NodeSystemQueryResponse, NodeTransferCmd, NodeTransferError, NodeTransferQuery,
NodeTransferQueryResponse,
};
use serde::{Deserialize, Serialize};
use std::fmt::{self, Debug, Formatter};
use threshold_crypto::PublicKey as BlsPublicKey;
use xor_name::XorName;
/// Node message sent over the network.
// TODO: this is currently holding just bytes as a placeholder, next step
// is to move all actual node messages structs and definitions within it.
#[derive(Clone, Eq, Serialize, Deserialize)]
pub struct NodeMessage(#[serde(with = "serde_bytes")] pub Vec<u8>);
impl NodeMessage {
/// Creates a new instance which wraps the provided node message bytes.
pub fn new(bytes: Bytes) -> Self {
Self(bytes.to_vec())
}
/// Convenience function to deserialize a 'NodeMessage' from bytes received over the wire.
/// It returns an error if the bytes don't correspond to a node message.
pub fn from(bytes: Bytes) -> crate::Result<Self> {
let deserialized = WireMsg::deserialize(bytes)?;
if let MessageType::NodeMessage { msg, .. } = deserialized {
Ok(msg)
} else {
Err(crate::Error::FailedToParse(
"bytes as a node message".to_string(),
))
}
}
/// serialize this NodeMessage into bytes ready to be sent over the wire.
pub fn serialize(&self, dest: XorName, dest_section_pk: BlsPublicKey) -> crate::Result<Bytes> {
WireMsg::serialize_node_msg(self, dest, dest_section_pk)
}
}
impl PartialEq for NodeMessage {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl Debug for NodeMessage {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
formatter
.debug_tuple("NodeMessage")
.field(&format_args!("{:10}", hex_fmt::HexFmt(&self.0)))
.finish()
}
}