Search in sources :

Example 1 with Point

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

the class Client method getFeature.

/**
 * Blocking unary call example.  Calls getFeature and prints the response.
 */
public void getFeature(int lat, int lon) {
    System.out.println("*** GetFeature: lat=" + lat + " lon=" + lon);
    Point request = Point.newBuilder().setLatitude(lat).setLongitude(lon).build();
    stub.getFeature(request, ar -> {
        if (ar.succeeded()) {
            Feature feature = ar.result();
            if (Util.exists(feature)) {
                System.out.println("Found feature called " + feature.getName() + " at " + Util.getLatitude(feature.getLocation()) + ", " + Util.getLongitude(feature.getLocation()));
            } else {
                System.out.println("Found no feature at " + Util.getLatitude(feature.getLocation()) + ", " + Util.getLongitude(feature.getLocation()));
            }
        }
    });
}
Also used : Point(io.grpc.examples.routeguide.Point) Feature(io.grpc.examples.routeguide.Feature)

Example 2 with Point

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

the class Server method calcDistance.

/**
 * Calculate the distance between two points using the "haversine" formula.
 * This code was taken from http://www.movable-type.co.uk/scripts/latlong.html.
 *
 * @param start The starting point
 * @param end The end point
 * @return The distance between the points in meters
 */
private static int calcDistance(Point start, Point end) {
    double lat1 = Util.getLatitude(start);
    double lat2 = Util.getLatitude(end);
    double lon1 = Util.getLongitude(start);
    double lon2 = Util.getLongitude(end);
    // meters
    int r = 6371000;
    double phi1 = Math.toRadians(lat1);
    double phi2 = Math.toRadians(lat2);
    double deltaPhi = Math.toRadians(lat2 - lat1);
    double deltaLambda = Math.toRadians(lon2 - lon1);
    double a = Math.sin(deltaPhi / 2) * Math.sin(deltaPhi / 2) + Math.cos(phi1) * Math.cos(phi2) * Math.sin(deltaLambda / 2) * Math.sin(deltaLambda / 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    return (int) (r * c);
}
Also used : Point(io.grpc.examples.routeguide.Point)

Example 3 with Point

use of io.grpc.examples.routeguide.Point 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, Future<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());
        }
    });
}
Also used : GrpcBidiExchange(io.vertx.grpc.GrpcBidiExchange) VertxServer(io.vertx.grpc.VertxServer) Rectangle(io.grpc.examples.routeguide.Rectangle) Point(io.grpc.examples.routeguide.Point) Feature(io.grpc.examples.routeguide.Feature) URL(java.net.URL) Point(io.grpc.examples.routeguide.Point) GrpcReadStream(io.vertx.grpc.GrpcReadStream) RouteNote(io.grpc.examples.routeguide.RouteNote) Future(io.vertx.core.Future) GrpcWriteStream(io.vertx.grpc.GrpcWriteStream)

Example 4 with Point

use of io.grpc.examples.routeguide.Point 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)

Example 5 with Point

use of io.grpc.examples.routeguide.Point in project motan by weibocom.

the class GrpcServerDemo method calcDistance.

/**
 * Calculate the distance between two points using the "haversine" formula.
 * This code was taken from http://www.movable-type.co.uk/scripts/latlong.html.
 *
 * @param start The starting point
 * @param end The end point
 * @return The distance between the points in meters
 */
private static double calcDistance(Point start, Point end) {
    double lat1 = RouteGuideUtil.getLatitude(start);
    double lat2 = RouteGuideUtil.getLatitude(end);
    double lon1 = RouteGuideUtil.getLongitude(start);
    double lon2 = RouteGuideUtil.getLongitude(end);
    // metres
    int r = 6371000;
    double φ1 = toRadians(lat1);
    double φ2 = toRadians(lat2);
    double Δφ = toRadians(lat2 - lat1);
    double Δλ = toRadians(lon2 - lon1);
    double a = sin(Δφ / 2) * sin(Δφ / 2) + cos(φ1) * cos(φ2) * sin(Δλ / 2) * sin(Δλ / 2);
    double c = 2 * atan2(sqrt(a), sqrt(1 - a));
    return r * c;
}
Also used : Point(io.grpc.examples.routeguide.Point)

Aggregations

Point (io.grpc.examples.routeguide.Point)5 Feature (io.grpc.examples.routeguide.Feature)3 Rectangle (io.grpc.examples.routeguide.Rectangle)2 RouteNote (io.grpc.examples.routeguide.RouteNote)2 GrpcService (com.weibo.motan.demo.service.GrpcService)1 RouteSummary (io.grpc.examples.routeguide.RouteSummary)1 StreamObserver (io.grpc.stub.StreamObserver)1 Future (io.vertx.core.Future)1 GrpcBidiExchange (io.vertx.grpc.GrpcBidiExchange)1 GrpcReadStream (io.vertx.grpc.GrpcReadStream)1 GrpcWriteStream (io.vertx.grpc.GrpcWriteStream)1 VertxServer (io.vertx.grpc.VertxServer)1 URL (java.net.URL)1 Random (java.util.Random)1 ApplicationContext (org.springframework.context.ApplicationContext)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1