summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorluwenpeng <[email protected]>2023-08-18 16:03:10 +0800
committerluwenpeng <[email protected]>2023-08-18 16:05:11 +0800
commitf18d8eadca0a2a81c7557c0b102e0186cfb1de0b (patch)
tree2c5652375b3b43f6d73659da4e846f0d34a48a4b /src
parente826e40db75b48f6eb1ec72f2d4886a83786b840 (diff)
[feature] Define session manager and implement methods for manipulating the session manager
Diffstat (limited to 'src')
-rw-r--r--src/event/event.rs6
-rw-r--r--src/session/manager.rs125
-rw-r--r--src/session/mod.rs3
3 files changed, 130 insertions, 4 deletions
diff --git a/src/event/event.rs b/src/event/event.rs
index de360ac..eadd622 100644
--- a/src/event/event.rs
+++ b/src/event/event.rs
@@ -99,7 +99,7 @@ mod tests {
#[test]
fn test_event_manager() {
// create plugin
- let mut plugin_cb = Pulgin::new(1, Rc::new(RefCell::new(String::from("Hello"))));
+ let plugin_cb = Pulgin::new(1, Rc::new(RefCell::new(String::from("Hello"))));
// create event manager
let mut mgr: EventManager = EventManager::new();
@@ -124,9 +124,9 @@ mod tests {
// dispatch event
let bytes = [0x48, 0x73];
- let mut packet = Packet::new(&bytes, bytes.len() as u32);
+ let packet = Packet::new(&bytes, bytes.len() as u32);
mgr.dispatch(&packet);
- assert_eq!(1, 0);
+ // assert_eq!(1, 0);
}
}
diff --git a/src/session/manager.rs b/src/session/manager.rs
new file mode 100644
index 0000000..ce07223
--- /dev/null
+++ b/src/session/manager.rs
@@ -0,0 +1,125 @@
+use crate::session::session::Session;
+use crate::session::session::SessionState;
+use chrono::Utc;
+use std::collections::HashMap;
+
+/******************************************************************************
+ * Struct
+ ******************************************************************************/
+
+#[derive(Debug)]
+pub struct SessionManager {
+ sessions: HashMap<String, Session>,
+}
+
+/******************************************************************************
+ * API
+ ******************************************************************************/
+
+impl SessionManager {
+ pub fn new() -> SessionManager {
+ SessionManager {
+ sessions: HashMap::new(),
+ }
+ }
+
+ pub fn get_session(&self, session_id: &str) -> Option<&Session> {
+ self.sessions.get(session_id)
+ }
+
+ pub fn get_session_mut(&mut self, session_id: &str) -> Option<&mut Session> {
+ self.sessions.get_mut(session_id)
+ }
+
+ pub fn insert_session(&mut self, session_id: String, session: Session) {
+ self.sessions.insert(session_id, session);
+ }
+
+ pub fn remove_session(&mut self, session_id: &str) -> Option<Session> {
+ self.sessions.remove(session_id)
+ }
+
+ pub fn expire_sessions(&mut self) {
+ let now = Utc::now().timestamp();
+ let mut expired_sessions = Vec::new();
+ for (session_id, session) in &self.sessions {
+ if session.get_session_expire_ts() < now {
+ expired_sessions.push(session_id.clone());
+ }
+ }
+ for session_id in expired_sessions {
+ let option = self.remove_session(&session_id);
+ if let Some(mut session) = option {
+ print!("Session expired: {}", session_id);
+ session.set_session_state(SessionState::Expired);
+ session.set_session_end_ts(Utc::now().timestamp());
+ // TODO trigger session expire event or call session's on_expire() method
+ }
+ }
+ }
+}
+
+/******************************************************************************
+ * TEST
+ ******************************************************************************/
+
+#[cfg(test)]
+mod tests {
+ use super::SessionManager;
+ use crate::session::session::Session;
+ use crate::session::session::SessionState;
+ use crate::session::tuple::FiveTuple;
+ use chrono::Utc;
+ use std::net::Ipv4Addr;
+ // use std::{thread, time};
+
+ #[test]
+ fn test_session_manager() {
+ // Create Session Manager
+ let mut session_mgr = SessionManager::new();
+
+ let five_tuple_4 = FiveTuple::from_v4(
+ Ipv4Addr::new(192, 168, 0, 1),
+ Ipv4Addr::new(192, 168, 0, 2),
+ 2345,
+ 80,
+ 1,
+ );
+ let session_id = five_tuple_4.to_string();
+
+ // Search Session
+ let option = session_mgr.get_session_mut(session_id.as_str());
+ assert_eq!(option.is_none(), true);
+
+ // Insert Session
+ let mut session = Session::new(five_tuple_4);
+ session.set_session_c2s_metrics(0, 2, 0, 120);
+ session.set_session_exdata("Hello".to_string(), "Word".to_string());
+ session_mgr.insert_session(session_id.clone(), session);
+
+ // Update Session
+ let option = session_mgr.get_session_mut(session_id.as_str());
+ assert_eq!(option.is_some(), true);
+ if let Some(session) = option {
+ session.set_session_exdata("Hello".to_string(), "Word!!!".to_string());
+ session.set_session_last_seen_ts(Utc::now().timestamp());
+ session.set_session_state(SessionState::Active);
+ }
+
+ // Expire Session
+ // thread::sleep(time::Duration::from_secs(61));
+ // session_mgr.expire_sessions();
+
+ // Delete session
+ let option = session_mgr.remove_session(session_id.as_str());
+ assert_eq!(option.is_some(), true);
+
+ // Research Session
+ let option = session_mgr.get_session_mut(session_id.as_str());
+ assert_eq!(option.is_none(), true);
+
+ dbg!(session_mgr);
+
+ // assert_eq!(1, 0);
+ }
+}
diff --git a/src/session/mod.rs b/src/session/mod.rs
index efb9275..38592c1 100644
--- a/src/session/mod.rs
+++ b/src/session/mod.rs
@@ -1,2 +1,3 @@
pub mod tuple;
-pub mod session; \ No newline at end of file
+pub mod session;
+pub mod manager; \ No newline at end of file