Inital pass at route guide
This commit is contained in:
@@ -14,6 +14,10 @@ path = "src/helloworld/server.rs"
|
||||
# name = "helloworld-client"
|
||||
# path = "src/helloworld/client.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "routeguide-server"
|
||||
path = "src/routeguide/server.rs"
|
||||
|
||||
[dependencies]
|
||||
tonic = { path = "../tonic" }
|
||||
tower-h2 = { path = "../tower-h2" }
|
||||
|
||||
@@ -4,4 +4,10 @@ fn main() {
|
||||
&["proto/helloworld"],
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
tonic_build::compile_protos(
|
||||
&["proto/routeguide/route_guide.proto"],
|
||||
&["proto/routeguide"],
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
// Copyright 2015 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
syntax = "proto3";
|
||||
|
||||
option java_multiple_files = true;
|
||||
option java_package = "io.grpc.examples.routeguide";
|
||||
option java_outer_classname = "RouteGuideProto";
|
||||
|
||||
package routeguide;
|
||||
|
||||
// Interface exported by the server.
|
||||
service RouteGuide {
|
||||
// A simple RPC.
|
||||
//
|
||||
// Obtains the feature at a given position.
|
||||
//
|
||||
// A feature with an empty name is returned if there's no feature at the given
|
||||
// position.
|
||||
rpc GetFeature(Point) returns (Feature) {}
|
||||
|
||||
// A server-to-client streaming RPC.
|
||||
//
|
||||
// Obtains the Features available within the given Rectangle. Results are
|
||||
// streamed rather than returned at once (e.g. in a response message with a
|
||||
// repeated field), as the rectangle may cover a large area and contain a
|
||||
// huge number of features.
|
||||
rpc ListFeatures(Rectangle) returns (stream Feature) {}
|
||||
|
||||
// A client-to-server streaming RPC.
|
||||
//
|
||||
// Accepts a stream of Points on a route being traversed, returning a
|
||||
// RouteSummary when traversal is completed.
|
||||
rpc RecordRoute(stream Point) returns (RouteSummary) {}
|
||||
|
||||
// A Bidirectional streaming RPC.
|
||||
//
|
||||
// Accepts a stream of RouteNotes sent while a route is being traversed,
|
||||
// while receiving other RouteNotes (e.g. from other users).
|
||||
rpc RouteChat(stream RouteNote) returns (stream RouteNote) {}
|
||||
}
|
||||
|
||||
// Points are represented as latitude-longitude pairs in the E7 representation
|
||||
// (degrees multiplied by 10**7 and rounded to the nearest integer).
|
||||
// Latitudes should be in the range +/- 90 degrees and longitude should be in
|
||||
// the range +/- 180 degrees (inclusive).
|
||||
message Point {
|
||||
int32 latitude = 1;
|
||||
int32 longitude = 2;
|
||||
}
|
||||
|
||||
// A latitude-longitude rectangle, represented as two diagonally opposite
|
||||
// points "lo" and "hi".
|
||||
message Rectangle {
|
||||
// One corner of the rectangle.
|
||||
Point lo = 1;
|
||||
|
||||
// The other corner of the rectangle.
|
||||
Point hi = 2;
|
||||
}
|
||||
|
||||
// A feature names something at a given point.
|
||||
//
|
||||
// If a feature could not be named, the name is empty.
|
||||
message Feature {
|
||||
// The name of the feature.
|
||||
string name = 1;
|
||||
|
||||
// The point where the feature is detected.
|
||||
Point location = 2;
|
||||
}
|
||||
|
||||
// A RouteNote is a message sent while at a given point.
|
||||
message RouteNote {
|
||||
// The location from which the message is sent.
|
||||
Point location = 1;
|
||||
|
||||
// The message to be sent.
|
||||
string message = 2;
|
||||
}
|
||||
|
||||
// A RouteSummary is received in response to a RecordRoute rpc.
|
||||
//
|
||||
// It contains the number of individual points received, the number of
|
||||
// detected features, and the total distance covered as the cumulative sum of
|
||||
// the distance between each point.
|
||||
message RouteSummary {
|
||||
// The number of points received.
|
||||
int32 point_count = 1;
|
||||
|
||||
// The number of known features passed while traversing the route.
|
||||
int32 feature_count = 2;
|
||||
|
||||
// The distance covered in metres.
|
||||
int32 distance = 3;
|
||||
|
||||
// The duration of the traversal in seconds.
|
||||
int32 elapsed_time = 4;
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
#![feature(async_await)]
|
||||
|
||||
use std::time::Duration;
|
||||
use tokio::{timer::Delay, net::TcpListener};
|
||||
use tokio::{net::TcpListener, timer::Delay};
|
||||
use tonic::{Request, Response, Status};
|
||||
use tower_h2::Server;
|
||||
|
||||
@@ -16,7 +16,10 @@ pub struct MyGreeter {
|
||||
|
||||
#[tonic::server(service = "helloworld.Greeter", proto = "hello_world")]
|
||||
impl MyGreeter {
|
||||
pub async fn say_hello(&self, request: Request<hello_world::HelloRequest>) -> Result<Response<hello_world::HelloReply>, Status> {
|
||||
pub async fn say_hello(
|
||||
&self,
|
||||
request: Request<hello_world::HelloRequest>,
|
||||
) -> Result<Response<hello_world::HelloReply>, Status> {
|
||||
println!("Got a request: {:?}", request);
|
||||
|
||||
let string = &self.data;
|
||||
@@ -27,7 +30,7 @@ impl MyGreeter {
|
||||
println!("My data: {:?}", string);
|
||||
|
||||
Delay::new(when).await;
|
||||
|
||||
|
||||
let reply = hello_world::HelloReply {
|
||||
message: "Zomg, it works!".into(),
|
||||
};
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
#![feature(async_await)]
|
||||
|
||||
use futures::Stream;
|
||||
use tokio::net::TcpListener;
|
||||
use tonic::{Request, Response, Status};
|
||||
use tower_h2::Server;
|
||||
|
||||
pub mod routeguide {
|
||||
include!(concat!(env!("OUT_DIR"), "/routeguide.rs"));
|
||||
}
|
||||
|
||||
use routeguide::*;
|
||||
|
||||
type BoxStream<T> = Pin<Box<dyn Stream<Item = Result<T, Status>> + Send + 'static>>;
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
pub struct RouteGuide {
|
||||
data: String,
|
||||
}
|
||||
|
||||
#[tonic::server(service = "routeguide.RouteGuide", proto = "routeguide")]
|
||||
impl RouteGuide {
|
||||
pub async fn get_feature(&self, _req: Request<Point>) -> Result<Response<Feature>, Status> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
pub async fn list_features(
|
||||
&self,
|
||||
_req: Request<Rectangle>,
|
||||
) -> Result<Response<BoxStream<Feature>>, Status> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
pub async fn record_route(
|
||||
&self,
|
||||
_req: Request<impl Stream<Item = Result<Point, Status>>>,
|
||||
) -> Result<Response<RouteSummary>, Status> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
// pub async fn route_chat(
|
||||
// &self,
|
||||
// _req: Request<impl Stream<Item = Result<RouteNote, Status>>>,
|
||||
// ) -> Result<Response<impl Stream<Item = Result<RouteNote, Status>>>, Status> {
|
||||
// unimplemented!()
|
||||
// }
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let addr = "[::1]:50051".parse().unwrap();
|
||||
let mut bind = TcpListener::bind(&addr)?;
|
||||
|
||||
let route_guide = RouteGuide::default();
|
||||
let mut server = Server::new(RouteGuideServer::new(route_guide), Default::default());
|
||||
|
||||
while let Ok((sock, _addr)) = bind.accept().await {
|
||||
if let Err(e) = sock.set_nodelay(true) {
|
||||
return Err(e.into());
|
||||
}
|
||||
|
||||
if let Err(e) = server.serve(sock).await {
|
||||
println!("H2 ERROR: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user