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
extern crate log;
use log::{debug,info,warn};
use seda_bus::{MessageBus};
use std::collections::HashMap;
use ra_common::models::Envelope;
pub trait Service {
fn operate(&mut self, operation: u8, env: Envelope);
}
pub struct LogService {
}
impl LogService {
pub fn new() -> LogService {
LogService {}
}
fn log(&mut self, env: Envelope) {
info!("{}", String::from_utf8(env.msg).unwrap())
}
}
impl Service for LogService {
fn operate(&mut self, operation: u8, env: Envelope) {
match operation {
1 => self.log(env),
_ => warn!("Unsupported op {}",operation)
}
}
}
pub struct ServiceBus {
m_bus: MessageBus,
services: HashMap<u8, Box<dyn Service>>
}
impl ServiceBus {
pub fn new() -> ServiceBus {
ServiceBus {
m_bus: MessageBus::new(),
services: HashMap::new()
}
}
pub fn register(&mut self, service: Box<dyn Service>) -> u8 {
let id :u8 = self.m_bus.register();
self.services.insert(id, service);
id
}
pub fn unregister(&mut self, service_id: u8) -> bool {
self.m_bus.unregister(service_id)
}
pub fn send(&mut self, mut env: Envelope) -> bool {
match env.slip.end_route() {
Some(route) => {
match self.services.get_mut(&route.service) {
Some(service) => {
service.operate(route.op, env);
true
},
None => false
}
},
None => {
debug!("{}", "No current route.");
true
}
}
}
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}