Search in sources :

Example 21 with HelloRequest

use of io.grpc.examples.helloworld.HelloRequest in project grpc-java by grpc.

the class AuthClientTest method setUp.

@Before
public void setUp() throws IOException {
    // Generate a unique in-process server name.
    String serverName = InProcessServerBuilder.generateName();
    // Create a server, add service, start, and register for automatic graceful shutdown.
    grpcCleanup.register(InProcessServerBuilder.forName(serverName).directExecutor().addService(ServerInterceptors.intercept(new GreeterGrpc.GreeterImplBase() {

        @Override
        public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
            HelloReply reply = HelloReply.newBuilder().setMessage("AuthClientTest user=" + request.getName()).build();
            responseObserver.onNext(reply);
            responseObserver.onCompleted();
        }
    }, mockServerInterceptor)).build().start());
    CallCredentials credentials = new JwtCredential("test-client");
    ManagedChannel channel = InProcessChannelBuilder.forName(serverName).directExecutor().build();
    client = new AuthClient(credentials, channel);
}
Also used : HelloRequest(io.grpc.examples.helloworld.HelloRequest) GreeterGrpc(io.grpc.examples.helloworld.GreeterGrpc) ManagedChannel(io.grpc.ManagedChannel) HelloReply(io.grpc.examples.helloworld.HelloReply) CallCredentials(io.grpc.CallCredentials) Before(org.junit.Before)

Example 22 with HelloRequest

use of io.grpc.examples.helloworld.HelloRequest in project grpc-java by grpc.

the class AuthClient method greet.

/**
 * Say hello to server.
 *
 * @param name name to set in HelloRequest
 * @return the message in the HelloReply from the server
 */
public String greet(String name) {
    logger.info("Will try to greet " + name + " ...");
    HelloRequest request = HelloRequest.newBuilder().setName(name).build();
    // Use a stub with the given call credentials applied to invoke the RPC.
    HelloReply response = blockingStub.withCallCredentials(callCredentials).sayHello(request);
    logger.info("Greeting: " + response.getMessage());
    return response.getMessage();
}
Also used : HelloRequest(io.grpc.examples.helloworld.HelloRequest) HelloReply(io.grpc.examples.helloworld.HelloReply)

Example 23 with HelloRequest

use of io.grpc.examples.helloworld.HelloRequest in project grpc-java by grpc.

the class HelloJsonClient method greet.

/**
 * Say hello to server.
 */
public void greet(String name) {
    logger.info("Will try to greet " + name + " ...");
    HelloRequest request = HelloRequest.newBuilder().setName(name).build();
    HelloReply response;
    try {
        response = blockingStub.sayHello(request);
    } catch (StatusRuntimeException e) {
        logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
        return;
    }
    logger.info("Greeting: " + response.getMessage());
}
Also used : HelloRequest(io.grpc.examples.helloworld.HelloRequest) StatusRuntimeException(io.grpc.StatusRuntimeException) HelloReply(io.grpc.examples.helloworld.HelloReply)

Example 24 with HelloRequest

use of io.grpc.examples.helloworld.HelloRequest in project grpc-java by grpc.

the class ErrorHandlingClient method advancedAsyncCall.

/**
 * This is more advanced and does not make use of the stub.  You should not normally need to do
 * this, but here is how you would.
 */
void advancedAsyncCall() {
    ClientCall<HelloRequest, HelloReply> call = channel.newCall(GreeterGrpc.getSayHelloMethod(), CallOptions.DEFAULT);
    final CountDownLatch latch = new CountDownLatch(1);
    call.start(new ClientCall.Listener<HelloReply>() {

        @Override
        public void onClose(Status status, Metadata trailers) {
            Verify.verify(status.getCode() == Status.Code.INTERNAL);
            Verify.verify(status.getDescription().contains("Narwhal"));
            // Cause is not transmitted over the wire.
            latch.countDown();
        }
    }, new Metadata());
    call.sendMessage(HelloRequest.newBuilder().setName("Marge").build());
    call.halfClose();
    if (!Uninterruptibles.awaitUninterruptibly(latch, 1, TimeUnit.SECONDS)) {
        throw new RuntimeException("timeout!");
    }
}
Also used : Status(io.grpc.Status) ClientCall(io.grpc.ClientCall) HelloRequest(io.grpc.examples.helloworld.HelloRequest) Metadata(io.grpc.Metadata) HelloReply(io.grpc.examples.helloworld.HelloReply) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 25 with HelloRequest

use of io.grpc.examples.helloworld.HelloRequest in project grpc-java by grpc.

the class ErrorHandlingClient method asyncCall.

void asyncCall() {
    GreeterStub stub = GreeterGrpc.newStub(channel);
    HelloRequest request = HelloRequest.newBuilder().setName("Homer").build();
    final CountDownLatch latch = new CountDownLatch(1);
    StreamObserver<HelloReply> responseObserver = new StreamObserver<HelloReply>() {

        @Override
        public void onNext(HelloReply value) {
        // Won't be called.
        }

        @Override
        public void onError(Throwable t) {
            Status status = Status.fromThrowable(t);
            Verify.verify(status.getCode() == Status.Code.INTERNAL);
            Verify.verify(status.getDescription().contains("Overbite"));
            // Cause is not transmitted over the wire..
            latch.countDown();
        }

        @Override
        public void onCompleted() {
        // Won't be called, since the server in this example always fails.
        }
    };
    stub.sayHello(request, responseObserver);
    if (!Uninterruptibles.awaitUninterruptibly(latch, 1, TimeUnit.SECONDS)) {
        throw new RuntimeException("timeout!");
    }
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) Status(io.grpc.Status) GreeterStub(io.grpc.examples.helloworld.GreeterGrpc.GreeterStub) HelloRequest(io.grpc.examples.helloworld.HelloRequest) CountDownLatch(java.util.concurrent.CountDownLatch) HelloReply(io.grpc.examples.helloworld.HelloReply)

Aggregations

HelloRequest (io.grpc.examples.helloworld.HelloRequest)27 HelloReply (io.grpc.examples.helloworld.HelloReply)20 StatusRuntimeException (io.grpc.StatusRuntimeException)8 GreeterGrpc (io.grpc.examples.helloworld.GreeterGrpc)8 ManagedChannel (io.grpc.ManagedChannel)6 Metadata (io.grpc.Metadata)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Status (io.grpc.Status)3 StreamObserver (io.grpc.stub.StreamObserver)3 AbstractVerticle (io.vertx.core.AbstractVerticle)3 Future (io.vertx.core.Future)3 JksOptions (io.vertx.core.net.JksOptions)3 VertxChannelBuilder (io.vertx.grpc.VertxChannelBuilder)3 VertxServer (io.vertx.grpc.VertxServer)3 Test (org.junit.Test)3 ClientCall (io.grpc.ClientCall)2 Server (io.grpc.Server)2 GreeterImplBase (io.grpc.examples.helloworld.GreeterGrpc.GreeterImplBase)2 GreeterStub (io.grpc.examples.helloworld.GreeterGrpc.GreeterStub)2 HttpVersion (io.vertx.core.http.HttpVersion)2