Mentions légales du service

Skip to content
Snippets Groups Projects
Commit cd3fbd38 authored by WILLIAMS Harvey's avatar WILLIAMS Harvey
Browse files

Doc comments

parent 96d8f25d
No related branches found
No related tags found
No related merge requests found
......@@ -10,7 +10,10 @@ async fn main() {
let mut net_a = Net::new("a").await;
let mut net_b = Net::new("b").await;
// Channel for b to receive messages
let mut recv_b = net_b.recv().await;
// Send message from a to b
net_a.send("b", "Hello world!".as_bytes().to_vec());
while let Some((from, msg)) = recv_b.recv().await {
......
......@@ -13,6 +13,7 @@ enum DispatchMessage {
/// Instruct dispatcher to send a message to a specific address
SendMessage {
to: String,
from: String,
message: Vec<u8>
},
/// Instruct dispatcher to provide a receiver through which messages to 'address' will be received.
......@@ -60,8 +61,8 @@ impl DispatchHandle {
}
/// Utility function to send a message
pub fn send_message(&mut self, to: &str, message: Vec<u8>) {
let message = DispatchMessage::SendMessage { to: to.to_string(), message };
pub fn send_message(&mut self, to: &str, from: &str, message: Vec<u8>) {
let message = DispatchMessage::SendMessage { to: to.to_string(), from: from.to_string(), message };
self.sender.try_send(message).expect("Actor should be alive.");
}
......@@ -79,12 +80,12 @@ struct Actor {
impl Actor {
pub fn handle_message(&mut self, message: DispatchMessage) {
match message {
DispatchMessage::SendMessage { to, message} => {
DispatchMessage::SendMessage { to, from, message} => {
// Look for senders to dispatch message to
if let Some(senders) = self.senders.get_mut(&to) {
senders
.iter_mut()
.try_for_each(|sender| sender.try_send((to.clone(), message.clone())))
.try_for_each(|sender| sender.try_send((from.clone(), message.clone())))
.expect("Expected sender channel to be open and not full");
} else {
eprintln!("No sender with address {} to dispatch - Message dropped.", to);
......
......@@ -25,7 +25,7 @@ impl Net {
impl Network for Net {
fn send(&mut self, to: &str, message: Vec<u8>) {
// Send message to the dispatch actor
self.dispatcher.send_message(to, message);
self.dispatcher.send_message(to, &self.address, message);
}
async fn recv(&mut self) -> mpsc::Receiver<(String, Vec<u8>)> {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment