Skip to content

Commit

Permalink
Merge pull request #13 from exprust/develop
Browse files Browse the repository at this point in the history
feature: create handler mod for handling exit process and connection
  • Loading branch information
Ando authored Aug 15, 2023
2 parents db46b3d + 52e8aea commit 7a62d61
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
pub mod server {
use std::{
io::{Read, Write},
net::{TcpListener, TcpStream},
process,
net::TcpListener,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
thread,
};

use crate::handler::Handler;
use crate::params::Params;
pub struct Server {}

impl Server {
pub fn new() {}
pub fn new() -> Self {

Check warning on line 16 in src/lib.rs

View workflow job for this annotation

GitHub Actions / lint

you should consider adding a `Default` implementation for `Server`
Server {}
}

pub fn listen(host: &str, port: u32) {
let mut addr = String::new();
Expand All @@ -34,7 +35,7 @@ pub mod server {
let listener = TcpListener::bind(&addr).expect("An error occured while listening app");
let term = Arc::new(AtomicBool::new(false));

self::Server::handle_exit_process(Arc::clone(&term).clone());
self::Handler::handle_exit_process(Arc::clone(&term).clone());

println!("App is listening on {}", &addr);

Expand All @@ -46,13 +47,29 @@ pub mod server {
}

match stream {
Ok(tcp_stream) => thread::spawn(move || self::Server::handle_connection(tcp_stream)),
Ok(tcp_stream) => thread::spawn(move || self::Handler::handle_connection(tcp_stream)),
Err(e) => panic!("{}", e),
};
}
}
}
}

pub mod handler {
use std::{
io::{Read, Write},
net::TcpStream,
process,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
};

pub struct Handler {}

fn handle_connection(mut tcp_stream: TcpStream) {
impl Handler {
pub fn handle_connection(mut tcp_stream: TcpStream) {
loop {
let mut buffer = [0; 1028];

Expand All @@ -78,7 +95,7 @@ pub mod server {
}
}

fn handle_exit_process(exit: Arc<AtomicBool>) {
pub fn handle_exit_process(exit: Arc<AtomicBool>) {
if let Err(e) = ctrlc::set_handler(move || {
exit.store(true, Ordering::SeqCst);
println!("Ctrl + C pressed. Exiting gracefully...");
Expand Down

0 comments on commit 7a62d61

Please sign in to comment.