Migrate to tonic
This commit is contained in:
parent
6d1582792c
commit
7a9b9c0f09
10 changed files with 1014 additions and 79 deletions
1
.tool-versions
Normal file
1
.tool-versions
Normal file
|
@ -0,0 +1 @@
|
||||||
|
rust 1.67.0
|
954
Cargo.lock
generated
954
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -2,6 +2,7 @@
|
||||||
name = "command_gateway"
|
name = "command_gateway"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
publish = false
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
@ -17,5 +18,9 @@ name = "client"
|
||||||
path = "src/client/main.rs"
|
path = "src/client/main.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "^1.0"
|
tokio = { version = "1.0", features = ["rt-multi-thread", "macros"] } # Required for tonic
|
||||||
jsonrpc-derive = "18.0"
|
prost = "0.11" # Required for tonic
|
||||||
|
tonic = "0.8"
|
||||||
|
|
||||||
|
[build-dependencies]
|
||||||
|
tonic-build = { version = "0.8", features = ["prost"] } # Required for tonic
|
5
README.md
Normal file
5
README.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
# Project
|
||||||
|
|
||||||
|
## Requirements
|
||||||
|
|
||||||
|
- `protobuf` see [tonic dependencies](https://github.com/hyperium/tonic#dependencies)
|
4
build.rs
Normal file
4
build.rs
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
tonic_build::compile_protos("proto/helloworld.proto")?;
|
||||||
|
Ok(())
|
||||||
|
}
|
17
proto/helloworld.proto
Normal file
17
proto/helloworld.proto
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
syntax = "proto3";
|
||||||
|
package helloworld;
|
||||||
|
|
||||||
|
service Greeter {
|
||||||
|
// Our SayHello rpc accepts HelloRequests and returns HelloReplies
|
||||||
|
rpc SayHello (HelloRequest) returns (HelloReply);
|
||||||
|
}
|
||||||
|
|
||||||
|
message HelloRequest {
|
||||||
|
// Request message contains the name to be greeted
|
||||||
|
string name = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message HelloReply {
|
||||||
|
// Reply contains the greeting message
|
||||||
|
string message = 1;
|
||||||
|
}
|
|
@ -1,41 +1,21 @@
|
||||||
use std::os::unix::net::{UnixListener, UnixStream};
|
use hello_world::greeter_client::GreeterClient;
|
||||||
use std::io::{Read, Write};
|
use hello_world::HelloRequest;
|
||||||
|
|
||||||
use anyhow::Context;
|
pub mod hello_world {
|
||||||
|
tonic::include_proto!("helloworld");
|
||||||
fn main() -> anyhow::Result<()> {
|
|
||||||
let socket_path = "mysocket";
|
|
||||||
|
|
||||||
let mut unix_stream =
|
|
||||||
UnixStream::connect(socket_path).context("Could not create stream")?;
|
|
||||||
|
|
||||||
write_request_and_shutdown(&mut unix_stream)?;
|
|
||||||
read_from_stream(&mut unix_stream)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_request_and_shutdown(unix_stream: &mut UnixStream) -> anyhow::Result<()> {
|
#[tokio::main]
|
||||||
unix_stream
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
.write(b"Hello?")
|
let mut client = GreeterClient::connect("http://[::1]:50051").await?;
|
||||||
.context("Failed at writing onto the unix stream")?;
|
|
||||||
|
|
||||||
println!("We sent a request");
|
let request = tonic::Request::new(HelloRequest {
|
||||||
println!("Shutting down writing on the stream, waiting for response...");
|
name: "Tonic".into(),
|
||||||
|
});
|
||||||
|
|
||||||
unix_stream
|
let response = client.say_hello(request).await?;
|
||||||
.shutdown(std::net::Shutdown::Write)
|
|
||||||
.context("Could not shutdown writing on the stream")?;
|
|
||||||
|
|
||||||
Ok(())
|
println!("RESPONSE={:?}", response);
|
||||||
}
|
|
||||||
|
|
||||||
fn read_from_stream(unix_stream: &mut UnixStream) -> anyhow::Result<()> {
|
|
||||||
let mut response = String::new();
|
|
||||||
unix_stream
|
|
||||||
.read_to_string(&mut response)
|
|
||||||
.context("Failed at reading the unix stream")?;
|
|
||||||
|
|
||||||
println!("We received this response: {}", response);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
|
@ -1 +0,0 @@
|
||||||
pub mod internal_rpc;
|
|
|
@ -1,39 +1,41 @@
|
||||||
use std::os::unix::net::{UnixListener, UnixStream};
|
use tonic::{transport::Server, Request, Response, Status};
|
||||||
use anyhow::Context;
|
|
||||||
use std::io::{Read, Write};
|
|
||||||
|
|
||||||
fn main() -> anyhow::Result<()> {
|
use hello_world::greeter_server::{Greeter, GreeterServer};
|
||||||
let socket_path = "mysocket";
|
use hello_world::{HelloReply, HelloRequest};
|
||||||
|
|
||||||
if std::fs::metadata(socket_path).is_ok() {
|
pub mod hello_world {
|
||||||
println!("A socket is already present. Deleting...");
|
tonic::include_proto!("helloworld");
|
||||||
std::fs::remove_file(socket_path).with_context(|| {
|
}
|
||||||
format!("could not delete previous socket at {:?}", socket_path)
|
|
||||||
})?;
|
|
||||||
}
|
|
||||||
|
|
||||||
let unix_listener =
|
#[derive(Default)]
|
||||||
UnixListener::bind(socket_path).context("Could not create the unix socket")?;
|
pub struct MyGreeter {}
|
||||||
|
|
||||||
// put the daemon logic in a loop to accept several connections
|
#[tonic::async_trait]
|
||||||
loop {
|
impl Greeter for MyGreeter {
|
||||||
let (mut unix_stream, socket_address) = unix_listener
|
async fn say_hello(
|
||||||
.accept()
|
&self,
|
||||||
.context("Failed at accepting a connection on the unix listener")?;
|
request: Request<HelloRequest>,
|
||||||
handle_stream(unix_stream)?;
|
) -> Result<Response<HelloReply>, Status> {
|
||||||
|
println!("Got a request from {:?}", request.remote_addr());
|
||||||
|
|
||||||
|
let reply = hello_world::HelloReply {
|
||||||
|
message: format!("Hello {}!", request.into_inner().name),
|
||||||
|
};
|
||||||
|
Ok(Response::new(reply))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_stream(mut unix_stream: UnixStream) -> anyhow::Result<()> {
|
#[tokio::main]
|
||||||
let mut message = String::new();
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
unix_stream
|
let addr = "[::1]:50051".parse().unwrap();
|
||||||
.read_to_string(&mut message)
|
let greeter = MyGreeter::default();
|
||||||
.context("Failed at reading the unix stream")?;
|
|
||||||
|
|
||||||
println!("We received this message: {}\nReplying...", message);
|
println!("GreeterServer listening on {}", addr);
|
||||||
|
|
||||||
|
Server::builder()
|
||||||
|
.add_service(GreeterServer::new(greeter))
|
||||||
|
.serve(addr)
|
||||||
|
.await?;
|
||||||
|
|
||||||
unix_stream
|
|
||||||
.write(b"I hear you!")
|
|
||||||
.context("Failed at writing onto the unix stream")?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
Reference in a new issue