blob: 0a84c7352007f175c26d17c95cd4fa5e8c0af6e4 [file] [log] [blame]
nnobleebebb7e2014-12-10 16:31:01 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
nnobleebebb7e2014-12-10 16:31:01 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
Nicolas "Pixel" Noble36f53232015-01-16 06:39:58 +010034#include <map>
35
nnobleebebb7e2014-12-10 16:31:01 -080036#include "src/compiler/cpp_generator.h"
nnobleebebb7e2014-12-10 16:31:01 -080037#include "src/compiler/cpp_generator_helpers.h"
Nicolas Nobled446eb82015-03-12 17:22:33 -070038
39#include "src/compiler/config.h"
40
Craig Tiller8c8d0aa2015-02-12 11:38:36 -080041#include <sstream>
nnobleebebb7e2014-12-10 16:31:01 -080042
43namespace grpc_cpp_generator {
44namespace {
45
Craig Tiller8c8d0aa2015-02-12 11:38:36 -080046template <class T>
Nicolas Nobled446eb82015-03-12 17:22:33 -070047grpc::string as_string(T x) {
Craig Tiller8c8d0aa2015-02-12 11:38:36 -080048 std::ostringstream out;
49 out << x;
50 return out.str();
51}
52
Nicolas Nobled446eb82015-03-12 17:22:33 -070053bool NoStreaming(const grpc::protobuf::MethodDescriptor *method) {
Craig Tillerb5dcec52015-01-13 11:13:42 -080054 return !method->client_streaming() && !method->server_streaming();
nnobleebebb7e2014-12-10 16:31:01 -080055}
56
Nicolas Nobled446eb82015-03-12 17:22:33 -070057bool ClientOnlyStreaming(const grpc::protobuf::MethodDescriptor *method) {
Craig Tillerb5dcec52015-01-13 11:13:42 -080058 return method->client_streaming() && !method->server_streaming();
nnobleebebb7e2014-12-10 16:31:01 -080059}
60
Nicolas Nobled446eb82015-03-12 17:22:33 -070061bool ServerOnlyStreaming(const grpc::protobuf::MethodDescriptor *method) {
yangg1b151092015-01-09 15:31:05 -080062 return !method->client_streaming() && method->server_streaming();
nnobleebebb7e2014-12-10 16:31:01 -080063}
64
Nicolas Nobled446eb82015-03-12 17:22:33 -070065bool BidiStreaming(const grpc::protobuf::MethodDescriptor *method) {
Craig Tillerb5dcec52015-01-13 11:13:42 -080066 return method->client_streaming() && method->server_streaming();
nnobleebebb7e2014-12-10 16:31:01 -080067}
68
Nicolas Nobled446eb82015-03-12 17:22:33 -070069bool HasUnaryCalls(const grpc::protobuf::FileDescriptor *file) {
Craig Tiller2dff17d2015-02-09 12:42:23 -080070 for (int i = 0; i < file->service_count(); i++) {
71 for (int j = 0; j < file->service(i)->method_count(); j++) {
72 if (NoStreaming(file->service(i)->method(j))) {
73 return true;
74 }
75 }
76 }
77 return false;
78}
79
Nicolas Nobled446eb82015-03-12 17:22:33 -070080bool HasClientOnlyStreaming(const grpc::protobuf::FileDescriptor *file) {
nnobleebebb7e2014-12-10 16:31:01 -080081 for (int i = 0; i < file->service_count(); i++) {
82 for (int j = 0; j < file->service(i)->method_count(); j++) {
83 if (ClientOnlyStreaming(file->service(i)->method(j))) {
84 return true;
85 }
86 }
87 }
88 return false;
89}
90
Nicolas Nobled446eb82015-03-12 17:22:33 -070091bool HasServerOnlyStreaming(const grpc::protobuf::FileDescriptor *file) {
nnobleebebb7e2014-12-10 16:31:01 -080092 for (int i = 0; i < file->service_count(); i++) {
93 for (int j = 0; j < file->service(i)->method_count(); j++) {
94 if (ServerOnlyStreaming(file->service(i)->method(j))) {
95 return true;
96 }
97 }
98 }
99 return false;
100}
101
Nicolas Nobled446eb82015-03-12 17:22:33 -0700102bool HasBidiStreaming(const grpc::protobuf::FileDescriptor *file) {
nnobleebebb7e2014-12-10 16:31:01 -0800103 for (int i = 0; i < file->service_count(); i++) {
104 for (int j = 0; j < file->service(i)->method_count(); j++) {
105 if (BidiStreaming(file->service(i)->method(j))) {
106 return true;
107 }
108 }
109 }
110 return false;
111}
112} // namespace
113
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100114grpc::string GetHeaderIncludes(const grpc::protobuf::FileDescriptor *file,
115 const Parameters &params) {
Nicolas Nobled446eb82015-03-12 17:22:33 -0700116 grpc::string temp =
Craig Tiller14a65f92015-02-09 13:13:14 -0800117 "#include <grpc++/impl/internal_stub.h>\n"
118 "#include <grpc++/impl/service_type.h>\n"
119 "#include <grpc++/status.h>\n"
nnobleebebb7e2014-12-10 16:31:01 -0800120 "\n"
121 "namespace grpc {\n"
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800122 "class CompletionQueue;\n"
nnobleebebb7e2014-12-10 16:31:01 -0800123 "class ChannelInterface;\n"
yangga4b6f5d2014-12-17 15:53:12 -0800124 "class RpcService;\n"
125 "class ServerContext;\n";
Craig Tiller2dff17d2015-02-09 12:42:23 -0800126 if (HasUnaryCalls(file)) {
Craig Tiller5ef5db12015-02-09 12:47:21 -0800127 temp.append(
Yang Gao3a5e5492015-02-18 14:32:38 -0800128 "template <class OutMessage> class ClientAsyncResponseReader;\n");
129 temp.append(
Craig Tiller5ef5db12015-02-09 12:47:21 -0800130 "template <class OutMessage> class ServerAsyncResponseWriter;\n");
Craig Tiller2dff17d2015-02-09 12:42:23 -0800131 }
nnobleebebb7e2014-12-10 16:31:01 -0800132 if (HasClientOnlyStreaming(file)) {
133 temp.append("template <class OutMessage> class ClientWriter;\n");
134 temp.append("template <class InMessage> class ServerReader;\n");
Craig Tiller2dff17d2015-02-09 12:42:23 -0800135 temp.append("template <class OutMessage> class ClientAsyncWriter;\n");
Yang Gao005f18a2015-02-13 10:22:33 -0800136 temp.append("template <class OutMessage, class InMessage> class ServerAsyncReader;\n");
nnobleebebb7e2014-12-10 16:31:01 -0800137 }
138 if (HasServerOnlyStreaming(file)) {
139 temp.append("template <class InMessage> class ClientReader;\n");
140 temp.append("template <class OutMessage> class ServerWriter;\n");
Craig Tiller2dff17d2015-02-09 12:42:23 -0800141 temp.append("template <class OutMessage> class ClientAsyncReader;\n");
142 temp.append("template <class InMessage> class ServerAsyncWriter;\n");
nnobleebebb7e2014-12-10 16:31:01 -0800143 }
144 if (HasBidiStreaming(file)) {
145 temp.append(
146 "template <class OutMessage, class InMessage>\n"
147 "class ClientReaderWriter;\n");
148 temp.append(
149 "template <class OutMessage, class InMessage>\n"
150 "class ServerReaderWriter;\n");
Craig Tiller225f7be2015-02-09 22:32:37 -0800151 temp.append(
152 "template <class OutMessage, class InMessage>\n"
153 "class ClientAsyncReaderWriter;\n");
154 temp.append(
155 "template <class OutMessage, class InMessage>\n"
156 "class ServerAsyncReaderWriter;\n");
nnobleebebb7e2014-12-10 16:31:01 -0800157 }
158 temp.append("} // namespace grpc\n");
159 return temp;
160}
161
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100162grpc::string GetSourceIncludes(const Parameters &param) {
Yang Gao3a5e5492015-02-18 14:32:38 -0800163 return "#include <grpc++/async_unary_call.h>\n"
164 "#include <grpc++/channel_interface.h>\n"
Craig Tiller80e00a82015-02-09 20:54:25 -0800165 "#include <grpc++/impl/client_unary_call.h>\n"
Craig Tiller2dff17d2015-02-09 12:42:23 -0800166 "#include <grpc++/impl/rpc_method.h>\n"
167 "#include <grpc++/impl/rpc_service_method.h>\n"
Craig Tiller14a65f92015-02-09 13:13:14 -0800168 "#include <grpc++/impl/service_type.h>\n"
Craig Tiller2dff17d2015-02-09 12:42:23 -0800169 "#include <grpc++/stream.h>\n";
nnobleebebb7e2014-12-10 16:31:01 -0800170}
171
Nicolas Nobled446eb82015-03-12 17:22:33 -0700172void PrintHeaderClientMethod(grpc::protobuf::io::Printer *printer,
173 const grpc::protobuf::MethodDescriptor *method,
174 std::map<grpc::string, grpc::string> *vars) {
nnobleebebb7e2014-12-10 16:31:01 -0800175 (*vars)["Method"] = method->name();
176 (*vars)["Request"] =
177 grpc_cpp_generator::ClassName(method->input_type(), true);
178 (*vars)["Response"] =
179 grpc_cpp_generator::ClassName(method->output_type(), true);
180 if (NoStreaming(method)) {
181 printer->Print(*vars,
182 "::grpc::Status $Method$(::grpc::ClientContext* context, "
Craig Tiller5ef5db12015-02-09 12:47:21 -0800183 "const $Request$& request, $Response$* response);\n");
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800184 printer->Print(
185 *vars,
186 "std::unique_ptr< ::grpc::ClientAsyncResponseReader< $Response$>> "
vjpai56c51292015-02-26 17:01:35 -0800187 "Async$Method$(::grpc::ClientContext* context, "
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800188 "const $Request$& request, "
189 "::grpc::CompletionQueue* cq, void* tag);\n");
nnobleebebb7e2014-12-10 16:31:01 -0800190 } else if (ClientOnlyStreaming(method)) {
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800191 printer->Print(
192 *vars,
193 "std::unique_ptr< ::grpc::ClientWriter< $Request$>> $Method$("
194 "::grpc::ClientContext* context, $Response$* response);\n");
195 printer->Print(
196 *vars,
vjpai56c51292015-02-26 17:01:35 -0800197 "std::unique_ptr< ::grpc::ClientAsyncWriter< $Request$>> Async$Method$("
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800198 "::grpc::ClientContext* context, $Response$* response, "
199 "::grpc::CompletionQueue* cq, void* tag);\n");
nnobleebebb7e2014-12-10 16:31:01 -0800200 } else if (ServerOnlyStreaming(method)) {
201 printer->Print(
202 *vars,
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800203 "std::unique_ptr< ::grpc::ClientReader< $Response$>> $Method$("
Yang Gao07d83042015-02-13 14:11:31 -0800204 "::grpc::ClientContext* context, const $Request$& request);\n");
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800205 printer->Print(
206 *vars,
vjpai56c51292015-02-26 17:01:35 -0800207 "std::unique_ptr< ::grpc::ClientAsyncReader< $Response$>> Async$Method$("
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800208 "::grpc::ClientContext* context, const $Request$& request, "
209 "::grpc::CompletionQueue* cq, void* tag);\n");
nnobleebebb7e2014-12-10 16:31:01 -0800210 } else if (BidiStreaming(method)) {
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800211 printer->Print(
212 *vars,
213 "std::unique_ptr< ::grpc::ClientReaderWriter< $Request$, $Response$>> "
214 "$Method$(::grpc::ClientContext* context);\n");
nnobleebebb7e2014-12-10 16:31:01 -0800215 printer->Print(*vars,
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800216 "std::unique_ptr< ::grpc::ClientAsyncReaderWriter< "
217 "$Request$, $Response$>> "
vjpai56c51292015-02-26 17:01:35 -0800218 "Async$Method$(::grpc::ClientContext* context, "
Yang Gao068c85b2015-02-12 15:21:24 -0800219 "::grpc::CompletionQueue* cq, void* tag);\n");
nnobleebebb7e2014-12-10 16:31:01 -0800220 }
221}
222
Craig Tiller5ef5db12015-02-09 12:47:21 -0800223void PrintHeaderServerMethodSync(
Nicolas Nobled446eb82015-03-12 17:22:33 -0700224 grpc::protobuf::io::Printer *printer,
225 const grpc::protobuf::MethodDescriptor *method,
226 std::map<grpc::string, grpc::string> *vars) {
nnobleebebb7e2014-12-10 16:31:01 -0800227 (*vars)["Method"] = method->name();
228 (*vars)["Request"] =
229 grpc_cpp_generator::ClassName(method->input_type(), true);
230 (*vars)["Response"] =
231 grpc_cpp_generator::ClassName(method->output_type(), true);
232 if (NoStreaming(method)) {
233 printer->Print(*vars,
yangga4b6f5d2014-12-17 15:53:12 -0800234 "virtual ::grpc::Status $Method$("
235 "::grpc::ServerContext* context, const $Request$* request, "
nnobleebebb7e2014-12-10 16:31:01 -0800236 "$Response$* response);\n");
237 } else if (ClientOnlyStreaming(method)) {
238 printer->Print(*vars,
239 "virtual ::grpc::Status $Method$("
yangga4b6f5d2014-12-17 15:53:12 -0800240 "::grpc::ServerContext* context, "
Yang Gao1ff11f62015-01-14 11:45:32 -0800241 "::grpc::ServerReader< $Request$>* reader, "
nnobleebebb7e2014-12-10 16:31:01 -0800242 "$Response$* response);\n");
243 } else if (ServerOnlyStreaming(method)) {
244 printer->Print(*vars,
yangga4b6f5d2014-12-17 15:53:12 -0800245 "virtual ::grpc::Status $Method$("
246 "::grpc::ServerContext* context, const $Request$* request, "
Yang Gao1ff11f62015-01-14 11:45:32 -0800247 "::grpc::ServerWriter< $Response$>* writer);\n");
nnobleebebb7e2014-12-10 16:31:01 -0800248 } else if (BidiStreaming(method)) {
Yang Gao5680ff42015-01-14 12:14:21 -0800249 printer->Print(
250 *vars,
251 "virtual ::grpc::Status $Method$("
252 "::grpc::ServerContext* context, "
253 "::grpc::ServerReaderWriter< $Response$, $Request$>* stream);"
254 "\n");
nnobleebebb7e2014-12-10 16:31:01 -0800255 }
256}
257
Craig Tiller5ef5db12015-02-09 12:47:21 -0800258void PrintHeaderServerMethodAsync(
Nicolas Nobled446eb82015-03-12 17:22:33 -0700259 grpc::protobuf::io::Printer *printer,
260 const grpc::protobuf::MethodDescriptor *method,
261 std::map<grpc::string, grpc::string> *vars) {
Craig Tiller2dff17d2015-02-09 12:42:23 -0800262 (*vars)["Method"] = method->name();
263 (*vars)["Request"] =
264 grpc_cpp_generator::ClassName(method->input_type(), true);
265 (*vars)["Response"] =
266 grpc_cpp_generator::ClassName(method->output_type(), true);
267 if (NoStreaming(method)) {
268 printer->Print(*vars,
Yang Gaoca3cb3e2015-02-12 00:05:11 -0800269 "void Request$Method$("
Craig Tiller2dff17d2015-02-09 12:42:23 -0800270 "::grpc::ServerContext* context, $Request$* request, "
271 "::grpc::ServerAsyncResponseWriter< $Response$>* response, "
272 "::grpc::CompletionQueue* cq, void *tag);\n");
273 } else if (ClientOnlyStreaming(method)) {
274 printer->Print(*vars,
Yang Gaoca3cb3e2015-02-12 00:05:11 -0800275 "void Request$Method$("
Craig Tiller2dff17d2015-02-09 12:42:23 -0800276 "::grpc::ServerContext* context, "
Yang Gao005f18a2015-02-13 10:22:33 -0800277 "::grpc::ServerAsyncReader< $Response$, $Request$>* reader, "
Craig Tiller2dff17d2015-02-09 12:42:23 -0800278 "::grpc::CompletionQueue* cq, void *tag);\n");
279 } else if (ServerOnlyStreaming(method)) {
280 printer->Print(*vars,
Yang Gaoca3cb3e2015-02-12 00:05:11 -0800281 "void Request$Method$("
Craig Tiller2dff17d2015-02-09 12:42:23 -0800282 "::grpc::ServerContext* context, $Request$* request, "
283 "::grpc::ServerAsyncWriter< $Response$>* writer, "
284 "::grpc::CompletionQueue* cq, void *tag);\n");
285 } else if (BidiStreaming(method)) {
286 printer->Print(
287 *vars,
Yang Gaoca3cb3e2015-02-12 00:05:11 -0800288 "void Request$Method$("
Craig Tiller2dff17d2015-02-09 12:42:23 -0800289 "::grpc::ServerContext* context, "
Craig Tiller225f7be2015-02-09 22:32:37 -0800290 "::grpc::ServerAsyncReaderWriter< $Response$, $Request$>* stream, "
Craig Tiller5ef5db12015-02-09 12:47:21 -0800291 "::grpc::CompletionQueue* cq, void *tag);\n");
Craig Tiller2dff17d2015-02-09 12:42:23 -0800292 }
293}
294
Nicolas Nobled446eb82015-03-12 17:22:33 -0700295void PrintHeaderService(grpc::protobuf::io::Printer *printer,
296 const grpc::protobuf::ServiceDescriptor *service,
297 std::map<grpc::string, grpc::string> *vars) {
nnobleebebb7e2014-12-10 16:31:01 -0800298 (*vars)["Service"] = service->name();
299
300 printer->Print(*vars,
Craig Tillercf133f42015-02-26 14:05:56 -0800301 "class $Service$ GRPC_FINAL {\n"
nnobleebebb7e2014-12-10 16:31:01 -0800302 " public:\n");
303 printer->Indent();
304
305 // Client side
Craig Tillerb5dcec52015-01-13 11:13:42 -0800306 printer->Print(
Craig Tillercf133f42015-02-26 14:05:56 -0800307 "class Stub GRPC_FINAL : public ::grpc::InternalStub {\n"
Craig Tillerb5dcec52015-01-13 11:13:42 -0800308 " public:\n");
nnobleebebb7e2014-12-10 16:31:01 -0800309 printer->Indent();
310 for (int i = 0; i < service->method_count(); ++i) {
311 PrintHeaderClientMethod(printer, service->method(i), vars);
312 }
313 printer->Outdent();
314 printer->Print("};\n");
315 printer->Print(
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800316 "static std::unique_ptr<Stub> NewStub(const std::shared_ptr< "
317 "::grpc::ChannelInterface>& "
nnobleebebb7e2014-12-10 16:31:01 -0800318 "channel);\n");
319
320 printer->Print("\n");
321
Craig Tiller2dff17d2015-02-09 12:42:23 -0800322 // Server side - Synchronous
Craig Tillerb5dcec52015-01-13 11:13:42 -0800323 printer->Print(
Craig Tiller14a65f92015-02-09 13:13:14 -0800324 "class Service : public ::grpc::SynchronousService {\n"
Craig Tillerb5dcec52015-01-13 11:13:42 -0800325 " public:\n");
nnobleebebb7e2014-12-10 16:31:01 -0800326 printer->Indent();
327 printer->Print("Service() : service_(nullptr) {}\n");
328 printer->Print("virtual ~Service();\n");
329 for (int i = 0; i < service->method_count(); ++i) {
Craig Tiller2dff17d2015-02-09 12:42:23 -0800330 PrintHeaderServerMethodSync(printer, service->method(i), vars);
331 }
Craig Tillercf133f42015-02-26 14:05:56 -0800332 printer->Print("::grpc::RpcService* service() GRPC_OVERRIDE GRPC_FINAL;\n");
Craig Tiller2dff17d2015-02-09 12:42:23 -0800333 printer->Outdent();
334 printer->Print(
335 " private:\n"
336 " ::grpc::RpcService* service_;\n");
337 printer->Print("};\n");
338
339 // Server side - Asynchronous
340 printer->Print(
Craig Tillercf133f42015-02-26 14:05:56 -0800341 "class AsyncService GRPC_FINAL : public ::grpc::AsynchronousService {\n"
Craig Tiller2dff17d2015-02-09 12:42:23 -0800342 " public:\n");
343 printer->Indent();
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800344 (*vars)["MethodCount"] = as_string(service->method_count());
345 printer->Print("explicit AsyncService(::grpc::CompletionQueue* cq);\n");
Craig Tiller0220cf12015-02-12 17:39:26 -0800346 printer->Print("~AsyncService() {};\n");
Craig Tiller2dff17d2015-02-09 12:42:23 -0800347 for (int i = 0; i < service->method_count(); ++i) {
348 PrintHeaderServerMethodAsync(printer, service->method(i), vars);
nnobleebebb7e2014-12-10 16:31:01 -0800349 }
nnobleebebb7e2014-12-10 16:31:01 -0800350 printer->Outdent();
nnobleebebb7e2014-12-10 16:31:01 -0800351 printer->Print("};\n");
352
353 printer->Outdent();
354 printer->Print("};\n");
355}
356
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100357grpc::string GetHeaderServices(const grpc::protobuf::FileDescriptor *file,
358 const Parameters &params) {
Nicolas Nobled446eb82015-03-12 17:22:33 -0700359 grpc::string output;
360 grpc::protobuf::io::StringOutputStream output_stream(&output);
361 grpc::protobuf::io::Printer printer(&output_stream, '$');
362 std::map<grpc::string, grpc::string> vars;
nnobleebebb7e2014-12-10 16:31:01 -0800363
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100364 if (!params.services_namespace.empty()) {
365 vars["services_namespace"] = params.services_namespace;
366 printer.Print(vars, "\nnamespace $services_namespace$ {\n\n");
367 }
368
nnobleebebb7e2014-12-10 16:31:01 -0800369 for (int i = 0; i < file->service_count(); ++i) {
samuelwca9f3592014-12-15 14:22:04 -0800370 PrintHeaderService(&printer, file->service(i), &vars);
nnobleebebb7e2014-12-10 16:31:01 -0800371 printer.Print("\n");
372 }
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100373
374 if (!params.services_namespace.empty()) {
375 printer.Print(vars, "} // namespace $services_namespace$\n\n");
376 }
377
nnobleebebb7e2014-12-10 16:31:01 -0800378 return output;
379}
380
Nicolas Nobled446eb82015-03-12 17:22:33 -0700381void PrintSourceClientMethod(grpc::protobuf::io::Printer *printer,
382 const grpc::protobuf::MethodDescriptor *method,
383 std::map<grpc::string, grpc::string> *vars) {
nnobleebebb7e2014-12-10 16:31:01 -0800384 (*vars)["Method"] = method->name();
385 (*vars)["Request"] =
386 grpc_cpp_generator::ClassName(method->input_type(), true);
387 (*vars)["Response"] =
388 grpc_cpp_generator::ClassName(method->output_type(), true);
389 if (NoStreaming(method)) {
390 printer->Print(*vars,
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100391 "::grpc::Status $ns$$Service$::Stub::$Method$("
nnobleebebb7e2014-12-10 16:31:01 -0800392 "::grpc::ClientContext* context, "
393 "const $Request$& request, $Response$* response) {\n");
394 printer->Print(*vars,
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800395 " return ::grpc::BlockingUnaryCall(channel(),"
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100396 "::grpc::RpcMethod($prefix$$Service$_method_names[$Idx$]), "
nnobleebebb7e2014-12-10 16:31:01 -0800397 "context, request, response);\n"
398 "}\n\n");
Yang Gao5680ff42015-01-14 12:14:21 -0800399 printer->Print(
400 *vars,
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800401 "std::unique_ptr< ::grpc::ClientAsyncResponseReader< $Response$>> "
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100402 "$ns$$Service$::Stub::Async$Method$(::grpc::ClientContext* context, "
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800403 "const $Request$& request, "
404 "::grpc::CompletionQueue* cq, void* tag) {\n");
yangg5bcea0d2015-01-06 10:35:03 -0800405 printer->Print(*vars,
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800406 " return std::unique_ptr< "
407 "::grpc::ClientAsyncResponseReader< $Response$>>(new "
408 "::grpc::ClientAsyncResponseReader< $Response$>("
409 "channel(), cq, "
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100410 "::grpc::RpcMethod($prefix$$Service$_method_names[$Idx$]), "
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800411 "context, request, tag));\n"
412 "}\n\n");
413 } else if (ClientOnlyStreaming(method)) {
414 printer->Print(*vars,
415 "std::unique_ptr< ::grpc::ClientWriter< $Request$>> "
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100416 "$ns$$Service$::Stub::$Method$("
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800417 "::grpc::ClientContext* context, $Response$* response) {\n");
418 printer->Print(*vars,
419 " return std::unique_ptr< ::grpc::ClientWriter< "
420 "$Request$>>(new ::grpc::ClientWriter< $Request$>("
Craig Tillerc4965752015-02-09 09:51:00 -0800421 "channel(),"
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100422 "::grpc::RpcMethod($prefix$$Service$_method_names[$Idx$], "
yangg5bcea0d2015-01-06 10:35:03 -0800423 "::grpc::RpcMethod::RpcType::CLIENT_STREAMING), "
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800424 "context, response));\n"
yangg5bcea0d2015-01-06 10:35:03 -0800425 "}\n\n");
Yang Gao068c85b2015-02-12 15:21:24 -0800426 printer->Print(*vars,
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800427 "std::unique_ptr< ::grpc::ClientAsyncWriter< $Request$>> "
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100428 "$ns$$Service$::Stub::Async$Method$("
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800429 "::grpc::ClientContext* context, $Response$* response, "
430 "::grpc::CompletionQueue* cq, void* tag) {\n");
431 printer->Print(*vars,
432 " return std::unique_ptr< ::grpc::ClientAsyncWriter< "
433 "$Request$>>(new ::grpc::ClientAsyncWriter< $Request$>("
Yang Gao068c85b2015-02-12 15:21:24 -0800434 "channel(), cq, "
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100435 "::grpc::RpcMethod($prefix$$Service$_method_names[$Idx$], "
Yang Gao068c85b2015-02-12 15:21:24 -0800436 "::grpc::RpcMethod::RpcType::CLIENT_STREAMING), "
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800437 "context, response, tag));\n"
Yang Gao068c85b2015-02-12 15:21:24 -0800438 "}\n\n");
nnobleebebb7e2014-12-10 16:31:01 -0800439 } else if (ServerOnlyStreaming(method)) {
440 printer->Print(
441 *vars,
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800442 "std::unique_ptr< ::grpc::ClientReader< $Response$>> "
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100443 "$ns$$Service$::Stub::$Method$("
Yang Gao07d83042015-02-13 14:11:31 -0800444 "::grpc::ClientContext* context, const $Request$& request) {\n");
yangg5bcea0d2015-01-06 10:35:03 -0800445 printer->Print(*vars,
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800446 " return std::unique_ptr< ::grpc::ClientReader< "
447 "$Response$>>(new ::grpc::ClientReader< $Response$>("
Craig Tillerc4965752015-02-09 09:51:00 -0800448 "channel(),"
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100449 "::grpc::RpcMethod($prefix$$Service$_method_names[$Idx$], "
yangg5bcea0d2015-01-06 10:35:03 -0800450 "::grpc::RpcMethod::RpcType::SERVER_STREAMING), "
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800451 "context, request));\n"
yangg5bcea0d2015-01-06 10:35:03 -0800452 "}\n\n");
Yang Gao068c85b2015-02-12 15:21:24 -0800453 printer->Print(*vars,
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800454 "std::unique_ptr< ::grpc::ClientAsyncReader< $Response$>> "
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100455 "$ns$$Service$::Stub::Async$Method$("
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800456 "::grpc::ClientContext* context, const $Request$& request, "
457 "::grpc::CompletionQueue* cq, void* tag) {\n");
458 printer->Print(*vars,
459 " return std::unique_ptr< ::grpc::ClientAsyncReader< "
460 "$Response$>>(new ::grpc::ClientAsyncReader< $Response$>("
Yang Gao068c85b2015-02-12 15:21:24 -0800461 "channel(), cq, "
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100462 "::grpc::RpcMethod($prefix$$Service$_method_names[$Idx$], "
Yang Gao068c85b2015-02-12 15:21:24 -0800463 "::grpc::RpcMethod::RpcType::SERVER_STREAMING), "
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800464 "context, request, tag));\n"
Yang Gao068c85b2015-02-12 15:21:24 -0800465 "}\n\n");
nnobleebebb7e2014-12-10 16:31:01 -0800466 } else if (BidiStreaming(method)) {
467 printer->Print(
468 *vars,
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800469 "std::unique_ptr< ::grpc::ClientReaderWriter< $Request$, $Response$>> "
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100470 "$ns$$Service$::Stub::$Method$(::grpc::ClientContext* context) {\n");
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800471 printer->Print(*vars,
472 " return std::unique_ptr< ::grpc::ClientReaderWriter< "
473 "$Request$, $Response$>>(new ::grpc::ClientReaderWriter< "
474 "$Request$, $Response$>("
475 "channel(),"
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100476 "::grpc::RpcMethod($prefix$$Service$_method_names[$Idx$], "
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800477 "::grpc::RpcMethod::RpcType::BIDI_STREAMING), "
478 "context));\n"
479 "}\n\n");
480 printer->Print(*vars,
481 "std::unique_ptr< ::grpc::ClientAsyncReaderWriter< "
482 "$Request$, $Response$>> "
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100483 "$ns$$Service$::Stub::Async$Method$(::grpc::ClientContext* context, "
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800484 "::grpc::CompletionQueue* cq, void* tag) {\n");
485 printer->Print(*vars,
486 " return std::unique_ptr< ::grpc::ClientAsyncReaderWriter< "
487 "$Request$, $Response$>>(new "
488 "::grpc::ClientAsyncReaderWriter< $Request$, $Response$>("
489 "channel(), cq, "
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100490 "::grpc::RpcMethod($prefix$$Service$_method_names[$Idx$], "
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800491 "::grpc::RpcMethod::RpcType::BIDI_STREAMING), "
492 "context, tag));\n"
493 "}\n\n");
nnobleebebb7e2014-12-10 16:31:01 -0800494 }
495}
496
Nicolas Nobled446eb82015-03-12 17:22:33 -0700497void PrintSourceServerMethod(grpc::protobuf::io::Printer *printer,
498 const grpc::protobuf::MethodDescriptor *method,
499 std::map<grpc::string, grpc::string> *vars) {
nnobleebebb7e2014-12-10 16:31:01 -0800500 (*vars)["Method"] = method->name();
501 (*vars)["Request"] =
502 grpc_cpp_generator::ClassName(method->input_type(), true);
503 (*vars)["Response"] =
504 grpc_cpp_generator::ClassName(method->output_type(), true);
505 if (NoStreaming(method)) {
506 printer->Print(*vars,
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100507 "::grpc::Status $ns$$Service$::Service::$Method$("
yangga4b6f5d2014-12-17 15:53:12 -0800508 "::grpc::ServerContext* context, "
nnobleebebb7e2014-12-10 16:31:01 -0800509 "const $Request$* request, $Response$* response) {\n");
510 printer->Print(
511 " return ::grpc::Status("
512 "::grpc::StatusCode::UNIMPLEMENTED);\n");
513 printer->Print("}\n\n");
514 } else if (ClientOnlyStreaming(method)) {
515 printer->Print(*vars,
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100516 "::grpc::Status $ns$$Service$::Service::$Method$("
yangga4b6f5d2014-12-17 15:53:12 -0800517 "::grpc::ServerContext* context, "
Yang Gao1ff11f62015-01-14 11:45:32 -0800518 "::grpc::ServerReader< $Request$>* reader, "
nnobleebebb7e2014-12-10 16:31:01 -0800519 "$Response$* response) {\n");
520 printer->Print(
521 " return ::grpc::Status("
522 "::grpc::StatusCode::UNIMPLEMENTED);\n");
523 printer->Print("}\n\n");
524 } else if (ServerOnlyStreaming(method)) {
525 printer->Print(*vars,
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100526 "::grpc::Status $ns$$Service$::Service::$Method$("
yangga4b6f5d2014-12-17 15:53:12 -0800527 "::grpc::ServerContext* context, "
nnobleebebb7e2014-12-10 16:31:01 -0800528 "const $Request$* request, "
Yang Gao1ff11f62015-01-14 11:45:32 -0800529 "::grpc::ServerWriter< $Response$>* writer) {\n");
nnobleebebb7e2014-12-10 16:31:01 -0800530 printer->Print(
531 " return ::grpc::Status("
532 "::grpc::StatusCode::UNIMPLEMENTED);\n");
533 printer->Print("}\n\n");
534 } else if (BidiStreaming(method)) {
535 printer->Print(*vars,
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100536 "::grpc::Status $ns$$Service$::Service::$Method$("
yangga4b6f5d2014-12-17 15:53:12 -0800537 "::grpc::ServerContext* context, "
Yang Gao1ff11f62015-01-14 11:45:32 -0800538 "::grpc::ServerReaderWriter< $Response$, $Request$>* "
nnobleebebb7e2014-12-10 16:31:01 -0800539 "stream) {\n");
540 printer->Print(
541 " return ::grpc::Status("
542 "::grpc::StatusCode::UNIMPLEMENTED);\n");
543 printer->Print("}\n\n");
544 }
545}
546
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800547void PrintSourceServerAsyncMethod(
Nicolas Nobled446eb82015-03-12 17:22:33 -0700548 grpc::protobuf::io::Printer *printer,
549 const grpc::protobuf::MethodDescriptor *method,
550 std::map<grpc::string, grpc::string> *vars) {
Craig Tiller225f7be2015-02-09 22:32:37 -0800551 (*vars)["Method"] = method->name();
552 (*vars)["Request"] =
553 grpc_cpp_generator::ClassName(method->input_type(), true);
554 (*vars)["Response"] =
555 grpc_cpp_generator::ClassName(method->output_type(), true);
556 if (NoStreaming(method)) {
557 printer->Print(*vars,
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100558 "void $ns$$Service$::AsyncService::Request$Method$("
Craig Tiller225f7be2015-02-09 22:32:37 -0800559 "::grpc::ServerContext* context, "
560 "$Request$* request, "
561 "::grpc::ServerAsyncResponseWriter< $Response$>* response, "
562 "::grpc::CompletionQueue* cq, void* tag) {\n");
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800563 printer->Print(
564 *vars,
565 " AsynchronousService::RequestAsyncUnary($Idx$, context, request, response, cq, tag);\n");
Craig Tiller225f7be2015-02-09 22:32:37 -0800566 printer->Print("}\n\n");
567 } else if (ClientOnlyStreaming(method)) {
568 printer->Print(*vars,
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100569 "void $ns$$Service$::AsyncService::Request$Method$("
Craig Tiller225f7be2015-02-09 22:32:37 -0800570 "::grpc::ServerContext* context, "
Yang Gao005f18a2015-02-13 10:22:33 -0800571 "::grpc::ServerAsyncReader< $Response$, $Request$>* reader, "
Craig Tiller225f7be2015-02-09 22:32:37 -0800572 "::grpc::CompletionQueue* cq, void* tag) {\n");
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800573 printer->Print(
574 *vars,
575 " AsynchronousService::RequestClientStreaming($Idx$, context, reader, cq, tag);\n");
Craig Tiller225f7be2015-02-09 22:32:37 -0800576 printer->Print("}\n\n");
577 } else if (ServerOnlyStreaming(method)) {
578 printer->Print(*vars,
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100579 "void $ns$$Service$::AsyncService::Request$Method$("
Craig Tiller225f7be2015-02-09 22:32:37 -0800580 "::grpc::ServerContext* context, "
581 "$Request$* request, "
582 "::grpc::ServerAsyncWriter< $Response$>* writer, "
583 "::grpc::CompletionQueue* cq, void* tag) {\n");
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800584 printer->Print(
585 *vars,
586 " AsynchronousService::RequestServerStreaming($Idx$, context, request, writer, cq, tag);\n");
Craig Tiller225f7be2015-02-09 22:32:37 -0800587 printer->Print("}\n\n");
588 } else if (BidiStreaming(method)) {
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800589 printer->Print(
590 *vars,
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100591 "void $ns$$Service$::AsyncService::Request$Method$("
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800592 "::grpc::ServerContext* context, "
593 "::grpc::ServerAsyncReaderWriter< $Response$, $Request$>* stream, "
594 "::grpc::CompletionQueue* cq, void *tag) {\n");
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800595 printer->Print(
596 *vars,
597 " AsynchronousService::RequestBidiStreaming($Idx$, context, stream, cq, tag);\n");
Craig Tiller225f7be2015-02-09 22:32:37 -0800598 printer->Print("}\n\n");
599 }
600}
601
Nicolas Nobled446eb82015-03-12 17:22:33 -0700602void PrintSourceService(grpc::protobuf::io::Printer *printer,
603 const grpc::protobuf::ServiceDescriptor *service,
604 std::map<grpc::string, grpc::string> *vars) {
nnobleebebb7e2014-12-10 16:31:01 -0800605 (*vars)["Service"] = service->name();
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800606
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100607 printer->Print(*vars, "static const char* $prefix$$Service$_method_names[] = {\n");
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800608 for (int i = 0; i < service->method_count(); ++i) {
609 (*vars)["Method"] = service->method(i)->name();
610 printer->Print(*vars, " \"/$Package$$Service$/$Method$\",\n");
611 }
612 printer->Print(*vars, "};\n\n");
613
Yang Gao5680ff42015-01-14 12:14:21 -0800614 printer->Print(
615 *vars,
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100616 "std::unique_ptr< $ns$$Service$::Stub> $ns$$Service$::NewStub("
Yang Gao5680ff42015-01-14 12:14:21 -0800617 "const std::shared_ptr< ::grpc::ChannelInterface>& channel) {\n"
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100618 " std::unique_ptr< $ns$$Service$::Stub> stub(new $ns$$Service$::Stub());\n"
Yang Gao5680ff42015-01-14 12:14:21 -0800619 " stub->set_channel(channel);\n"
620 " return stub;\n"
Todd Poynor2a6fd262015-02-26 20:04:26 -0800621 "}\n\n");
nnobleebebb7e2014-12-10 16:31:01 -0800622 for (int i = 0; i < service->method_count(); ++i) {
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800623 (*vars)["Idx"] = as_string(i);
nnobleebebb7e2014-12-10 16:31:01 -0800624 PrintSourceClientMethod(printer, service->method(i), vars);
625 }
626
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800627 (*vars)["MethodCount"] = as_string(service->method_count());
628 printer->Print(
629 *vars,
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100630 "$ns$$Service$::AsyncService::AsyncService(::grpc::CompletionQueue* cq) : "
631 "::grpc::AsynchronousService(cq, $prefix$$Service$_method_names, $MethodCount$) "
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800632 "{}\n\n");
633
nnobleebebb7e2014-12-10 16:31:01 -0800634 printer->Print(*vars,
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100635 "$ns$$Service$::Service::~Service() {\n"
nnobleebebb7e2014-12-10 16:31:01 -0800636 " delete service_;\n"
637 "}\n\n");
638 for (int i = 0; i < service->method_count(); ++i) {
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800639 (*vars)["Idx"] = as_string(i);
nnobleebebb7e2014-12-10 16:31:01 -0800640 PrintSourceServerMethod(printer, service->method(i), vars);
Craig Tiller225f7be2015-02-09 22:32:37 -0800641 PrintSourceServerAsyncMethod(printer, service->method(i), vars);
nnobleebebb7e2014-12-10 16:31:01 -0800642 }
643 printer->Print(*vars,
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100644 "::grpc::RpcService* $ns$$Service$::Service::service() {\n");
nnobleebebb7e2014-12-10 16:31:01 -0800645 printer->Indent();
Craig Tillerb5dcec52015-01-13 11:13:42 -0800646 printer->Print(
647 "if (service_ != nullptr) {\n"
648 " return service_;\n"
649 "}\n");
nnobleebebb7e2014-12-10 16:31:01 -0800650 printer->Print("service_ = new ::grpc::RpcService();\n");
651 for (int i = 0; i < service->method_count(); ++i) {
Nicolas Nobled446eb82015-03-12 17:22:33 -0700652 const grpc::protobuf::MethodDescriptor *method = service->method(i);
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800653 (*vars)["Idx"] = as_string(i);
nnobleebebb7e2014-12-10 16:31:01 -0800654 (*vars)["Method"] = method->name();
655 (*vars)["Request"] =
656 grpc_cpp_generator::ClassName(method->input_type(), true);
657 (*vars)["Response"] =
658 grpc_cpp_generator::ClassName(method->output_type(), true);
659 if (NoStreaming(method)) {
660 printer->Print(
661 *vars,
662 "service_->AddMethod(new ::grpc::RpcServiceMethod(\n"
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100663 " $prefix$$Service$_method_names[$Idx$],\n"
yangg5bcea0d2015-01-06 10:35:03 -0800664 " ::grpc::RpcMethod::NORMAL_RPC,\n"
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100665 " new ::grpc::RpcMethodHandler< $ns$$Service$::Service, $Request$, "
nnobleebebb7e2014-12-10 16:31:01 -0800666 "$Response$>(\n"
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100667 " std::function< ::grpc::Status($ns$$Service$::Service*, "
yangga4b6f5d2014-12-17 15:53:12 -0800668 "::grpc::ServerContext*, const $Request$*, $Response$*)>("
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100669 "&$ns$$Service$::Service::$Method$), this),\n"
nnobleebebb7e2014-12-10 16:31:01 -0800670 " new $Request$, new $Response$));\n");
671 } else if (ClientOnlyStreaming(method)) {
672 printer->Print(
673 *vars,
674 "service_->AddMethod(new ::grpc::RpcServiceMethod(\n"
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100675 " $prefix$$Service$_method_names[$Idx$],\n"
yangg5bcea0d2015-01-06 10:35:03 -0800676 " ::grpc::RpcMethod::CLIENT_STREAMING,\n"
Yang Gao1ff11f62015-01-14 11:45:32 -0800677 " new ::grpc::ClientStreamingHandler< "
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100678 "$ns$$Service$::Service, $Request$, $Response$>(\n"
679 " std::function< ::grpc::Status($ns$$Service$::Service*, "
yangga4b6f5d2014-12-17 15:53:12 -0800680 "::grpc::ServerContext*, "
Yang Gao1ff11f62015-01-14 11:45:32 -0800681 "::grpc::ServerReader< $Request$>*, $Response$*)>("
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100682 "&$ns$$Service$::Service::$Method$), this),\n"
nnobleebebb7e2014-12-10 16:31:01 -0800683 " new $Request$, new $Response$));\n");
684 } else if (ServerOnlyStreaming(method)) {
685 printer->Print(
686 *vars,
687 "service_->AddMethod(new ::grpc::RpcServiceMethod(\n"
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100688 " $prefix$$Service$_method_names[$Idx$],\n"
yangg5bcea0d2015-01-06 10:35:03 -0800689 " ::grpc::RpcMethod::SERVER_STREAMING,\n"
Yang Gao1ff11f62015-01-14 11:45:32 -0800690 " new ::grpc::ServerStreamingHandler< "
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100691 "$ns$$Service$::Service, $Request$, $Response$>(\n"
692 " std::function< ::grpc::Status($ns$$Service$::Service*, "
yangga4b6f5d2014-12-17 15:53:12 -0800693 "::grpc::ServerContext*, "
Yang Gao1ff11f62015-01-14 11:45:32 -0800694 "const $Request$*, ::grpc::ServerWriter< $Response$>*)>("
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100695 "&$ns$$Service$::Service::$Method$), this),\n"
nnobleebebb7e2014-12-10 16:31:01 -0800696 " new $Request$, new $Response$));\n");
697 } else if (BidiStreaming(method)) {
698 printer->Print(
699 *vars,
700 "service_->AddMethod(new ::grpc::RpcServiceMethod(\n"
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100701 " $prefix$$Service$_method_names[$Idx$],\n"
yangg5bcea0d2015-01-06 10:35:03 -0800702 " ::grpc::RpcMethod::BIDI_STREAMING,\n"
Yang Gao1ff11f62015-01-14 11:45:32 -0800703 " new ::grpc::BidiStreamingHandler< "
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100704 "$ns$$Service$::Service, $Request$, $Response$>(\n"
705 " std::function< ::grpc::Status($ns$$Service$::Service*, "
yangga4b6f5d2014-12-17 15:53:12 -0800706 "::grpc::ServerContext*, "
Yang Gao1ff11f62015-01-14 11:45:32 -0800707 "::grpc::ServerReaderWriter< $Response$, $Request$>*)>("
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100708 "&$ns$$Service$::Service::$Method$), this),\n"
nnobleebebb7e2014-12-10 16:31:01 -0800709 " new $Request$, new $Response$));\n");
710 }
711 }
712 printer->Print("return service_;\n");
713 printer->Outdent();
714 printer->Print("}\n\n");
715}
716
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100717grpc::string GetSourceServices(const grpc::protobuf::FileDescriptor *file,
718 const Parameters &params) {
Nicolas Nobled446eb82015-03-12 17:22:33 -0700719 grpc::string output;
720 grpc::protobuf::io::StringOutputStream output_stream(&output);
721 grpc::protobuf::io::Printer printer(&output_stream, '$');
722 std::map<grpc::string, grpc::string> vars;
yangg5bcea0d2015-01-06 10:35:03 -0800723 // Package string is empty or ends with a dot. It is used to fully qualify
724 // method names.
725 vars["Package"] = file->package();
726 if (!file->package().empty()) {
727 vars["Package"].append(".");
728 }
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100729 if (!params.services_namespace.empty()) {
730 vars["ns"] = params.services_namespace + "::";
731 vars["prefix"] = params.services_namespace;
732 } else {
733 vars["ns"] = "";
734 vars["prefix"] = "";
735 }
nnobleebebb7e2014-12-10 16:31:01 -0800736
737 for (int i = 0; i < file->service_count(); ++i) {
samuelwca9f3592014-12-15 14:22:04 -0800738 PrintSourceService(&printer, file->service(i), &vars);
nnobleebebb7e2014-12-10 16:31:01 -0800739 printer.Print("\n");
740 }
741 return output;
742}
743
Craig Tiller190d3602015-02-18 09:23:38 -0800744} // namespace grpc_cpp_generator