-
Notifications
You must be signed in to change notification settings - Fork 26
Thread module to be used for server connection handler #18
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub use self::server::Server; | ||
pub use self::thread::{Message, Thread}; | ||
|
||
mod server; | ||
mod thread; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
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(); | ||
} | ||
|
||
/// Spawns a new thread to handle incoming connections. | ||
/// # Panics | ||
/// If the thread could not be spawned. | ||
fn start_connection_handler(&self) { | ||
if let Err(e) = self.connection_handler_thread.execute(|| { | ||
// listen | ||
// read | ||
// write | ||
}) { | ||
panic!("Could not start the connection handler: {}", e); | ||
} | ||
} | ||
} | ||
|
||
impl Default for Server { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Server; | ||
|
||
#[test] | ||
fn no_panic() { | ||
let server = Server::new(); | ||
server.start(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
use log::{error, info}; | ||
use std::sync::{mpsc, Arc, Mutex}; | ||
use std::thread; | ||
|
||
use crate::error::Res; | ||
|
||
pub struct Thread { | ||
worker: Worker, | ||
sender: mpsc::Sender<Message>, | ||
} | ||
|
||
struct Worker { | ||
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::spawn(0, receiver), | ||
sender, | ||
} | ||
} | ||
|
||
/// Sends a function to the running thread for it to be executed. | ||
/// # Errors | ||
/// If the thread has been terminated. | ||
pub fn execute<F>(&self, f: F) -> Res<()> | ||
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))?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl Default for Thread { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl Drop for Thread { | ||
fn drop(&mut self) { | ||
info!("Terminating thread {}", self.worker.id); | ||
if let Ok(()) = self.sender.send(Message::Terminate) { | ||
if let Some(thread) = self.worker.thread.take() { | ||
thread.join().unwrap(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl Worker { | ||
fn spawn(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Message>>>) -> Worker { | ||
let thread = thread::spawn(move || loop { | ||
let message: Message; | ||
|
||
// Receive a message and release the lock before executing anything else. | ||
if let Ok(receiver) = receiver.lock() { | ||
if let Ok(msg) = receiver.recv() { | ||
message = msg; | ||
} else { | ||
error!("Sender channel is closed."); | ||
break; | ||
} | ||
} else { | ||
error!("Failed to lock the mutex."); | ||
break; | ||
} | ||
|
||
match message { | ||
Message::NewJob(job) => { | ||
info!("Worker {} received a job; executing.", id); | ||
job(); | ||
} | ||
Message::Terminate => { | ||
info!("Worker {} is shutting down.", id); | ||
break; | ||
} | ||
} | ||
}); | ||
|
||
Worker { | ||
id, | ||
thread: Some(thread), | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.