From bb26f910ff4856aef467d8645add8de5baa64d43 Mon Sep 17 00:00:00 2001 From: Alexey Date: Wed, 24 Nov 2021 20:28:49 +0300 Subject: [PATCH] Created IP inteface. Implemented send method --- src/interface.rs | 6 ++-- src/interfaces/ip.rs | 77 +++++++++++++++++++++++++++++++++++++++++++ src/interfaces/mod.rs | 3 ++ src/lib.rs | 9 +++++ src/res.rs | 9 +++++ src/transport.rs | 16 +++++---- 6 files changed, 109 insertions(+), 11 deletions(-) create mode 100644 src/interfaces/ip.rs diff --git a/src/interface.rs b/src/interface.rs index b4b4c16..d96d533 100644 --- a/src/interface.rs +++ b/src/interface.rs @@ -10,10 +10,10 @@ use crate::res::IFResult; pub(crate) type TargetingData = String; /// In an std environment we require that the interface can be send safely between threads -#[cfg(not(std))] +#[cfg(not(feature = "std"))] pub trait InterfaceRequirements {} -#[cfg(std)] +#[cfg(feature = "std")] pub trait InterfaceRequirements = Send + Sync; @@ -53,8 +53,6 @@ pub mod test_interface { messages: Vec<(Vec, TargetingData)>, } - impl InterfaceRequirements for TestInterface {} - impl Interface for TestInterface { fn main_loop_iteration(&mut self) -> IFResult<()> { Ok(()) diff --git a/src/interfaces/ip.rs b/src/interfaces/ip.rs new file mode 100644 index 0000000..3ffceeb --- /dev/null +++ b/src/interfaces/ip.rs @@ -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, + 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) -> 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> { 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 {} + + + diff --git a/src/interfaces/mod.rs b/src/interfaces/mod.rs index 9a66dd6..7b6c990 100644 --- a/src/interfaces/mod.rs +++ b/src/interfaces/mod.rs @@ -1,3 +1,6 @@ +#[cfg(feature = "std")] +pub mod ip; + use crate::interface::Interface; use alloc::vec; diff --git a/src/lib.rs b/src/lib.rs index 80d2577..057a0ec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,9 @@ #![no_std] #![allow(dead_code)] +#![feature(trait_alias)] + +#[cfg(feature = "std")] +extern crate std; extern crate alloc; extern crate rand; @@ -16,6 +20,11 @@ mod interfaces; mod res; mod tunnel; +#[cfg(std)] +use crate::interfaces::ip; +#[cfg(not(feature = "std"))] +use aboba; + #[cfg(test)] mod tests { } diff --git a/src/res.rs b/src/res.rs index 9ba0799..601039a 100644 --- a/src/res.rs +++ b/src/res.rs @@ -13,6 +13,8 @@ pub enum IFError { SerializationError(String), /// Error in rsa CryptoError(String), + /// Error in std::io + IoError(String), } @@ -23,6 +25,13 @@ pub enum IFError { // } // } +#[cfg(feature = "std")] +impl From for IFError { + fn from(e: std::io::Error) -> Self { + Self::IoError(format!("{:?}", e)) + } +} + impl From for IFError { fn from(e: serde_cbor::Error) -> Self { Self::SerializationError(format!("{:?}", e)) diff --git a/src/transport.rs b/src/transport.rs index 89d5dea..f48f528 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -6,8 +6,10 @@ use crate::interface::{Interface, TargetingData}; use crate::message::MessageBytes; use crate::res::IFResult; -#[cfg(std)] +#[cfg(feature = "std")] use rayon::prelude::*; +#[cfg(feature = "std")] +use std::println; /// An identification of a peer - something that we can use to send a message to id @@ -41,7 +43,7 @@ pub struct Transport { impl Transport { /// Create new transport with given interfaces pub fn new(interfaces: Vec>) -> Self { - #[cfg(not(std))] + #[cfg(not(feature = "std"))] if interfaces.iter().map(|interface| interface.has_blocking_main() as u8).sum::() > 1 { panic!("There is two interfaces with blocking main loops and we have no threads because this is no_std!"); } @@ -82,14 +84,14 @@ impl Transport { match peer { // Broadcast None => { - #[cfg(not(std))] + #[cfg(not(feature = "std"))] { for interface in &mut self.interfaces { interface.send(&message, None)?; } } // If we have concurrency, we will do it concurrently - #[cfg(std)] + #[cfg(feature = "std")] { self.interfaces.par_iter_mut().map(|interface| interface.send(&message, None)).for_each(drop); } @@ -114,7 +116,7 @@ impl Transport { // If there was an error, print it .map(|res| match res { (id, Err(e)) => { - #[cfg(std)] + #[cfg(feature = "std")] println!("An error occurred while receiving: {:?}", e); (id, Err(e)) } @@ -130,9 +132,9 @@ impl Transport { /// Run one iteration of the main loop pub fn main_loop_iteration(&mut self) -> IFResult<()> { - #[cfg(std)] + #[cfg(feature = "std")] self.interfaces.par_iter_mut().map(|interface| interface.main_loop_iteration()).collect::>()?; - #[cfg(not(std))] + #[cfg(not(feature = "std"))] { self.interfaces.iter_mut().try_for_each(|interface| if !interface.has_blocking_main() { interface.main_loop_iteration() } else { Ok(()) })?; let blocking_interface_index = self.interfaces.iter().position(|interface| interface.has_blocking_main());