@java.lang.Override @java.lang.SuppressWarnings("unchecked") public io.grpc.stub.StreamObserver<Req> invoke( io.grpc.stub.StreamObserver<Resp> responseObserver) { switch (methodId) { case METHODID_FULL_DUPLEX_CALL: io.vertx.grpc.GrpcReadStream<io.vertx.example.grpc.Messages.StreamingOutputCallRequest> request0 = io.vertx.grpc.GrpcReadStream.<io.vertx.example.grpc.Messages.StreamingOutputCallRequest>create(); serviceImpl.fullDuplexCall( io.vertx.grpc.GrpcBidiExchange.<io.vertx.example.grpc.Messages.StreamingOutputCallRequest, io.vertx.example.grpc.Messages.StreamingOutputCallResponse>create( request0, (io.grpc.stub.StreamObserver<io.vertx.example.grpc.Messages.StreamingOutputCallResponse>) responseObserver)); return (io.grpc.stub.StreamObserver<Req>) request0.readObserver(); default: throw new AssertionError(); } } }
/** * <pre> * A sequence of requests with each request served by the server immediately. * As one request could lead to multiple responses, this interface * demonstrates the idea of full duplexing. * </pre> */ public void fullDuplexCall( io.vertx.grpc.GrpcBidiExchange<io.vertx.example.grpc.Messages.StreamingOutputCallRequest, io.vertx.example.grpc.Messages.StreamingOutputCallResponse> exchange) { exchange.setReadObserver(asyncUnimplementedStreamingCall(getFullDuplexCallMethod(), exchange.writeObserver())); }
@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();
@Override public void fullDuplexCall(GrpcBidiExchange<Messages.StreamingOutputCallRequest, Messages.StreamingOutputCallResponse> exchange) { exchange .handler(req -> { System.out.println("Server: received request"); vertx.setTimer(500L, t -> { exchange.write(Messages.StreamingOutputCallResponse.newBuilder().build()); }); }); } };
@Override public void start() throws Exception { // Create the channel ManagedChannel channel = VertxChannelBuilder .forAddress(vertx, "localhost", 8080) .usePlaintext(true) .build(); // Get a stub to use for interacting with the remote service ConversationalServiceGrpc.ConversationalServiceVertxStub stub = ConversationalServiceGrpc.newVertxStub(channel); // Make a request Messages.StreamingOutputCallRequest request = Messages.StreamingOutputCallRequest.newBuilder().build(); // Call the remote service stub.fullDuplexCall(exchange -> { exchange .handler(req -> { System.out.println("Client: received response"); vertx.setTimer(500L, t -> { exchange.write(Messages.StreamingOutputCallRequest.newBuilder().build()); }); }); // start the conversation exchange.write(Messages.StreamingOutputCallRequest.newBuilder().build()); }); } }
/** * 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(); }); }
/** * <pre> * 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). * </pre> */ public void routeChat( io.vertx.grpc.GrpcBidiExchange<io.grpc.examples.routeguide.RouteNote, io.grpc.examples.routeguide.RouteNote> exchange) { exchange.setReadObserver(asyncUnimplementedStreamingCall(getRouteChatMethod(), exchange.writeObserver())); }
/** * <pre> * A sequence of requests with each request served by the server immediately. * As one request could lead to multiple responses, this interface * demonstrates the idea of full duplexing. * </pre> */ public void fullDuplexCall(io.vertx.core.Handler< io.vertx.grpc.GrpcBidiExchange<io.vertx.example.grpc.Messages.StreamingOutputCallResponse, io.vertx.example.grpc.Messages.StreamingOutputCallRequest>> handler) { final io.vertx.grpc.GrpcReadStream<io.vertx.example.grpc.Messages.StreamingOutputCallResponse> readStream = io.vertx.grpc.GrpcReadStream.<io.vertx.example.grpc.Messages.StreamingOutputCallResponse>create(); handler.handle(io.vertx.grpc.GrpcBidiExchange.create(readStream, asyncBidiStreamingCall( getChannel().newCall(getFullDuplexCallMethod(), getCallOptions()), readStream.readObserver()))); } }
@Override public void fullDuplexCall(GrpcBidiExchange<StreamingOutputCallRequest, StreamingOutputCallResponse> exchange) { exchange .exceptionHandler(will::fail) .handler(item -> { will.assertNotNull(item); cnt.incrementAndGet(); exchange.write(StreamingOutputCallResponse.newBuilder().build()); }) .endHandler(v -> { will.assertEquals(10, cnt.get()); exchange.end(); }); } }, startServer -> {
/** * <pre> * Server -> Client * </pre> */ public void outputCall( io.vertx.grpc.GrpcBidiExchange<io.vertx.tp.ipc.eon.StreamServerRequest, io.vertx.tp.ipc.eon.StreamServerResponse> exchange) { exchange.setReadObserver(asyncUnimplementedStreamingCall(METHOD_OUTPUT_CALL, exchange.writeObserver())); }
/** * <pre> * 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). * </pre> */ public void routeChat(io.vertx.core.Handler< io.vertx.grpc.GrpcBidiExchange<io.grpc.examples.routeguide.RouteNote, io.grpc.examples.routeguide.RouteNote>> handler) { final io.vertx.grpc.GrpcReadStream<io.grpc.examples.routeguide.RouteNote> readStream = io.vertx.grpc.GrpcReadStream.<io.grpc.examples.routeguide.RouteNote>create(); handler.handle(io.vertx.grpc.GrpcBidiExchange.create(readStream, asyncBidiStreamingCall( getChannel().newCall(getRouteChatMethod(), getCallOptions()), readStream.readObserver()))); } }
@Override public void halfDuplexCall(GrpcBidiExchange<StreamingOutputCallRequest, StreamingOutputCallResponse> exchange) { List<StreamingOutputCallRequest> buffer = new ArrayList<>(); exchange .exceptionHandler(will::fail) .handler(item -> { will.assertNotNull(item); cnt.incrementAndGet(); buffer.add(item); }) .endHandler(v -> { will.assertEquals(10, cnt.get()); for (int i = 0; i < buffer.size(); i++) { exchange.write(StreamingOutputCallResponse.newBuilder().build()); } exchange.end(); }); } }, startServer -> {
/** * <pre> * Full: Client -> Server -> Client * </pre> */ public void dupliexCall( io.vertx.grpc.GrpcBidiExchange<io.vertx.tp.ipc.eon.StreamClientRequest, io.vertx.tp.ipc.eon.StreamServerResponse> exchange) { exchange.setReadObserver(asyncUnimplementedStreamingCall(METHOD_DUPLIEX_CALL, exchange.writeObserver())); }
@java.lang.Override @java.lang.SuppressWarnings("unchecked") public io.grpc.stub.StreamObserver<Req> invoke( io.grpc.stub.StreamObserver<Resp> responseObserver) { switch (methodId) { case METHODID_RECORD_ROUTE: io.vertx.grpc.GrpcReadStream<io.grpc.examples.routeguide.Point> request2 = io.vertx.grpc.GrpcReadStream.<io.grpc.examples.routeguide.Point>create(); serviceImpl.recordRoute(request2, (io.vertx.core.Future<io.grpc.examples.routeguide.RouteSummary>) io.vertx.core.Future.<io.grpc.examples.routeguide.RouteSummary>future().setHandler(ar -> { if (ar.succeeded()) { ((io.grpc.stub.StreamObserver<io.grpc.examples.routeguide.RouteSummary>) responseObserver).onNext(ar.result()); responseObserver.onCompleted(); } else { responseObserver.onError(ar.cause()); } })); return (io.grpc.stub.StreamObserver<Req>) request2.readObserver(); case METHODID_ROUTE_CHAT: io.vertx.grpc.GrpcReadStream<io.grpc.examples.routeguide.RouteNote> request3 = io.vertx.grpc.GrpcReadStream.<io.grpc.examples.routeguide.RouteNote>create(); serviceImpl.routeChat( io.vertx.grpc.GrpcBidiExchange.<io.grpc.examples.routeguide.RouteNote, io.grpc.examples.routeguide.RouteNote>create( request3, (io.grpc.stub.StreamObserver<io.grpc.examples.routeguide.RouteNote>) responseObserver)); return (io.grpc.stub.StreamObserver<Req>) request3.readObserver(); default: throw new AssertionError(); } } }
.exceptionHandler(will::fail) .handler(item -> { will.assertTrue(down.get()); will.assertNotNull(item); cnt.incrementAndGet(); }) .endHandler(v -> { will.assertEquals(10, cnt.get()); test.complete(); exchange.write(StreamingOutputCallRequest.newBuilder().build()); exchange.end();
/** * <pre> * Client -> Server * </pre> */ public void inputCall( io.vertx.grpc.GrpcBidiExchange<io.vertx.tp.ipc.eon.StreamClientRequest, io.vertx.tp.ipc.eon.StreamClientResponse> exchange) { exchange.setReadObserver(asyncUnimplementedStreamingCall(METHOD_INPUT_CALL, exchange.writeObserver())); }
@java.lang.Override @java.lang.SuppressWarnings("unchecked") public io.grpc.stub.StreamObserver<Req> invoke( io.grpc.stub.StreamObserver<Resp> responseObserver) { switch (methodId) { case METHODID_OUTPUT_CALL: io.vertx.grpc.GrpcReadStream<io.vertx.tp.ipc.eon.StreamServerRequest> request0 = io.vertx.grpc.GrpcReadStream.<io.vertx.tp.ipc.eon.StreamServerRequest>create(); serviceImpl.outputCall( io.vertx.grpc.GrpcBidiExchange.<io.vertx.tp.ipc.eon.StreamServerRequest, io.vertx.tp.ipc.eon.StreamServerResponse>create( request0, (io.grpc.stub.StreamObserver<io.vertx.tp.ipc.eon.StreamServerResponse>) responseObserver)); return (io.grpc.stub.StreamObserver<Req>) request0.readObserver(); default: throw new AssertionError(); } } }
buildStub().fullDuplexCall(exchange -> { exchange .exceptionHandler(will::fail) .handler(item -> { will.assertNotNull(item); cnt.incrementAndGet(); }) .endHandler(v -> { will.assertEquals(10, cnt.get()); test.complete(); exchange.write(StreamingOutputCallRequest.newBuilder().build()); exchange.end(); }); } else {
/** * <pre> * A sequence of requests with each request served by the server immediately. * As one request could lead to multiple responses, this interface * demonstrates the idea of full duplexing. * </pre> */ public void fullDuplexCall( io.vertx.grpc.GrpcBidiExchange<io.grpc.testing.integration.Messages.StreamingOutputCallRequest, io.grpc.testing.integration.Messages.StreamingOutputCallResponse> exchange) { exchange.setReadObserver(asyncUnimplementedStreamingCall(getFullDuplexCallMethod(), exchange.writeObserver())); }
@java.lang.Override @java.lang.SuppressWarnings("unchecked") public io.grpc.stub.StreamObserver<Req> invoke( io.grpc.stub.StreamObserver<Resp> responseObserver) { switch (methodId) { case METHODID_INPUT_CALL: io.vertx.grpc.GrpcReadStream<io.vertx.tp.ipc.eon.StreamClientRequest> request0 = io.vertx.grpc.GrpcReadStream.<io.vertx.tp.ipc.eon.StreamClientRequest>create(); serviceImpl.inputCall( io.vertx.grpc.GrpcBidiExchange.<io.vertx.tp.ipc.eon.StreamClientRequest, io.vertx.tp.ipc.eon.StreamClientResponse>create( request0, (io.grpc.stub.StreamObserver<io.vertx.tp.ipc.eon.StreamClientResponse>) responseObserver)); return (io.grpc.stub.StreamObserver<Req>) request0.readObserver(); default: throw new AssertionError(); } } }