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

feature: create handler mod for handling exit process and connection #13

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Changes from all commits
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
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`

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 @@ -27,14 +28,14 @@
panic!("An error occured, parameters need to be valid local host");
}

addr.push_str(&host);

Check warning on line 31 in src/lib.rs

View workflow job for this annotation

GitHub Actions / lint

this expression creates a reference which is immediately dereferenced by the compiler

Check warning on line 31 in src/lib.rs

View workflow job for this annotation

GitHub Actions / lint

this expression creates a reference which is immediately dereferenced by the compiler
addr.push(':');
addr.push_str(&port.to_string());

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 @@
}

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 @@
}
}

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 All @@ -100,7 +117,7 @@
let is_host_empty = host.is_empty();
let is_port_empty = port == 0;

match (is_host_empty, is_port_empty) {

Check warning on line 120 in src/lib.rs

View workflow job for this annotation

GitHub Actions / lint

match expression looks like `matches!` macro

Check warning on line 120 in src/lib.rs

View workflow job for this annotation

GitHub Actions / lint

match expression looks like `matches!` macro
(true, true) => true,
_ => false,
}
Expand Down
Loading