|
|
|
@ -15,19 +15,40 @@ impl IronForce {
|
|
|
|
|
IronForce { transport: Transport::new(), key_pack: KeyPack::gen() } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn new_message(&self, msg_type: MsgType, body: Vec<u8>) -> Message { |
|
|
|
|
Message::new(msg_type, body, &self.key_pack) |
|
|
|
|
fn is_valid_message(&self, msg: &Message) -> bool { |
|
|
|
|
// todo: some kind of PoW
|
|
|
|
|
msg.verify() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn service_msg(&self, body: Vec<u8>) { |
|
|
|
|
fn new_message(&self, msg_type: MsgType, body: &Vec<u8>) -> Message { |
|
|
|
|
Message::new(msg_type, body.clone(), &self.key_pack) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn service_msg(&self, body: &Vec<u8>) { |
|
|
|
|
self.transport.send(&self.new_message(MsgType::Service, body)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn multicast(&self, body: Vec<u8>) { |
|
|
|
|
pub fn multicast(&self, body: &Vec<u8>) { |
|
|
|
|
self.transport.send(&self.new_message(MsgType::MultiCast, body)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn send_message_to(&self, to: PublicKey, body: Vec<u8>) { |
|
|
|
|
pub fn send_message_to(&self, to: PublicKey, body: &Vec<u8>) { |
|
|
|
|
self.transport.send(&self.new_message(MsgType::ToTarget(to), body)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn handle_message(&self, msg: &Message) { |
|
|
|
|
if self.is_valid_message(msg) { |
|
|
|
|
match &msg.msg_type { |
|
|
|
|
MsgType::Service => { |
|
|
|
|
self.service_msg(&msg.body) |
|
|
|
|
}, |
|
|
|
|
MsgType::MultiCast => { |
|
|
|
|
|
|
|
|
|
}, |
|
|
|
|
MsgType::ToTarget(Target) => { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|