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