Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread module to be used for server connection handler #18

Merged
merged 3 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pub mod cli;
pub mod error;
pub mod helpers;
pub mod net;
pub mod report;
pub mod threshold;
pub mod user;
5 changes: 5 additions & 0 deletions src/net/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub use self::server::Server;
pub use self::thread::Thread;

mod server;
mod thread;
46 changes: 46 additions & 0 deletions src/net/server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::net::Thread;

pub struct Server {
connection_handler_thread: Thread,
}

impl Server {
#[must_use]
pub fn new() -> Server {
Server {
connection_handler_thread: Thread::new(),
}
}

pub fn start(&self) {
self.start_connection_handler();
}

fn start_connection_handler(&self) {
self.connection_handler_thread.execute(|| {
// listen
// read
// write
});
}
}

impl Default for Server {
fn default() -> Self {
Self::new()
}
}
taikiy marked this conversation as resolved.
Show resolved Hide resolved

#[cfg(test)]
mod tests {
use super::Server;
use std::thread;
use std::time;

#[test]
fn test_no_panic() {
let server = Server::new();
server.start();
thread::sleep(time::Duration::from_millis(500));
}
taikiy marked this conversation as resolved.
Show resolved Hide resolved
}
85 changes: 85 additions & 0 deletions src/net/thread.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use std::sync::{mpsc, Arc, Mutex};
use std::thread;

pub struct Thread {
worker: Worker,
sender: mpsc::Sender<Message>,
}

pub struct Worker {
taikiy marked this conversation as resolved.
Show resolved Hide resolved
id: usize,
taikiy marked this conversation as resolved.
Show resolved Hide resolved
thread: Option<thread::JoinHandle<()>>,
taikiy marked this conversation as resolved.
Show resolved Hide resolved
}

type Job = Box<dyn FnOnce() + Send + 'static>;

pub enum Message {
NewJob(Job),
Terminate,
}

impl Thread {
#[must_use]
pub fn new() -> Thread {
let (sender, receiver) = mpsc::channel();
let receiver = Arc::new(Mutex::new(receiver));
taikiy marked this conversation as resolved.
Show resolved Hide resolved

Thread {
worker: Worker::new(0, receiver),
sender,
}
}

/// Sends a function to the running thread for it to be executed.
/// # Panics
/// If the thread has been terminated.
pub fn execute<F>(&self, f: F)
where
F: FnOnce() + Send + 'static,
martinthomson marked this conversation as resolved.
Show resolved Hide resolved
{
let job = Box::new(f);

self.sender.send(Message::NewJob(job)).unwrap();
taikiy marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl Default for Thread {
fn default() -> Self {
Self::new()
}
}

impl Drop for Thread {
fn drop(&mut self) {
println!("Terminating thread {}", self.worker.id);
taikiy marked this conversation as resolved.
Show resolved Hide resolved
self.sender.send(Message::Terminate).unwrap();
taikiy marked this conversation as resolved.
Show resolved Hide resolved

if let Some(thread) = self.worker.thread.take() {
thread.join().unwrap();
}
}
}

impl Worker {
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Message>>>) -> Worker {
taikiy marked this conversation as resolved.
Show resolved Hide resolved
let thread = thread::spawn(move || loop {
let message = receiver.lock().unwrap().recv().unwrap();
taikiy marked this conversation as resolved.
Show resolved Hide resolved

match message {
Message::NewJob(job) => {
println!("Worker {} received a job; executing.", id);
job();
}
Message::Terminate => {
println!("Worker {} is shutting down.", id);
break;
}
}
});

Worker {
id,
thread: Some(thread),
}
}
}