44import io .vertx .core .Promise ;
55import io .vertx .core .Handler ;
66import io .vertx .core .http .HttpMethod ;
7- import io .vertx .core .http .HttpServerRequest ;
87import io .vertx .core .streams .ReadStream ;
98import io .vertx .core .streams .WriteStream ;
109import io .vertx .grpc .common .GrpcStatus ;
1413import io .vertx .grpc .common .GrpcWriteStream ;
1514import io .vertx .grpc .common .GrpcMessageDecoder ;
1615import io .vertx .grpc .common .GrpcMessageEncoder ;
17- import io .vertx .grpc .server .GrpcServerResponse ;
1816import io .vertx .grpc .server .GrpcServer ;
1917
20- import java .util .ArrayList ;
2118import java .util .List ;
2219
23- public class GreeterService {
24-
20+ /**
21+ * <p>Provides support for RPC methods implementations of the Greeter gRPC service.</p>
22+ *
23+ * <p>The following methods of this class should be overridden to provide an implementation of the service:</p>
24+ * <ul>
25+ * <li>SayHello</li>
26+ * </ul>
27+ */
28+ public class GreeterService {
29+
30+ /**
31+ * SayHello protobuf RPC server service method.
32+ */
2533 public static final ServiceMethod <examples .HelloRequest , examples .HelloReply > SayHello = ServiceMethod .server (
2634 ServiceName .create ("helloworld" , "Greeter" ),
2735 "SayHello" ,
2836 GrpcMessageEncoder .encoder (),
2937 GrpcMessageDecoder .decoder (examples .HelloRequest .parser ()));
3038
31- public static final java .util .List <ServiceMethod <?, ?>> all () {
39+ /**
40+ * @return a mutable list of the known protobuf RPC server service methods.
41+ */
42+ public static java .util .List <ServiceMethod <?, ?>> all () {
3243 java .util .List <ServiceMethod <?, ?>> all = new java .util .ArrayList <>();
3344 all .add (SayHello );
3445 return all ;
3546 }
3647
48+ /**
49+ * Json server service methods.
50+ */
3751 public static final class Json {
3852
53+ /**
54+ * SayHello json RPC server service method.
55+ */
3956 public static final ServiceMethod <examples .HelloRequest , examples .HelloReply > SayHello = ServiceMethod .server (
4057 ServiceName .create ("helloworld" , "Greeter" ),
4158 "SayHello" ,
4259 GrpcMessageEncoder .json (),
4360 GrpcMessageDecoder .json (() -> examples .HelloRequest .newBuilder ()));
4461
45- public static final java .util .List <ServiceMethod <?, ?>> all () {
62+ /**
63+ * @return a mutable list of the known json RPC server service methods.
64+ */
65+ public static java .util .List <ServiceMethod <?, ?>> all () {
4666 java .util .List <ServiceMethod <?, ?>> all = new java .util .ArrayList <>();
4767 all .add (SayHello );
4868 return all ;
4969 }
5070 }
5171
72+ /**
73+ * Transcoded server service methods.
74+ */
5275 public static final class Transcoding {
5376
5477 private static final io .vertx .grpc .transcoding .MethodTranscodingOptions SayHello_OPTIONS = new io .vertx .grpc .transcoding .MethodTranscodingOptions ()
@@ -59,41 +82,59 @@ public static final class Transcoding {
5982 .setResponseBody ("" )
6083 ;
6184
62- public static final io .vertx .grpc .transcoding .TranscodingServiceMethod <examples .HelloRequest , examples .HelloReply > SayHello = io .vertx .grpc .transcoding .TranscodingServiceMethod .server (
63- ServiceName .create ("helloworld" , "Greeter" ),
64- "SayHello" ,
65- GrpcMessageEncoder .json (),
66- GrpcMessageDecoder .json (() -> examples .HelloRequest .newBuilder ()),
67- SayHello_OPTIONS );
85+ /**
86+ * SayHello transcoded RPC server service method.
87+ */
88+ public static final io .vertx .grpc .transcoding .TranscodingServiceMethod <examples .HelloRequest , examples .HelloReply > SayHello = io .vertx .grpc .transcoding .TranscodingServiceMethod .server (
89+ ServiceName .create ("helloworld" , "Greeter" ),
90+ "SayHello" ,
91+ GrpcMessageEncoder .json (),
92+ GrpcMessageDecoder .json (() -> examples .HelloRequest .newBuilder ()),
93+ SayHello_OPTIONS );
6894
69- public static final java .util .List <ServiceMethod <?, ?>> all () {
95+ /**
96+ * @return a mutable list of the known transcoded RPC server service methods.
97+ */
98+ public static java .util .List <ServiceMethod <?, ?>> all () {
7099 java .util .List <ServiceMethod <?, ?>> all = new java .util .ArrayList <>();
71100 all .add (SayHello );
72101 return all ;
73102 }
74103 }
75104
76- public Future <examples .HelloReply > sayHello (examples .HelloRequest request ) {
77- throw new UnsupportedOperationException ("Not implemented" );
78- }
79- public void sayHello (examples .HelloRequest request , Promise <examples .HelloReply > response ) {
80- sayHello (request )
81- .onSuccess (msg -> response .complete (msg ))
82- .onFailure (error -> response .fail (error ));
83- }
84105
106+ /**
107+ * Override this method to implement the SayHello RPC.
108+ */
109+ protected Future <examples .HelloReply > sayHello (examples .HelloRequest request ) {
110+ throw new UnsupportedOperationException ("Not implemented" );
111+ }
112+
113+ protected void sayHello (examples .HelloRequest request , Promise <examples .HelloReply > response ) {
114+ sayHello (request )
115+ .onSuccess (msg -> response .complete (msg ))
116+ .onFailure (error -> response .fail (error ));
117+ }
118+
119+ /**
120+ * Service method to RPC method binder.
121+ */
85122 public class Binder {
123+
86124 private final List <ServiceMethod <?, ?>> serviceMethods ;
125+
87126 private Binder (List <ServiceMethod <?, ?>> serviceMethods ) {
88127 this .serviceMethods = serviceMethods ;
89128 }
129+
90130 private void validate () {
91131 for (ServiceMethod <?, ?> serviceMethod : serviceMethods ) {
92132 if (resolveHandler (serviceMethod ) == null ) {
93133 throw new IllegalArgumentException ("Invalid service method:" + serviceMethod );
94134 }
95135 }
96136 }
137+
97138 private <Req , Resp > Handler <io .vertx .grpc .server .GrpcServerRequest <Req , Resp >> resolveHandler (ServiceMethod <Req , Resp > serviceMethod ) {
98139 if (SayHello == serviceMethod || Json .SayHello == serviceMethod ) {
99140 Handler <io .vertx .grpc .server .GrpcServerRequest <examples .HelloRequest , examples .HelloReply >> handler = GreeterService .this ::handle_sayHello ;
@@ -102,25 +143,36 @@ private <Req, Resp> Handler<io.vertx.grpc.server.GrpcServerRequest<Req, Resp>> r
102143 }
103144 return null ;
104145 }
146+
105147 private <Req , Resp > void bindHandler (GrpcServer server , ServiceMethod <Req , Resp > serviceMethod ) {
106148 Handler <io .vertx .grpc .server .GrpcServerRequest <Req , Resp >> handler = resolveHandler (serviceMethod );
107149 server .callHandler (serviceMethod , handler );
108150 }
151+
152+ /**
153+ * Bind the contained service methods to the {@code server}.
154+ */
109155 public void to (GrpcServer server ) {
110156 for (ServiceMethod <?, ?> serviceMethod : serviceMethods ) {
111157 bindHandler (server , serviceMethod );
112158 }
113159 }
114160 }
115161
116- public Binder bind (List <ServiceMethod <?, ?>> serviceMethods ) {
117- Binder binder = new Binder (serviceMethods );
162+ /**
163+ * @return a binder for the list of {@code methods}
164+ */
165+ public Binder bind (List <ServiceMethod <?, ?>> methods ) {
166+ Binder binder = new Binder (methods );
118167 binder .validate ();
119168 return binder ;
120169 }
121170
122- public Binder bind (ServiceMethod <?, ?>... serviceMethods ) {
123- return bind (java .util .Arrays .asList (serviceMethods ));
171+ /**
172+ * @return a binder for the {@code methods}
173+ */
174+ public Binder bind (ServiceMethod <?, ?>... methods ) {
175+ return bind (java .util .Arrays .asList (methods ));
124176 }
125177
126178 private void handle_sayHello (io .vertx .grpc .server .GrpcServerRequest <examples .HelloRequest , examples .HelloReply > request ) {
0 commit comments