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"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
publish = false
|
||||
|
||||
# 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"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "^1.0"
|
||||
jsonrpc-derive = "18.0"
|
||||
tokio = { version = "1.0", features = ["rt-multi-thread", "macros"] } # Required for tonic
|
||||
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 std::io::{Read, Write};
|
||||
use hello_world::greeter_client::GreeterClient;
|
||||
use hello_world::HelloRequest;
|
||||
|
||||
use anyhow::Context;
|
||||
|
||||
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(())
|
||||
pub mod hello_world {
|
||||
tonic::include_proto!("helloworld");
|
||||
}
|
||||
|
||||
fn write_request_and_shutdown(unix_stream: &mut UnixStream) -> anyhow::Result<()> {
|
||||
unix_stream
|
||||
.write(b"Hello?")
|
||||
.context("Failed at writing onto the unix stream")?;
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut client = GreeterClient::connect("http://[::1]:50051").await?;
|
||||
|
||||
println!("We sent a request");
|
||||
println!("Shutting down writing on the stream, waiting for response...");
|
||||
let request = tonic::Request::new(HelloRequest {
|
||||
name: "Tonic".into(),
|
||||
});
|
||||
|
||||
unix_stream
|
||||
.shutdown(std::net::Shutdown::Write)
|
||||
.context("Could not shutdown writing on the stream")?;
|
||||
let response = client.say_hello(request).await?;
|
||||
|
||||
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(())
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
pub mod internal_rpc;
|
|
@ -1,39 +1,41 @@
|
|||
use std::os::unix::net::{UnixListener, UnixStream};
|
||||
use anyhow::Context;
|
||||
use std::io::{Read, Write};
|
||||
use tonic::{transport::Server, Request, Response, Status};
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let socket_path = "mysocket";
|
||||
use hello_world::greeter_server::{Greeter, GreeterServer};
|
||||
use hello_world::{HelloReply, HelloRequest};
|
||||
|
||||
if std::fs::metadata(socket_path).is_ok() {
|
||||
println!("A socket is already present. Deleting...");
|
||||
std::fs::remove_file(socket_path).with_context(|| {
|
||||
format!("could not delete previous socket at {:?}", socket_path)
|
||||
})?;
|
||||
}
|
||||
pub mod hello_world {
|
||||
tonic::include_proto!("helloworld");
|
||||
}
|
||||
|
||||
let unix_listener =
|
||||
UnixListener::bind(socket_path).context("Could not create the unix socket")?;
|
||||
#[derive(Default)]
|
||||
pub struct MyGreeter {}
|
||||
|
||||
// put the daemon logic in a loop to accept several connections
|
||||
loop {
|
||||
let (mut unix_stream, socket_address) = unix_listener
|
||||
.accept()
|
||||
.context("Failed at accepting a connection on the unix listener")?;
|
||||
handle_stream(unix_stream)?;
|
||||
#[tonic::async_trait]
|
||||
impl Greeter for MyGreeter {
|
||||
async fn say_hello(
|
||||
&self,
|
||||
request: Request<HelloRequest>,
|
||||
) -> 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<()> {
|
||||
let mut message = String::new();
|
||||
unix_stream
|
||||
.read_to_string(&mut message)
|
||||
.context("Failed at reading the unix stream")?;
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let addr = "[::1]:50051".parse().unwrap();
|
||||
let greeter = MyGreeter::default();
|
||||
|
||||
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(())
|
||||
}
|
Reference in a new issue