Lev
3 years ago
6 changed files with 117 additions and 17 deletions
@ -1,5 +1,74 @@
|
||||
use alloc::vec::Vec; |
||||
use alloc::vec; |
||||
use crate::crypto::PublicKey; |
||||
use crate::message::{Message, MessageType, ServiceMessageType}; |
||||
use crate::res::{IFError, IFResult}; |
||||
use crate::transport::Transport; |
||||
use crate::tunnel::Tunnel; |
||||
|
||||
|
||||
/// Main worker
|
||||
pub struct IronForce { |
||||
transport: Transport |
||||
/// the struct that manages communicating with neighbor nodes
|
||||
transport: Transport, |
||||
/// Tunnels that are known to this node
|
||||
tunnels: Vec<Tunnel>, |
||||
/// Additional modules that may be plugged in later,
|
||||
/// for example internet access (like Tor)
|
||||
/// and some kind of decentralized storage
|
||||
additional_modules: Vec<()>, |
||||
/// Non-service messages to give outside
|
||||
messages: Vec<Message>, |
||||
} |
||||
|
||||
impl IronForce { |
||||
/// Create new worker
|
||||
pub fn new() -> Self { |
||||
Self { |
||||
transport: Transport::new(crate::interfaces::get_interfaces()), |
||||
tunnels: vec![], |
||||
additional_modules: vec![], |
||||
messages: vec![], |
||||
} |
||||
} |
||||
|
||||
/// Create a new tunnel to another node
|
||||
fn create_new_tunnel(&mut self, _destination: PublicKey) -> IFResult<Tunnel> { |
||||
todo!() |
||||
} |
||||
|
||||
/// Send a message through tunnel
|
||||
fn send_through_tunnel(&mut self, _tunnel_id: u64, _message: Message, _direction: Option<bool>) -> IFResult<()> { |
||||
todo!() |
||||
} |
||||
|
||||
/// Send a message to another node,
|
||||
/// creating a new tunnel if needed
|
||||
pub fn send_message(&mut self, message: Message, destination: PublicKey) -> IFResult<()> { |
||||
if let Some(Some(tunnel_id)) = self.tunnels.iter() |
||||
.find(|t| t.target_node.as_ref() == Some(&destination) || t.nodes_in_tunnel.as_ref().map(|nodes| nodes.contains(&destination)) == Some(true)) |
||||
.map(|tunnel| tunnel.id) { |
||||
self.send_through_tunnel(tunnel_id, message, None) |
||||
} else { |
||||
Err(IFError::TunnelNotFound) |
||||
} |
||||
} |
||||
|
||||
/// Process a message: if it's a service message, act accordingly.
|
||||
/// Otherwise, add to `self.messages`
|
||||
fn process_message(&mut self, message: Message) { |
||||
match message.message_type { |
||||
MessageType::Service(msg_type) => match msg_type { |
||||
ServiceMessageType::TunnelBuilding(_tunnel) => { |
||||
todo!() |
||||
} |
||||
} |
||||
MessageType::SingleCast | MessageType::Broadcast => { self.messages.push(message) } |
||||
} |
||||
} |
||||
|
||||
/// Get a message from `self.messages`
|
||||
pub fn read_message(&mut self) -> Option<Message> { |
||||
self.messages.pop() |
||||
} |
||||
} |
||||
|
@ -1,30 +1,36 @@
|
||||
use alloc::vec::Vec; |
||||
use crate::crypto::PublicKey; |
||||
use serde::{Serialize, Deserialize}; |
||||
|
||||
/// A tunnel that is used for communication
|
||||
#[derive(Serialize, Clone, Deserialize)] |
||||
pub struct Tunnel { |
||||
/// Tunnel's id
|
||||
id: Option<u64>, |
||||
/// Ids that are
|
||||
local_ids: Vec<u64>, |
||||
/// Tunnel's id.
|
||||
/// By the way, this id is `None` until the tunnel is validated in the backward movement
|
||||
pub id: Option<u64>, |
||||
/// Ids, each of them is just for local storage on each node until a final global id is created
|
||||
pub local_ids: Vec<u64>, |
||||
/// Ids of peers (in transport) by which we can send a message - one for backward direction, another for forward
|
||||
peer_ids: (u64, u64), |
||||
pub peer_ids: (u64, u64), |
||||
/// Time at which this tunnel should be destroyed (UNIX epoch)
|
||||
ttd: u64, |
||||
/// Public keys of nodes
|
||||
nodes_in_tunnel: Option<Vec<PublicKey>>, |
||||
pub ttd: u64, |
||||
/// Public keys of nodes in the tunnel
|
||||
pub nodes_in_tunnel: Option<Vec<PublicKey>>, |
||||
/// Is this tunnel used for multicast?
|
||||
is_multicast: bool, |
||||
pub is_multicast: bool, |
||||
/// If we created this tunnel, then this is the node that it's used to communicate with
|
||||
pub target_node: Option<PublicKey>, |
||||
} |
||||
|
||||
/// Tunnel, but only the fields that are ok to share
|
||||
#[derive(Serialize, Clone, Deserialize)] |
||||
pub struct TunnelPublic { |
||||
/// Tunnel's id
|
||||
id: Option<u64>, |
||||
/// Ids that are
|
||||
/// Ids, each of them is just for local storage on each node until a final global id is created
|
||||
local_ids: Vec<u64>, |
||||
/// Time at which this tunnel should be destroyed (UNIX epoch)
|
||||
ttd: u64, |
||||
/// Public keys of nodes
|
||||
/// Public keys of nodes in the tunnel
|
||||
nodes_in_tunnel: Option<Vec<PublicKey>>, |
||||
} |
||||
|
Loading…
Reference in new issue