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
mod errors;
use crate::{client::MessageId, MessageType, WireMsg};
use bytes::Bytes;
pub use errors::Error;
use serde::{Deserialize, Serialize};
use sn_data_types::ReplicaPublicKeySet;
use std::{collections::BTreeMap, fmt, net::SocketAddr};
use xor_name::{Prefix, XorName};
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub enum Message {
GetSectionRequest(XorName),
GetSectionResponse(GetSectionResponse),
InfrastructureUpdate(ErrorResponse),
}
#[derive(Serialize, Deserialize, Hash, PartialEq, PartialOrd, Ord, Eq, Clone)]
pub struct InfrastructureInformation {
pub prefix: Prefix,
pub pk_set: ReplicaPublicKeySet,
pub elders: BTreeMap<XorName, SocketAddr>,
}
impl fmt::Debug for InfrastructureInformation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"InfrastructureInformation {{ prefix: {:?}, pk_set: PkSet {{ public_key: {:?} }}, elders: {:?} }}",
self.prefix,
self.pk_set.public_key(),
self.elders
)
}
}
#[derive(Debug, Serialize, Deserialize, Hash, PartialEq, PartialOrd, Ord, Eq, Clone)]
pub struct ErrorResponse {
pub correlation_id: MessageId,
pub error: Error,
}
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub enum GetSectionResponse {
Success(InfrastructureInformation),
Redirect(Vec<SocketAddr>),
SectionInfrastructureUpdate(Error),
}
impl Message {
pub fn from(bytes: Bytes) -> crate::Result<Self> {
let deserialized = WireMsg::deserialize(bytes)?;
if let MessageType::InfrastructureMessage(query) = deserialized {
Ok(query)
} else {
Err(crate::Error::FailedToParse(
"bytes as an infrastructure query message".to_string(),
))
}
}
pub fn serialize(&self) -> crate::Result<Bytes> {
WireMsg::serialize_infrastructure_msg(self)
}
}