blob: 3c8ca8ab45dc0880f6a71d679502cef0aac6eacc [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
Craig Tiller277d3cf2015-04-14 14:04:51 -070069grpc::string FilenameIdentifier(const grpc::string &filename) {
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +020070 grpc::string result;
71 for (unsigned i = 0; i < filename.size(); i++) {
72 char c = filename[i];
73 if (isalnum(c)) {
74 result.push_back(c);
75 } else {
76 static char hex[] = "0123456789abcdef";
77 result.push_back('_');
78 result.push_back(hex[(c >> 4) & 0xf]);
79 result.push_back(hex[c & 0xf]);
80 }
81 }
82 return result;
83}
nnobleebebb7e2014-12-10 16:31:01 -080084} // namespace
85
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +020086grpc::string GetHeaderPrologue(const grpc::protobuf::FileDescriptor *file,
87 const Parameters &params) {
88 grpc::string output;
Jan Tattermusch5dcebd92015-05-27 15:30:59 -070089 {
90 // Scope the output stream so it closes and finalizes output to the string.
91 grpc::protobuf::io::StringOutputStream output_stream(&output);
92 grpc::protobuf::io::Printer printer(&output_stream, '$');
93 std::map<grpc::string, grpc::string> vars;
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +020094
Jan Tattermusch5dcebd92015-05-27 15:30:59 -070095 vars["filename"] = file->name();
96 vars["filename_identifier"] = FilenameIdentifier(file->name());
97 vars["filename_base"] = grpc_generator::StripProto(file->name());
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +020098
Jan Tattermusch5dcebd92015-05-27 15:30:59 -070099 printer.Print(vars, "// Generated by the gRPC protobuf plugin.\n");
Craig Tillerce40de52015-06-05 07:14:58 -0700100 printer.Print(vars,
101 "// If you make any local change, they will be lost.\n");
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700102 printer.Print(vars, "// source: $filename$\n");
103 printer.Print(vars, "#ifndef GRPC_$filename_identifier$__INCLUDED\n");
104 printer.Print(vars, "#define GRPC_$filename_identifier$__INCLUDED\n");
105 printer.Print(vars, "\n");
106 printer.Print(vars, "#include \"$filename_base$.pb.h\"\n");
107 printer.Print(vars, "\n");
108 }
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200109 return output;
110}
111
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100112grpc::string GetHeaderIncludes(const grpc::protobuf::FileDescriptor *file,
113 const Parameters &params) {
Nicolas Nobled446eb82015-03-12 17:22:33 -0700114 grpc::string temp =
yang-g9fb35a52015-08-21 15:49:35 -0700115 "#include <grpc++/support/async_stream.h>\n"
Craig Tillerbd6c6182015-04-10 17:08:15 -0700116 "#include <grpc++/impl/rpc_method.h>\n"
Craig Tiller50a7a682015-06-04 12:53:40 -0700117 "#include <grpc++/impl/proto_utils.h>\n"
Craig Tiller14a65f92015-02-09 13:13:14 -0800118 "#include <grpc++/impl/service_type.h>\n"
yang-g9e2f90c2015-08-21 15:35:03 -0700119 "#include <grpc++/support/async_unary_call.h>\n"
120 "#include <grpc++/support/status.h>\n"
yang-g9e2f90c2015-08-21 15:35:03 -0700121 "#include <grpc++/support/stub_options.h>\n"
yang-g9fb35a52015-08-21 15:49:35 -0700122 "#include <grpc++/support/sync_stream.h>\n"
nnobleebebb7e2014-12-10 16:31:01 -0800123 "\n"
124 "namespace grpc {\n"
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800125 "class CompletionQueue;\n"
yang-g8c2be9f2015-08-19 16:28:09 -0700126 "class Channel;\n"
yangga4b6f5d2014-12-17 15:53:12 -0800127 "class RpcService;\n"
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700128 "class ServerCompletionQueue;\n"
Yang Gaoc6924c82015-05-05 10:42:51 -0700129 "class ServerContext;\n"
130 "} // namespace grpc\n\n";
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200131
Yang Gao1dc1a432015-04-10 13:53:11 -0700132 if (!file->package().empty()) {
133 std::vector<grpc::string> parts =
134 grpc_generator::tokenize(file->package(), ".");
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200135
Yang Gao1dc1a432015-04-10 13:53:11 -0700136 for (auto part = parts.begin(); part != parts.end(); part++) {
137 temp.append("namespace ");
138 temp.append(*part);
139 temp.append(" {\n");
140 }
141 temp.append("\n");
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200142 }
143
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200144 return temp;
nnobleebebb7e2014-12-10 16:31:01 -0800145}
146
Craig Tillerce40de52015-06-05 07:14:58 -0700147void PrintHeaderClientMethodInterfaces(
148 grpc::protobuf::io::Printer *printer,
149 const grpc::protobuf::MethodDescriptor *method,
150 std::map<grpc::string, grpc::string> *vars, bool is_public) {
nnobleebebb7e2014-12-10 16:31:01 -0800151 (*vars)["Method"] = method->name();
152 (*vars)["Request"] =
153 grpc_cpp_generator::ClassName(method->input_type(), true);
154 (*vars)["Response"] =
155 grpc_cpp_generator::ClassName(method->output_type(), true);
Yang Gaoc6924c82015-05-05 10:42:51 -0700156
157 if (is_public) {
158 if (NoStreaming(method)) {
159 printer->Print(
160 *vars,
161 "virtual ::grpc::Status $Method$(::grpc::ClientContext* context, "
162 "const $Request$& request, $Response$* response) = 0;\n");
Craig Tillerce40de52015-06-05 07:14:58 -0700163 printer->Print(*vars,
164 "std::unique_ptr< "
165 "::grpc::ClientAsyncResponseReaderInterface< $Response$>> "
166 "Async$Method$(::grpc::ClientContext* context, "
167 "const $Request$& request, "
168 "::grpc::CompletionQueue* cq) {\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700169 printer->Indent();
Craig Tillerce40de52015-06-05 07:14:58 -0700170 printer->Print(*vars,
171 "return std::unique_ptr< "
172 "::grpc::ClientAsyncResponseReaderInterface< $Response$>>("
173 "Async$Method$Raw(context, request, cq));\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700174 printer->Outdent();
175 printer->Print("}\n");
176 } else if (ClientOnlyStreaming(method)) {
177 printer->Print(
178 *vars,
179 "std::unique_ptr< ::grpc::ClientWriterInterface< $Request$>>"
180 " $Method$("
181 "::grpc::ClientContext* context, $Response$* response) {\n");
182 printer->Indent();
183 printer->Print(
184 *vars,
185 "return std::unique_ptr< ::grpc::ClientWriterInterface< $Request$>>"
186 "($Method$Raw(context, response));\n");
187 printer->Outdent();
188 printer->Print("}\n");
189 printer->Print(
190 *vars,
191 "std::unique_ptr< ::grpc::ClientAsyncWriterInterface< $Request$>>"
Craig Tillerce40de52015-06-05 07:14:58 -0700192 " Async$Method$(::grpc::ClientContext* context, $Response$* "
193 "response, "
Yang Gaoc6924c82015-05-05 10:42:51 -0700194 "::grpc::CompletionQueue* cq, void* tag) {\n");
195 printer->Indent();
Craig Tillerce40de52015-06-05 07:14:58 -0700196 printer->Print(*vars,
197 "return std::unique_ptr< "
198 "::grpc::ClientAsyncWriterInterface< $Request$>>("
199 "Async$Method$Raw(context, response, cq, tag));\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700200 printer->Outdent();
201 printer->Print("}\n");
202 } else if (ServerOnlyStreaming(method)) {
203 printer->Print(
204 *vars,
205 "std::unique_ptr< ::grpc::ClientReaderInterface< $Response$>>"
206 " $Method$(::grpc::ClientContext* context, const $Request$& request)"
207 " {\n");
208 printer->Indent();
209 printer->Print(
210 *vars,
211 "return std::unique_ptr< ::grpc::ClientReaderInterface< $Response$>>"
212 "($Method$Raw(context, request));\n");
213 printer->Outdent();
214 printer->Print("}\n");
215 printer->Print(
216 *vars,
217 "std::unique_ptr< ::grpc::ClientAsyncReaderInterface< $Response$>> "
218 "Async$Method$("
219 "::grpc::ClientContext* context, const $Request$& request, "
220 "::grpc::CompletionQueue* cq, void* tag) {\n");
221 printer->Indent();
Craig Tillerce40de52015-06-05 07:14:58 -0700222 printer->Print(*vars,
223 "return std::unique_ptr< "
224 "::grpc::ClientAsyncReaderInterface< $Response$>>("
225 "Async$Method$Raw(context, request, cq, tag));\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700226 printer->Outdent();
227 printer->Print("}\n");
228 } else if (BidiStreaming(method)) {
Craig Tillerce40de52015-06-05 07:14:58 -0700229 printer->Print(*vars,
230 "std::unique_ptr< ::grpc::ClientReaderWriterInterface< "
231 "$Request$, $Response$>> "
232 "$Method$(::grpc::ClientContext* context) {\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700233 printer->Indent();
234 printer->Print(
235 *vars,
236 "return std::unique_ptr< "
237 "::grpc::ClientReaderWriterInterface< $Request$, $Response$>>("
238 "$Method$Raw(context));\n");
239 printer->Outdent();
240 printer->Print("}\n");
241 printer->Print(
242 *vars,
243 "std::unique_ptr< "
244 "::grpc::ClientAsyncReaderWriterInterface< $Request$, $Response$>> "
245 "Async$Method$(::grpc::ClientContext* context, "
246 "::grpc::CompletionQueue* cq, void* tag) {\n");
247 printer->Indent();
248 printer->Print(
249 *vars,
250 "return std::unique_ptr< "
251 "::grpc::ClientAsyncReaderWriterInterface< $Request$, $Response$>>("
252 "Async$Method$Raw(context, cq, tag));\n");
253 printer->Outdent();
254 printer->Print("}\n");
255 }
256 } else {
257 if (NoStreaming(method)) {
258 printer->Print(
259 *vars,
260 "virtual ::grpc::ClientAsyncResponseReaderInterface< $Response$>* "
261 "Async$Method$Raw(::grpc::ClientContext* context, "
262 "const $Request$& request, "
Craig Tiller5f871ac2015-05-08 13:05:51 -0700263 "::grpc::CompletionQueue* cq) = 0;\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700264 } else if (ClientOnlyStreaming(method)) {
265 printer->Print(
266 *vars,
267 "virtual ::grpc::ClientWriterInterface< $Request$>*"
268 " $Method$Raw("
269 "::grpc::ClientContext* context, $Response$* response) = 0;\n");
Craig Tillerce40de52015-06-05 07:14:58 -0700270 printer->Print(*vars,
271 "virtual ::grpc::ClientAsyncWriterInterface< $Request$>*"
272 " Async$Method$Raw(::grpc::ClientContext* context, "
273 "$Response$* response, "
274 "::grpc::CompletionQueue* cq, void* tag) = 0;\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700275 } else if (ServerOnlyStreaming(method)) {
276 printer->Print(
277 *vars,
278 "virtual ::grpc::ClientReaderInterface< $Response$>* $Method$Raw("
279 "::grpc::ClientContext* context, const $Request$& request) = 0;\n");
280 printer->Print(
281 *vars,
282 "virtual ::grpc::ClientAsyncReaderInterface< $Response$>* "
283 "Async$Method$Raw("
284 "::grpc::ClientContext* context, const $Request$& request, "
285 "::grpc::CompletionQueue* cq, void* tag) = 0;\n");
286 } else if (BidiStreaming(method)) {
Craig Tillerce40de52015-06-05 07:14:58 -0700287 printer->Print(*vars,
288 "virtual ::grpc::ClientReaderWriterInterface< $Request$, "
289 "$Response$>* "
290 "$Method$Raw(::grpc::ClientContext* context) = 0;\n");
291 printer->Print(*vars,
292 "virtual ::grpc::ClientAsyncReaderWriterInterface< "
293 "$Request$, $Response$>* "
294 "Async$Method$Raw(::grpc::ClientContext* context, "
295 "::grpc::CompletionQueue* cq, void* tag) = 0;\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700296 }
297 }
298}
299
300void PrintHeaderClientMethod(grpc::protobuf::io::Printer *printer,
301 const grpc::protobuf::MethodDescriptor *method,
302 std::map<grpc::string, grpc::string> *vars,
303 bool is_public) {
304 (*vars)["Method"] = method->name();
305 (*vars)["Request"] =
306 grpc_cpp_generator::ClassName(method->input_type(), true);
307 (*vars)["Response"] =
308 grpc_cpp_generator::ClassName(method->output_type(), true);
Yang Gaoc6924c82015-05-05 10:42:51 -0700309 if (is_public) {
310 if (NoStreaming(method)) {
311 printer->Print(
312 *vars,
313 "::grpc::Status $Method$(::grpc::ClientContext* context, "
314 "const $Request$& request, $Response$* response) GRPC_OVERRIDE;\n");
315 printer->Print(
316 *vars,
317 "std::unique_ptr< ::grpc::ClientAsyncResponseReader< $Response$>> "
318 "Async$Method$(::grpc::ClientContext* context, "
319 "const $Request$& request, "
Craig Tiller5f871ac2015-05-08 13:05:51 -0700320 "::grpc::CompletionQueue* cq) {\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700321 printer->Indent();
Craig Tillerce40de52015-06-05 07:14:58 -0700322 printer->Print(*vars,
323 "return std::unique_ptr< "
324 "::grpc::ClientAsyncResponseReader< $Response$>>("
325 "Async$Method$Raw(context, request, cq));\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700326 printer->Outdent();
327 printer->Print("}\n");
328 } else if (ClientOnlyStreaming(method)) {
329 printer->Print(
330 *vars,
331 "std::unique_ptr< ::grpc::ClientWriter< $Request$>>"
332 " $Method$("
333 "::grpc::ClientContext* context, $Response$* response) {\n");
334 printer->Indent();
Craig Tillerce40de52015-06-05 07:14:58 -0700335 printer->Print(*vars,
336 "return std::unique_ptr< ::grpc::ClientWriter< $Request$>>"
337 "($Method$Raw(context, response));\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700338 printer->Outdent();
339 printer->Print("}\n");
Craig Tillerce40de52015-06-05 07:14:58 -0700340 printer->Print(*vars,
341 "std::unique_ptr< ::grpc::ClientAsyncWriter< $Request$>>"
342 " Async$Method$(::grpc::ClientContext* context, "
343 "$Response$* response, "
344 "::grpc::CompletionQueue* cq, void* tag) {\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700345 printer->Indent();
346 printer->Print(
347 *vars,
348 "return std::unique_ptr< ::grpc::ClientAsyncWriter< $Request$>>("
349 "Async$Method$Raw(context, response, cq, tag));\n");
350 printer->Outdent();
351 printer->Print("}\n");
352 } else if (ServerOnlyStreaming(method)) {
353 printer->Print(
354 *vars,
355 "std::unique_ptr< ::grpc::ClientReader< $Response$>>"
356 " $Method$(::grpc::ClientContext* context, const $Request$& request)"
357 " {\n");
358 printer->Indent();
359 printer->Print(
360 *vars,
361 "return std::unique_ptr< ::grpc::ClientReader< $Response$>>"
362 "($Method$Raw(context, request));\n");
363 printer->Outdent();
364 printer->Print("}\n");
365 printer->Print(
366 *vars,
367 "std::unique_ptr< ::grpc::ClientAsyncReader< $Response$>> "
368 "Async$Method$("
369 "::grpc::ClientContext* context, const $Request$& request, "
370 "::grpc::CompletionQueue* cq, void* tag) {\n");
371 printer->Indent();
372 printer->Print(
373 *vars,
374 "return std::unique_ptr< ::grpc::ClientAsyncReader< $Response$>>("
375 "Async$Method$Raw(context, request, cq, tag));\n");
376 printer->Outdent();
377 printer->Print("}\n");
378 } else if (BidiStreaming(method)) {
379 printer->Print(
380 *vars,
381 "std::unique_ptr< ::grpc::ClientReaderWriter< $Request$, $Response$>>"
382 " $Method$(::grpc::ClientContext* context) {\n");
383 printer->Indent();
Craig Tillerce40de52015-06-05 07:14:58 -0700384 printer->Print(*vars,
385 "return std::unique_ptr< "
386 "::grpc::ClientReaderWriter< $Request$, $Response$>>("
387 "$Method$Raw(context));\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700388 printer->Outdent();
389 printer->Print("}\n");
Craig Tillerce40de52015-06-05 07:14:58 -0700390 printer->Print(*vars,
391 "std::unique_ptr< ::grpc::ClientAsyncReaderWriter< "
392 "$Request$, $Response$>> "
393 "Async$Method$(::grpc::ClientContext* context, "
394 "::grpc::CompletionQueue* cq, void* tag) {\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700395 printer->Indent();
Craig Tillerce40de52015-06-05 07:14:58 -0700396 printer->Print(*vars,
397 "return std::unique_ptr< "
398 "::grpc::ClientAsyncReaderWriter< $Request$, $Response$>>("
399 "Async$Method$Raw(context, cq, tag));\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700400 printer->Outdent();
401 printer->Print("}\n");
402 }
403 } else {
404 if (NoStreaming(method)) {
Craig Tillerce40de52015-06-05 07:14:58 -0700405 printer->Print(*vars,
406 "::grpc::ClientAsyncResponseReader< $Response$>* "
407 "Async$Method$Raw(::grpc::ClientContext* context, "
408 "const $Request$& request, "
409 "::grpc::CompletionQueue* cq) GRPC_OVERRIDE;\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700410 } else if (ClientOnlyStreaming(method)) {
Craig Tillerce40de52015-06-05 07:14:58 -0700411 printer->Print(*vars,
412 "::grpc::ClientWriter< $Request$>* $Method$Raw("
413 "::grpc::ClientContext* context, $Response$* response) "
414 "GRPC_OVERRIDE;\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700415 printer->Print(
416 *vars,
417 "::grpc::ClientAsyncWriter< $Request$>* Async$Method$Raw("
418 "::grpc::ClientContext* context, $Response$* response, "
419 "::grpc::CompletionQueue* cq, void* tag) GRPC_OVERRIDE;\n");
420 } else if (ServerOnlyStreaming(method)) {
Craig Tillerce40de52015-06-05 07:14:58 -0700421 printer->Print(*vars,
422 "::grpc::ClientReader< $Response$>* $Method$Raw("
423 "::grpc::ClientContext* context, const $Request$& request)"
424 " GRPC_OVERRIDE;\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700425 printer->Print(
426 *vars,
427 "::grpc::ClientAsyncReader< $Response$>* Async$Method$Raw("
428 "::grpc::ClientContext* context, const $Request$& request, "
429 "::grpc::CompletionQueue* cq, void* tag) GRPC_OVERRIDE;\n");
430 } else if (BidiStreaming(method)) {
431 printer->Print(
432 *vars,
433 "::grpc::ClientReaderWriter< $Request$, $Response$>* "
434 "$Method$Raw(::grpc::ClientContext* context) GRPC_OVERRIDE;\n");
435 printer->Print(
436 *vars,
437 "::grpc::ClientAsyncReaderWriter< $Request$, $Response$>* "
438 "Async$Method$Raw(::grpc::ClientContext* context, "
439 "::grpc::CompletionQueue* cq, void* tag) GRPC_OVERRIDE;\n");
440 }
nnobleebebb7e2014-12-10 16:31:01 -0800441 }
442}
443
Craig Tillerbd6c6182015-04-10 17:08:15 -0700444void PrintHeaderClientMethodData(grpc::protobuf::io::Printer *printer,
445 const grpc::protobuf::MethodDescriptor *method,
446 std::map<grpc::string, grpc::string> *vars) {
447 (*vars)["Method"] = method->name();
448 printer->Print(*vars, "const ::grpc::RpcMethod rpcmethod_$Method$_;\n");
449}
450
Craig Tiller277d3cf2015-04-14 14:04:51 -0700451void PrintHeaderServerMethodSync(grpc::protobuf::io::Printer *printer,
452 const grpc::protobuf::MethodDescriptor *method,
453 std::map<grpc::string, grpc::string> *vars) {
nnobleebebb7e2014-12-10 16:31:01 -0800454 (*vars)["Method"] = method->name();
455 (*vars)["Request"] =
456 grpc_cpp_generator::ClassName(method->input_type(), true);
457 (*vars)["Response"] =
458 grpc_cpp_generator::ClassName(method->output_type(), true);
459 if (NoStreaming(method)) {
460 printer->Print(*vars,
yangga4b6f5d2014-12-17 15:53:12 -0800461 "virtual ::grpc::Status $Method$("
462 "::grpc::ServerContext* context, const $Request$* request, "
nnobleebebb7e2014-12-10 16:31:01 -0800463 "$Response$* response);\n");
464 } else if (ClientOnlyStreaming(method)) {
465 printer->Print(*vars,
466 "virtual ::grpc::Status $Method$("
yangga4b6f5d2014-12-17 15:53:12 -0800467 "::grpc::ServerContext* context, "
Yang Gao1ff11f62015-01-14 11:45:32 -0800468 "::grpc::ServerReader< $Request$>* reader, "
nnobleebebb7e2014-12-10 16:31:01 -0800469 "$Response$* response);\n");
470 } else if (ServerOnlyStreaming(method)) {
471 printer->Print(*vars,
yangga4b6f5d2014-12-17 15:53:12 -0800472 "virtual ::grpc::Status $Method$("
473 "::grpc::ServerContext* context, const $Request$* request, "
Yang Gao1ff11f62015-01-14 11:45:32 -0800474 "::grpc::ServerWriter< $Response$>* writer);\n");
nnobleebebb7e2014-12-10 16:31:01 -0800475 } else if (BidiStreaming(method)) {
Yang Gao5680ff42015-01-14 12:14:21 -0800476 printer->Print(
477 *vars,
478 "virtual ::grpc::Status $Method$("
479 "::grpc::ServerContext* context, "
480 "::grpc::ServerReaderWriter< $Response$, $Request$>* stream);"
481 "\n");
nnobleebebb7e2014-12-10 16:31:01 -0800482 }
483}
484
Craig Tiller5ef5db12015-02-09 12:47:21 -0800485void PrintHeaderServerMethodAsync(
Nicolas Nobled446eb82015-03-12 17:22:33 -0700486 grpc::protobuf::io::Printer *printer,
487 const grpc::protobuf::MethodDescriptor *method,
488 std::map<grpc::string, grpc::string> *vars) {
Craig Tiller2dff17d2015-02-09 12:42:23 -0800489 (*vars)["Method"] = method->name();
490 (*vars)["Request"] =
491 grpc_cpp_generator::ClassName(method->input_type(), true);
492 (*vars)["Response"] =
493 grpc_cpp_generator::ClassName(method->output_type(), true);
494 if (NoStreaming(method)) {
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700495 printer->Print(
496 *vars,
497 "void Request$Method$("
498 "::grpc::ServerContext* context, $Request$* request, "
499 "::grpc::ServerAsyncResponseWriter< $Response$>* response, "
500 "::grpc::CompletionQueue* new_call_cq, "
501 "::grpc::ServerCompletionQueue* notification_cq, void *tag);\n");
Craig Tiller2dff17d2015-02-09 12:42:23 -0800502 } else if (ClientOnlyStreaming(method)) {
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700503 printer->Print(
504 *vars,
505 "void Request$Method$("
506 "::grpc::ServerContext* context, "
507 "::grpc::ServerAsyncReader< $Response$, $Request$>* reader, "
508 "::grpc::CompletionQueue* new_call_cq, "
509 "::grpc::ServerCompletionQueue* notification_cq, void *tag);\n");
Craig Tiller2dff17d2015-02-09 12:42:23 -0800510 } else if (ServerOnlyStreaming(method)) {
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700511 printer->Print(
512 *vars,
513 "void Request$Method$("
514 "::grpc::ServerContext* context, $Request$* request, "
515 "::grpc::ServerAsyncWriter< $Response$>* writer, "
516 "::grpc::CompletionQueue* new_call_cq, "
517 "::grpc::ServerCompletionQueue* notification_cq, void *tag);\n");
Craig Tiller2dff17d2015-02-09 12:42:23 -0800518 } else if (BidiStreaming(method)) {
519 printer->Print(
520 *vars,
Yang Gaoca3cb3e2015-02-12 00:05:11 -0800521 "void Request$Method$("
Craig Tiller2dff17d2015-02-09 12:42:23 -0800522 "::grpc::ServerContext* context, "
Craig Tiller225f7be2015-02-09 22:32:37 -0800523 "::grpc::ServerAsyncReaderWriter< $Response$, $Request$>* stream, "
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700524 "::grpc::CompletionQueue* new_call_cq, "
525 "::grpc::ServerCompletionQueue* notification_cq, void *tag);\n");
Craig Tiller2dff17d2015-02-09 12:42:23 -0800526 }
527}
528
Nicolas Nobled446eb82015-03-12 17:22:33 -0700529void PrintHeaderService(grpc::protobuf::io::Printer *printer,
530 const grpc::protobuf::ServiceDescriptor *service,
531 std::map<grpc::string, grpc::string> *vars) {
nnobleebebb7e2014-12-10 16:31:01 -0800532 (*vars)["Service"] = service->name();
533
534 printer->Print(*vars,
Craig Tillercf133f42015-02-26 14:05:56 -0800535 "class $Service$ GRPC_FINAL {\n"
nnobleebebb7e2014-12-10 16:31:01 -0800536 " public:\n");
537 printer->Indent();
538
539 // Client side
Craig Tillerb5dcec52015-01-13 11:13:42 -0800540 printer->Print(
Yang Gao72e0fb82015-05-01 16:24:07 -0700541 "class StubInterface {\n"
Craig Tillerb5dcec52015-01-13 11:13:42 -0800542 " public:\n");
nnobleebebb7e2014-12-10 16:31:01 -0800543 printer->Indent();
Yang Gao72e0fb82015-05-01 16:24:07 -0700544 printer->Print("virtual ~StubInterface() {}\n");
545 for (int i = 0; i < service->method_count(); ++i) {
Yang Gaoc6924c82015-05-05 10:42:51 -0700546 PrintHeaderClientMethodInterfaces(printer, service->method(i), vars, true);
547 }
548 printer->Outdent();
549 printer->Print("private:\n");
550 printer->Indent();
551 for (int i = 0; i < service->method_count(); ++i) {
552 PrintHeaderClientMethodInterfaces(printer, service->method(i), vars, false);
Yang Gao72e0fb82015-05-01 16:24:07 -0700553 }
554 printer->Outdent();
555 printer->Print("};\n");
556 printer->Print(
yang-gef003082015-08-20 11:40:51 -0700557 "class Stub GRPC_FINAL : public StubInterface"
558 " {\n public:\n");
Yang Gao72e0fb82015-05-01 16:24:07 -0700559 printer->Indent();
yang-g8c2be9f2015-08-19 16:28:09 -0700560 printer->Print("Stub(const std::shared_ptr< ::grpc::Channel>& channel);\n");
nnobleebebb7e2014-12-10 16:31:01 -0800561 for (int i = 0; i < service->method_count(); ++i) {
Yang Gaoc6924c82015-05-05 10:42:51 -0700562 PrintHeaderClientMethod(printer, service->method(i), vars, true);
nnobleebebb7e2014-12-10 16:31:01 -0800563 }
564 printer->Outdent();
Yang Gaoc6924c82015-05-05 10:42:51 -0700565 printer->Print("\n private:\n");
Craig Tillerbd6c6182015-04-10 17:08:15 -0700566 printer->Indent();
yang-gef003082015-08-20 11:40:51 -0700567 printer->Print("std::shared_ptr< ::grpc::Channel> channel_;\n");
Craig Tillerbd6c6182015-04-10 17:08:15 -0700568 for (int i = 0; i < service->method_count(); ++i) {
Yang Gaoc6924c82015-05-05 10:42:51 -0700569 PrintHeaderClientMethod(printer, service->method(i), vars, false);
570 }
571 for (int i = 0; i < service->method_count(); ++i) {
Craig Tillerbd6c6182015-04-10 17:08:15 -0700572 PrintHeaderClientMethodData(printer, service->method(i), vars);
573 }
574 printer->Outdent();
nnobleebebb7e2014-12-10 16:31:01 -0800575 printer->Print("};\n");
576 printer->Print(
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800577 "static std::unique_ptr<Stub> NewStub(const std::shared_ptr< "
yang-g8c2be9f2015-08-19 16:28:09 -0700578 "::grpc::Channel>& channel, "
yang-g297a25b2015-08-03 16:43:46 -0700579 "const ::grpc::StubOptions& options = ::grpc::StubOptions());\n");
nnobleebebb7e2014-12-10 16:31:01 -0800580
581 printer->Print("\n");
582
Craig Tiller2dff17d2015-02-09 12:42:23 -0800583 // Server side - Synchronous
Craig Tillerb5dcec52015-01-13 11:13:42 -0800584 printer->Print(
Craig Tiller14a65f92015-02-09 13:13:14 -0800585 "class Service : public ::grpc::SynchronousService {\n"
Craig Tillerb5dcec52015-01-13 11:13:42 -0800586 " public:\n");
nnobleebebb7e2014-12-10 16:31:01 -0800587 printer->Indent();
Vijay Pai7f715702015-10-12 22:47:58 +0000588 printer->Print("Service();\n");
nnobleebebb7e2014-12-10 16:31:01 -0800589 printer->Print("virtual ~Service();\n");
590 for (int i = 0; i < service->method_count(); ++i) {
Craig Tiller2dff17d2015-02-09 12:42:23 -0800591 PrintHeaderServerMethodSync(printer, service->method(i), vars);
592 }
Craig Tillercf133f42015-02-26 14:05:56 -0800593 printer->Print("::grpc::RpcService* service() GRPC_OVERRIDE GRPC_FINAL;\n");
Craig Tiller2dff17d2015-02-09 12:42:23 -0800594 printer->Outdent();
595 printer->Print(
596 " private:\n"
Vijay Pai7f715702015-10-12 22:47:58 +0000597 " std::unique_ptr< ::grpc::RpcService> service_;\n");
Craig Tiller2dff17d2015-02-09 12:42:23 -0800598 printer->Print("};\n");
599
600 // Server side - Asynchronous
601 printer->Print(
Craig Tillercf133f42015-02-26 14:05:56 -0800602 "class AsyncService GRPC_FINAL : public ::grpc::AsynchronousService {\n"
Craig Tiller2dff17d2015-02-09 12:42:23 -0800603 " public:\n");
604 printer->Indent();
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800605 (*vars)["MethodCount"] = as_string(service->method_count());
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700606 printer->Print("explicit AsyncService();\n");
Craig Tiller0220cf12015-02-12 17:39:26 -0800607 printer->Print("~AsyncService() {};\n");
Craig Tiller2dff17d2015-02-09 12:42:23 -0800608 for (int i = 0; i < service->method_count(); ++i) {
609 PrintHeaderServerMethodAsync(printer, service->method(i), vars);
nnobleebebb7e2014-12-10 16:31:01 -0800610 }
nnobleebebb7e2014-12-10 16:31:01 -0800611 printer->Outdent();
nnobleebebb7e2014-12-10 16:31:01 -0800612 printer->Print("};\n");
613
614 printer->Outdent();
615 printer->Print("};\n");
616}
617
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100618grpc::string GetHeaderServices(const grpc::protobuf::FileDescriptor *file,
619 const Parameters &params) {
Nicolas Nobled446eb82015-03-12 17:22:33 -0700620 grpc::string output;
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700621 {
Craig Tillerce40de52015-06-05 07:14:58 -0700622 // Scope the output stream so it closes and finalizes output to the string.
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700623 grpc::protobuf::io::StringOutputStream output_stream(&output);
624 grpc::protobuf::io::Printer printer(&output_stream, '$');
625 std::map<grpc::string, grpc::string> vars;
nnobleebebb7e2014-12-10 16:31:01 -0800626
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700627 if (!params.services_namespace.empty()) {
628 vars["services_namespace"] = params.services_namespace;
629 printer.Print(vars, "\nnamespace $services_namespace$ {\n\n");
630 }
631
632 for (int i = 0; i < file->service_count(); ++i) {
633 PrintHeaderService(&printer, file->service(i), &vars);
634 printer.Print("\n");
635 }
636
637 if (!params.services_namespace.empty()) {
638 printer.Print(vars, "} // namespace $services_namespace$\n\n");
639 }
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100640 }
nnobleebebb7e2014-12-10 16:31:01 -0800641 return output;
642}
643
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200644grpc::string GetHeaderEpilogue(const grpc::protobuf::FileDescriptor *file,
645 const Parameters &params) {
646 grpc::string output;
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700647 {
648 // Scope the output stream so it closes and finalizes output to the string.
649 grpc::protobuf::io::StringOutputStream output_stream(&output);
650 grpc::protobuf::io::Printer printer(&output_stream, '$');
651 std::map<grpc::string, grpc::string> vars;
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200652
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700653 vars["filename"] = file->name();
654 vars["filename_identifier"] = FilenameIdentifier(file->name());
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200655
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700656 if (!file->package().empty()) {
657 std::vector<grpc::string> parts =
658 grpc_generator::tokenize(file->package(), ".");
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200659
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700660 for (auto part = parts.rbegin(); part != parts.rend(); part++) {
661 vars["part"] = *part;
662 printer.Print(vars, "} // namespace $part$\n");
663 }
664 printer.Print(vars, "\n");
Yang Gao1dc1a432015-04-10 13:53:11 -0700665 }
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700666
Yang Gao1dc1a432015-04-10 13:53:11 -0700667 printer.Print(vars, "\n");
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700668 printer.Print(vars, "#endif // GRPC_$filename_identifier$__INCLUDED\n");
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200669 }
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200670 return output;
671}
672
673grpc::string GetSourcePrologue(const grpc::protobuf::FileDescriptor *file,
674 const Parameters &params) {
675 grpc::string output;
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700676 {
677 // Scope the output stream so it closes and finalizes output to the string.
678 grpc::protobuf::io::StringOutputStream output_stream(&output);
679 grpc::protobuf::io::Printer printer(&output_stream, '$');
680 std::map<grpc::string, grpc::string> vars;
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200681
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700682 vars["filename"] = file->name();
683 vars["filename_base"] = grpc_generator::StripProto(file->name());
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200684
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700685 printer.Print(vars, "// Generated by the gRPC protobuf plugin.\n");
Craig Tillerce40de52015-06-05 07:14:58 -0700686 printer.Print(vars,
687 "// If you make any local change, they will be lost.\n");
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700688 printer.Print(vars, "// source: $filename$\n\n");
689 printer.Print(vars, "#include \"$filename_base$.pb.h\"\n");
690 printer.Print(vars, "#include \"$filename_base$.grpc.pb.h\"\n");
691 printer.Print(vars, "\n");
692 }
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200693 return output;
694}
695
696grpc::string GetSourceIncludes(const grpc::protobuf::FileDescriptor *file,
697 const Parameters &param) {
698 grpc::string output;
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700699 {
700 // Scope the output stream so it closes and finalizes output to the string.
701 grpc::protobuf::io::StringOutputStream output_stream(&output);
702 grpc::protobuf::io::Printer printer(&output_stream, '$');
703 std::map<grpc::string, grpc::string> vars;
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200704
yang-g8c2be9f2015-08-19 16:28:09 -0700705 printer.Print(vars, "#include <grpc++/channel.h>\n");
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700706 printer.Print(vars, "#include <grpc++/impl/client_unary_call.h>\n");
707 printer.Print(vars, "#include <grpc++/impl/rpc_service_method.h>\n");
708 printer.Print(vars, "#include <grpc++/impl/service_type.h>\n");
yang-g9e2f90c2015-08-21 15:35:03 -0700709 printer.Print(vars, "#include <grpc++/support/async_unary_call.h>\n");
yang-g9fb35a52015-08-21 15:49:35 -0700710 printer.Print(vars, "#include <grpc++/support/async_stream.h>\n");
711 printer.Print(vars, "#include <grpc++/support/sync_stream.h>\n");
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200712
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700713 if (!file->package().empty()) {
714 std::vector<grpc::string> parts =
715 grpc_generator::tokenize(file->package(), ".");
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200716
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700717 for (auto part = parts.begin(); part != parts.end(); part++) {
718 vars["part"] = *part;
719 printer.Print(vars, "namespace $part$ {\n");
720 }
Yang Gao1dc1a432015-04-10 13:53:11 -0700721 }
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700722
723 printer.Print(vars, "\n");
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200724 }
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200725 return output;
726}
727
Nicolas Nobled446eb82015-03-12 17:22:33 -0700728void PrintSourceClientMethod(grpc::protobuf::io::Printer *printer,
729 const grpc::protobuf::MethodDescriptor *method,
730 std::map<grpc::string, grpc::string> *vars) {
nnobleebebb7e2014-12-10 16:31:01 -0800731 (*vars)["Method"] = method->name();
732 (*vars)["Request"] =
733 grpc_cpp_generator::ClassName(method->input_type(), true);
734 (*vars)["Response"] =
735 grpc_cpp_generator::ClassName(method->output_type(), true);
736 if (NoStreaming(method)) {
737 printer->Print(*vars,
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100738 "::grpc::Status $ns$$Service$::Stub::$Method$("
nnobleebebb7e2014-12-10 16:31:01 -0800739 "::grpc::ClientContext* context, "
740 "const $Request$& request, $Response$* response) {\n");
741 printer->Print(*vars,
yang-gef003082015-08-20 11:40:51 -0700742 " return ::grpc::BlockingUnaryCall(channel_.get(), "
Craig Tillerbd6c6182015-04-10 17:08:15 -0700743 "rpcmethod_$Method$_, "
nnobleebebb7e2014-12-10 16:31:01 -0800744 "context, request, response);\n"
745 "}\n\n");
Yang Gao5680ff42015-01-14 12:14:21 -0800746 printer->Print(
747 *vars,
Yang Gaoc6924c82015-05-05 10:42:51 -0700748 "::grpc::ClientAsyncResponseReader< $Response$>* "
749 "$ns$$Service$::Stub::Async$Method$Raw(::grpc::ClientContext* context, "
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800750 "const $Request$& request, "
Craig Tiller3676b382015-05-06 13:01:05 -0700751 "::grpc::CompletionQueue* cq) {\n");
yangg5bcea0d2015-01-06 10:35:03 -0800752 printer->Print(*vars,
Yang Gaoc6924c82015-05-05 10:42:51 -0700753 " return new "
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800754 "::grpc::ClientAsyncResponseReader< $Response$>("
yang-gef003082015-08-20 11:40:51 -0700755 "channel_.get(), cq, "
Craig Tillerbd6c6182015-04-10 17:08:15 -0700756 "rpcmethod_$Method$_, "
Craig Tiller5f871ac2015-05-08 13:05:51 -0700757 "context, request);\n"
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800758 "}\n\n");
759 } else if (ClientOnlyStreaming(method)) {
760 printer->Print(*vars,
Yang Gaoc6924c82015-05-05 10:42:51 -0700761 "::grpc::ClientWriter< $Request$>* "
762 "$ns$$Service$::Stub::$Method$Raw("
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800763 "::grpc::ClientContext* context, $Response$* response) {\n");
764 printer->Print(*vars,
Yang Gaoc6924c82015-05-05 10:42:51 -0700765 " return new ::grpc::ClientWriter< $Request$>("
yang-gef003082015-08-20 11:40:51 -0700766 "channel_.get(), "
Craig Tillerbd6c6182015-04-10 17:08:15 -0700767 "rpcmethod_$Method$_, "
Yang Gaoc6924c82015-05-05 10:42:51 -0700768 "context, response);\n"
yangg5bcea0d2015-01-06 10:35:03 -0800769 "}\n\n");
Yang Gao068c85b2015-02-12 15:21:24 -0800770 printer->Print(*vars,
Yang Gaoc6924c82015-05-05 10:42:51 -0700771 "::grpc::ClientAsyncWriter< $Request$>* "
772 "$ns$$Service$::Stub::Async$Method$Raw("
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800773 "::grpc::ClientContext* context, $Response$* response, "
774 "::grpc::CompletionQueue* cq, void* tag) {\n");
775 printer->Print(*vars,
Yang Gaoc6924c82015-05-05 10:42:51 -0700776 " return new ::grpc::ClientAsyncWriter< $Request$>("
yang-gef003082015-08-20 11:40:51 -0700777 "channel_.get(), cq, "
Craig Tillerbd6c6182015-04-10 17:08:15 -0700778 "rpcmethod_$Method$_, "
Yang Gaoc6924c82015-05-05 10:42:51 -0700779 "context, response, tag);\n"
Yang Gao068c85b2015-02-12 15:21:24 -0800780 "}\n\n");
nnobleebebb7e2014-12-10 16:31:01 -0800781 } else if (ServerOnlyStreaming(method)) {
782 printer->Print(
783 *vars,
Yang Gaoc6924c82015-05-05 10:42:51 -0700784 "::grpc::ClientReader< $Response$>* "
785 "$ns$$Service$::Stub::$Method$Raw("
Yang Gao07d83042015-02-13 14:11:31 -0800786 "::grpc::ClientContext* context, const $Request$& request) {\n");
yangg5bcea0d2015-01-06 10:35:03 -0800787 printer->Print(*vars,
Yang Gaoc6924c82015-05-05 10:42:51 -0700788 " return new ::grpc::ClientReader< $Response$>("
yang-gef003082015-08-20 11:40:51 -0700789 "channel_.get(), "
Craig Tillerbd6c6182015-04-10 17:08:15 -0700790 "rpcmethod_$Method$_, "
Yang Gaoc6924c82015-05-05 10:42:51 -0700791 "context, request);\n"
yangg5bcea0d2015-01-06 10:35:03 -0800792 "}\n\n");
Yang Gao068c85b2015-02-12 15:21:24 -0800793 printer->Print(*vars,
Yang Gaoc6924c82015-05-05 10:42:51 -0700794 "::grpc::ClientAsyncReader< $Response$>* "
795 "$ns$$Service$::Stub::Async$Method$Raw("
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800796 "::grpc::ClientContext* context, const $Request$& request, "
797 "::grpc::CompletionQueue* cq, void* tag) {\n");
798 printer->Print(*vars,
Yang Gaoc6924c82015-05-05 10:42:51 -0700799 " return new ::grpc::ClientAsyncReader< $Response$>("
yang-gef003082015-08-20 11:40:51 -0700800 "channel_.get(), cq, "
Craig Tillerbd6c6182015-04-10 17:08:15 -0700801 "rpcmethod_$Method$_, "
Yang Gaoc6924c82015-05-05 10:42:51 -0700802 "context, request, tag);\n"
Yang Gao068c85b2015-02-12 15:21:24 -0800803 "}\n\n");
nnobleebebb7e2014-12-10 16:31:01 -0800804 } else if (BidiStreaming(method)) {
805 printer->Print(
806 *vars,
Yang Gaoc6924c82015-05-05 10:42:51 -0700807 "::grpc::ClientReaderWriter< $Request$, $Response$>* "
808 "$ns$$Service$::Stub::$Method$Raw(::grpc::ClientContext* context) {\n");
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800809 printer->Print(*vars,
Yang Gaoc6924c82015-05-05 10:42:51 -0700810 " return new ::grpc::ClientReaderWriter< "
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800811 "$Request$, $Response$>("
yang-gef003082015-08-20 11:40:51 -0700812 "channel_.get(), "
Craig Tillerbd6c6182015-04-10 17:08:15 -0700813 "rpcmethod_$Method$_, "
Yang Gaoc6924c82015-05-05 10:42:51 -0700814 "context);\n"
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800815 "}\n\n");
Craig Tiller277d3cf2015-04-14 14:04:51 -0700816 printer->Print(
817 *vars,
Yang Gaoc6924c82015-05-05 10:42:51 -0700818 "::grpc::ClientAsyncReaderWriter< $Request$, $Response$>* "
819 "$ns$$Service$::Stub::Async$Method$Raw(::grpc::ClientContext* context, "
Craig Tiller277d3cf2015-04-14 14:04:51 -0700820 "::grpc::CompletionQueue* cq, void* tag) {\n");
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800821 printer->Print(*vars,
Yang Gaoc6924c82015-05-05 10:42:51 -0700822 " return new "
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800823 "::grpc::ClientAsyncReaderWriter< $Request$, $Response$>("
yang-gef003082015-08-20 11:40:51 -0700824 "channel_.get(), cq, "
Craig Tillerbd6c6182015-04-10 17:08:15 -0700825 "rpcmethod_$Method$_, "
Yang Gaoc6924c82015-05-05 10:42:51 -0700826 "context, tag);\n"
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800827 "}\n\n");
nnobleebebb7e2014-12-10 16:31:01 -0800828 }
829}
830
Nicolas Nobled446eb82015-03-12 17:22:33 -0700831void PrintSourceServerMethod(grpc::protobuf::io::Printer *printer,
832 const grpc::protobuf::MethodDescriptor *method,
833 std::map<grpc::string, grpc::string> *vars) {
nnobleebebb7e2014-12-10 16:31:01 -0800834 (*vars)["Method"] = method->name();
835 (*vars)["Request"] =
836 grpc_cpp_generator::ClassName(method->input_type(), true);
837 (*vars)["Response"] =
838 grpc_cpp_generator::ClassName(method->output_type(), true);
839 if (NoStreaming(method)) {
840 printer->Print(*vars,
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100841 "::grpc::Status $ns$$Service$::Service::$Method$("
yangga4b6f5d2014-12-17 15:53:12 -0800842 "::grpc::ServerContext* context, "
nnobleebebb7e2014-12-10 16:31:01 -0800843 "const $Request$* request, $Response$* response) {\n");
Nicolas "Pixel" Nobleb14fbf72015-06-10 23:49:23 +0200844 printer->Print(" (void) context;\n");
845 printer->Print(" (void) request;\n");
846 printer->Print(" (void) response;\n");
nnobleebebb7e2014-12-10 16:31:01 -0800847 printer->Print(
848 " return ::grpc::Status("
Yang Gaoc1a2c312015-06-16 10:59:46 -0700849 "::grpc::StatusCode::UNIMPLEMENTED, \"\");\n");
nnobleebebb7e2014-12-10 16:31:01 -0800850 printer->Print("}\n\n");
851 } else if (ClientOnlyStreaming(method)) {
852 printer->Print(*vars,
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100853 "::grpc::Status $ns$$Service$::Service::$Method$("
yangga4b6f5d2014-12-17 15:53:12 -0800854 "::grpc::ServerContext* context, "
Yang Gao1ff11f62015-01-14 11:45:32 -0800855 "::grpc::ServerReader< $Request$>* reader, "
nnobleebebb7e2014-12-10 16:31:01 -0800856 "$Response$* response) {\n");
Nicolas "Pixel" Nobleb14fbf72015-06-10 23:49:23 +0200857 printer->Print(" (void) context;\n");
858 printer->Print(" (void) reader;\n");
859 printer->Print(" (void) response;\n");
nnobleebebb7e2014-12-10 16:31:01 -0800860 printer->Print(
861 " return ::grpc::Status("
Yang Gaoc1a2c312015-06-16 10:59:46 -0700862 "::grpc::StatusCode::UNIMPLEMENTED, \"\");\n");
nnobleebebb7e2014-12-10 16:31:01 -0800863 printer->Print("}\n\n");
864 } else if (ServerOnlyStreaming(method)) {
865 printer->Print(*vars,
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100866 "::grpc::Status $ns$$Service$::Service::$Method$("
yangga4b6f5d2014-12-17 15:53:12 -0800867 "::grpc::ServerContext* context, "
nnobleebebb7e2014-12-10 16:31:01 -0800868 "const $Request$* request, "
Yang Gao1ff11f62015-01-14 11:45:32 -0800869 "::grpc::ServerWriter< $Response$>* writer) {\n");
Nicolas "Pixel" Nobleb14fbf72015-06-10 23:49:23 +0200870 printer->Print(" (void) context;\n");
871 printer->Print(" (void) request;\n");
872 printer->Print(" (void) writer;\n");
nnobleebebb7e2014-12-10 16:31:01 -0800873 printer->Print(
874 " return ::grpc::Status("
Yang Gaoc1a2c312015-06-16 10:59:46 -0700875 "::grpc::StatusCode::UNIMPLEMENTED, \"\");\n");
nnobleebebb7e2014-12-10 16:31:01 -0800876 printer->Print("}\n\n");
877 } else if (BidiStreaming(method)) {
878 printer->Print(*vars,
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100879 "::grpc::Status $ns$$Service$::Service::$Method$("
yangga4b6f5d2014-12-17 15:53:12 -0800880 "::grpc::ServerContext* context, "
Yang Gao1ff11f62015-01-14 11:45:32 -0800881 "::grpc::ServerReaderWriter< $Response$, $Request$>* "
nnobleebebb7e2014-12-10 16:31:01 -0800882 "stream) {\n");
Nicolas "Pixel" Nobleb14fbf72015-06-10 23:49:23 +0200883 printer->Print(" (void) context;\n");
884 printer->Print(" (void) stream;\n");
nnobleebebb7e2014-12-10 16:31:01 -0800885 printer->Print(
886 " return ::grpc::Status("
Yang Gaoc1a2c312015-06-16 10:59:46 -0700887 "::grpc::StatusCode::UNIMPLEMENTED, \"\");\n");
nnobleebebb7e2014-12-10 16:31:01 -0800888 printer->Print("}\n\n");
889 }
890}
891
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800892void PrintSourceServerAsyncMethod(
Nicolas Nobled446eb82015-03-12 17:22:33 -0700893 grpc::protobuf::io::Printer *printer,
894 const grpc::protobuf::MethodDescriptor *method,
895 std::map<grpc::string, grpc::string> *vars) {
Craig Tiller225f7be2015-02-09 22:32:37 -0800896 (*vars)["Method"] = method->name();
897 (*vars)["Request"] =
898 grpc_cpp_generator::ClassName(method->input_type(), true);
899 (*vars)["Response"] =
900 grpc_cpp_generator::ClassName(method->output_type(), true);
901 if (NoStreaming(method)) {
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700902 printer->Print(
903 *vars,
904 "void $ns$$Service$::AsyncService::Request$Method$("
905 "::grpc::ServerContext* context, "
906 "$Request$* request, "
907 "::grpc::ServerAsyncResponseWriter< $Response$>* response, "
908 "::grpc::CompletionQueue* new_call_cq, "
909 "::grpc::ServerCompletionQueue* notification_cq, void *tag) {\n");
Craig Tiller277d3cf2015-04-14 14:04:51 -0700910 printer->Print(*vars,
911 " AsynchronousService::RequestAsyncUnary($Idx$, context, "
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700912 "request, response, new_call_cq, notification_cq, tag);\n");
Craig Tiller225f7be2015-02-09 22:32:37 -0800913 printer->Print("}\n\n");
914 } else if (ClientOnlyStreaming(method)) {
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700915 printer->Print(
916 *vars,
917 "void $ns$$Service$::AsyncService::Request$Method$("
918 "::grpc::ServerContext* context, "
919 "::grpc::ServerAsyncReader< $Response$, $Request$>* reader, "
920 "::grpc::CompletionQueue* new_call_cq, "
921 "::grpc::ServerCompletionQueue* notification_cq, void *tag) {\n");
Craig Tiller277d3cf2015-04-14 14:04:51 -0700922 printer->Print(*vars,
923 " AsynchronousService::RequestClientStreaming($Idx$, "
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700924 "context, reader, new_call_cq, notification_cq, tag);\n");
Craig Tiller225f7be2015-02-09 22:32:37 -0800925 printer->Print("}\n\n");
926 } else if (ServerOnlyStreaming(method)) {
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700927 printer->Print(
928 *vars,
929 "void $ns$$Service$::AsyncService::Request$Method$("
930 "::grpc::ServerContext* context, "
931 "$Request$* request, "
932 "::grpc::ServerAsyncWriter< $Response$>* writer, "
933 "::grpc::CompletionQueue* new_call_cq, "
934 "::grpc::ServerCompletionQueue* notification_cq, void *tag) {\n");
935 printer->Print(
936 *vars,
937 " AsynchronousService::RequestServerStreaming($Idx$, "
938 "context, request, writer, new_call_cq, notification_cq, tag);\n");
Craig Tiller225f7be2015-02-09 22:32:37 -0800939 printer->Print("}\n\n");
940 } else if (BidiStreaming(method)) {
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800941 printer->Print(
942 *vars,
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100943 "void $ns$$Service$::AsyncService::Request$Method$("
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800944 "::grpc::ServerContext* context, "
945 "::grpc::ServerAsyncReaderWriter< $Response$, $Request$>* stream, "
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700946 "::grpc::CompletionQueue* new_call_cq, "
947 "::grpc::ServerCompletionQueue* notification_cq, void *tag) {\n");
Craig Tiller277d3cf2015-04-14 14:04:51 -0700948 printer->Print(*vars,
949 " AsynchronousService::RequestBidiStreaming($Idx$, "
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700950 "context, stream, new_call_cq, notification_cq, tag);\n");
Craig Tiller225f7be2015-02-09 22:32:37 -0800951 printer->Print("}\n\n");
952 }
953}
954
Nicolas Nobled446eb82015-03-12 17:22:33 -0700955void PrintSourceService(grpc::protobuf::io::Printer *printer,
956 const grpc::protobuf::ServiceDescriptor *service,
957 std::map<grpc::string, grpc::string> *vars) {
nnobleebebb7e2014-12-10 16:31:01 -0800958 (*vars)["Service"] = service->name();
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800959
Craig Tiller277d3cf2015-04-14 14:04:51 -0700960 printer->Print(*vars,
961 "static const char* $prefix$$Service$_method_names[] = {\n");
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800962 for (int i = 0; i < service->method_count(); ++i) {
963 (*vars)["Method"] = service->method(i)->name();
964 printer->Print(*vars, " \"/$Package$$Service$/$Method$\",\n");
965 }
966 printer->Print(*vars, "};\n\n");
967
yang-g8c2be9f2015-08-19 16:28:09 -0700968 printer->Print(*vars,
969 "std::unique_ptr< $ns$$Service$::Stub> $ns$$Service$::NewStub("
970 "const std::shared_ptr< ::grpc::Channel>& channel, "
971 "const ::grpc::StubOptions& options) {\n"
972 " std::unique_ptr< $ns$$Service$::Stub> stub(new "
973 "$ns$$Service$::Stub(channel));\n"
974 " return stub;\n"
975 "}\n\n");
Craig Tiller277d3cf2015-04-14 14:04:51 -0700976 printer->Print(*vars,
977 "$ns$$Service$::Stub::Stub(const std::shared_ptr< "
yang-g8c2be9f2015-08-19 16:28:09 -0700978 "::grpc::Channel>& channel)\n");
Craig Tillerbd6c6182015-04-10 17:08:15 -0700979 printer->Indent();
yang-gef003082015-08-20 11:40:51 -0700980 printer->Print(": channel_(channel)");
Craig Tillerbd6c6182015-04-10 17:08:15 -0700981 for (int i = 0; i < service->method_count(); ++i) {
982 const grpc::protobuf::MethodDescriptor *method = service->method(i);
Craig Tillerbd6c6182015-04-10 17:08:15 -0700983 (*vars)["Method"] = method->name();
984 (*vars)["Idx"] = as_string(i);
985 if (NoStreaming(method)) {
986 (*vars)["StreamingType"] = "NORMAL_RPC";
987 } else if (ClientOnlyStreaming(method)) {
988 (*vars)["StreamingType"] = "CLIENT_STREAMING";
989 } else if (ServerOnlyStreaming(method)) {
990 (*vars)["StreamingType"] = "SERVER_STREAMING";
991 } else {
992 (*vars)["StreamingType"] = "BIDI_STREAMING";
993 }
yang-g431f8c22015-08-20 10:59:29 -0700994 printer->Print(*vars,
995 ", rpcmethod_$Method$_("
996 "$prefix$$Service$_method_names[$Idx$], "
997 "::grpc::RpcMethod::$StreamingType$, "
998 "channel"
999 ")\n");
Craig Tillerbd6c6182015-04-10 17:08:15 -07001000 }
Craig Tiller3beef682015-04-14 13:55:03 -07001001 printer->Print("{}\n\n");
Craig Tillerbd6c6182015-04-10 17:08:15 -07001002 printer->Outdent();
1003
nnobleebebb7e2014-12-10 16:31:01 -08001004 for (int i = 0; i < service->method_count(); ++i) {
Craig Tiller8c8d0aa2015-02-12 11:38:36 -08001005 (*vars)["Idx"] = as_string(i);
nnobleebebb7e2014-12-10 16:31:01 -08001006 PrintSourceClientMethod(printer, service->method(i), vars);
1007 }
1008
Craig Tiller8c8d0aa2015-02-12 11:38:36 -08001009 (*vars)["MethodCount"] = as_string(service->method_count());
Craig Tiller277d3cf2015-04-14 14:04:51 -07001010 printer->Print(*vars,
Craig Tillerf9e6adf2015-05-06 11:45:59 -07001011 "$ns$$Service$::AsyncService::AsyncService() : "
1012 "::grpc::AsynchronousService("
Craig Tiller277d3cf2015-04-14 14:04:51 -07001013 "$prefix$$Service$_method_names, $MethodCount$) "
1014 "{}\n\n");
Craig Tiller8c8d0aa2015-02-12 11:38:36 -08001015
nnobleebebb7e2014-12-10 16:31:01 -08001016 printer->Print(*vars,
Vijay Pai7f715702015-10-12 22:47:58 +00001017 "$ns$$Service$::Service::Service() {\n"
1018 "}\n\n");
1019 printer->Print(*vars,
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +01001020 "$ns$$Service$::Service::~Service() {\n"
nnobleebebb7e2014-12-10 16:31:01 -08001021 "}\n\n");
1022 for (int i = 0; i < service->method_count(); ++i) {
Craig Tiller1c9a2a92015-02-12 14:10:25 -08001023 (*vars)["Idx"] = as_string(i);
nnobleebebb7e2014-12-10 16:31:01 -08001024 PrintSourceServerMethod(printer, service->method(i), vars);
Craig Tiller225f7be2015-02-09 22:32:37 -08001025 PrintSourceServerAsyncMethod(printer, service->method(i), vars);
nnobleebebb7e2014-12-10 16:31:01 -08001026 }
1027 printer->Print(*vars,
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +01001028 "::grpc::RpcService* $ns$$Service$::Service::service() {\n");
nnobleebebb7e2014-12-10 16:31:01 -08001029 printer->Indent();
Craig Tillerb5dcec52015-01-13 11:13:42 -08001030 printer->Print(
Vijay Pai7f715702015-10-12 22:47:58 +00001031 "if (service_) {\n"
1032 " return service_.get();\n"
Craig Tillerb5dcec52015-01-13 11:13:42 -08001033 "}\n");
Vijay Pai7f715702015-10-12 22:47:58 +00001034 printer->Print("service_ = std::unique_ptr< ::grpc::RpcService>(new ::grpc::RpcService());\n");
nnobleebebb7e2014-12-10 16:31:01 -08001035 for (int i = 0; i < service->method_count(); ++i) {
Nicolas Nobled446eb82015-03-12 17:22:33 -07001036 const grpc::protobuf::MethodDescriptor *method = service->method(i);
Craig Tiller8c8d0aa2015-02-12 11:38:36 -08001037 (*vars)["Idx"] = as_string(i);
nnobleebebb7e2014-12-10 16:31:01 -08001038 (*vars)["Method"] = method->name();
1039 (*vars)["Request"] =
1040 grpc_cpp_generator::ClassName(method->input_type(), true);
1041 (*vars)["Response"] =
1042 grpc_cpp_generator::ClassName(method->output_type(), true);
1043 if (NoStreaming(method)) {
1044 printer->Print(
1045 *vars,
1046 "service_->AddMethod(new ::grpc::RpcServiceMethod(\n"
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +01001047 " $prefix$$Service$_method_names[$Idx$],\n"
yangg5bcea0d2015-01-06 10:35:03 -08001048 " ::grpc::RpcMethod::NORMAL_RPC,\n"
Craig Tiller277d3cf2015-04-14 14:04:51 -07001049 " new ::grpc::RpcMethodHandler< $ns$$Service$::Service, "
1050 "$Request$, "
nnobleebebb7e2014-12-10 16:31:01 -08001051 "$Response$>(\n"
Craig Tiller50a7a682015-06-04 12:53:40 -07001052 " std::mem_fn(&$ns$$Service$::Service::$Method$), this)));\n");
nnobleebebb7e2014-12-10 16:31:01 -08001053 } else if (ClientOnlyStreaming(method)) {
1054 printer->Print(
1055 *vars,
1056 "service_->AddMethod(new ::grpc::RpcServiceMethod(\n"
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +01001057 " $prefix$$Service$_method_names[$Idx$],\n"
yangg5bcea0d2015-01-06 10:35:03 -08001058 " ::grpc::RpcMethod::CLIENT_STREAMING,\n"
Yang Gao1ff11f62015-01-14 11:45:32 -08001059 " new ::grpc::ClientStreamingHandler< "
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +01001060 "$ns$$Service$::Service, $Request$, $Response$>(\n"
Craig Tiller50a7a682015-06-04 12:53:40 -07001061 " std::mem_fn(&$ns$$Service$::Service::$Method$), this)));\n");
nnobleebebb7e2014-12-10 16:31:01 -08001062 } else if (ServerOnlyStreaming(method)) {
1063 printer->Print(
1064 *vars,
1065 "service_->AddMethod(new ::grpc::RpcServiceMethod(\n"
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +01001066 " $prefix$$Service$_method_names[$Idx$],\n"
yangg5bcea0d2015-01-06 10:35:03 -08001067 " ::grpc::RpcMethod::SERVER_STREAMING,\n"
Yang Gao1ff11f62015-01-14 11:45:32 -08001068 " new ::grpc::ServerStreamingHandler< "
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +01001069 "$ns$$Service$::Service, $Request$, $Response$>(\n"
Craig Tiller50a7a682015-06-04 12:53:40 -07001070 " std::mem_fn(&$ns$$Service$::Service::$Method$), this)));\n");
nnobleebebb7e2014-12-10 16:31:01 -08001071 } else if (BidiStreaming(method)) {
1072 printer->Print(
1073 *vars,
1074 "service_->AddMethod(new ::grpc::RpcServiceMethod(\n"
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +01001075 " $prefix$$Service$_method_names[$Idx$],\n"
yangg5bcea0d2015-01-06 10:35:03 -08001076 " ::grpc::RpcMethod::BIDI_STREAMING,\n"
Yang Gao1ff11f62015-01-14 11:45:32 -08001077 " new ::grpc::BidiStreamingHandler< "
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +01001078 "$ns$$Service$::Service, $Request$, $Response$>(\n"
Craig Tiller50a7a682015-06-04 12:53:40 -07001079 " std::mem_fn(&$ns$$Service$::Service::$Method$), this)));\n");
nnobleebebb7e2014-12-10 16:31:01 -08001080 }
1081 }
Vijay Pai7f715702015-10-12 22:47:58 +00001082 printer->Print("return service_.get();\n");
nnobleebebb7e2014-12-10 16:31:01 -08001083 printer->Outdent();
1084 printer->Print("}\n\n");
1085}
1086
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +01001087grpc::string GetSourceServices(const grpc::protobuf::FileDescriptor *file,
1088 const Parameters &params) {
Nicolas Nobled446eb82015-03-12 17:22:33 -07001089 grpc::string output;
Jan Tattermusch5dcebd92015-05-27 15:30:59 -07001090 {
1091 // Scope the output stream so it closes and finalizes output to the string.
1092 grpc::protobuf::io::StringOutputStream output_stream(&output);
1093 grpc::protobuf::io::Printer printer(&output_stream, '$');
1094 std::map<grpc::string, grpc::string> vars;
1095 // Package string is empty or ends with a dot. It is used to fully qualify
1096 // method names.
1097 vars["Package"] = file->package();
1098 if (!file->package().empty()) {
1099 vars["Package"].append(".");
1100 }
1101 if (!params.services_namespace.empty()) {
1102 vars["ns"] = params.services_namespace + "::";
1103 vars["prefix"] = params.services_namespace;
1104 } else {
1105 vars["ns"] = "";
1106 vars["prefix"] = "";
1107 }
nnobleebebb7e2014-12-10 16:31:01 -08001108
Jan Tattermusch5dcebd92015-05-27 15:30:59 -07001109 for (int i = 0; i < file->service_count(); ++i) {
1110 PrintSourceService(&printer, file->service(i), &vars);
1111 printer.Print("\n");
1112 }
nnobleebebb7e2014-12-10 16:31:01 -08001113 }
1114 return output;
1115}
1116
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +02001117grpc::string GetSourceEpilogue(const grpc::protobuf::FileDescriptor *file,
1118 const Parameters &params) {
1119 grpc::string temp;
1120
Yang Gao1dc1a432015-04-10 13:53:11 -07001121 if (!file->package().empty()) {
1122 std::vector<grpc::string> parts =
1123 grpc_generator::tokenize(file->package(), ".");
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +02001124
Yang Gao1dc1a432015-04-10 13:53:11 -07001125 for (auto part = parts.begin(); part != parts.end(); part++) {
1126 temp.append("} // namespace ");
1127 temp.append(*part);
1128 temp.append("\n");
1129 }
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +02001130 temp.append("\n");
1131 }
1132
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +02001133 return temp;
1134}
1135
Craig Tiller190d3602015-02-18 09:23:38 -08001136} // namespace grpc_cpp_generator