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
mod handler;
pub use handler::SystemHandler;
#[cfg(test)]
mod tests;
use crate::OckamError;
use ockam_core::compat::{boxed::Box, collections::BTreeMap};
use ockam_core::{Address, Result, Routed, Worker};
pub struct WorkerSystem<W: Worker> {
map: BTreeMap<Address, Box<dyn SystemHandler<W::Context, W::Message> + Send + 'static>>,
}
impl<W: Worker> Default for WorkerSystem<W> {
fn default() -> Self {
Self {
map: BTreeMap::new(),
}
}
}
impl<W: Worker> WorkerSystem<W> {
pub fn attach<A, H>(&mut self, addr: A, handler: H)
where
A: Into<Address>,
H: SystemHandler<W::Context, W::Message> + Send + 'static,
{
self.map.insert(addr.into(), Box::new(handler));
}
pub async fn handle_message(
&mut self,
ctx: &mut W::Context,
msg: Routed<W::Message>,
) -> Result<()> {
let addr = msg.msg_addr();
match self.map.get_mut(&addr) {
Some(handle) => handle.handle_message(ctx, msg).await,
None => Err(OckamError::SystemAddressNotBound.into()),
}
}
}