Skip to content

Commit

Permalink
chore(protocol): add examples
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Moreira de Jesus <martin.moreira-de-jesus@protonmail.com>
  • Loading branch information
mmoreiradj committed Apr 16, 2024
1 parent 4f98ba0 commit 3f5db89
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/serverless_protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
19 changes: 19 additions & 0 deletions src/serverless_protocol/README.md
Original file line number Diff line number Diff line change
@@ -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=<path_to_first_pty> --serial-path-b=<path_to_second_pty>
```

This example will show how processes can communicate over a serial connection.
75 changes: 75 additions & 0 deletions src/serverless_protocol/examples/serverless_protocol_example.rs
Original file line number Diff line number Diff line change
@@ -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<thread::JoinHandle<()>> = 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();
}
}

0 comments on commit 3f5db89

Please sign in to comment.