From 9e435c4b59b2b0596c72b8153d8dcbf7b24928b1 Mon Sep 17 00:00:00 2001 From: Martin Moreira de Jesus Date: Tue, 16 Apr 2024 22:25:27 +0200 Subject: [PATCH] chore(protocol): add examples Signed-off-by: Martin Moreira de Jesus --- src/serverless_protocol/Cargo.toml | 9 ++- src/serverless_protocol/README.md | 19 +++++ .../examples/serverless_protocol_example.rs | 75 +++++++++++++++++++ 3 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 src/serverless_protocol/README.md create mode 100644 src/serverless_protocol/examples/serverless_protocol_example.rs diff --git a/src/serverless_protocol/Cargo.toml b/src/serverless_protocol/Cargo.toml index bd484d1..bb44194 100644 --- a/src/serverless_protocol/Cargo.toml +++ b/src/serverless_protocol/Cargo.toml @@ -7,6 +7,13 @@ edition = "2021" name = "serverless_protocol" [dependencies] -serde = "1.0.197" +serde = { version = "1.0.197", features = ["derive"] } serde_json = "1.0.115" serialport = "4.3.0" + +[dev-dependencies] +clap = { version = "4.5.2", features = ["derive"] } + +[[example]] +name = "serverless_protocol_example" +path = "examples/serverless_protocol_example.rs" diff --git a/src/serverless_protocol/README.md b/src/serverless_protocol/README.md new file mode 100644 index 0000000..07813f1 --- /dev/null +++ b/src/serverless_protocol/README.md @@ -0,0 +1,19 @@ +# Serverless Protocol Over Serial + +Allows the VMM and Agent to communicate over a serial connection. + +## How to test it + +1. Use socat to create a virtual serial port: + +```bash +socat -d -d pty,raw,echo=0 pty,raw,echo=0 +``` + +2. Run the example: + +```bash +cargo run --example cargo run --example serverless_protocol_example -- --serial-path-a= --serial-path-b= +``` + +This example will show how processes can communicate over a serial connection. diff --git a/src/serverless_protocol/examples/serverless_protocol_example.rs b/src/serverless_protocol/examples/serverless_protocol_example.rs new file mode 100644 index 0000000..3a0df65 --- /dev/null +++ b/src/serverless_protocol/examples/serverless_protocol_example.rs @@ -0,0 +1,75 @@ +extern crate clap; +extern crate serverless_protocol; + +use std::{thread, time::Duration}; + +use clap::Parser; +use serverless_protocol::{ + messages::{MessageType, Payload, StartMessage}, + CloudletMessage, CloudletProtocol, +}; + +#[derive(Debug, Parser)] +#[command(version, about, long_about = None)] +struct Args { + #[arg(long)] + serial_path_a: String, + + #[arg(long)] + serial_path_b: String, +} + +fn main() { + let args = Args::parse(); + println!("{:?}", args); + + let mut handles: Vec> = Vec::new(); + + let handle_a = thread::spawn(|| { + let serial_path = args.serial_path_a; + + let serial_port = serialport::new(serial_path, 115_200) + .timeout(Duration::from_secs(10)) + .open_native() + .expect("Failed to open serial port"); + + let mut protocol = CloudletProtocol::new(serial_port); + + println!("waiting for message"); + + let message = protocol + .read_message() + .expect("Failed to read message from serial port"); + + println!("{:?}", message); + }); + + let handle_b = thread::spawn(|| { + let serial_path = args.serial_path_b; + + let serial_port = serialport::new(serial_path, 115_200) + .timeout(Duration::from_secs(10)) + .open_native() + .expect("Failed to open serial port"); + + let mut protocol = CloudletProtocol::new(serial_port); + + let message = CloudletMessage::new( + MessageType::Start, + Payload::Start(StartMessage::new("Hello, World!".to_string())), + ); + + println!("sending message: {:?}", message); + + protocol.send_message(message); + + println!("message sent") + }); + + handles.push(handle_a); + handles.push(handle_b); + + for handle in handles { + handle.join().unwrap(); + } +}