docs(multiplexer): tidy up examples

This commit is contained in:
Santiago Carmuega 2021-12-11 19:35:05 -03:00
parent 84347dd84c
commit 77e208951e
3 changed files with 30 additions and 10 deletions

View file

@ -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.

View file

@ -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());
}
});
}

View file

@ -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)));
}
});
}