From eed8f88e2b41f3e3ecf5b93e54948da3a0a2e996 Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Thu, 11 Jul 2019 12:25:19 +0200 Subject: [PATCH] Move the session handler to a dedicated file --- opensmtpd/src/lib.rs | 38 ++---------------------- opensmtpd/src/session_handler.rs | 50 ++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 35 deletions(-) create mode 100644 opensmtpd/src/session_handler.rs diff --git a/opensmtpd/src/lib.rs b/opensmtpd/src/lib.rs index 9588439..a8b059b 100644 --- a/opensmtpd/src/lib.rs +++ b/opensmtpd/src/lib.rs @@ -10,8 +10,10 @@ mod entry; mod errors; mod event_handlers; mod logger; +mod session_handler; -use log::{debug, error, warn}; +use crate::session_handler::SessionHandler; +use log::{error, warn}; use std::collections::HashMap; use std::io; use std::str::FromStr; @@ -44,40 +46,6 @@ pub enum Response { #[derive(Clone, Default)] pub struct NoContext; -struct SessionHandler { - entry_rx: mpsc::Receiver, - event_handlers: Vec>, -} - -impl SessionHandler { - fn new(entry_rx: mpsc::Receiver, handlers_rx: &mpsc::Receiver>) -> Self { - debug!( - "New thread for session {}", - thread::current().name().unwrap() - ); - let mut event_handlers = Vec::new(); - for h in handlers_rx.iter() { - debug!("Event handler registered"); - event_handlers.push(h); - } - SessionHandler { - entry_rx, - event_handlers, - } - } - - fn read_entries(&self) { - let mut context: T = Default::default(); - for e in self.entry_rx.iter() { - for h in self.event_handlers.iter() { - if h.is_callable(&e.event) { - h.call(&e, &mut context); - } - } - } - } -} - #[derive(Default)] pub struct SmtpIn { sessions: HashMap, thread::JoinHandle<()>)>, diff --git a/opensmtpd/src/session_handler.rs b/opensmtpd/src/session_handler.rs new file mode 100644 index 0000000..9c7eb79 --- /dev/null +++ b/opensmtpd/src/session_handler.rs @@ -0,0 +1,50 @@ +// Copyright (c) 2019 Rodolphe Bréard +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use crate::entry::Entry; +use crate::event_handlers::EventHandler; +use log::debug; +use std::sync::mpsc; +use std::thread; + +pub struct SessionHandler { + entry_rx: mpsc::Receiver, + event_handlers: Vec>, +} + +impl SessionHandler { + pub fn new( + entry_rx: mpsc::Receiver, + handlers_rx: &mpsc::Receiver>, + ) -> Self { + debug!( + "New thread for session {}", + thread::current().name().unwrap() + ); + let mut event_handlers = Vec::new(); + for h in handlers_rx.iter() { + debug!("Event handler registered"); + event_handlers.push(h); + } + SessionHandler { + entry_rx, + event_handlers, + } + } + + pub fn read_entries(&self) { + let mut context: T = Default::default(); + for e in self.entry_rx.iter() { + for h in self.event_handlers.iter() { + if h.is_callable(&e.event) { + h.call(&e, &mut context); + } + } + } + } +}