1#[derive(Debug, Clone)]
2pub struct Session{
3 is_self_a_router: bool,
4 subscriptions: Vec<String>,
5}
6
7impl Session{
8 pub fn new() -> Self{
9 Self{ is_self_a_router: false, subscriptions: Vec::new()}
10 }
11 pub fn set_a_router(&mut self, is_a_router: bool) -> Option<()>{
12 match is_a_router != self.is_self_a_router {
13 true => {
14 self.is_self_a_router = is_a_router;
15 Some(())
16 }
17 false => { None }
18 }
19 }
20 pub fn is_a_router(&self) -> bool{ self.is_self_a_router }
21 pub fn clear_subscriptions(&mut self) -> &Self{
22 self.subscriptions = Vec::new();
23 self
24 }
25 pub fn sub(&mut self, key: String) -> bool{
26 if self.subscriptions.contains(&key){
27 return false;
28 }
29 self.subscriptions.push(key);
30 self.subscriptions.sort();
31 return true;
32 }
33 pub fn unsub(&mut self, key: String) -> bool{
34 match self.subscriptions.binary_search(&key){
35 Ok(position) => {
36 self.subscriptions.remove(position);
37 true
38 }
39 Err(_) => { false }
40 }
41 }
42 pub fn is_sub(&self, key: &String) -> bool{
43 self.subscriptions.contains(&key)
44 }
45 pub fn get_subscriptions(&self) -> Vec<String>{
46 self.subscriptions.clone()
47 }
48}