diff --git a/.gitignore b/.gitignore index 1ac9212d19..c7e0a47968 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,7 @@ \#*\# .\#* +java/**/bin java/**/build java/.gradle java/local.properties diff --git a/rust/grpc/examples/echo_client.rs b/rust/grpc/examples/echo_client.rs new file mode 100644 index 0000000000..f581fa07be --- /dev/null +++ b/rust/grpc/examples/echo_client.rs @@ -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(()) +} diff --git a/rust/grpc/src/client.rs b/rust/grpc/src/client.rs index ca8c146e66..28df089418 100644 --- a/rust/grpc/src/client.rs +++ b/rust/grpc/src/client.rs @@ -6,13 +6,17 @@ 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 { Ok(GrpcClient { + target: DEFAULT_TARGET.to_owned(), tokio_runtime: tokio::runtime::Builder::new_current_thread() .enable_io() .build() @@ -20,15 +24,40 @@ impl GrpcClient { }) } + pub fn target(&mut self, target: &str) { + self.target = target.to_owned(); + } + + pub fn echo_message(&self, message: &str) -> Result { + 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 { + 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>) -> Result> { 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>) -> Result> { - 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>) -> Result> { + 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![]; diff --git a/rust/grpc/src/proto/proxy.proto b/rust/grpc/src/proto/proxy.proto index 95bd7ca8e8..8bf9470133 100644 --- a/rust/grpc/src/proto/proxy.proto +++ b/rust/grpc/src/proto/proxy.proto @@ -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 {