Browse Source

Created IP inteface. Implemented send method

interface-2
Prokhor 3 years ago
parent
commit
b266a0928f
  1. 6
      src/interface.rs
  2. 77
      src/interfaces/ip.rs
  3. 3
      src/interfaces/mod.rs
  4. 9
      src/lib.rs
  5. 9
      src/res.rs
  6. 16
      src/transport.rs

6
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<u8>, TargetingData)>,
}
impl InterfaceRequirements for TestInterface {}
impl Interface for TestInterface {
fn main_loop_iteration(&mut self) -> IFResult<()> {
Ok(())

77
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<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 {}

3
src/interfaces/mod.rs

@ -1,3 +1,6 @@
#[cfg(feature = "std")]
pub mod ip;
use crate::interface::Interface;
use alloc::vec;

9
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 {
}

9
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<std::io::Error> for IFError {
fn from(e: std::io::Error) -> Self {
Self::IoError(format!("{:?}", e))
}
}
impl From<serde_cbor::Error> for IFError {
fn from(e: serde_cbor::Error) -> Self {
Self::SerializationError(format!("{:?}", e))

16
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<Box<dyn Interface>>) -> Self {
#[cfg(not(std))]
#[cfg(not(feature = "std"))]
if interfaces.iter().map(|interface| interface.has_blocking_main() as u8).sum::<u8>() > 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::<IFResult<_>>()?;
#[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());

Loading…
Cancel
Save