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
119 changes: 119 additions & 0 deletions vertx-grpc-docs/src/main/java/examples/GreeterClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package examples;

import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.net.SocketAddress;
import io.vertx.grpc.client.GrpcClient;
import io.vertx.core.streams.ReadStream;
import io.vertx.core.streams.WriteStream;
import io.vertx.grpc.common.GrpcStatus;
import io.vertx.grpc.common.ServiceName;
import io.vertx.grpc.common.ServiceMethod;
import io.vertx.grpc.common.GrpcReadStream;
import io.vertx.grpc.common.GrpcWriteStream;
import io.vertx.grpc.common.GrpcMessageDecoder;
import io.vertx.grpc.common.GrpcMessageEncoder;

/**
* <p>A client for invoking the Greeter gRPC service.</p>
*/
@io.vertx.codegen.annotations.VertxGen
public interface GreeterClient {

/**
* SayHello protobuf RPC client service method.
*/
@io.vertx.codegen.annotations.GenIgnore(io.vertx.codegen.annotations.GenIgnore.PERMITTED_TYPE)
ServiceMethod<examples.HelloReply, examples.HelloRequest> SayHello = ServiceMethod.client(
ServiceName.create("helloworld", "Greeter"),
"SayHello",
GrpcMessageEncoder.encoder(),
GrpcMessageDecoder.decoder(examples.HelloReply.parser()));

/**
* Json client service methods.
*/
@io.vertx.codegen.annotations.GenIgnore(io.vertx.codegen.annotations.GenIgnore.PERMITTED_TYPE)
final class Json {

/**
* SayHello json RPC client service method.
*/
public static final ServiceMethod<examples.HelloReply, examples.HelloRequest> SayHello = ServiceMethod.client(
ServiceName.create("helloworld", "Greeter"),
"SayHello",
GrpcMessageEncoder.json(),
GrpcMessageDecoder.json(() -> examples.HelloReply.newBuilder()));
}

/**
* Create and return a Greeter gRPC service client. The assumed wire format is Protobuf.
*
* @param client the gRPC client
* @param host the host providing the service
* @return the configured client
*/
static GreeterClient create(GrpcClient client, SocketAddress host) {
return new GreeterClientImpl(client, host);
}

/**
* Create and return a Greeter gRPC service client.
*
* @param client the gRPC client
* @param host the host providing the service
* @param wireFormat the wire format
* @return the configured client
*/
static GreeterClient create(GrpcClient client, SocketAddress host, io.vertx.grpc.common.WireFormat wireFormat) {
return new GreeterClientImpl(client, host, wireFormat);
}

/**
* Calls the SayHello RPC service method.
*
* @param request the examples.HelloRequest request message
* @return a future of the examples.HelloReply response message
*/
@io.vertx.codegen.annotations.GenIgnore(io.vertx.codegen.annotations.GenIgnore.PERMITTED_TYPE)
Future<examples.HelloReply> sayHello(examples.HelloRequest request);
}

/**
* The proxy implementation.
*/
class GreeterClientImpl implements GreeterClient {

private final GrpcClient client;
private final SocketAddress socketAddress;
private final io.vertx.grpc.common.WireFormat wireFormat;

GreeterClientImpl(GrpcClient client, SocketAddress socketAddress) {
this(client, socketAddress, io.vertx.grpc.common.WireFormat.PROTOBUF);
}

GreeterClientImpl(GrpcClient client, SocketAddress socketAddress, io.vertx.grpc.common.WireFormat wireFormat) {
this.client = java.util.Objects.requireNonNull(client);
this.socketAddress = java.util.Objects.requireNonNull(socketAddress);
this.wireFormat = java.util.Objects.requireNonNull(wireFormat);
}

public Future<examples.HelloReply> sayHello(examples.HelloRequest request) {
ServiceMethod<examples.HelloReply, examples.HelloRequest> serviceMethod;
switch (wireFormat) {
case PROTOBUF:
serviceMethod = SayHello;
break;
case JSON:
serviceMethod = Json.SayHello;
break;
default:
throw new AssertionError();
}
return client.request(socketAddress, serviceMethod).compose(req -> {
req.end(request);
return req.response().compose(resp -> resp.last());
});
}

}
191 changes: 191 additions & 0 deletions vertx-grpc-docs/src/main/java/examples/GreeterService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
package examples;

import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.Handler;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.streams.ReadStream;
import io.vertx.core.streams.WriteStream;
import io.vertx.grpc.common.GrpcStatus;
import io.vertx.grpc.common.ServiceName;
import io.vertx.grpc.common.ServiceMethod;
import io.vertx.grpc.common.GrpcReadStream;
import io.vertx.grpc.common.GrpcWriteStream;
import io.vertx.grpc.common.GrpcMessageDecoder;
import io.vertx.grpc.common.GrpcMessageEncoder;
import io.vertx.grpc.server.GrpcServer;

import java.util.List;

/**
* <p>Provides support for RPC methods implementations of the Greeter gRPC service.</p>
*
* <p>The following methods of this class should be overridden to provide an implementation of the service:</p>
* <ul>
* <li>SayHello</li>
* </ul>
*/
public class GreeterService {

/**
* SayHello protobuf RPC server service method.
*/
public static final ServiceMethod<examples.HelloRequest, examples.HelloReply> SayHello = ServiceMethod.server(
ServiceName.create("helloworld", "Greeter"),
"SayHello",
GrpcMessageEncoder.encoder(),
GrpcMessageDecoder.decoder(examples.HelloRequest.parser()));

/**
* @return a mutable list of the known protobuf RPC server service methods.
*/
public static java.util.List<ServiceMethod<?, ?>> all() {
java.util.List<ServiceMethod<?, ?>> all = new java.util.ArrayList<>();
all.add(SayHello);
return all;
}

/**
* Json server service methods.
*/
public static final class Json {

/**
* SayHello json RPC server service method.
*/
public static final ServiceMethod<examples.HelloRequest, examples.HelloReply> SayHello = ServiceMethod.server(
ServiceName.create("helloworld", "Greeter"),
"SayHello",
GrpcMessageEncoder.json(),
GrpcMessageDecoder.json(() -> examples.HelloRequest.newBuilder()));

/**
* @return a mutable list of the known json RPC server service methods.
*/
public static java.util.List<ServiceMethod<?, ?>> all() {
java.util.List<ServiceMethod<?, ?>> all = new java.util.ArrayList<>();
all.add(SayHello);
return all;
}
}

/**
* Transcoded server service methods.
*/
public static final class Transcoding {

private static final io.vertx.grpc.transcoding.MethodTranscodingOptions SayHello_OPTIONS = new io.vertx.grpc.transcoding.MethodTranscodingOptions()
.setSelector("")
.setHttpMethod(HttpMethod.valueOf("GET"))
.setPath("/v1/hello/{name}")
.setBody("")
.setResponseBody("")
;

/**
* SayHello transcoded RPC server service method.
*/
public static final io.vertx.grpc.transcoding.TranscodingServiceMethod<examples.HelloRequest, examples.HelloReply> SayHello = io.vertx.grpc.transcoding.TranscodingServiceMethod.server(
ServiceName.create("helloworld", "Greeter"),
"SayHello",
GrpcMessageEncoder.json(),
GrpcMessageDecoder.json(() -> examples.HelloRequest.newBuilder()),
SayHello_OPTIONS);

/**
* @return a mutable list of the known transcoded RPC server service methods.
*/
public static java.util.List<ServiceMethod<?, ?>> all() {
java.util.List<ServiceMethod<?, ?>> all = new java.util.ArrayList<>();
all.add(SayHello);
return all;
}
}


/**
* Override this method to implement the SayHello RPC.
*/
protected Future<examples.HelloReply> sayHello(examples.HelloRequest request) {
throw new UnsupportedOperationException("Not implemented");
}

protected void sayHello(examples.HelloRequest request, Promise<examples.HelloReply> response) {
sayHello(request)
.onSuccess(msg -> response.complete(msg))
.onFailure(error -> response.fail(error));
}

/**
* Service method to RPC method binder.
*/
public class Binder {

private final List<ServiceMethod<?, ?>> serviceMethods;

private Binder(List<ServiceMethod<?, ?>> serviceMethods) {
this.serviceMethods = serviceMethods;
}

private void validate() {
for (ServiceMethod<?, ?> serviceMethod : serviceMethods) {
if (resolveHandler(serviceMethod) == null) {
throw new IllegalArgumentException("Invalid service method:" + serviceMethod);
}
}
}

private <Req, Resp> Handler<io.vertx.grpc.server.GrpcServerRequest<Req, Resp>> resolveHandler(ServiceMethod<Req, Resp> serviceMethod) {
if (SayHello == serviceMethod || Json.SayHello == serviceMethod) {
Handler<io.vertx.grpc.server.GrpcServerRequest<examples.HelloRequest, examples.HelloReply>> handler = GreeterService.this::handle_sayHello;
Handler<?> handler2 = handler;
return (Handler<io.vertx.grpc.server.GrpcServerRequest<Req, Resp>>) handler2;
}
return null;
}

private <Req, Resp> void bindHandler(GrpcServer server, ServiceMethod<Req, Resp> serviceMethod) {
Handler<io.vertx.grpc.server.GrpcServerRequest<Req, Resp>> handler = resolveHandler(serviceMethod);
server.callHandler(serviceMethod, handler);
}

/**
* Bind the contained service methods to the {@code server}.
*/
public void to(GrpcServer server) {
for (ServiceMethod<?, ?> serviceMethod : serviceMethods) {
bindHandler(server, serviceMethod);
}
}
}

/**
* @return a binder for the list of {@code methods}
*/
public Binder bind(List<ServiceMethod<?, ?>> methods) {
Binder binder = new Binder(methods);
binder.validate();
return binder;
}

/**
* @return a binder for the {@code methods}
*/
public Binder bind(ServiceMethod<?, ?>... methods) {
return bind(java.util.Arrays.asList(methods));
}

private void handle_sayHello(io.vertx.grpc.server.GrpcServerRequest<examples.HelloRequest, examples.HelloReply> request) {
Promise<examples.HelloReply> promise = Promise.promise();
request.handler(msg -> {
try {
sayHello(msg, promise);
} catch (RuntimeException err) {
promise.tryFail(err);
}
});
promise.future()
.onFailure(err -> request.response().status(GrpcStatus.INTERNAL).end())
.onSuccess(resp -> request.response().end(resp));
}
}
Loading