Search in sources :

Example 1 with GrpcWriteStream

use of io.vertx.grpc.GrpcWriteStream 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());
        }
    });
}
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) Promise(io.vertx.core.Promise) GrpcReadStream(io.vertx.grpc.GrpcReadStream) RouteNote(io.grpc.examples.routeguide.RouteNote) Future(io.vertx.core.Future) GrpcWriteStream(io.vertx.grpc.GrpcWriteStream)

Example 2 with GrpcWriteStream

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

the class Server method start.

@Override
public void start() throws Exception {
    // The rcp service
    ConsumerServiceGrpc.ConsumerServiceVertxImplBase service = new ConsumerServiceGrpc.ConsumerServiceVertxImplBase() {

        @Override
        public void streamingOutputCall(Messages.StreamingOutputCallRequest request, GrpcWriteStream<Messages.StreamingOutputCallResponse> response) {
            final AtomicInteger counter = new AtomicInteger();
            vertx.setPeriodic(1000L, t -> {
                response.write(Messages.StreamingOutputCallResponse.newBuilder().setPayload(Messages.Payload.newBuilder().setTypeValue(PayloadType.COMPRESSABLE.getNumber()).setBody(ByteString.copyFrom(String.valueOf(counter.incrementAndGet()), Charset.forName("UTF-8")))).build());
            });
        }
    };
    // Create the server
    VertxServer rpcServer = VertxServerBuilder.forPort(vertx, 8080).addService(service).build();
    // start the server
    rpcServer.start(ar -> {
        if (ar.failed()) {
            ar.cause().printStackTrace();
        }
    });
}
Also used : Messages(io.vertx.example.grpc.Messages) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) VertxServer(io.vertx.grpc.VertxServer) GrpcWriteStream(io.vertx.grpc.GrpcWriteStream) ConsumerServiceGrpc(io.vertx.example.grpc.ConsumerServiceGrpc)

Aggregations

GrpcWriteStream (io.vertx.grpc.GrpcWriteStream)2 VertxServer (io.vertx.grpc.VertxServer)2 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 Future (io.vertx.core.Future)1 Promise (io.vertx.core.Promise)1 ConsumerServiceGrpc (io.vertx.example.grpc.ConsumerServiceGrpc)1 Messages (io.vertx.example.grpc.Messages)1 GrpcBidiExchange (io.vertx.grpc.GrpcBidiExchange)1 GrpcReadStream (io.vertx.grpc.GrpcReadStream)1 URL (java.net.URL)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1