From 8f5a562ff84aa93701b0747d2cf3e1ea776d8af1 Mon Sep 17 00:00:00 2001 From: ennucore Date: Thu, 25 Nov 2021 07:33:34 +0300 Subject: [PATCH] Move test for ip from examples back to the file --- src/interfaces/ip.rs | 64 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 14 deletions(-) diff --git a/src/interfaces/ip.rs b/src/interfaces/ip.rs index 3212f9e..eb969ce 100644 --- a/src/interfaces/ip.rs +++ b/src/interfaces/ip.rs @@ -9,7 +9,7 @@ use crate::std::io::{Read, Write}; use crate::std::println; -// #[derive(Clone)] +/// Interface for interactions using tcp sockets pub struct IPInterface { pub id: String, pub connections: Vec, @@ -21,10 +21,11 @@ impl InterfaceRequirements for IPInterface {} impl Interface for IPInterface { fn main_loop_iteration(&mut self) -> IFResult<()> { println!("Mainloop {:?}", self.listener.local_addr()); - - match self.listener.accept() { - Ok((stream, addr)) => {println!("New client: {:?}", addr); self.connections.push(stream)}, + Ok((stream, addr)) => { + println!("New client: {:?}", addr); + self.connections.push(stream) + } Err(e) => println!("couldn't get client: {:?}", e), } // for stream in self.listener.incoming() { @@ -41,7 +42,6 @@ impl Interface for IPInterface { // } println!("Hello from mainloop"); for mut connection in &self.connections { - let mut size_arr: [u8; 4] = [0, 0, 0, 0]; connection.read_exact(&mut size_arr)?; let mut size: u32 = 0; @@ -49,8 +49,6 @@ impl Interface for IPInterface { size = size * 256 + *size_byte as u32; } println!("Size: {:?}", size); - - } Ok(()) @@ -73,23 +71,61 @@ impl Interface for IPInterface { None => { self.connections.push(net::TcpStream::connect(addr)?); self.connections.len() - 1 - }, + } Some(i) => i }; - - println!("Sending message to {:?}", self.connections[index].peer_addr().unwrap()); - + println!("Sending message to {:?}", self.connections[index].peer_addr().unwrap()); self.connections[index].write_all(message)?; - println!("Sent message"); - Ok(()) } fn receive(&mut self) -> IFResult> { todo!() } } +#[cfg(test)] +use alloc::vec; + #[test] -fn create_connection() { } +fn test_creating_connection() { + let listener = match net::TcpListener::bind("0.0.0.0:60000") { + Ok(tmp) => tmp, + Err(_) => { return; } + }; + let mut interface1 = IPInterface { + id: String::from("IP interface"), + connections: vec![], + listener, + }; + + let listener = match net::TcpListener::bind("0.0.0.0:50000") { + Ok(tmp) => tmp, + Err(_) => { return; } + }; + let mut interface2 = IPInterface { + id: String::from("IP interface"), + connections: vec![], + listener, + }; + + let t1 = std::thread::spawn(move || { + interface1.send(&[0, 0, 1, 1], Some(String::from("0.0.0.0:50000"))).unwrap(); + interface1 + }); + let t2 = std::thread::spawn(move || { + interface2.main_loop_iteration().unwrap(); + interface2 + }); + let res1 = t1.join(); + match res1 { + Ok(_) => println!("Ok"), + Err(e) => println!("{:?}", e) + } + let res2 = t2.join(); + match res2 { + Ok(_) => println!("Ok"), + Err(e) => println!("{:?}", e) + } +} #[cfg(test)] pub mod test_ip_interface {}