@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 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 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(); }); }
@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 -> {
@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 -> {
exchange.write(StreamingOutputCallRequest.newBuilder().build());
exchange.write(StreamingOutputCallRequest.newBuilder().build());
int val = count.decrementAndGet(); if (val >= 0) { exchange.write(Item.newBuilder().setValue("the-value-" + (numItems - val - 1)).build()); } else { vertx.cancelTimer(id);