1use std::collections::HashMap;
4use std::sync::{Arc, RwLock};
5
6use crate::session::stomp_session::{StompSession, FLAG_ADMIN, FLAG_DOWNSTREAM};
7use chrono::{Utc, DateTime};
8
9pub struct Context {
12 pub session: Arc<RwLock<StompSession>>,
14 pub attributes: HashMap<&'static str, String>,
16 pub is_secure: bool,
18 pub is_web_sockets: bool,
20 is_admin: Option<bool>,
23 is_downstream: Option<bool>,
25 timestamp: Option<DateTime<Utc>>,
27}
28
29impl Context {
30 pub(crate) fn new(session: Arc<RwLock<StompSession>>) -> Context {
31 Context {
32 session,
33 attributes: Default::default(),
34 is_secure: false,
35 is_web_sockets: false,
36 is_admin: None,
37 is_downstream: None,
38 timestamp: None,
39 }
40 }
41
42 pub fn mock() -> Context {
44 Context {
45 session: Arc::new(RwLock::new(StompSession::new())),
46 attributes: Default::default(),
47 is_secure: false,
48 is_web_sockets: false,
49 is_admin: None,
50 is_downstream: None,
51 timestamp: None,
52 }
53 }
54
55 pub fn now(&mut self) -> DateTime<Utc> {
57 if let None = self.timestamp {
58 self.timestamp = Some(Utc::now());
59 }
60 self.timestamp.unwrap()
61 }
62
63 pub fn is_admin(&mut self) -> bool {
65 if let None = self.is_admin {
66 self.is_admin = Some(self.session.read().unwrap().get_flag(FLAG_ADMIN));
67 }
68 self.is_admin.unwrap()
69 }
70
71 pub fn is_downstream(&mut self) -> bool {
73 if let None = self.is_admin {
74 self.is_downstream = Some(self.session.read().unwrap().get_flag(FLAG_DOWNSTREAM));
75 }
76 self.is_downstream.unwrap()
77 }
78
79 pub(crate) fn downstream(&self) -> String {
81 self.session.read().unwrap().downstream_connector.as_ref().unwrap().read().unwrap().downstream.name.clone()
82 }
83}