Skip to content

Commit

Permalink
Merge pull request signalapp#6 from tiainen/grpc-echo-service
Browse files Browse the repository at this point in the history
Grpc echo service
  • Loading branch information
tiainen authored May 10, 2023
2 parents 9a07a20 + f199396 commit 7269f7f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
\#*\#
.\#*

java/**/bin
java/**/build
java/.gradle
java/local.properties
Expand Down
13 changes: 13 additions & 0 deletions rust/grpc/examples/echo_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use signal_grpc::GrpcClient;

use signal_grpc::Result;

fn main() -> Result<()> {
let grpc_client = GrpcClient::new()?;

let reply = grpc_client.echo_message("PING")?;

println!("REPLY: {}", reply);

Ok(())
}
35 changes: 32 additions & 3 deletions rust/grpc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,58 @@
use crate::{error::{Error, Result}, proto};
use std::collections::HashMap;

const DEFAULT_TARGET: &str = "https://grpcproxy.gluonhq.net:443";

pub struct GrpcClient {
target: String,
tokio_runtime: tokio::runtime::Runtime,
}

impl GrpcClient {
pub fn new() -> Result<Self> {
Ok(GrpcClient {
target: DEFAULT_TARGET.to_owned(),
tokio_runtime: tokio::runtime::Builder::new_current_thread()
.enable_io()
.build()
.map_err(|e| Error::InvalidArgument(format!("tokio.create_runtime: {:?}", e)))?
})
}

pub fn target(&mut self, target: &str) {
self.target = target.to_owned();
}

pub fn echo_message(&self, message: &str) -> Result<String> {
println!("Received echo message: message={}", message);
self.tokio_runtime.block_on(async {
self.async_echo_message(message).await
})
}

async fn async_echo_message(&self, message: &str) -> Result<String> {
let mut tunnel = proto::proxy::tunnel_client::TunnelClient::connect(self.target.clone()).await
.map_err(|e| Error::InvalidArgument(format!("tunnel.connect: {:?}", e)))?;

let request = proto::proxy::EchoRequest {
message: message.to_owned()
};

let response = tunnel.echo_message(request).await
.map_err(|e| Error::InvalidArgument(format!("echo.send_message: {:?}", e)))?;

Ok(response.get_ref().message.clone())
}

pub fn send_message(&self, method: String, url_fragment: String, body: &[u8], headers: HashMap<String, Vec<String>>) -> Result<Vec<u8>> {
println!("Tunneling gRPC message: method={} url_fragment={}, body.len={}, headers={:?}", method, url_fragment, body.len(), headers);
self.tokio_runtime.block_on(async {
self.tunnel_message(method, url_fragment, body, headers).await
self.async_send_message(method, url_fragment, body, headers).await
})
}

async fn tunnel_message(&self, method: String, url_fragment: String, body: &[u8], headers: HashMap<String, Vec<String>>) -> Result<Vec<u8>> {
let mut tunnel = proto::proxy::tunnel_client::TunnelClient::connect("https://grpcproxy.gluonhq.net:443").await
async fn async_send_message(&self, method: String, url_fragment: String, body: &[u8], headers: HashMap<String, Vec<String>>) -> Result<Vec<u8>> {
let mut tunnel = proto::proxy::tunnel_client::TunnelClient::connect(self.target.clone()).await
.map_err(|e| Error::InvalidArgument(format!("tunnel.connect: {:?}", e)))?;

let mut request_headers = vec![];
Expand Down
11 changes: 10 additions & 1 deletion rust/grpc/src/proto/proxy.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@ syntax = "proto3";
package signal.grpc.proxy;

service Tunnel {
rpc SendSomeMessage (SignalRpcMessage) returns (SignalRpcReply) {}
rpc EchoMessage (EchoRequest) returns (EchoReply) {}
rpc SendSomeMessage (SignalRpcMessage) returns (SignalRpcReply) {}
}

message EchoRequest {
string message = 1;
}

message EchoReply {
string message = 1;
}

message SignalRpcMessage {
Expand Down

0 comments on commit 7269f7f

Please sign in to comment.