Prokhor
3 years ago
6 changed files with 109 additions and 11 deletions
@ -0,0 +1,77 @@
|
||||
use std::net; |
||||
use alloc::vec; |
||||
use alloc::vec::Vec; |
||||
use alloc::string::String; |
||||
|
||||
use crate::interface; |
||||
use crate::interface::{Interface, InterfaceRequirements, TargetingData}; |
||||
use crate::message::MessageBytes; |
||||
use crate::res::{IFError, IFResult}; |
||||
use crate::std::io::Write; |
||||
use crate::std::println; |
||||
|
||||
|
||||
// #[derive(Clone)]
|
||||
struct IPInterface { |
||||
id: String, |
||||
connections: Vec<net::TcpStream>, |
||||
listener: net::TcpListener, |
||||
} |
||||
|
||||
|
||||
impl Interface for IPInterface { |
||||
fn main_loop_iteration(&mut self) -> IFResult<()> { |
||||
todo!() |
||||
} |
||||
|
||||
fn has_blocking_main(&self) -> bool { |
||||
false |
||||
} |
||||
fn id(&self) -> &str { |
||||
&*self.id |
||||
} |
||||
fn send(&mut self, message: &[u8], interface_data: Option<TargetingData>) -> IFResult<()> { |
||||
let mut addr: net::SocketAddr = match interface_data { |
||||
Some(ip_string) => ip_string.parse().expect("Unable to parse address"), |
||||
None => return Err(IFError::General(String::from("Not enough info to create connection"))) |
||||
}; |
||||
println!("Hello there"); |
||||
|
||||
let index = match self.connections.iter().position(|connection| connection.peer_addr().ok() == Some(addr)) { |
||||
None => { |
||||
self.connections.push(net::TcpStream::connect(addr)?); |
||||
self.connections.len() - 1 |
||||
}, |
||||
Some(i) => i |
||||
}; |
||||
|
||||
self.connections[index].write(message); |
||||
|
||||
Ok(()) |
||||
} |
||||
fn receive(&mut self) -> IFResult<Option<(MessageBytes, TargetingData)>> { todo!() } |
||||
} |
||||
|
||||
#[test] |
||||
fn create_connection() { |
||||
let option_listener = net::TcpListener::bind("127.0.0.1:60000"); |
||||
let mut listener; |
||||
match option_listener { |
||||
Ok(tmp) => { listener = tmp } |
||||
Err(_) => { return; } |
||||
} |
||||
let mut interface = IPInterface { |
||||
id: String::from("IP interface"), |
||||
connections: vec![], |
||||
listener, |
||||
}; |
||||
interface.send(&[], Some(String::from("127.0.0.1:60000"))); |
||||
interface.listener. |
||||
|
||||
} |
||||
|
||||
#[cfg(test)] |
||||
pub mod test_ip_interface {} |
||||
|
||||
|
||||
|
Loading…
Reference in new issue