Mentions légales du service

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

Pushing some changes to the main branch

parent cd3fbd38
No related branches found
No related tags found
No related merge requests found
......@@ -2,13 +2,23 @@ use rust_networking_examples::Network;
mod net;
use net::Net;
use net::{DispatchHandle, Net};
/*
main.rs
A simple test program demonstrating sending a message between two instances of Net.
*/
/// Our main function decorated with the tokio::main attribute to setup an async runtime.
#[tokio::main]
async fn main() {
let mut net_a = Net::new("a").await;
let mut net_b = Net::new("b").await;
// Create a single dispatch actor, which will handle message routing
let dispatcher = DispatchHandle::new();
// Create two network instances, with addresses "a" and "b"
let mut net_a = Net::new("a", dispatcher.clone()).await;
let mut net_b = Net::new("b", dispatcher).await;
// Channel for b to receive messages
let mut recv_b = net_b.recv().await;
......@@ -16,7 +26,8 @@ async fn main() {
// Send message from a to b
net_a.send("b", "Hello world!".as_bytes().to_vec());
// See what b receives
while let Some((from, msg)) = recv_b.recv().await {
println!("Received {:?} from {}", String::from_utf8(msg), from);
println!("b received {:?} from {}", String::from_utf8(msg).unwrap(), from);
}
}
mod dispatch;
use dispatch::{DispatchHandle, DISPATCHER};
use rust_networking_examples::Network;
use tokio::sync::mpsc;
pub use dispatch::DispatchHandle;
/// Handle for sending and receiving on our network simulated using async channels.
#[derive(Clone)]
pub struct Net {
......@@ -15,8 +16,8 @@ pub struct Net {
}
impl Net {
pub async fn new(address: &str) -> Net {
Net { dispatcher: DISPATCHER.clone(), address: address.to_string() }
pub async fn new(address: &str, dispatcher: DispatchHandle) -> Net {
Net { dispatcher, address: address.to_string() }
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment