|
|
@ -1,17 +1,15 @@ |
|
|
|
use crate::message::{Message, MsgType}; |
|
|
|
use crate::message::Message; |
|
|
|
|
|
|
|
#[cfg(not(feature = "std"))] use crate::message::MsgType; |
|
|
|
use crate::crypto::PublicKey; |
|
|
|
use crate::crypto::PublicKey; |
|
|
|
|
|
|
|
#[cfg(feature = "std")] use crate::way::Way; |
|
|
|
#[cfg(feature = "std")] |
|
|
|
|
|
|
|
use crate::way::Way; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "std")] |
|
|
|
#[cfg(feature = "std")] |
|
|
|
use std::thread; |
|
|
|
use std::thread; |
|
|
|
#[cfg(feature = "std")] |
|
|
|
#[cfg(feature = "std")] |
|
|
|
use std::sync::{Arc, Mutex}; |
|
|
|
use std::sync::{Arc, Mutex}; |
|
|
|
|
|
|
|
|
|
|
|
use alloc::vec::Vec; |
|
|
|
use alloc::vec::Vec; |
|
|
|
#[cfg(feature = "std")] |
|
|
|
#[cfg(feature = "std")] use alloc::boxed::Box; |
|
|
|
use alloc::boxed::Box; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(not(feature = "std"))] |
|
|
|
#[cfg(not(feature = "std"))] |
|
|
|
pub struct Transport {} |
|
|
|
pub struct Transport {} |
|
|
@ -19,7 +17,7 @@ pub struct Transport {} |
|
|
|
#[cfg(feature = "std")] |
|
|
|
#[cfg(feature = "std")] |
|
|
|
pub struct Transport { |
|
|
|
pub struct Transport { |
|
|
|
pub ways: Vec<Box<dyn Way + Send + Sync>>, |
|
|
|
pub ways: Vec<Box<dyn Way + Send + Sync>>, |
|
|
|
pub msg_pool: Vec<Message>, |
|
|
|
pub msg_pool: Vec<(Message, PublicKey)>, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[cfg(not(feature = "std"))] |
|
|
|
#[cfg(not(feature = "std"))] |
|
|
@ -32,7 +30,6 @@ impl Transport { |
|
|
|
// todo
|
|
|
|
// todo
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[allow(dead_code)] |
|
|
|
|
|
|
|
pub fn receive(&self) -> (Message, PublicKey) { |
|
|
|
pub fn receive(&self) -> (Message, PublicKey) { |
|
|
|
(Message::new(MsgType::Service, |
|
|
|
(Message::new(MsgType::Service, |
|
|
|
Vec::<u8>::new(), |
|
|
|
Vec::<u8>::new(), |
|
|
@ -43,7 +40,7 @@ impl Transport { |
|
|
|
#[cfg(feature = "std")] |
|
|
|
#[cfg(feature = "std")] |
|
|
|
impl Transport { |
|
|
|
impl Transport { |
|
|
|
pub fn new() -> Self { |
|
|
|
pub fn new() -> Self { |
|
|
|
let transport = Self { ways: Vec::<Box<dyn Way + Send + Sync>>::new(), msg_pool: Vec::<Message>::new() }; |
|
|
|
let transport = Self { ways: Vec::<Box<dyn Way + Send + Sync>>::new(), msg_pool: Vec::<(Message, PublicKey)>::new() }; |
|
|
|
transport |
|
|
|
transport |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -53,17 +50,21 @@ impl Transport { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn receive(&self) -> (Message, PublicKey) { |
|
|
|
pub fn receive(&mut self) -> (Message, PublicKey) { |
|
|
|
(Message::new(MsgType::MultiCast, |
|
|
|
if self.msg_pool.len() != 0 { |
|
|
|
Vec::<u8>::new(), |
|
|
|
let msg = self.msg_pool[0].clone(); |
|
|
|
&crate::crypto::Keys::gen()), PublicKey { key: [1u8; 32] }) |
|
|
|
self.msg_pool = self.msg_pool[1..].to_vec(); |
|
|
|
|
|
|
|
return msg; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return self.receive(); // recursion: infinite loop
|
|
|
|
|
|
|
|
// until we get a message (see exit condition above)
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn way_receiving_loop(&mut self, i: i32) { |
|
|
|
fn way_receiving_loop(&mut self, i: i32) { |
|
|
|
let way = &self.ways[i as usize]; |
|
|
|
let way = &self.ways[i as usize]; |
|
|
|
loop { |
|
|
|
loop { |
|
|
|
let mut msg = way.receive(); |
|
|
|
let (msg, key) = way.receive(); |
|
|
|
self.msg_pool.push(msg); |
|
|
|
self.msg_pool.push((msg, key)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|