-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsg.rs
252 lines (233 loc) · 6.26 KB
/
msg.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// SPDX-License-Identifier: Apache-2.0
// Copyright Open Network Fabric Authors
pub use crate::objects::*;
use crate::wire::WireError;
use std::fmt::Display;
/* Msg definitions */
#[doc = "A request message"]
#[derive(Debug, PartialEq)]
pub struct RpcRequest {
pub op: RpcOp,
pub seqn: MsgSeqn,
pub obj: Option<RpcObject>,
}
#[doc = "A response message"]
#[derive(Debug, PartialEq)]
pub struct RpcResponse {
pub op: RpcOp,
pub seqn: MsgSeqn,
pub rescode: RpcResultCode,
// wire: num objects
pub objs: Vec<RpcObject>,
}
#[doc = "A control message"]
#[derive(Debug, PartialEq, Default)]
pub struct RpcControl {
// Todo: not needed now
}
#[doc = "A notification message"]
#[derive(Debug, PartialEq, Default)]
pub struct RpcNotification {
// Todo: not needed now
}
#[doc = "A generic message wrapping all possible message types"]
#[derive(Debug, PartialEq)]
pub enum RpcMsg {
Control(RpcControl),
Request(RpcRequest),
Response(RpcResponse),
Notification(RpcNotification),
}
/* Msg: utils */
impl RpcMsg {
#[allow(dead_code)]
pub(crate) fn get_control(&self) -> Result<&RpcControl, ()> {
if let RpcMsg::Control(data) = self {
Ok(data)
} else {
Err(()) // should panic instead ?
}
}
#[allow(dead_code)]
pub(crate) fn get_request(&self) -> Result<&RpcRequest, ()> {
if let RpcMsg::Request(data) = self {
Ok(data)
} else {
Err(()) // should panic instead ?
}
}
#[allow(dead_code)]
pub(crate) fn get_response(&self) -> Result<&RpcResponse, ()> {
if let RpcMsg::Response(data) = self {
Ok(data)
} else {
Err(()) // should panic instead ?
}
}
#[allow(dead_code)]
pub(crate) fn get_notification(&self) -> Result<&RpcNotification, ()> {
if let RpcMsg::Notification(data) = self {
Ok(data)
} else {
Err(()) // should panic instead ?
}
}
#[allow(dead_code)]
pub(crate) fn is_control(&self) -> bool {
matches!(self, &RpcMsg::Control(_))
}
#[allow(dead_code)]
pub(crate) fn is_request(&self) -> bool {
matches!(self, &RpcMsg::Request(_))
}
#[allow(dead_code)]
pub(crate) fn is_response(&self) -> bool {
matches!(self, &RpcMsg::Response(_))
}
#[allow(dead_code)]
pub(crate) fn is_notification(&self) -> bool {
matches!(self, &RpcMsg::Notification(_))
}
}
/* Request: utils */
impl RpcRequest {
#[allow(dead_code)]
pub fn new(op: RpcOp, seqn: u64) -> Self {
Self {
op,
seqn,
obj: None,
}
}
#[allow(dead_code)]
pub fn set_object(mut self, object: RpcObject) -> Self {
self.obj = Some(object);
self
}
#[allow(dead_code)]
pub fn get_op(&self) -> RpcOp {
self.op
}
#[allow(dead_code)]
pub fn get_seqn(&self) -> u64 {
self.seqn
}
#[allow(dead_code)]
pub fn get_object(&self) -> Option<&RpcObject> {
self.obj.as_ref()
}
}
/* Response: utils */
impl RpcResponse {
#[allow(dead_code)]
pub fn new(op: RpcOp, seqn: u64, rescode: RpcResultCode) -> Self {
Self {
op,
seqn,
rescode,
objs: vec![],
}
}
#[allow(dead_code)]
pub fn add_object(&mut self, object: RpcObject) -> Result<(), WireError> {
if self.objs.len() == MsgNumObjects::MAX as usize {
Err(WireError::TooManyObjects)
} else {
self.objs.push(object);
Ok(())
}
}
}
#[doc = "Utility trait to help building `RpcMsgs` and encoding them.
Each message type may implement it. An implementation with generics may be possible too,
but this is simpler and more explicit."]
pub trait WrapMsg {
#[doc = "Wraps a specific message into an RpcMsg, consuming it."]
fn wrap_in_msg(self) -> RpcMsg;
#[doc = "Tells what type an RpcMsg contains. The return value MsgType is already a wire type"]
fn msg_type(&self) -> MsgType;
}
impl WrapMsg for RpcMsg {
fn wrap_in_msg(self) -> RpcMsg {
self
}
fn msg_type(&self) -> MsgType {
match self {
RpcMsg::Control(m) => m.msg_type(),
RpcMsg::Request(m) => m.msg_type(),
RpcMsg::Response(m) => m.msg_type(),
RpcMsg::Notification(m) => m.msg_type(),
}
}
}
impl WrapMsg for RpcRequest {
fn wrap_in_msg(self) -> RpcMsg {
RpcMsg::Request(self)
}
fn msg_type(&self) -> MsgType {
MsgType::Request
}
}
impl WrapMsg for RpcResponse {
fn wrap_in_msg(self) -> RpcMsg {
RpcMsg::Response(self)
}
fn msg_type(&self) -> MsgType {
MsgType::Response
}
}
impl WrapMsg for RpcNotification {
fn wrap_in_msg(self) -> RpcMsg {
RpcMsg::Notification(self)
}
fn msg_type(&self) -> MsgType {
MsgType::Notification
}
}
impl WrapMsg for RpcControl {
fn wrap_in_msg(self) -> RpcMsg {
RpcMsg::Control(self)
}
fn msg_type(&self) -> MsgType {
MsgType::Control
}
}
/* Display for terser logs */
impl Display for RpcRequest {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Request #{} {:?}", self.seqn, self.op)?;
if let Some(object) = &self.obj {
write!(f, " {}", object)?;
}
Ok(())
}
}
impl Display for RpcResponse {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Response to #{} result: {:?}", self.seqn, self.rescode)?;
for object in &self.objs {
write!(f, " {}", object)?;
}
Ok(())
}
}
impl Display for RpcNotification {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Notification")
}
}
impl Display for RpcControl {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Control")
}
}
impl Display for RpcMsg {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
RpcMsg::Control(m) => m.fmt(f),
RpcMsg::Request(m) => m.fmt(f),
RpcMsg::Response(m) => m.fmt(f),
RpcMsg::Notification(m) => m.fmt(f),
}
}
}