Added an example with a simple host. Now we need a simple client that can call it and get an answer back.

This commit is contained in:
Martin Slot 2023-12-19 08:06:59 +01:00
parent a0ae950d03
commit 6570d5daf7
8 changed files with 85 additions and 23 deletions

19
Cargo.lock generated
View File

@ -143,14 +143,6 @@ dependencies = [
"prost-build",
]
[[package]]
name = "commander_runner"
version = "0.1.0"
dependencies = [
"commander_host",
"commander_kernel",
]
[[package]]
name = "criterion"
version = "0.4.0"
@ -257,6 +249,17 @@ dependencies = [
"libc",
]
[[package]]
name = "examples"
version = "0.0.0"
dependencies = [
"bytes",
"commander_host",
"commander_kernel",
"prost",
"prost-build",
]
[[package]]
name = "fastrand"
version = "1.9.0"

View File

@ -1,3 +1,3 @@
[workspace]
members = ["commander_runner", "commander_host", "kernel", "tests"]
members = ["commander_host", "kernel", "tests", "examples"]

View File

@ -1,11 +0,0 @@
[package]
name = "commander_runner"
version = "0.1.0"
edition = "2021"
publish = ["descore"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
commander_host = { path = "../commander_host", version = "0.1.0", registry = "descore" }
commander_kernel = { path = "../kernel", version = "0.1.0", registry = "descore" }

View File

@ -1,3 +0,0 @@
fn main() {
println!("hello world");
}

18
examples/Cargo.toml Normal file
View File

@ -0,0 +1,18 @@
[package]
name = "examples"
version = "0.0.0"
edition = "2021"
publish = false
[build-dependencies]
prost-build = "0.11"
[dev-dependencies]
bytes = "1.1.0"
prost = "0.11"
commander_kernel = { path = "../kernel" }
commander_host = { path = "../commander_host" }
[[example]]
name = "simple_host"
path = "simple_host/main.rs"

6
examples/build.rs Normal file
View File

@ -0,0 +1,6 @@
use std::io::Result;
fn main() -> Result<()> {
prost_build::compile_protos(&["simple.proto"], &["."])?;
Ok(())
}

7
examples/simple.proto Normal file
View File

@ -0,0 +1,7 @@
syntax = "proto3";
package simple_message;
message MessageObject {
string text = 1;
}

View File

@ -0,0 +1,42 @@
use commander_host::Host;
use commander_kernel::router::Action;
use commander_kernel::tcp::ConnectOptions;
use prost::Message;
use std::io::Cursor;
include!(concat!(env!("OUT_DIR"), "/simple_message.rs"));
fn main() {
let connect_options = ConnectOptions::new(String::from("127.0.0.1"), 6661);
let mut host = Host::new(connect_options.clone());
let action = ExampleAction;
host.add_action("route/example", Box::new(action));
match host.start() {
Ok(_) => println!("host started"),
Err(_e) => {
panic!("didn't start host")
}
}
println!("Host started. Hit any key to stop");
let mut buffer = String::new();
std::io::stdin().read_line(&mut buffer);
match host.stop() {
Ok(_) => println!("host stopped"),
Err(_e) => panic!("couldn't stop"),
}
}
struct ExampleAction;
impl Action for ExampleAction {
fn work(&self, data: &Vec<u8>) -> Vec<u8> {
let message = MessageObject::decode(Cursor::new(&data));
let message = message.unwrap();
let return_string = format!("Hello from Example. Got: {}", message.text);
return_string.as_bytes().to_vec()
}
}