Add client, client codegen, helloworld and routeguide client examples

This commit is contained in:
Lucio Franco
2019-08-17 23:31:32 -04:00
parent 3951bcf7fe
commit 2ebad2d778
26 changed files with 883 additions and 364 deletions
+32
View File
@@ -0,0 +1,32 @@
#![feature(async_await)]
use tokio::net::TcpStream;
use tower_h2::{add_origin::AddOrigin, Connection};
pub mod hello_world {
include!(concat!(env!("OUT_DIR"), "/helloworld.rs"));
tonic::client!(service = "helloworld.Greeter", proto = "self");
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "[::1]:50051".parse()?;
let io = TcpStream::connect(&addr).await?;
let origin = http::Uri::from_shared(format!("http://{}", addr).into()).unwrap();
let svc = Connection::handshake(io).await?;
let svc = AddOrigin::new(svc, origin);
let mut client = hello_world::GreeterClient::new(svc);
let request = tonic::Request::new(hello_world::HelloRequest {
name: "hello".into(),
});
let response = client.say_hello(request).await?;
println!("RESPONSE={:?}", response);
Ok(())
}
+66
View File
@@ -0,0 +1,66 @@
#![feature(async_await)]
use route_guide::{Point, RouteNote};
use std::time::{Duration, Instant};
use tokio::{net::TcpStream, timer::Interval};
use tonic::Request;
use tower_h2::{add_origin::AddOrigin, Connection};
use futures::TryStreamExt;
mod route_guide {
include!(concat!(env!("OUT_DIR"), "/routeguide.rs"));
tonic::client!(service = "routeguide.RouteGuide", proto = "self");
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "[::1]:10000".parse()?;
let io = TcpStream::connect(&addr).await?;
let origin = http::Uri::from_shared(format!("http://{}", addr).into()).unwrap();
let svc = Connection::handshake(io).await?;
let svc = AddOrigin::new(svc, origin);
let mut client = route_guide::RouteGuideClient::new(svc);
let start = Instant::now();
let response = client
.get_feature(Request::new(Point {
latitude: 409146138,
longitude: -746188906,
}))
.await?;
println!("FEATURE = {:?}", response);
let outbound = async_stream::try_stream! {
let mut interval = Interval::new_interval(Duration::from_secs(1));
while let Some(time) = interval.next().await {
let elapsed = time.duration_since(start);
let note = RouteNote {
location: Some(Point {
latitude: 409146138 + elapsed.as_secs() as i32,
longitude: -746188906,
}),
message: format!("at {:?}", elapsed),
};
yield note;
}
};
let request = Request::new(outbound);
let response = client.route_chat(request).await?;
let mut inbound = response.into_inner();
while let Some(note) = inbound.try_next().await? {
println!("NOTE = {:?}", note);
}
Ok(())
}
+13 -14
View File
@@ -3,19 +3,22 @@
mod data;
use futures::{Stream, StreamExt};
use tokio::{net::TcpListener, sync::{mpsc, Lock}};
use tonic::{Request, Response, Status};
use tower_h2::Server;
use std::sync::Arc;
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::sync::Arc;
use std::time::Instant;
use tokio::{
net::TcpListener,
sync::{mpsc, Lock},
};
use tonic::{Request, Response, Status};
use tower_h2::Server;
pub mod routeguide {
include!(concat!(env!("OUT_DIR"), "/routeguide.rs"));
}
use routeguide::{Point, Rectangle, Feature, RouteNote, RouteSummary};
use routeguide::{Feature, Point, Rectangle, RouteNote, RouteSummary};
#[derive(Debug)]
pub struct RouteGuide {
@@ -51,7 +54,7 @@ impl RouteGuide {
&self,
request: Request<Rectangle>,
) -> Result<Response<mpsc::Receiver<Result<Feature, Status>>>, Status> {
use std::thread;
use std::thread;
println!("ListFeatures = {:?}", request);
@@ -70,22 +73,20 @@ impl RouteGuide {
println!(" /// done sending");
});
Ok(Response::new(rx))
}
pub async fn record_route(
&self,
request: Request<impl Stream<Item = Result<Point, Status>>>,
) -> Result<Response<RouteSummary>, Status>
{
) -> Result<Response<RouteSummary>, Status> {
println!("RecordRoute");
let stream = request.into_inner();
// Pin the inbound stream to the stack so that we can call next on it
// Pin the inbound stream to the stack so that we can call next on it
futures::pin_mut!(stream);
let mut summary = RouteSummary::default();
let mut last_point = None;
let now = Instant::now();
@@ -127,8 +128,6 @@ impl RouteGuide {
let stream = request.into_inner();
let mut state = self.state.clone();
let output = async_stream::try_stream! {
futures::pin_mut!(stream);