-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Martin Moreira de Jesus <martin.moreira-de-jesus@protonmail.com>
- Loading branch information
1 parent
4f98ba0
commit 3f5db89
Showing
3 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
75
src/serverless_protocol/examples/serverless_protocol_example.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |