Browse Source

Move test for ip from examples back to the file

master
Lev 3 years ago
parent
commit
e0e7a49520
  1. 62
      src/interfaces/ip.rs

62
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<net::TcpStream>,
@ -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());
self.connections[index].write_all(message)?;
println!("Sent message");
Ok(())
}
fn receive(&mut self) -> IFResult<Option<(MessageBytes, TargetingData)>> { 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 {}

Loading…
Cancel
Save