Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ protected GrpcClientImpl(Vertx vertx, GrpcClientOptions grpcOptions, HttpClient
this.closeClient = close;
}

public Vertx vertx() {
return vertx;
}

public Future<GrpcClientRequest<Buffer, Buffer>> request(RequestOptions options) {
return client.request(options)
.map(httpRequest -> {
Expand Down
20 changes: 10 additions & 10 deletions vertx-grpc-docs/src/main/java/examples/GrpcServerExamples.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public void messageLevelAPI(GrpcServer server) {
}

public void unaryStub1(GrpcServer server) {
GreeterService stub = new GreeterService() {
GreeterService service = new GreeterService() {
@Override
public Future<HelloReply> sayHello(HelloRequest request) {
return Future.succeededFuture(HelloReply.newBuilder().setMessage("Hello " + request.getName()).build());
Expand All @@ -265,24 +265,24 @@ public Future<HelloReply> sayHello(HelloRequest request) {
}

public void unaryStub2(GrpcServer server) {
GreeterService stub = new GreeterService() {
GreeterService service = new GreeterService() {
@Override
public void sayHello(HelloRequest request, Completable<HelloReply> response) {
response.succeed(HelloReply.newBuilder().setMessage("Hello " + request.getName()).build());
}
};
}

public void unaryStub3(GreeterGrpcService stub, GrpcServer server) {
server.addService(stub);
public void unaryStub3(GreeterGrpcService service, GrpcServer server) {
server.addService(service);
}

public void unaryStub4(GreeterGrpcService stub, GrpcServer server) {
server.addService(GreeterGrpcService.of(stub));
public void unaryStub4(GreeterService service, GrpcServer server) {
server.addService(GreeterGrpcService.of(service));
}

public void streamingRequestStub(GrpcServer server) {
StreamingGrpcService stub = new StreamingGrpcService() {
StreamingGrpcService service = new StreamingGrpcService() {
@Override
public void sink(ReadStream<Item> stream, Completable<Empty> response) {
stream.handler(item -> {
Expand All @@ -292,15 +292,15 @@ public void sink(ReadStream<Item> stream, Completable<Empty> response) {
stream.endHandler(v -> response.succeed(Empty.getDefaultInstance()));
}
};
server.addService(stub);
server.addService(service);
}

private Future<ReadStream<Item>> streamOfItems() {
throw new UnsupportedOperationException();
}

public void streamingResponseStub1() {
StreamingService stub = new StreamingService() {
StreamingService service = new StreamingService() {
@Override
public Future<ReadStream<Item>> source(Empty request) {
return streamOfItems();
Expand All @@ -309,7 +309,7 @@ public Future<ReadStream<Item>> source(Empty request) {
}

public void streamingResponseStub2() {
StreamingService stub = new StreamingService() {
StreamingService service = new StreamingService() {
@Override
public void source(Empty request, WriteStream<Item> response) {
response.write(Item.newBuilder().setValue("value-1").build());
Expand Down
114 changes: 114 additions & 0 deletions vertx-grpc-docs/src/main/java/examples/grpc/GreeterGrpcIo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package examples.grpc;

import static examples.grpc.GreeterGrpc.getServiceDescriptor;
import static io.grpc.stub.ServerCalls.asyncUnaryCall;
import static io.grpc.stub.ServerCalls.asyncServerStreamingCall;
import static io.grpc.stub.ServerCalls.asyncClientStreamingCall;
import static io.grpc.stub.ServerCalls.asyncBidiStreamingCall;

import io.grpc.ClientCall;

import io.grpc.stub.StreamObserver;

import io.vertx.grpcio.client.GrpcIoClientChannel;
import io.vertx.grpcio.client.impl.GrpcIoClientImpl;

public final class GreeterGrpcIo {
private GreeterGrpcIo() {}

public static GreeterStub newStub(io.vertx.grpcio.client.GrpcIoClient client, io.vertx.core.net.SocketAddress socketAddress) {
return newStub(new io.vertx.grpcio.client.GrpcIoClientChannel(client, socketAddress));
}

public static GreeterStub newStub(io.grpc.Channel channel) {
return new GreeterStub(channel);
}


public static final class GreeterStub extends io.grpc.stub.AbstractStub<GreeterStub> implements GreeterClient {
private final io.vertx.core.internal.ContextInternal context;
private GreeterGrpc.GreeterStub delegateStub;

private GreeterStub(io.grpc.Channel channel) {
super(channel);
this.delegateStub = GreeterGrpc.newStub(channel);
this.context = (io.vertx.core.internal.ContextInternal) ((GrpcIoClientImpl)((GrpcIoClientChannel)getChannel()).client()).vertx().getOrCreateContext();
}

private GreeterStub(io.grpc.Channel channel, io.grpc.CallOptions callOptions) {
super(channel, callOptions);
delegateStub = GreeterGrpc.newStub(channel).build(channel, callOptions);
this.context = (io.vertx.core.internal.ContextInternal) ((GrpcIoClientImpl)((GrpcIoClientChannel)getChannel()).client()).vertx().getOrCreateContext();
}

@Override
protected GreeterStub build(io.grpc.Channel channel, io.grpc.CallOptions callOptions) {
return new GreeterStub(channel, callOptions);
}


public io.vertx.core.Future<examples.grpc.HelloReply> sayHello(examples.grpc.HelloRequest request) {
return io.vertx.grpcio.common.impl.stub.ClientCalls.oneToOne(context, request, delegateStub::sayHello);
}

}

public static io.vertx.grpc.server.Service of(GreeterService service) {
String compression = null;
return io.vertx.grpcio.server.GrpcIoServiceBridge.bridge(io.grpc.ServerServiceDefinition.builder(getServiceDescriptor())
.addMethod(
examples.grpc.GreeterGrpc.getSayHelloMethod(),
asyncUnaryCall(
new MethodHandlers<
examples.grpc.HelloRequest,
examples.grpc.HelloReply>(
service, METHODID_SAY_HELLO, compression)))
.build());
}

private static final int METHODID_SAY_HELLO = 0;

private static final class MethodHandlers<Req, Resp> implements
io.grpc.stub.ServerCalls.UnaryMethod<Req, Resp>,
io.grpc.stub.ServerCalls.ServerStreamingMethod<Req, Resp>,
io.grpc.stub.ServerCalls.ClientStreamingMethod<Req, Resp>,
io.grpc.stub.ServerCalls.BidiStreamingMethod<Req, Resp> {

private final GreeterService serviceImpl;
private final int methodId;
private final String compression;

MethodHandlers(GreeterService serviceImpl, int methodId, String compression) {
this.serviceImpl = serviceImpl;
this.methodId = methodId;
this.compression = compression;
}

@java.lang.Override
@java.lang.SuppressWarnings("unchecked")
public void invoke(Req request, io.grpc.stub.StreamObserver<Resp> responseObserver) {
switch (methodId) {
case METHODID_SAY_HELLO:
io.vertx.grpcio.common.impl.stub.ServerCalls.<examples.grpc.HelloRequest, examples.grpc.HelloReply>oneToOne(
(io.vertx.core.internal.ContextInternal) io.vertx.core.Vertx.currentContext(),
(examples.grpc.HelloRequest) request,
(io.grpc.stub.StreamObserver<examples.grpc.HelloReply>) responseObserver,
compression,
serviceImpl::sayHello);
break;
default:
throw new java.lang.AssertionError();
}
}

@java.lang.Override
@java.lang.SuppressWarnings("unchecked")
public io.grpc.stub.StreamObserver<Req> invoke(io.grpc.stub.StreamObserver<Resp> responseObserver) {
StreamObserver<Req> reqStreamObserver;
switch (methodId) {
default:
throw new java.lang.AssertionError();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ public void bind(GrpcServer server) {
builder(this).bind(all()).build().bind(server);
}

/**
* @return a service binding all methods of the given {@code service}
*/
public static Service of(GreeterService service) {
return builder(service).bind(all()).build();
}

/**
* SayHello protobuf RPC server service method.
*/
Expand Down Expand Up @@ -115,13 +122,6 @@ public static Service of(GreeterService service) {
}
}

/**
* @return a service binding all methods of the given {@code service}
*/
public static Service of(GreeterService service) {
return builder(service).bind(all()).build();
}

/**
* @return a free form builder that gives the opportunity to bind only certain methods of a service
*/
Expand Down Expand Up @@ -219,13 +219,13 @@ private void handle_sayHello(io.vertx.grpc.server.GrpcServerRequest<examples.grp
if (err == null) {
request.response().end(res);
} else {
request.response().status(GrpcStatus.INTERNAL).end();
request.response().status(GrpcStatus.UNKNOWN).end();
}
});
} catch (UnsupportedOperationException err) {
request.response().status(GrpcStatus.UNIMPLEMENTED).end();
} catch (RuntimeException err) {
request.response().status(GrpcStatus.INTERNAL).end();
request.response().status(GrpcStatus.UNKNOWN).end();
}
});
}
Expand Down
114 changes: 114 additions & 0 deletions vertx-grpc-docs/src/main/java/examples/grpc/GreeterGrpcStub.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package examples.grpc;

import static examples.grpc.GreeterGrpc.getServiceDescriptor;
import static io.grpc.stub.ServerCalls.asyncUnaryCall;
import static io.grpc.stub.ServerCalls.asyncServerStreamingCall;
import static io.grpc.stub.ServerCalls.asyncClientStreamingCall;
import static io.grpc.stub.ServerCalls.asyncBidiStreamingCall;

import io.grpc.ClientCall;

import io.grpc.stub.StreamObserver;

import io.vertx.grpcio.client.GrpcIoClientChannel;
import io.vertx.grpcio.client.impl.GrpcIoClientImpl;

public final class GreeterGrpcStub {
private GreeterGrpcStub() {}

public static GreeterVertxStub newVertxStub(io.vertx.grpcio.client.GrpcIoClient client, io.vertx.core.net.SocketAddress socketAddress) {
return newVertxStub(new io.vertx.grpcio.client.GrpcIoClientChannel(client, socketAddress));
}

public static GreeterVertxStub newVertxStub(io.grpc.Channel channel) {
return new GreeterVertxStub(channel);
}


public static final class GreeterVertxStub extends io.grpc.stub.AbstractStub<GreeterVertxStub> implements GreeterClient {
private final io.vertx.core.internal.ContextInternal context;
private GreeterGrpc.GreeterStub delegateStub;

private GreeterVertxStub(io.grpc.Channel channel) {
super(channel);
delegateStub = GreeterGrpc.newStub(channel);
this.context = (io.vertx.core.internal.ContextInternal) ((GrpcIoClientImpl)((GrpcIoClientChannel)getChannel()).client()).vertx().getOrCreateContext();
}

private GreeterVertxStub(io.grpc.Channel channel, io.grpc.CallOptions callOptions) {
super(channel, callOptions);
delegateStub = GreeterGrpc.newStub(channel).build(channel, callOptions);
this.context = (io.vertx.core.internal.ContextInternal) ((GrpcIoClientImpl)((GrpcIoClientChannel)getChannel()).client()).vertx().getOrCreateContext();
}

@Override
protected GreeterVertxStub build(io.grpc.Channel channel, io.grpc.CallOptions callOptions) {
return new GreeterVertxStub(channel, callOptions);
}


public io.vertx.core.Future<examples.grpc.HelloReply> sayHello(examples.grpc.HelloRequest request) {
return io.vertx.grpcio.common.impl.stub.ClientCalls.oneToOne(context, request, delegateStub::sayHello);
}

}

public static io.vertx.grpc.server.Service of(GreeterService service) {
String compression = null;
return io.vertx.grpcio.server.GrpcIoServiceBridge.bridge(io.grpc.ServerServiceDefinition.builder(getServiceDescriptor())
.addMethod(
examples.grpc.GreeterGrpc.getSayHelloMethod(),
asyncUnaryCall(
new MethodHandlers<
examples.grpc.HelloRequest,
examples.grpc.HelloReply>(
service, METHODID_SAY_HELLO, compression)))
.build());
}

private static final int METHODID_SAY_HELLO = 0;

private static final class MethodHandlers<Req, Resp> implements
io.grpc.stub.ServerCalls.UnaryMethod<Req, Resp>,
io.grpc.stub.ServerCalls.ServerStreamingMethod<Req, Resp>,
io.grpc.stub.ServerCalls.ClientStreamingMethod<Req, Resp>,
io.grpc.stub.ServerCalls.BidiStreamingMethod<Req, Resp> {

private final GreeterService serviceImpl;
private final int methodId;
private final String compression;

MethodHandlers(GreeterService serviceImpl, int methodId, String compression) {
this.serviceImpl = serviceImpl;
this.methodId = methodId;
this.compression = compression;
}

@java.lang.Override
@java.lang.SuppressWarnings("unchecked")
public void invoke(Req request, io.grpc.stub.StreamObserver<Resp> responseObserver) {
switch (methodId) {
case METHODID_SAY_HELLO:
io.vertx.grpcio.common.impl.stub.ServerCalls.<examples.grpc.HelloRequest, examples.grpc.HelloReply>oneToOne(
(io.vertx.core.internal.ContextInternal) io.vertx.core.Vertx.currentContext(),
(examples.grpc.HelloRequest) request,
(io.grpc.stub.StreamObserver<examples.grpc.HelloReply>) responseObserver,
compression,
serviceImpl::sayHello);
break;
default:
throw new java.lang.AssertionError();
}
}

@java.lang.Override
@java.lang.SuppressWarnings("unchecked")
public io.grpc.stub.StreamObserver<Req> invoke(io.grpc.stub.StreamObserver<Resp> responseObserver) {
StreamObserver<Req> reqStreamObserver;
switch (methodId) {
default:
throw new java.lang.AssertionError();
}
}
}
}
Loading