added router

This commit is contained in:
Martin Slot 2022-08-09 20:29:03 +02:00
parent 3e68cc5896
commit 4868f39806
2 changed files with 93 additions and 57 deletions

View File

@ -22,6 +22,33 @@ pub mod entities {
}
}
pub mod routers {
pub struct RequestRouter;
impl RequestRouter {}
}
pub mod decoders {
use std::io::Cursor;
use prost::Message;
use crate::entities::Request;
pub struct RequestDecoder;
impl RequestDecoder {
pub fn decode(&self, buffer: Vec<u8>) -> Request {
Request::decode(Cursor::new(buffer)).unwrap()
}
}
}
enum MessageKind {
CPU,
RAM,
ProcessList,
}
pub struct VecByteBuffer {
buffer: LinkedList<Vec<u8>>,
}

View File

@ -5,7 +5,7 @@ use std::{
sync::atomic::AtomicBool,
};
use crate::VecByteBuffer;
use crate::{decoders::RequestDecoder, routers::RequestRouter, VecByteBuffer};
struct TcpReader {
temp_buffer_len: usize,
@ -16,7 +16,7 @@ impl TcpReader {
TcpReader { temp_buffer_len }
}
pub fn read<T: Read>(&self, stream: &mut T) -> String {
pub fn read<T: Read>(&self, stream: &mut T) -> Vec<u8> {
let mut vec_byte_buffer = VecByteBuffer::new();
loop {
@ -32,7 +32,68 @@ impl TcpReader {
vec_byte_buffer.push(&buffer, bytes_len);
}
str::from_utf8(&vec_byte_buffer.merge()).unwrap().to_owned()
vec_byte_buffer.merge()
}
}
pub struct StatusServer {
stop: AtomicBool,
reader: TcpReader,
decoder: RequestDecoder,
router: RequestRouter,
pub port: i32,
pub ip: String,
}
impl StatusServer {
pub fn new(port: i32, ip: &str) -> StatusServer {
StatusServer {
ip: String::from(ip),
reader: TcpReader::new(128),
decoder: RequestDecoder,
router: RequestRouter,
port,
stop: AtomicBool::new(false),
}
}
pub fn start(&self) {
let listener = TcpListener::bind(format!("{}:{}", self.ip, self.port)).unwrap();
println!("starting server");
for stream in listener.incoming() {
if !self.stop.load(std::sync::atomic::Ordering::Relaxed) {
println!("got stream");
let mut stream = stream.unwrap();
let data = self.reader.read(&mut stream);
let request = self.decoder.decode(data);
} else {
println!("break");
break;
}
}
}
pub fn stop(&self) {
self.stop.store(true, std::sync::atomic::Ordering::Relaxed);
println!("stop");
let result = TcpStream::connect(format!("{}:{}", self.ip, self.port));
let mut stream = match result {
Ok(stream) => stream,
Err(error) => {
println!("error stopping {}", error);
/* the server could be closed already because another request has activated the stop => do nothing */
return;
}
};
// we write an empty set to trigger the incoming loop that checks the stop bool that
// has been set to true
stream.write(b"");
stream.flush();
stream.shutdown(std::net::Shutdown::Both);
}
}
@ -49,7 +110,8 @@ mod tests {
let reader = TcpReader::new(2);
let mut mock_stream = MockTcpStream::new("hello".as_bytes().to_vec());
let data = reader.read(&mut mock_stream);
assert_eq!(data, "hello");
let string = String::from_utf8(data).unwrap();
assert_eq!(string, "hello");
}
struct MockTcpStream {
@ -70,56 +132,3 @@ mod tests {
}
}
}
pub struct StatusServer {
stop: AtomicBool,
pub port: i32,
pub ip: String,
}
impl StatusServer {
pub fn new(port: i32, ip: &str) -> StatusServer {
StatusServer {
ip: String::from(ip),
port,
stop: AtomicBool::new(false),
}
}
pub fn start(&self) {
let listener = TcpListener::bind(format!("{}:{}", self.ip, self.port)).unwrap();
println!("starting server");
for stream in listener.incoming() {
if !self.stop.load(std::sync::atomic::Ordering::Relaxed) {
println!("got stream");
let stream = stream.unwrap();
} else {
println!("break");
break;
}
}
}
pub fn stop(&self) {
self.stop.store(true, std::sync::atomic::Ordering::Relaxed);
println!("stop");
let result = TcpStream::connect(format!("{}:{}", self.ip, self.port));
let mut stream = match result {
Ok(stream) => stream,
Err(error) => {
println!("error stopping {}", error);
/* the stream could be closed already => do nothing */
return;
}
};
// we write an empty set to trigger the incoming loop that checks the stop bool that
// has been set to true
stream.write(b"");
stream.flush();
stream.shutdown(std::net::Shutdown::Both);
}
}