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
use crate::relay::ShutdownHandle;
use crate::tokio::sync::mpsc::{channel, Receiver, Sender};
use crate::{error::Error, relay::RelayMessage};
use ockam_core::compat::vec::Vec;
use ockam_core::{Address, AddressSet};
#[derive(Debug)]
pub enum NodeMessage {
StartWorker(AddressSet, Sender<RelayMessage>, Sender<NodeReplyResult>),
ListWorkers(Sender<NodeReplyResult>),
StopWorker(Address, Sender<NodeReplyResult>),
StartProcessor(
Address,
Sender<RelayMessage>,
Sender<NodeReplyResult>,
ShutdownHandle,
),
StopProcessor(Address, Sender<NodeReplyResult>),
StopNode,
SenderReq(Address, Sender<NodeReplyResult>),
Router(u8, Address, Sender<NodeReplyResult>),
CheckAddress(AddressSet, Sender<NodeReplyResult>),
}
impl NodeMessage {
pub fn start_worker(
address: AddressSet,
sender: Sender<RelayMessage>,
) -> (Self, Receiver<NodeReplyResult>) {
let (tx, rx) = channel(1);
(Self::StartWorker(address, sender, tx), rx)
}
pub fn start_processor(
address: Address,
sender: Sender<RelayMessage>,
shutdown_handle: ShutdownHandle,
) -> (Self, Receiver<NodeReplyResult>) {
let (tx, rx) = channel(1);
(
Self::StartProcessor(address, sender, tx, shutdown_handle),
rx,
)
}
pub fn stop_processor(address: Address) -> (Self, Receiver<NodeReplyResult>) {
let (tx, rx) = channel(1);
(Self::StopProcessor(address, tx), rx)
}
pub fn list_workers() -> (Self, Receiver<NodeReplyResult>) {
let (tx, rx) = channel(1);
(Self::ListWorkers(tx), rx)
}
pub fn stop_worker(address: Address) -> (Self, Receiver<NodeReplyResult>) {
let (tx, rx) = channel(1);
(Self::StopWorker(address, tx), rx)
}
pub fn stop_node() -> Self {
Self::StopNode
}
pub fn sender_request(route: Address) -> (Self, Receiver<NodeReplyResult>) {
let (tx, rx) = channel(1);
(Self::SenderReq(route, tx), rx)
}
pub fn check_address(addrs: AddressSet) -> (Self, Receiver<NodeReplyResult>) {
let (tx, rx) = channel(1);
(Self::CheckAddress(addrs, tx), rx)
}
}
pub type NodeReplyResult = Result<NodeReply, NodeError>;
#[derive(Debug)]
pub enum NodeReply {
Ok,
Workers(Vec<Address>),
Sender {
addr: Address,
sender: Sender<RelayMessage>,
wrap: bool,
},
}
#[derive(Debug)]
pub enum NodeError {
NoSuchWorker(Address),
NoSuchProcessor(Address),
WorkerExists(Address),
RouterExists,
}
impl NodeReply {
pub fn ok() -> NodeReplyResult {
Ok(NodeReply::Ok)
}
pub fn no_such_worker(a: Address) -> NodeReplyResult {
Err(NodeError::NoSuchWorker(a))
}
pub fn no_such_processor(a: Address) -> NodeReplyResult {
Err(NodeError::NoSuchProcessor(a))
}
pub fn worker_exists(a: Address) -> NodeReplyResult {
Err(NodeError::WorkerExists(a))
}
pub fn router_exists() -> NodeReplyResult {
Err(NodeError::RouterExists)
}
pub fn workers(v: Vec<Address>) -> NodeReplyResult {
Ok(Self::Workers(v))
}
pub fn sender(addr: Address, sender: Sender<RelayMessage>, wrap: bool) -> NodeReplyResult {
Ok(NodeReply::Sender { addr, sender, wrap })
}
pub fn take_sender(self) -> Result<(Address, Sender<RelayMessage>, bool), Error> {
match self {
Self::Sender { addr, sender, wrap } => Ok((addr, sender, wrap)),
_ => Err(Error::InternalIOFailure),
}
}
pub fn take_workers(self) -> Result<Vec<Address>, Error> {
match self {
Self::Workers(w) => Ok(w),
_ => Err(Error::InternalIOFailure),
}
}
pub fn is_ok(self) -> Result<(), Error> {
match self {
Self::Ok => Ok(()),
_ => Err(Error::InternalIOFailure),
}
}
}