got basic message handling
This commit is contained in:
parent
31dc567e29
commit
2d3d27fbfb
4 changed files with 30 additions and 14 deletions
11
src/main.rs
11
src/main.rs
|
@ -1,3 +1,8 @@
|
||||||
|
use std::sync::mpsc;
|
||||||
|
use std::io::{Write};
|
||||||
|
use std::thread;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
use modules::socket_handler::Socket;
|
use modules::socket_handler::Socket;
|
||||||
|
|
||||||
|
|
||||||
|
@ -9,9 +14,5 @@ mod modules;
|
||||||
const SERVER_ADDRESS: &str = "localhost:5000";
|
const SERVER_ADDRESS: &str = "localhost:5000";
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let listener = Socket::make_listener(SERVER_ADDRESS);
|
println!("hello world");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
drop(listener);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
use workctl::sync_flag;
|
use workctl::sync_flag;
|
||||||
use std::net::{TcpListener, TcpStream, Shutdown};
|
use std::net::{TcpListener, TcpStream, Shutdown};
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
|
use std::sync::mpsc::Sender;
|
||||||
use std::thread::{self, JoinHandle};
|
use std::thread::{self, JoinHandle};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use super::message_handler::StateMessage;
|
||||||
|
|
||||||
trait SocketCallback {
|
trait SocketCallback {
|
||||||
fn handle_message(message: String);
|
fn handle_message(message: String);
|
||||||
|
@ -20,12 +21,13 @@ impl Socket {
|
||||||
TcpListener::bind(address).unwrap()
|
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 (tx, thread_stop_flag) = sync_flag::new_syncflag(true);
|
||||||
let handle = thread::spawn(move || {
|
let handle = thread::spawn(move || {
|
||||||
|
listener.set_nonblocking(true).unwrap();
|
||||||
while thread_stop_flag.get() {
|
while thread_stop_flag.get() {
|
||||||
for (stream, addr) in listener.accept() {
|
for (stream, addr) in listener.accept() {
|
||||||
|
Socket::handle_client(stream, update_tx.clone());
|
||||||
}
|
}
|
||||||
thread::sleep(Duration::from_millis(100));
|
thread::sleep(Duration::from_millis(100));
|
||||||
}
|
}
|
||||||
|
@ -34,9 +36,9 @@ impl Socket {
|
||||||
(tx, handle)
|
(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();
|
let read_size = stream.read(&mut buffer).unwrap();
|
||||||
|
|
||||||
|
@ -45,7 +47,8 @@ impl Socket {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let output = String::from_utf8_lossy(&buffer[..]);
|
let output = String::from_utf8_lossy(&buffer[0..read_size]);
|
||||||
println!("recieved: {}", output);
|
println!("recieved: {}", output);
|
||||||
|
update_tx.send(output.into_owned()).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -33,7 +33,8 @@ impl StatesIOHandler {
|
||||||
}
|
}
|
||||||
StateMessage::CloseListener => {
|
StateMessage::CloseListener => {
|
||||||
tx_1.send(message_handler.get_states()).unwrap();
|
tx_1.send(message_handler.get_states()).unwrap();
|
||||||
break;}
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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};
|
use crate::{SERVER_ADDRESS, modules::socket_handler::Socket};
|
||||||
|
|
||||||
|
|
||||||
|
@ -28,13 +33,19 @@ fn panic_no_listener() {
|
||||||
#[test]
|
#[test]
|
||||||
fn can_handle_messages() {
|
fn can_handle_messages() {
|
||||||
let listener = Socket::make_listener(SERVER_ADDRESS);
|
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 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();
|
join_handle.join().unwrap();
|
||||||
|
thread::sleep(Duration::from_millis(1000));
|
||||||
|
|
||||||
flag.set(false);
|
flag.set(false);
|
||||||
connection_handle.join().unwrap();
|
connection_handle.join().unwrap();
|
||||||
|
let message = rx_1.recv().unwrap();
|
||||||
|
assert_eq!(message, String::from("this is a test"));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue