-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathGreeterGrpcService.java
More file actions
212 lines (178 loc) · 6.1 KB
/
Copy pathGreeterGrpcService.java
File metadata and controls
212 lines (178 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package examples.grpc;
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.GrpcMessageDecoder;
import io.vertx.grpc.common.GrpcMessageEncoder;
import io.vertx.grpc.server.GrpcServerRequest;
import io.vertx.grpc.server.GrpcServer;
import io.vertx.grpc.server.ServiceContainer;
import io.vertx.grpc.server.ServiceMethodInvoker;
import io.vertx.grpc.server.Service;
import io.vertx.grpc.server.StatusException;
import com.google.protobuf.Descriptors;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
/**
* <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 GreeterGrpcService extends GreeterService implements Service {
/**
* Greeter service name.
*/
public static final ServiceName SERVICE_NAME = ServiceName.create("examples.grpc", "Greeter");
/**
* Greeter service descriptor.
*/
public static final Descriptors.ServiceDescriptor SERVICE_DESCRIPTOR = Docs.getDescriptor().findServiceByName("Greeter");
@Override
public ServiceName name() {
return SERVICE_NAME;
}
@Override
public Descriptors.ServiceDescriptor descriptor() {
return SERVICE_DESCRIPTOR;
}
/**
* @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 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;
}
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.grpc.HelloRequest, examples.grpc.HelloReply> SayHello = io.vertx.grpc.transcoding.TranscodingServiceMethod.server(
SERVICE_NAME,
"SayHello",
GrpcMessageEncoder.encoder(),
GrpcMessageDecoder.decoder(examples.grpc.HelloRequest.newBuilder()),
SayHello_OPTIONS,
false
);
private final Invoker invoker = new Invoker(this, all());
@Override
public <Req, Resp> ServiceMethodInvoker<Req, Resp> invoker(ServiceMethod<Req, Resp> method) {
return invoker.invoker(method);
}
@Override
public List<ServiceMethod<?, ?>> methods() {
return invoker.methods();
}
/**
* @return a free form builder that gives the opportunity to bind only certain methods of a service
*/
public static Builder builder(GreeterService service) {
return new Builder(service);
}
/**
* Service builder.
*/
public static class Builder {
private final List<ServiceMethod<?, ?>> serviceMethods = new ArrayList<>();
private final GreeterService instance;
private Builder(GreeterService instance) {
this.instance = instance;
}
/**
* @return this builder
*/
public Builder bind(List<ServiceMethod<?, ?>> methods) {
serviceMethods.addAll(methods);
return this;
}
/**
* @return this builder
*/
public Builder bind(ServiceMethod<?, ?>... methods) {
return bind(java.util.Arrays.asList(methods));
}
public Service build() {
return new Invoker(instance, new ArrayList<>(serviceMethods));
}
}
private static class Invoker implements Service {
private final GreeterService instance;
private final List<ServiceMethod<?, ?>> serviceMethods;
private final Map<String, ServiceMethodInvoker<?, ?>> invokers;
public Invoker(GreeterService instance, List<ServiceMethod<?, ?>> serviceMethods) {
Map<String, ServiceMethodInvoker<?, ?>> invokers = new HashMap<>();
for (ServiceMethod<?, ?> serviceMethod : serviceMethods) {
ServiceMethodInvoker<?, ?> invoker = resolveHandler(serviceMethod);
invokers.put(serviceMethod.methodName(), invoker);
}
this.instance = instance;
this.invokers = invokers;
this.serviceMethods = serviceMethods;
}
@Override
public ServiceName name() {
return SERVICE_NAME;
}
@Override
public Descriptors.ServiceDescriptor descriptor() {
return SERVICE_DESCRIPTOR;
}
@Override
public List<ServiceMethod<?, ?>> methods() {
return serviceMethods;
}
@Override
public <Req, Resp> ServiceMethodInvoker<Req, Resp> invoker(ServiceMethod<Req, Resp> method) {
ServiceMethodInvoker methodInvoker = invokers.get(method.methodName());
if (methodInvoker != null) {
return methodInvoker;
} else {
return Service.super.invoker(method);
}
}
private <Req, Resp> ServiceMethodInvoker<Req, Resp> resolveHandler(ServiceMethod<Req, Resp> serviceMethod) {
if (SayHello == serviceMethod) {
ServiceMethodInvoker<examples.grpc.HelloRequest, examples.grpc.HelloReply> handler = this::handle_sayHello;
ServiceMethodInvoker<?, ?> handler2 = handler;
return (ServiceMethodInvoker<Req, Resp>) handler2;
}
return null;
}
private void handle_sayHello(io.vertx.grpc.server.GrpcServerRequest<examples.grpc.HelloRequest, examples.grpc.HelloReply> request) {
request.handler(msg -> {
instance.sayHello(msg, (res, err) -> {
if (err == null) {
request.response().end(res);
} else {
request.response().fail(err);
}
});
});
}
}
}