From 77e208951e1a1d60c470409e6134772e005c0425 Mon Sep 17 00:00:00 2001 From: Santiago Carmuega Date: Sat, 11 Dec 2021 19:35:05 -0300 Subject: [PATCH] docs(multiplexer): tidy up examples --- pallas-multiplexer/README.md | 18 ++++++++++++++++-- pallas-multiplexer/examples/listener.rs | 12 ++++++++---- pallas-multiplexer/examples/sender.rs | 10 ++++++---- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/pallas-multiplexer/README.md b/pallas-multiplexer/README.md index 1e8a9bb..3e56244 100644 --- a/pallas-multiplexer/README.md +++ b/pallas-multiplexer/README.md @@ -19,7 +19,7 @@ The following diagram provides an overview of the components involved: ![Multiplexer Diagram](docs/diagram.png) -## Example Usage +## Usage The following code provides a very rough example of how to setup a client that connects to a node and spawns two concurrent threads running independently, both communication over the same bearer using _Pallas_ multiplexer. @@ -69,6 +69,20 @@ thread::spawn(move || { }); ``` -For a working example of a two peers communicating (a sender and a listener), check the [examples folder](examples). +## Run Examples + +For a working example of a two peers communicating (a sender and a listener), check the [examples folder](examples). To run the examples, open two different terminals and run a different peer in each one: + +```sh +# on terminal 1, start the listener +RUST_LOG=info cargo run --example listener +``` + +```sh +# on terminal 2, start the sender +RUST_LOG=info cargo run --example sender +``` + +## Real World Usage For a more complex, real-world example, check the [Oura](https://github.com/txpipe/oura) repo, it provides a full-blown client tool designed to live-stream block data from a local or remote node. \ No newline at end of file diff --git a/pallas-multiplexer/examples/listener.rs b/pallas-multiplexer/examples/listener.rs index cf531a8..1da8694 100644 --- a/pallas-multiplexer/examples/listener.rs +++ b/pallas-multiplexer/examples/listener.rs @@ -1,5 +1,6 @@ -use std::{os::unix::net::UnixListener, thread, time::Duration}; +use std::{net::TcpListener, thread, time::Duration}; +use log::info; use pallas_multiplexer::{Channel, Multiplexer}; const PROTOCOLS: [u16; 2] = [0x8002u16, 0x8003u16]; @@ -7,8 +8,9 @@ const PROTOCOLS: [u16; 2] = [0x8002u16, 0x8003u16]; fn main() { env_logger::init(); - //let server = TcpListener::bind("0.0.0.0:3001").unwrap(); - let server = UnixListener::bind("/tmp/pallas").unwrap(); + let server = TcpListener::bind("0.0.0.0:3001").unwrap(); + + info!("listening for connections on port 3001"); let (bearer, _) = server.accept().unwrap(); let mut muxer = Multiplexer::setup(bearer, &PROTOCOLS).unwrap(); @@ -17,11 +19,13 @@ fn main() { let handle = muxer.use_channel(protocol); thread::spawn(move || { + info!("starting thread for protocol: {}", protocol); + let Channel(_, rx) = handle; loop { let payload = rx.recv().unwrap(); - println!("id:{}, length:{}", protocol, payload.len()); + info!("got message within thread, id:{}, length:{}", protocol, payload.len()); } }); } diff --git a/pallas-multiplexer/examples/sender.rs b/pallas-multiplexer/examples/sender.rs index 03af24f..3627342 100644 --- a/pallas-multiplexer/examples/sender.rs +++ b/pallas-multiplexer/examples/sender.rs @@ -1,5 +1,6 @@ -use std::{os::unix::net::UnixStream, thread, time::Duration}; +use std::{net::TcpStream, thread, time::Duration}; +use log::info; use pallas_multiplexer::{Channel, Multiplexer}; const PROTOCOLS: [u16; 2] = [0x0002u16, 0x0003u16]; @@ -7,8 +8,8 @@ const PROTOCOLS: [u16; 2] = [0x0002u16, 0x0003u16]; fn main() { env_logger::init(); - //let bearer = TcpStream::connect("127.0.0.1:3001").unwrap(); - let bearer = UnixStream::connect("/tmp/pallas").unwrap(); + info!("connecting to tcp socket on 127.0.0.1:3001"); + let bearer = TcpStream::connect("127.0.0.1:3001").unwrap(); let mut muxer = Multiplexer::setup(bearer, &PROTOCOLS).unwrap(); for protocol in PROTOCOLS { @@ -19,8 +20,9 @@ fn main() { loop { let payload = vec![1; 65545]; + info!("sending dumb payload for protocol: {}", protocol); tx.send(payload).unwrap(); - thread::sleep(Duration::from_millis(50u64 + (protocol as u64 * 10u64))); + thread::sleep(Duration::from_millis(500u64 + (protocol as u64 * 10u64))); } }); }