-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
21 changed files
with
195 additions
and
21 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
[workspace] | ||
resolver = "2" | ||
|
||
members = [ | ||
"rucat_server", | ||
"rucat_common" | ||
"rucat_common", | ||
"rucat_engine" | ||
] |
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
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,4 @@ | ||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
tonic_build::compile_protos("proto/engine.proto")?; | ||
Ok(()) | ||
} |
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,14 @@ | ||
syntax = "proto3"; | ||
package engine_grpc; | ||
|
||
service Greeter { | ||
rpc SayHello (HelloRequest) returns (HelloReply); | ||
} | ||
|
||
message HelloRequest { | ||
string name = 1; | ||
} | ||
|
||
message HelloReply { | ||
string message = 1; | ||
} |
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 |
---|---|---|
@@ -1,4 +1,8 @@ | ||
pub mod error; | ||
//! Common types and utilities for the Rucat projects. | ||
pub mod error; | ||
pub mod engine_grpc { | ||
tonic::include_proto!("engine_grpc"); | ||
} | ||
/// Unique identifier for an engine. | ||
pub type EngineId = String; |
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,11 @@ | ||
[package] | ||
name = "rucat_engine" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
rucat_common = {path = "../rucat_common"} | ||
clap = { version = "4.5.7", features = ["derive"] } | ||
clap_derive = "4.5.5" | ||
tonic = "0.11.0" | ||
tokio = { version = "1.38.0", features = ["macros", "rt-multi-thread"] } |
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,55 @@ | ||
use clap::Parser; | ||
use std::net::{Ipv6Addr, SocketAddr, SocketAddrV6}; | ||
use tonic::{transport::Server, Request, Response, Status}; | ||
|
||
use rucat_common::engine_grpc::greeter_server::{Greeter, GreeterServer}; | ||
use rucat_common::engine_grpc::{HelloReply, HelloRequest}; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct MyGreeter {} | ||
|
||
#[tonic::async_trait] | ||
impl Greeter for MyGreeter { | ||
async fn say_hello( | ||
&self, | ||
request: Request<HelloRequest>, | ||
) -> Result<Response<HelloReply>, Status> { | ||
println!("Got a request: {:?}", request); | ||
|
||
let reply = HelloReply { | ||
message: format!("Hello {}!", request.into_inner().name), | ||
}; | ||
|
||
Ok(Response::new(reply)) | ||
} | ||
} | ||
|
||
#[derive(Parser, Debug)] | ||
#[command(version, about, long_about = None)] | ||
struct Args { | ||
/// IPv6 address of the engine | ||
#[arg(long, default_value_t = Ipv6Addr::LOCALHOST)] | ||
ip: Ipv6Addr, | ||
|
||
/// Port of the engine binding | ||
#[arg(long)] | ||
port: u16, | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let Args { ip, port } = Args::parse(); | ||
let addr = SocketAddrV6::new(ip, port, 0, 0); | ||
let greeter = MyGreeter::default(); | ||
|
||
println!("start from rucat engine!"); | ||
|
||
Server::builder() | ||
.add_service(GreeterServer::new(greeter)) | ||
.serve(SocketAddr::V6(addr)) | ||
.await?; | ||
|
||
println!("Hello, world from rucat engine!"); | ||
|
||
Ok(()) | ||
} |
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 was deleted.
Oops, something went wrong.
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,5 @@ | ||
//! Module that contains engine rest request handlers and | ||
//! rpc between server and engine. | ||
pub(crate) mod router; | ||
mod rpc; |
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,41 @@ | ||
//! RPC between server and engine. | ||
//! | ||
//! Create a new process for the engine and start the gRPC server. | ||
//! The engine will be listening on the given port. (localhost for now) | ||
use rucat_common::{ | ||
engine_grpc::{greeter_client::GreeterClient, HelloRequest}, | ||
error::{Result, RucatError}, | ||
}; | ||
|
||
use tokio::process::Command; | ||
use tracing::info; | ||
|
||
/// Create a new process for the engine and start the gRPC server. | ||
/// The engine will be listening on the given port. (localhost for now) | ||
pub(super) async fn create_engine(engine_binary_path: &str, port: u16) -> Result<()> { | ||
// Start the engine process. | ||
Command::new(engine_binary_path) | ||
.args(["--ip", "::1", "--port", port.to_string().as_str()]) | ||
.spawn()?; | ||
|
||
// TODO: better way to wait for the engine to start. | ||
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await; | ||
|
||
let mut client = GreeterClient::connect(format!("http://[::1]:{}", port)) | ||
.await | ||
.map_err(|e| RucatError::FailedToStartEngine(e.to_string()))?; | ||
|
||
let request = tonic::Request::new(HelloRequest { | ||
name: "Tonic".into(), | ||
}); | ||
|
||
let response = client | ||
.say_hello(request) | ||
.await | ||
.map_err(|e| RucatError::FailedToStartEngine(e.to_string()))?; | ||
|
||
info!("RESPONSE={:?}", response.into_inner().message); | ||
|
||
Ok(()) | ||
} |
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
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
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
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,4 @@ | ||
# build rucat server and rucat engine | ||
cargo build --release | ||
# run rucat server | ||
RUST_LOG=debug cargo run --release --bin rucat_server -- --engine-binary-path target/release/rucat_engine |