Use a builder instead of a raw function
This pattern will, in the future, allow the registration of events handlers and context objects.
This commit is contained in:
parent
c25dfb253a
commit
98e4beadd3
3 changed files with 75 additions and 70 deletions
|
@ -1,6 +1,7 @@
|
|||
use env_logger::{Builder, Env};
|
||||
use opensmtpd::SmtpIn;
|
||||
|
||||
fn main() {
|
||||
Builder::from_env(Env::default().default_filter_or("debug")).init();
|
||||
opensmtpd::run();
|
||||
SmtpIn::new().run();
|
||||
}
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
use crate::errors::Error;
|
||||
use nom::{
|
||||
alt, alt_complete, call, complete, cond, do_parse, error_position, map_res, named, tag,
|
||||
take_until, take_while,
|
||||
};
|
||||
use nom::{alt, alt_complete, call, complete, cond, do_parse, error_position, map_res, named, tag,
|
||||
take_until, take_while};
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Kind {
|
||||
|
|
38
src/lib.rs
38
src/lib.rs
|
@ -9,6 +9,11 @@ use std::io;
|
|||
use std::sync::mpsc;
|
||||
use std::thread;
|
||||
|
||||
pub struct SmtpIn {
|
||||
sessions: HashMap<u64, (mpsc::Sender<Entry>, thread::JoinHandle<()>)>,
|
||||
}
|
||||
|
||||
impl SmtpIn {
|
||||
/// Read a line from the standard input.
|
||||
/// Since EOF should not append, it is considered as an error.
|
||||
fn read() -> Result<String, Error> {
|
||||
|
@ -22,12 +27,9 @@ fn read() -> Result<String, Error> {
|
|||
|
||||
/// Dispatch the entry into its session's thread. If such thread does not
|
||||
/// already exists, creates it.
|
||||
fn dispatch(
|
||||
sessions: &mut HashMap<u64, (mpsc::Sender<Entry>, thread::JoinHandle<()>)>,
|
||||
input: &str,
|
||||
) -> Result<(), Error> {
|
||||
fn dispatch(&mut self, input: &str) -> Result<(), Error> {
|
||||
let entry = Entry::from_str(input)?;
|
||||
let channel = match sessions.get(&entry.session_id) {
|
||||
let channel = match self.sessions.get(&entry.session_id) {
|
||||
Some((r, _)) => r,
|
||||
None => {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
|
@ -41,8 +43,8 @@ fn dispatch(
|
|||
debug!("thread {}: {:?}", thread::current().name().unwrap(), e);
|
||||
}
|
||||
})?;
|
||||
sessions.insert(entry.session_id, (tx, handle));
|
||||
let (r, _) = sessions.get(&entry.session_id).unwrap();
|
||||
self.sessions.insert(entry.session_id, (tx, handle));
|
||||
let (r, _) = self.sessions.get(&entry.session_id).unwrap();
|
||||
r
|
||||
}
|
||||
};
|
||||
|
@ -54,11 +56,9 @@ fn dispatch(
|
|||
/// drained so all the references to the senders are dropped, which will
|
||||
/// cause the receivers threads to exit. Then, we uses the join handlers in
|
||||
/// order to wait for the actual exit.
|
||||
fn graceful_exit_children(
|
||||
sessions: &mut HashMap<u64, (mpsc::Sender<Entry>, thread::JoinHandle<()>)>,
|
||||
) {
|
||||
fn graceful_exit_children(&mut self) {
|
||||
let mut handles = Vec::new();
|
||||
for (_, (_, h)) in sessions.drain() {
|
||||
for (_, (_, h)) in self.sessions.drain() {
|
||||
handles.push(h);
|
||||
}
|
||||
for h in handles {
|
||||
|
@ -66,21 +66,27 @@ fn graceful_exit_children(
|
|||
}
|
||||
}
|
||||
|
||||
pub fn new() -> Self {
|
||||
SmtpIn {
|
||||
sessions: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Run the infinite loop that will read and process input from stdin.
|
||||
pub fn run() {
|
||||
let mut sessions = HashMap::new();
|
||||
pub fn run(&mut self) {
|
||||
loop {
|
||||
let line = match read() {
|
||||
let line = match SmtpIn::read() {
|
||||
Ok(l) => l,
|
||||
Err(e) => {
|
||||
graceful_exit_children(&mut sessions);
|
||||
self.graceful_exit_children();
|
||||
error!("{}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
};
|
||||
match dispatch(&mut sessions, &line) {
|
||||
match self.dispatch(&line) {
|
||||
Ok(_) => {}
|
||||
Err(e) => warn!("{}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue