diff --git a/examples/local/src/main.rs b/examples/local/src/main.rs index 50226b5652c69bf7cc7b6031a807de6160270301..124a09e76608bce932eb595a57a926eeb3d41717 100644 --- a/examples/local/src/main.rs +++ b/examples/local/src/main.rs @@ -2,23 +2,23 @@ use rust_networking_examples::Network; mod net; -use net::{DispatchHandle, Net}; +use net::{Dispatcher, Connector}; /* main.rs - A simple test program demonstrating sending a message between two instances of Net. + A simple test program demonstrating sending a message between two instances of Connector. */ /// Our main function decorated with the tokio::main attribute to setup an async runtime. #[tokio::main] async fn main() { - // Create a single dispatch actor, which will handle message routing - let dispatcher = DispatchHandle::new(); + // Create a single message dispatch actor, which will handle message routing + let dispatcher = Dispatcher::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; + // Create two connector instances, with addresses "a" and "b" + let mut net_a = Connector::new("a", dispatcher.clone()).await; + let mut net_b = Connector::new("b", dispatcher).await; // Channel for b to receive messages let mut recv_b = net_b.recv().await; diff --git a/examples/local/src/net/dispatch.rs b/examples/local/src/net/dispatch.rs index dc66b073e2e2178869663b29688192462783596b..848b6be67f8fa71d2a0e251fcd6a61f7dbdb654f 100644 --- a/examples/local/src/net/dispatch.rs +++ b/examples/local/src/net/dispatch.rs @@ -22,12 +22,12 @@ enum DispatchMessage { /// The handle to the dispatch actor, note the 'Clone' trait. #[derive(Clone)] -pub struct DispatchHandle { +pub struct Dispatcher { sender: mpsc::Sender<DispatchMessage> } -impl DispatchHandle { - pub fn new() -> DispatchHandle { +impl Dispatcher { + pub fn new() -> Dispatcher { // Open a multiple sender, single consumer channel to communicate with the actor. let (sender, mut receiver) = mpsc::channel(ACTOR_MESSAGE_BUFFER); @@ -41,7 +41,7 @@ impl DispatchHandle { }); // Return the handle - DispatchHandle { sender } + Dispatcher { sender } } /// Utility function to register a new address diff --git a/examples/local/src/net/mod.rs b/examples/local/src/net/mod.rs index 407492e20a5a8977d202f492640867a6bc05e8a7..fe129c165560df54647bdae03406d1fecc459e63 100644 --- a/examples/local/src/net/mod.rs +++ b/examples/local/src/net/mod.rs @@ -3,27 +3,27 @@ mod dispatch; use rust_networking_examples::Network; use tokio::sync::mpsc; -pub use dispatch::DispatchHandle; +pub use dispatch::Dispatcher; /// Handle for sending and receiving on our network simulated using async channels. #[derive(Clone)] -pub struct Net { +pub struct Connector { /// Handle to our dispatcher, for sending messager - dispatcher: DispatchHandle, + dispatcher: Dispatcher, /// Our address address: String, } -impl Net { - pub async fn new(address: &str, dispatcher: DispatchHandle) -> Net { - Net { dispatcher, address: address.to_string() } +impl Connector { + pub async fn new(address: &str, dispatcher: Dispatcher) -> Connector { + Connector { dispatcher, address: address.to_string() } } } // Here we implement our Network interface. #[async_trait::async_trait] -impl Network for Net { +impl Network for Connector { fn send(&mut self, to: &str, message: Vec<u8>) { // Send message to the dispatch actor self.dispatcher.send_message(to, &self.address, message); diff --git a/examples/tcp/README.md b/examples/tcp/README.md index 9d692b15f815a435b29a4dde5cd65b2d9e8b51c6..87a9b755ded7a1f91274ab9fbd5fbe63f62a7fba 100644 --- a/examples/tcp/README.md +++ b/examples/tcp/README.md @@ -2,11 +2,12 @@ An implementation of the network interface, using TCP sockets. Identifiers follow the format of `{IP}:{port}`. -Identifers now behave slightly differently to reflect the realities of using raw sockets, in contrast to `examples/local` where they consistently map 1-1 to an instance of `Connector`. On receiving messages: +In our `examples/local` code, Identifiers consistently map 1-1 to instances of `Connector` in the form of unique labels. +Identifers in this implementation now behave slightly differently, reflecting the realities of using raw sockets. On receiving messages: - `{IP}` may be either an IPV4 address or an IPV6 address, depending on your OS and networking environment. The `{port}` is the source port for the tcp connection. -- `{port}`, will be unpredictable and assigned by the operating system of which other peer's `Connector` instance is located. +- `{port}`, will be unpredictable and assigned by the operating system on which other peer's `Connector` instance is located. - `Connector` may have one or more open sockets with another `Connector` instance, each with a distinct source port number. -When designing your application, you may want to be able to retain some form of unique identifier for each Connector instance on the network: -- If you can be certain that only one instance of `Connector` is behind each IP address, perhaps on a local network such as Grid5000 in which each node has at most one connector; the IP address (with the port stripped out) may be suitable to identify peers. -- Otherwise, you may want to define some messaging protocol on top of the interface yourself. For instance using public key cryptography to consistently distinguish peers. \ No newline at end of file +When designing your distributed application, you may want to retain some form of unique label for each Connector instance on the network: +- If you can be certain that only one instance of `Connector` is behind each IP address, perhaps on a local network such as Grid5000 (assuming each node has at most one connector) - the IP address with the port stripped out may be suitable for this purpose. +- Otherwise, you may want to define some messaging protocol on top of the interface yourself. For instance using public key cryptography to securely and consistently distinguish peers. \ No newline at end of file diff --git a/examples/tcp/src/tcp/mod.rs b/examples/tcp/src/tcp/mod.rs index c27e16bb98476b816981d3af229e0b60801e4455..3bd8b237cde0a7820fce74f7e382b370061ad9e9 100644 --- a/examples/tcp/src/tcp/mod.rs +++ b/examples/tcp/src/tcp/mod.rs @@ -7,8 +7,10 @@ pub use actor::ActorHandle; use rust_networking_examples::Network; use tokio::sync::{mpsc, oneshot}; +/// Used to place a finite buffer size on channels created in this implementation. const DEFAULT_CHANNEL_SIZE: usize = 1024; +#[derive(Clone)] pub struct TcpConnector { handle: ActorHandle } @@ -19,6 +21,7 @@ impl TcpConnector { } } +// Here we implement our Network interface. #[async_trait::async_trait] impl Network for TcpConnector { /// Send a message to the identifier specified by the 'to' field.