got basic message handling

This commit is contained in:
Nickiel12 2021-12-17 18:02:34 -08:00
parent 31dc567e29
commit 2d3d27fbfb
4 changed files with 30 additions and 14 deletions

View file

@ -1,3 +1,8 @@
use std::sync::mpsc;
use std::io::{Write};
use std::thread;
use std::time::Duration;
use modules::socket_handler::Socket;
@ -9,9 +14,5 @@ mod modules;
const SERVER_ADDRESS: &str = "localhost:5000";
fn main() {
let listener = Socket::make_listener(SERVER_ADDRESS);
drop(listener);
println!("hello world");
}

View file

@ -1,10 +1,11 @@
use workctl::sync_flag;
use std::net::{TcpListener, TcpStream, Shutdown};
use std::io::{Read, Write};
use std::sync::mpsc::Sender;
use std::thread::{self, JoinHandle};
use std::time::Duration;
use super::message_handler::StateMessage;
trait SocketCallback {
fn handle_message(message: String);
@ -20,12 +21,13 @@ impl Socket {
TcpListener::bind(address).unwrap()
}
pub fn handle_connections(listener: TcpListener) -> (sync_flag::SyncFlagTx, JoinHandle<()>){
pub fn handle_connections(listener: TcpListener, update_tx: Sender<String>) -> (sync_flag::SyncFlagTx, JoinHandle<()>){
let (tx, thread_stop_flag) = sync_flag::new_syncflag(true);
let handle = thread::spawn(move || {
listener.set_nonblocking(true).unwrap();
while thread_stop_flag.get() {
for (stream, addr) in listener.accept() {
Socket::handle_client(stream, update_tx.clone());
}
thread::sleep(Duration::from_millis(100));
}
@ -34,9 +36,9 @@ impl Socket {
(tx, handle)
}
pub fn handle_client(mut stream: TcpStream) {
pub fn handle_client(mut stream: TcpStream, update_tx: Sender<String>) {
let mut buffer = [0; 1024];
let mut buffer = [0; 1024];
let read_size = stream.read(&mut buffer).unwrap();
@ -45,7 +47,8 @@ impl Socket {
return
}
let output = String::from_utf8_lossy(&buffer[..]);
let output = String::from_utf8_lossy(&buffer[0..read_size]);
println!("recieved: {}", output);
update_tx.send(output.into_owned()).unwrap();
}
}

View file

@ -33,7 +33,8 @@ impl StatesIOHandler {
}
StateMessage::CloseListener => {
tx_1.send(message_handler.get_states()).unwrap();
break;}
break;
}
}
}

View file

@ -1,3 +1,8 @@
use std::sync::mpsc;
use std::io::{Write};
use std::thread;
use std::time::Duration;
use crate::{SERVER_ADDRESS, modules::socket_handler::Socket};
@ -28,13 +33,19 @@ fn panic_no_listener() {
#[test]
fn can_handle_messages() {
let listener = Socket::make_listener(SERVER_ADDRESS);
let (tx_1, rx_1) = mpsc::channel::<String>();
let (mut flag, connection_handle) = Socket::handle_connections(listener);
let (mut flag, connection_handle) = Socket::handle_connections(listener, tx_1);
let join_handle = std::thread::spawn(move || {
let _outgoing = std::net::TcpStream::connect(SERVER_ADDRESS).unwrap();
let mut outgoing = std::net::TcpStream::connect(SERVER_ADDRESS).unwrap();
outgoing.write("this is a test".as_bytes()).unwrap();
});
join_handle.join().unwrap();
thread::sleep(Duration::from_millis(1000));
flag.set(false);
connection_handle.join().unwrap();
let message = rx_1.recv().unwrap();
assert_eq!(message, String::from("this is a test"));
}