Browse Source

Sending messages

interface-2
Lev 3 years ago
parent
commit
af5d27dc97
  1. 2
      src/interface.rs
  2. 47
      src/ironforce.rs
  3. 4
      src/message.rs

2
src/interface.rs

@ -53,7 +53,6 @@ pub mod test_interface {
use alloc::vec;
use alloc::string::ToString;
use alloc::sync::Arc;
use std::println;
use spin::Mutex;
#[derive(Default)]
@ -113,7 +112,6 @@ pub mod test_interface {
.storage
.lock()
.push((Vec::from(message), target.unwrap_or_default()));
println!("Sending in testinterface. Storage: {:?}", self.storage.lock());
Ok(())
}

47
src/ironforce.rs

@ -73,11 +73,24 @@ impl IronForce {
/// Send a message through tunnel
fn send_through_tunnel(
&mut self,
_tunnel_id: u64,
_message: Message,
_direction: Option<bool>,
tunnel_id: u64,
mut message: Message,
direction: Option<bool>,
) -> IFResult<()> {
todo!()
let tunnel: Tunnel = if let Some(tun) = self.tunnels.iter().cloned().find(|t| t.id == Some(tunnel_id)) { tun } else { return Err(IFError::TunnelNotFound) };
message.tunnel_id = tunnel_id;
let peer_ids = match (direction, tunnel.peer_ids) {
(_, (x, 0)) => vec![x],
(_, (0, x)) => vec![x],
(None, (x1, x2)) => vec![x1, x2],
(Some(true), (x1, _x2)) => vec![x1],
(Some(false), (_x1, x2)) => vec![x2],
};
let msg_bytes = serde_cbor::to_vec(&message)?;
for peer in peer_ids {
self.transport.send_message(msg_bytes.clone(), Some(peer))?;
}
Ok(())
}
/// Send a message to another node,
@ -209,6 +222,7 @@ mod if_testing {
use alloc::boxed::Box;
use alloc::vec;
use alloc::vec::Vec;
use crate::message::{Message, MessageType};
fn create_test_network() -> Vec<IronForce> {
let interfaces = create_test_interfaces(5);
@ -238,7 +252,30 @@ mod if_testing {
network[1].main_loop_iteration()?;
network[0].main_loop_iteration()?;
assert!(!network[0].tunnels.is_empty());
// println!("T: {:?}", network[0].tunnels);
Ok(())
}
#[test]
fn test_sending_message() -> IFResult<()> {
let mut network = create_test_network();
let key_1 = network[1].keys.get_public();
network[0].initialize_tunnel_creation(key_1.clone())?;
network[0].main_loop_iteration()?;
network[1].main_loop_iteration()?;
network[0].main_loop_iteration()?;
let zero_keys = network[0].keys.clone();
network[0].send_message(
Message::build()
.message_type(MessageType::SingleCast)
.sign(&zero_keys)
.recipient(key_1.clone())
.content(b"hello".to_vec())
.build()?,
key_1)?;
network[1].main_loop_iteration()?;
let msg = network[1].read_message();
assert!(msg.is_some());
assert_eq!(msg.unwrap().get_decrypted(&network[1].keys).unwrap().as_slice(), b"hello");
Ok(())
}
}

4
src/message.rs

@ -138,7 +138,7 @@ pub struct Message {
/// Optional: hash of the message encrypted for the recipient, so that the recipient can know that this message is for them, but nobody else
recipient_verification: Option<Vec<u8>>,
/// ID of the tunnel that is used
tunnel_id: u64,
pub tunnel_id: u64,
/// Network info
network_info: NetworkInfo,
}
@ -185,7 +185,7 @@ impl Message {
}
/// Get decrypted content of the message
pub fn get_decrypted(&self, keys: Keys) -> IFResult<Vec<u8>> {
pub fn get_decrypted(&self, keys: &Keys) -> IFResult<Vec<u8>> {
Ok(match &self.content {
MessageContent::Plain(c) => c.clone(),
MessageContent::Encrypted(encrypted_content) => {

Loading…
Cancel
Save