Search in sources :

Example 1 with RouteSummary

use of io.grpc.examples.routeguide.RouteSummary in project vertx-examples by vert-x3.

the class Client method recordRoute.

/**
 * Async client-streaming example. Sends {@code numPoints} randomly chosen points from {@code
 * features} with a variable delay in between. Prints the statistics when they are sent from the
 * server.
 */
public void recordRoute(List<Feature> features, int numPoints) throws InterruptedException {
    System.out.println("*** RecordRoute");
    stub.recordRoute(exchange -> {
        RouteSender sender = new RouteSender(features, exchange);
        exchange.handler(ar -> {
            sender.result = ar;
            if (ar.succeeded()) {
                RouteSummary summary = ar.result();
                System.out.println("Finished trip with " + summary.getPointCount() + " points. Passed " + summary.getFeatureCount() + " features.Travelled " + summary.getDistance() + " meters. It took " + summary.getElapsedTime() + " seconds.");
                System.out.println("Finished RecordRoute");
            } else {
                System.out.println("RecordRoute Failed: " + Status.fromThrowable(ar.cause()));
            }
        });
        // Send numPoints points randomly selected from the features list.
        sender.send(numPoints);
    });
}
Also used : RouteSummary(io.grpc.examples.routeguide.RouteSummary)

Example 2 with RouteSummary

use of io.grpc.examples.routeguide.RouteSummary 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);
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) RouteSummary(io.grpc.examples.routeguide.RouteSummary) Rectangle(io.grpc.examples.routeguide.Rectangle) Point(io.grpc.examples.routeguide.Point) Feature(io.grpc.examples.routeguide.Feature) Point(io.grpc.examples.routeguide.Point) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) GrpcService(com.weibo.motan.demo.service.GrpcService) Random(java.util.Random) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) RouteNote(io.grpc.examples.routeguide.RouteNote)

Aggregations

RouteSummary (io.grpc.examples.routeguide.RouteSummary)2 GrpcService (com.weibo.motan.demo.service.GrpcService)1 Feature (io.grpc.examples.routeguide.Feature)1 Point (io.grpc.examples.routeguide.Point)1 Rectangle (io.grpc.examples.routeguide.Rectangle)1 RouteNote (io.grpc.examples.routeguide.RouteNote)1 StreamObserver (io.grpc.stub.StreamObserver)1 Random (java.util.Random)1 ApplicationContext (org.springframework.context.ApplicationContext)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1