use of io.grpc.examples.routeguide.RouteNote in project motan by weibocom.
the class GrpcServerDemo method routeChat.
/**
* Receives a stream of message/location pairs, and responds with a stream of all previous
* messages at each of those locations.
*
* @param responseObserver an observer to receive the stream of previous messages.
* @return an observer to handle requested message/location pairs.
*/
public StreamObserver<RouteNote> routeChat(final StreamObserver<RouteNote> responseObserver) {
return new StreamObserver<RouteNote>() {
public void onNext(RouteNote note) {
List<RouteNote> notes = getOrCreateNotes(note.getLocation());
// Respond with all previous notes at this location.
for (RouteNote prevNote : notes.toArray(new RouteNote[0])) {
responseObserver.onNext(prevNote);
}
// Now add the new note to the list
notes.add(note);
}
public void onError(Throwable t) {
}
public void onCompleted() {
responseObserver.onCompleted();
}
};
}
use of io.grpc.examples.routeguide.RouteNote in project motan by weibocom.
the class GrpcClientDemo method main.
public static void main(String[] args) throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] { "classpath:motan_demo_client_grpc.xml" });
GrpcService service = (GrpcService) ctx.getBean("motanDemoReferer");
// unary
for (int i = 0; i < 2; i++) {
Point request = Point.newBuilder().setLatitude(100 + i).setLongitude(150 + i).build();
System.out.println(service.getFeature(request));
Thread.sleep(1000);
}
// server streaming
Rectangle request = Rectangle.newBuilder().setLo(Point.newBuilder().setLatitude(400000000).setLongitude(-750000000).build()).setHi(Point.newBuilder().setLatitude(420000000).setLongitude(-730000000).build()).build();
StreamObserver<Feature> responseObserver = new StreamObserver<Feature>() {
@Override
public void onNext(Feature value) {
System.out.println(value);
}
@Override
public void onError(Throwable t) {
// TODO Auto-generated method stub
}
@Override
public void onCompleted() {
System.out.println("response complete!");
}
};
service.listFeatures(request, responseObserver);
Thread.sleep(2000);
// client streaming
StreamObserver<RouteSummary> routeSummaryObserver = new StreamObserver<RouteSummary>() {
@Override
public void onNext(RouteSummary value) {
System.out.println(value);
}
@Override
public void onError(Throwable t) {
// TODO Auto-generated method stub
}
@Override
public void onCompleted() {
System.out.println("response complete!");
}
};
StreamObserver<Point> requestObserver = service.recordRoute(routeSummaryObserver);
Random random = new Random();
for (int i = 0; i < 5; i++) {
Point point = Point.newBuilder().setLatitude(random.nextInt()).setLongitude(random.nextInt()).build();
requestObserver.onNext(point);
Thread.sleep(200);
}
requestObserver.onCompleted();
Thread.sleep(2000);
// biderict-streaming
StreamObserver<RouteNote> biRequestObserver = service.routeChat(new StreamObserver<RouteNote>() {
public void onNext(RouteNote value) {
System.out.println(value);
}
public void onError(Throwable t) {
t.printStackTrace();
}
public void onCompleted() {
System.out.println("routenote complete");
}
});
try {
RouteNote[] requests = { newNote("First message", 0, 0), newNote("Second message", 0, 1), newNote("Third message", 1, 0), newNote("Fourth message", 1, 1) };
for (RouteNote note : requests) {
biRequestObserver.onNext(note);
}
} catch (RuntimeException e) {
biRequestObserver.onError(e);
throw e;
}
biRequestObserver.onCompleted();
Thread.sleep(2000);
System.out.println("motan demo is finish.");
System.exit(0);
}
use of io.grpc.examples.routeguide.RouteNote in project vertx-examples by vert-x3.
the class Client method routeChat.
/**
* Bi-directional example, which can only be asynchronous. Send some chat messages, and print any
* chat messages that are sent from the server.
*/
public void routeChat() {
System.out.println("*** RouteChat");
stub.routeChat(exchange -> {
exchange.handler(note -> {
System.out.println("Got message \"" + note.getMessage() + "\" at " + note.getLocation().getLatitude() + ", " + note.getLocation().getLongitude());
});
exchange.exceptionHandler(err -> {
System.out.println("RouteChat Failed: " + Status.fromThrowable(err));
});
exchange.endHandler(v -> {
System.out.println("Finished RouteChat");
});
RouteNote[] requests = { newNote("First message", 0, 0), newNote("Second message", 0, 1), newNote("Third message", 1, 0), newNote("Fourth message", 1, 1) };
for (RouteNote request : requests) {
System.out.println("Sending message \"" + request.getMessage() + "\" at " + request.getLocation().getLatitude() + ", " + request.getLocation().getLongitude());
exchange.write(request);
}
exchange.end();
});
}
use of io.grpc.examples.routeguide.RouteNote in project vertx-examples by vert-x3.
the class Server method start.
@Override
public void start() throws Exception {
URL featureFile = Util.getDefaultFeaturesFile();
features = Util.parseFeatures(featureFile);
VertxServer server = VertxServerBuilder.forAddress(vertx, "localhost", 8080).addService(new RouteGuideGrpc.RouteGuideVertxImplBase() {
@Override
public void getFeature(Point request, Promise<Feature> response) {
response.complete(checkFeature(request));
}
@Override
public void listFeatures(Rectangle request, GrpcWriteStream<Feature> response) {
int left = Math.min(request.getLo().getLongitude(), request.getHi().getLongitude());
int right = Math.max(request.getLo().getLongitude(), request.getHi().getLongitude());
int top = Math.max(request.getLo().getLatitude(), request.getHi().getLatitude());
int bottom = Math.min(request.getLo().getLatitude(), request.getHi().getLatitude());
for (Feature feature : features) {
if (!Util.exists(feature)) {
continue;
}
int lat = feature.getLocation().getLatitude();
int lon = feature.getLocation().getLongitude();
if (lon >= left && lon <= right && lat >= bottom && lat <= top) {
response.write(feature);
}
}
response.end();
}
@Override
public void recordRoute(GrpcReadStream<Point> request, Future<RouteSummary> response) {
request.exceptionHandler(err -> {
System.out.println("recordRoute cancelled");
});
RouteRecorder recorder = new RouteRecorder();
request.handler(recorder::append);
request.endHandler(v -> {
response.complete(recorder.build());
});
}
@Override
public void routeChat(GrpcBidiExchange<RouteNote, RouteNote> exchange) {
exchange.handler(note -> {
List<RouteNote> notes = getOrCreateNotes(note.getLocation());
// Respond with all previous notes at this location.
for (RouteNote prevNote : notes.toArray(new RouteNote[0])) {
exchange.write(prevNote);
}
// Now add the new note to the list
notes.add(note);
});
exchange.exceptionHandler(err -> {
System.out.println("routeChat cancelled");
});
exchange.endHandler(v -> exchange.end());
}
}).build();
server.start(ar -> {
if (ar.succeeded()) {
System.out.println("gRPC service started");
} else {
System.out.println("Could not start server " + ar.cause().getMessage());
}
});
}
Aggregations