|
|
|
@ -1,41 +1,43 @@
|
|
|
|
|
use crate::message::{Message, MsgType}; |
|
|
|
|
use crate::crypto::PublicKey; |
|
|
|
|
|
|
|
|
|
#[cfg(feature = "std")] |
|
|
|
|
use crate::way::Way; |
|
|
|
|
#[cfg(feature = "std")] |
|
|
|
|
use std::thread; |
|
|
|
|
|
|
|
|
|
use alloc::vec::Vec; |
|
|
|
|
use alloc::boxed::Box; |
|
|
|
|
|
|
|
|
|
#[cfg(not(feature = "std"))] |
|
|
|
|
pub struct Transport { |
|
|
|
|
id: [u8; 64] |
|
|
|
|
} |
|
|
|
|
pub struct Transport {} |
|
|
|
|
|
|
|
|
|
#[cfg(feature = "std")] |
|
|
|
|
pub struct Transport { |
|
|
|
|
ways: Vec<Box<dyn Way>>, |
|
|
|
|
id: [u8; 64] |
|
|
|
|
ways: Vec<Box<dyn Way + Send>>, |
|
|
|
|
msg_pool: Vec<Message>, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[cfg(not(feature = "std"))] |
|
|
|
|
impl Transport { |
|
|
|
|
pub fn new() -> Self { |
|
|
|
|
Self {id: [0; 64]} |
|
|
|
|
Self {} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn send(&self, msg: &Message) { |
|
|
|
|
pub fn send(&self, msg: &Message) {} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn receive(&self) -> Message { |
|
|
|
|
Message::new(MsgType::Service, |
|
|
|
|
Vec::<u8>::new(), |
|
|
|
|
&crate::crypto::Keys::gen()) |
|
|
|
|
pub fn receive(&self) -> (Message, PublicKey) { |
|
|
|
|
(Message::new(MsgType::Service, |
|
|
|
|
Vec::<u8>::new(), |
|
|
|
|
&crate::crypto::Keys::gen()), PublicKey { key: [0u8; 32] }) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[cfg(feature = "std")] |
|
|
|
|
impl Transport { |
|
|
|
|
pub fn new() -> Self { |
|
|
|
|
Self {ways: Vec::<Box<dyn Way>>::new(), id: [0; 64]} |
|
|
|
|
let transport = Self { ways: Vec::<Box<dyn Way + Send>>::new(), msg_pool: Vec::<Message>::new() }; |
|
|
|
|
transport |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn send(&self, msg: &Message) { |
|
|
|
@ -44,9 +46,22 @@ impl Transport {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn receive(&self) -> Message { |
|
|
|
|
Message::new(MsgType::Service, |
|
|
|
|
Vec::<u8>::new(), |
|
|
|
|
&crate::crypto::Keys::gen()) |
|
|
|
|
pub fn receive(&self) -> (Message, PublicKey) { |
|
|
|
|
(Message::new(MsgType::MultiCast, |
|
|
|
|
Vec::<u8>::new(), |
|
|
|
|
&crate::crypto::Keys::gen()), PublicKey { key: [1u8; 32] }) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn receive_through_way_in_loop_and_add_in_pool(&mut self, way: &mut Box<dyn Way + Send>) { |
|
|
|
|
loop { |
|
|
|
|
let mut msg = way.receive(); |
|
|
|
|
self.msg_pool.push(msg); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn start_receiving_thread(&'static mut self) { |
|
|
|
|
thread::spawn(|| { |
|
|
|
|
self.receive_through_way_in_loop_and_add_in_pool(&mut self.ways[0]); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|