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() { ...@@ -10,7 +10,10 @@ async fn main() {
let mut net_a = Net::new("a").await; let mut net_a = Net::new("a").await;
let mut net_b = Net::new("b").await; let mut net_b = Net::new("b").await;
// Channel for b to receive messages
let mut recv_b = net_b.recv().await; let mut recv_b = net_b.recv().await;
// Send message from a to b
net_a.send("b", "Hello world!".as_bytes().to_vec()); net_a.send("b", "Hello world!".as_bytes().to_vec());
while let Some((from, msg)) = recv_b.recv().await { while let Some((from, msg)) = recv_b.recv().await {
......
...@@ -13,6 +13,7 @@ enum DispatchMessage { ...@@ -13,6 +13,7 @@ enum DispatchMessage {
/// Instruct dispatcher to send a message to a specific address /// Instruct dispatcher to send a message to a specific address
SendMessage { SendMessage {
to: String, to: String,
from: String,
message: Vec<u8> message: Vec<u8>
}, },
/// Instruct dispatcher to provide a receiver through which messages to 'address' will be received. /// Instruct dispatcher to provide a receiver through which messages to 'address' will be received.
...@@ -60,8 +61,8 @@ impl DispatchHandle { ...@@ -60,8 +61,8 @@ impl DispatchHandle {
} }
/// Utility function to send a message /// Utility function to send a message
pub fn send_message(&mut self, to: &str, message: Vec<u8>) { pub fn send_message(&mut self, to: &str, from: &str, message: Vec<u8>) {
let message = DispatchMessage::SendMessage { to: to.to_string(), message }; let message = DispatchMessage::SendMessage { to: to.to_string(), from: from.to_string(), message };
self.sender.try_send(message).expect("Actor should be alive."); self.sender.try_send(message).expect("Actor should be alive.");
} }
...@@ -79,12 +80,12 @@ struct Actor { ...@@ -79,12 +80,12 @@ struct Actor {
impl Actor { impl Actor {
pub fn handle_message(&mut self, message: DispatchMessage) { pub fn handle_message(&mut self, message: DispatchMessage) {
match message { match message {
DispatchMessage::SendMessage { to, message} => { DispatchMessage::SendMessage { to, from, message} => {
// Look for senders to dispatch message to // Look for senders to dispatch message to
if let Some(senders) = self.senders.get_mut(&to) { if let Some(senders) = self.senders.get_mut(&to) {
senders senders
.iter_mut() .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"); .expect("Expected sender channel to be open and not full");
} else { } else {
eprintln!("No sender with address {} to dispatch - Message dropped.", to); eprintln!("No sender with address {} to dispatch - Message dropped.", to);
......
...@@ -25,7 +25,7 @@ impl Net { ...@@ -25,7 +25,7 @@ impl Net {
impl Network for Net { impl Network for Net {
fn send(&mut self, to: &str, message: Vec<u8>) { fn send(&mut self, to: &str, message: Vec<u8>) {
// Send message to the dispatch actor // 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>)> { 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