Browse Source

Service msg

master
Lev 4 years ago
parent
commit
fbc85fa325
  1. 13
      src/ironforce.rs
  2. 37
      src/tunnel.rs

13
src/ironforce.rs

@ -35,7 +35,18 @@ impl IronForce {
}
fn service_msg(&self, body: &Vec<u8>) {
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 {

37
src/tunnel.rs

@ -42,23 +42,36 @@ impl Tunnel {
}
}
pub fn next(&self, me: &PublicKey, to: &PublicKey) -> Option<PublicKey> {
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<usize> {
match self.nodes
.iter()
.enumerate()
.find(|&node| node.1 == to).unwrap().0;
.find(|&current_node| current_node.1 == node) {
Some((index, _)) => Some(index),
None => None
}
}
pub fn get_next(&self, me: &PublicKey, forward: bool) -> Option<PublicKey> {
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<PublicKey> {
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<PublicKey> {

Loading…
Cancel
Save