From fbc85fa325a01e06599bc82f0f2e7fd33375782a Mon Sep 17 00:00:00 2001 From: ennucore Date: Tue, 19 May 2020 14:08:03 +0300 Subject: [PATCH] Service msg --- src/ironforce.rs | 13 ++++++++++++- src/tunnel.rs | 37 +++++++++++++++++++++++++------------ 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/ironforce.rs b/src/ironforce.rs index 8e8fcbe..7c9e795 100644 --- a/src/ironforce.rs +++ b/src/ironforce.rs @@ -35,7 +35,18 @@ impl IronForce { } fn service_msg(&self, body: &Vec) { - self.transport.send_service(&self.new_message(MsgType::Service, body)) + let msg = self.new_message(MsgType::Service, body); + for tunnel in &self.tunnels { + match tunnel.get_next(&self.key_pack.get_public(), true) { + Some(next_forward) => {self.send_through_tunnel_to(&msg, tunnel, &next_forward)} + None => {} + } + match tunnel.get_next(&self.key_pack.get_public(), false) { + Some(next_backwards) => {self.send_through_tunnel_to(&msg, tunnel, &next_backwards)} + None => {} + } + } + } pub fn multicast(&self, msg: &Message) -> u32 { diff --git a/src/tunnel.rs b/src/tunnel.rs index 8f65242..4563938 100644 --- a/src/tunnel.rs +++ b/src/tunnel.rs @@ -42,23 +42,36 @@ impl Tunnel { } } - pub fn next(&self, me: &PublicKey, to: &PublicKey) -> Option { - let my_index = self.nodes - .iter() - .enumerate() - .find(|&node| node.1 == me).unwrap().0; - let to_index = self.nodes + fn index(&self, node: &PublicKey) -> Option { + match self.nodes .iter() .enumerate() - .find(|&node| node.1 == to).unwrap().0; + .find(|¤t_node| current_node.1 == node) { + Some((index, _)) => Some(index), + None => None + } + } + + pub fn get_next(&self, me: &PublicKey, forward: bool) -> Option { + let my_index = self.index(me).unwrap(); + if forward { + if my_index + 1 < self.nodes.len() { + Some(self.nodes[my_index + 1].clone()) + } else {None} + } else { + if my_index > 0 { + Some(self.nodes[my_index - 1].clone()) + } else { None } + } + } + + pub fn next(&self, me: &PublicKey, to: &PublicKey) -> Option { + let my_index = self.index(me); + let to_index = self.index(to); if to_index == my_index { return None; } - return if to_index < my_index { - Option::from(self.nodes[my_index - 1].clone()) - } else { - Option::from(self.nodes[my_index + 1].clone()) - }; + self.get_next(me, to_index > my_index) } pub fn next_nodes(&self, me: &PublicKey) -> Vec {