-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
registry.rs
131 lines (99 loc) · 2.78 KB
/
registry.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
use crate::broadcast::{BastionMessage, Sender};
use crate::children::{Child, Children};
use crate::supervisor::Supervisor;
use chashmap::CHashMap;
use uuid::Uuid;
pub(super) struct Registry {
registered: CHashMap<Uuid, Registrant>,
}
struct Registrant {
sender: Sender,
ty: RegistrantType,
}
#[derive(Eq, PartialEq)]
enum RegistrantType {
Supervisor,
Children,
Child,
}
impl Registry {
pub(super) fn new() -> Self {
// FIXME: with_capacity?
let registered = CHashMap::new();
Registry { registered }
}
pub(super) fn add_supervisor(&self, supervisor: &Supervisor) {
let id = supervisor.id().clone();
let sender = supervisor.sender().clone();
let registrant = Registrant::supervisor(sender);
self.registered.insert(id, registrant);
}
pub(super) fn add_children(&self, children: &Children) {
let id = children.id().clone();
let sender = children.sender().clone();
let registrant = Registrant::children(sender);
self.registered.insert(id, registrant);
}
pub(super) fn add_child(&self, child: &Child) {
let id = child.id().clone();
let sender = child.sender().clone();
let registrant = Registrant::child(sender);
self.registered.insert(id, registrant);
}
pub(super) fn send_supervisor(&self, id: &Uuid, msg: BastionMessage) -> Result<(), BastionMessage> {
let registrant = if let Some(registrant) = self.registered.get(id) {
registrant
} else {
return Err(msg);
};
if !registrant.is_supervisor() {
return Err(msg);
}
registrant.sender.unbounded_send(msg).map_err(|err| err.into_inner())
}
pub(super) fn send_children(&self, id: &Uuid, msg: BastionMessage) -> Result<(), BastionMessage> {
let registrant = if let Some(registrant) = self.registered.get(id) {
registrant
} else {
return Err(msg);
};
if !registrant.is_children() {
return Err(msg);
}
registrant.sender.unbounded_send(msg).map_err(|err| err.into_inner())
}
pub(super) fn send_child(&self, id: &Uuid, msg: BastionMessage) -> Result<(), BastionMessage> {
let registrant = if let Some(registrant) = self.registered.get(id) {
registrant
} else {
return Err(msg);
};
if !registrant.is_child() {
return Err(msg);
}
registrant.sender.unbounded_send(msg).map_err(|err| err.into_inner())
}
}
impl Registrant {
fn supervisor(sender: Sender) -> Self {
let ty = RegistrantType::Supervisor;
Registrant { sender, ty }
}
fn children(sender: Sender) -> Self {
let ty = RegistrantType::Children;
Registrant { sender, ty }
}
fn child(sender: Sender) -> Self {
let ty = RegistrantType::Child;
Registrant { sender, ty }
}
fn is_supervisor(&self) -> bool {
self.ty == RegistrantType::Supervisor
}
fn is_children(&self) -> bool {
self.ty == RegistrantType::Children
}
fn is_child(&self) -> bool {
self.ty == RegistrantType::Child
}
}