blob: 8724f97e8be496435a5ec235dd2e9fa85facb776 [file] [log] [blame]
nnobleebebb7e2014-12-10 16:31:01 -08001/*
2 *
3 * Copyright 2014, Google Inc.
4 * 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 <string>
35#include <map>
36
nnobleebebb7e2014-12-10 16:31:01 -080037#include "src/compiler/cpp_generator.h"
38
39#include "src/compiler/cpp_generator_helpers.h"
40#include <google/protobuf/descriptor.h>
41#include <google/protobuf/descriptor.pb.h>
42#include <google/protobuf/io/printer.h>
43#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
44
45namespace grpc_cpp_generator {
46namespace {
47
Craig Tillerecd49342015-01-18 14:36:47 -080048bool NoStreaming(const google::protobuf::MethodDescriptor *method) {
Craig Tillerb5dcec52015-01-13 11:13:42 -080049 return !method->client_streaming() && !method->server_streaming();
nnobleebebb7e2014-12-10 16:31:01 -080050}
51
Craig Tillerecd49342015-01-18 14:36:47 -080052bool ClientOnlyStreaming(const google::protobuf::MethodDescriptor *method) {
Craig Tillerb5dcec52015-01-13 11:13:42 -080053 return method->client_streaming() && !method->server_streaming();
nnobleebebb7e2014-12-10 16:31:01 -080054}
55
Craig Tillerecd49342015-01-18 14:36:47 -080056bool ServerOnlyStreaming(const google::protobuf::MethodDescriptor *method) {
yangg1b151092015-01-09 15:31:05 -080057 return !method->client_streaming() && method->server_streaming();
nnobleebebb7e2014-12-10 16:31:01 -080058}
59
Craig Tillerecd49342015-01-18 14:36:47 -080060bool BidiStreaming(const google::protobuf::MethodDescriptor *method) {
Craig Tillerb5dcec52015-01-13 11:13:42 -080061 return method->client_streaming() && method->server_streaming();
nnobleebebb7e2014-12-10 16:31:01 -080062}
63
Craig Tillerecd49342015-01-18 14:36:47 -080064bool HasClientOnlyStreaming(const google::protobuf::FileDescriptor *file) {
nnobleebebb7e2014-12-10 16:31:01 -080065 for (int i = 0; i < file->service_count(); i++) {
66 for (int j = 0; j < file->service(i)->method_count(); j++) {
67 if (ClientOnlyStreaming(file->service(i)->method(j))) {
68 return true;
69 }
70 }
71 }
72 return false;
73}
74
Craig Tillerecd49342015-01-18 14:36:47 -080075bool HasServerOnlyStreaming(const google::protobuf::FileDescriptor *file) {
nnobleebebb7e2014-12-10 16:31:01 -080076 for (int i = 0; i < file->service_count(); i++) {
77 for (int j = 0; j < file->service(i)->method_count(); j++) {
78 if (ServerOnlyStreaming(file->service(i)->method(j))) {
79 return true;
80 }
81 }
82 }
83 return false;
84}
85
Craig Tillerecd49342015-01-18 14:36:47 -080086bool HasBidiStreaming(const google::protobuf::FileDescriptor *file) {
nnobleebebb7e2014-12-10 16:31:01 -080087 for (int i = 0; i < file->service_count(); i++) {
88 for (int j = 0; j < file->service(i)->method_count(); j++) {
89 if (BidiStreaming(file->service(i)->method(j))) {
90 return true;
91 }
92 }
93 }
94 return false;
95}
96} // namespace
97
Craig Tillerecd49342015-01-18 14:36:47 -080098std::string GetHeaderIncludes(const google::protobuf::FileDescriptor *file) {
Nicolas Noblef5c5d802015-01-15 16:36:13 -080099 std::string temp =
yangg1b151092015-01-09 15:31:05 -0800100 "#include \"grpc++/impl/internal_stub.h\"\n"
temiola33a21682014-12-11 15:13:40 -0800101 "#include \"grpc++/status.h\"\n"
nnobleebebb7e2014-12-10 16:31:01 -0800102 "\n"
103 "namespace grpc {\n"
104 "class ChannelInterface;\n"
yangga4b6f5d2014-12-17 15:53:12 -0800105 "class RpcService;\n"
106 "class ServerContext;\n";
nnobleebebb7e2014-12-10 16:31:01 -0800107 if (HasClientOnlyStreaming(file)) {
108 temp.append("template <class OutMessage> class ClientWriter;\n");
109 temp.append("template <class InMessage> class ServerReader;\n");
110 }
111 if (HasServerOnlyStreaming(file)) {
112 temp.append("template <class InMessage> class ClientReader;\n");
113 temp.append("template <class OutMessage> class ServerWriter;\n");
114 }
115 if (HasBidiStreaming(file)) {
116 temp.append(
117 "template <class OutMessage, class InMessage>\n"
118 "class ClientReaderWriter;\n");
119 temp.append(
120 "template <class OutMessage, class InMessage>\n"
121 "class ServerReaderWriter;\n");
122 }
123 temp.append("} // namespace grpc\n");
124 return temp;
125}
126
Nicolas Noblef5c5d802015-01-15 16:36:13 -0800127std::string GetSourceIncludes() {
yangg1b151092015-01-09 15:31:05 -0800128 return "#include \"grpc++/channel_interface.h\"\n"
129 "#include \"grpc++/impl/rpc_method.h\"\n"
130 "#include \"grpc++/impl/rpc_service_method.h\"\n"
temiola33a21682014-12-11 15:13:40 -0800131 "#include \"grpc++/stream.h\"\n";
nnobleebebb7e2014-12-10 16:31:01 -0800132}
133
Craig Tillerecd49342015-01-18 14:36:47 -0800134void PrintHeaderClientMethod(google::protobuf::io::Printer *printer,
135 const google::protobuf::MethodDescriptor *method,
136 std::map<std::string, std::string> *vars) {
nnobleebebb7e2014-12-10 16:31:01 -0800137 (*vars)["Method"] = method->name();
138 (*vars)["Request"] =
139 grpc_cpp_generator::ClassName(method->input_type(), true);
140 (*vars)["Response"] =
141 grpc_cpp_generator::ClassName(method->output_type(), true);
142 if (NoStreaming(method)) {
143 printer->Print(*vars,
144 "::grpc::Status $Method$(::grpc::ClientContext* context, "
145 "const $Request$& request, $Response$* response);\n\n");
146 } else if (ClientOnlyStreaming(method)) {
147 printer->Print(
148 *vars,
Yang Gao1ff11f62015-01-14 11:45:32 -0800149 "::grpc::ClientWriter< $Request$>* $Method$("
nnobleebebb7e2014-12-10 16:31:01 -0800150 "::grpc::ClientContext* context, $Response$* response);\n\n");
151 } else if (ServerOnlyStreaming(method)) {
152 printer->Print(
153 *vars,
Yang Gao1ff11f62015-01-14 11:45:32 -0800154 "::grpc::ClientReader< $Response$>* $Method$("
nnobleebebb7e2014-12-10 16:31:01 -0800155 "::grpc::ClientContext* context, const $Request$* request);\n\n");
156 } else if (BidiStreaming(method)) {
157 printer->Print(*vars,
Yang Gao1ff11f62015-01-14 11:45:32 -0800158 "::grpc::ClientReaderWriter< $Request$, $Response$>* "
nnobleebebb7e2014-12-10 16:31:01 -0800159 "$Method$(::grpc::ClientContext* context);\n\n");
160 }
161}
162
Craig Tillerecd49342015-01-18 14:36:47 -0800163void PrintHeaderServerMethod(google::protobuf::io::Printer *printer,
164 const google::protobuf::MethodDescriptor *method,
165 std::map<std::string, std::string> *vars) {
nnobleebebb7e2014-12-10 16:31:01 -0800166 (*vars)["Method"] = method->name();
167 (*vars)["Request"] =
168 grpc_cpp_generator::ClassName(method->input_type(), true);
169 (*vars)["Response"] =
170 grpc_cpp_generator::ClassName(method->output_type(), true);
171 if (NoStreaming(method)) {
172 printer->Print(*vars,
yangga4b6f5d2014-12-17 15:53:12 -0800173 "virtual ::grpc::Status $Method$("
174 "::grpc::ServerContext* context, const $Request$* request, "
nnobleebebb7e2014-12-10 16:31:01 -0800175 "$Response$* response);\n");
176 } else if (ClientOnlyStreaming(method)) {
177 printer->Print(*vars,
178 "virtual ::grpc::Status $Method$("
yangga4b6f5d2014-12-17 15:53:12 -0800179 "::grpc::ServerContext* context, "
Yang Gao1ff11f62015-01-14 11:45:32 -0800180 "::grpc::ServerReader< $Request$>* reader, "
nnobleebebb7e2014-12-10 16:31:01 -0800181 "$Response$* response);\n");
182 } else if (ServerOnlyStreaming(method)) {
183 printer->Print(*vars,
yangga4b6f5d2014-12-17 15:53:12 -0800184 "virtual ::grpc::Status $Method$("
185 "::grpc::ServerContext* context, const $Request$* request, "
Yang Gao1ff11f62015-01-14 11:45:32 -0800186 "::grpc::ServerWriter< $Response$>* writer);\n");
nnobleebebb7e2014-12-10 16:31:01 -0800187 } else if (BidiStreaming(method)) {
Yang Gao5680ff42015-01-14 12:14:21 -0800188 printer->Print(
189 *vars,
190 "virtual ::grpc::Status $Method$("
191 "::grpc::ServerContext* context, "
192 "::grpc::ServerReaderWriter< $Response$, $Request$>* stream);"
193 "\n");
nnobleebebb7e2014-12-10 16:31:01 -0800194 }
195}
196
Craig Tillerecd49342015-01-18 14:36:47 -0800197void PrintHeaderService(google::protobuf::io::Printer *printer,
198 const google::protobuf::ServiceDescriptor *service,
199 std::map<std::string, std::string> *vars) {
nnobleebebb7e2014-12-10 16:31:01 -0800200 (*vars)["Service"] = service->name();
201
202 printer->Print(*vars,
203 "class $Service$ {\n"
204 " public:\n");
205 printer->Indent();
206
207 // Client side
Craig Tillerb5dcec52015-01-13 11:13:42 -0800208 printer->Print(
209 "class Stub : public ::grpc::InternalStub {\n"
210 " public:\n");
nnobleebebb7e2014-12-10 16:31:01 -0800211 printer->Indent();
212 for (int i = 0; i < service->method_count(); ++i) {
213 PrintHeaderClientMethod(printer, service->method(i), vars);
214 }
215 printer->Outdent();
216 printer->Print("};\n");
217 printer->Print(
Yang Gao1ff11f62015-01-14 11:45:32 -0800218 "static Stub* NewStub(const std::shared_ptr< ::grpc::ChannelInterface>& "
nnobleebebb7e2014-12-10 16:31:01 -0800219 "channel);\n");
220
221 printer->Print("\n");
222
223 // Server side
Craig Tillerb5dcec52015-01-13 11:13:42 -0800224 printer->Print(
225 "class Service {\n"
226 " public:\n");
nnobleebebb7e2014-12-10 16:31:01 -0800227 printer->Indent();
228 printer->Print("Service() : service_(nullptr) {}\n");
229 printer->Print("virtual ~Service();\n");
230 for (int i = 0; i < service->method_count(); ++i) {
231 PrintHeaderServerMethod(printer, service->method(i), vars);
232 }
233 printer->Print("::grpc::RpcService* service();\n");
234 printer->Outdent();
Craig Tillerb5dcec52015-01-13 11:13:42 -0800235 printer->Print(
236 " private:\n"
237 " ::grpc::RpcService* service_;\n");
nnobleebebb7e2014-12-10 16:31:01 -0800238 printer->Print("};\n");
239
240 printer->Outdent();
241 printer->Print("};\n");
242}
243
Craig Tillerecd49342015-01-18 14:36:47 -0800244std::string GetHeaderServices(const google::protobuf::FileDescriptor *file) {
Nicolas Noblef5c5d802015-01-15 16:36:13 -0800245 std::string output;
nnobleebebb7e2014-12-10 16:31:01 -0800246 google::protobuf::io::StringOutputStream output_stream(&output);
247 google::protobuf::io::Printer printer(&output_stream, '$');
Nicolas Noblef5c5d802015-01-15 16:36:13 -0800248 std::map<std::string, std::string> vars;
nnobleebebb7e2014-12-10 16:31:01 -0800249
250 for (int i = 0; i < file->service_count(); ++i) {
samuelwca9f3592014-12-15 14:22:04 -0800251 PrintHeaderService(&printer, file->service(i), &vars);
nnobleebebb7e2014-12-10 16:31:01 -0800252 printer.Print("\n");
253 }
254 return output;
255}
256
Craig Tillerecd49342015-01-18 14:36:47 -0800257void PrintSourceClientMethod(google::protobuf::io::Printer *printer,
258 const google::protobuf::MethodDescriptor *method,
259 std::map<std::string, std::string> *vars) {
nnobleebebb7e2014-12-10 16:31:01 -0800260 (*vars)["Method"] = method->name();
261 (*vars)["Request"] =
262 grpc_cpp_generator::ClassName(method->input_type(), true);
263 (*vars)["Response"] =
264 grpc_cpp_generator::ClassName(method->output_type(), true);
265 if (NoStreaming(method)) {
266 printer->Print(*vars,
267 "::grpc::Status $Service$::Stub::$Method$("
268 "::grpc::ClientContext* context, "
269 "const $Request$& request, $Response$* response) {\n");
270 printer->Print(*vars,
271 " return channel()->StartBlockingRpc("
yangg5bcea0d2015-01-06 10:35:03 -0800272 "::grpc::RpcMethod(\"/$Package$$Service$/$Method$\"), "
nnobleebebb7e2014-12-10 16:31:01 -0800273 "context, request, response);\n"
274 "}\n\n");
275 } else if (ClientOnlyStreaming(method)) {
Yang Gao5680ff42015-01-14 12:14:21 -0800276 printer->Print(
277 *vars,
278 "::grpc::ClientWriter< $Request$>* $Service$::Stub::$Method$("
279 "::grpc::ClientContext* context, $Response$* response) {\n");
yangg5bcea0d2015-01-06 10:35:03 -0800280 printer->Print(*vars,
Yang Gao1ff11f62015-01-14 11:45:32 -0800281 " return new ::grpc::ClientWriter< $Request$>("
yangg5bcea0d2015-01-06 10:35:03 -0800282 "channel()->CreateStream("
283 "::grpc::RpcMethod(\"/$Package$$Service$/$Method$\", "
284 "::grpc::RpcMethod::RpcType::CLIENT_STREAMING), "
285 "context, nullptr, response));\n"
286 "}\n\n");
nnobleebebb7e2014-12-10 16:31:01 -0800287 } else if (ServerOnlyStreaming(method)) {
288 printer->Print(
289 *vars,
Yang Gao1ff11f62015-01-14 11:45:32 -0800290 "::grpc::ClientReader< $Response$>* $Service$::Stub::$Method$("
nnobleebebb7e2014-12-10 16:31:01 -0800291 "::grpc::ClientContext* context, const $Request$* request) {\n");
yangg5bcea0d2015-01-06 10:35:03 -0800292 printer->Print(*vars,
Yang Gao1ff11f62015-01-14 11:45:32 -0800293 " return new ::grpc::ClientReader< $Response$>("
yangg5bcea0d2015-01-06 10:35:03 -0800294 "channel()->CreateStream("
295 "::grpc::RpcMethod(\"/$Package$$Service$/$Method$\", "
296 "::grpc::RpcMethod::RpcType::SERVER_STREAMING), "
297 "context, request, nullptr));\n"
298 "}\n\n");
nnobleebebb7e2014-12-10 16:31:01 -0800299 } else if (BidiStreaming(method)) {
300 printer->Print(
301 *vars,
Yang Gao1ff11f62015-01-14 11:45:32 -0800302 "::grpc::ClientReaderWriter< $Request$, $Response$>* "
nnobleebebb7e2014-12-10 16:31:01 -0800303 "$Service$::Stub::$Method$(::grpc::ClientContext* context) {\n");
304 printer->Print(
305 *vars,
Yang Gao1ff11f62015-01-14 11:45:32 -0800306 " return new ::grpc::ClientReaderWriter< $Request$, $Response$>("
yangg5bcea0d2015-01-06 10:35:03 -0800307 "channel()->CreateStream("
308 "::grpc::RpcMethod(\"/$Package$$Service$/$Method$\", "
nnobleebebb7e2014-12-10 16:31:01 -0800309 "::grpc::RpcMethod::RpcType::BIDI_STREAMING), "
310 "context, nullptr, nullptr));\n"
311 "}\n\n");
312 }
313}
314
Craig Tillerecd49342015-01-18 14:36:47 -0800315void PrintSourceServerMethod(google::protobuf::io::Printer *printer,
316 const google::protobuf::MethodDescriptor *method,
317 std::map<std::string, std::string> *vars) {
nnobleebebb7e2014-12-10 16:31:01 -0800318 (*vars)["Method"] = method->name();
319 (*vars)["Request"] =
320 grpc_cpp_generator::ClassName(method->input_type(), true);
321 (*vars)["Response"] =
322 grpc_cpp_generator::ClassName(method->output_type(), true);
323 if (NoStreaming(method)) {
324 printer->Print(*vars,
325 "::grpc::Status $Service$::Service::$Method$("
yangga4b6f5d2014-12-17 15:53:12 -0800326 "::grpc::ServerContext* context, "
nnobleebebb7e2014-12-10 16:31:01 -0800327 "const $Request$* request, $Response$* response) {\n");
328 printer->Print(
329 " return ::grpc::Status("
330 "::grpc::StatusCode::UNIMPLEMENTED);\n");
331 printer->Print("}\n\n");
332 } else if (ClientOnlyStreaming(method)) {
333 printer->Print(*vars,
334 "::grpc::Status $Service$::Service::$Method$("
yangga4b6f5d2014-12-17 15:53:12 -0800335 "::grpc::ServerContext* context, "
Yang Gao1ff11f62015-01-14 11:45:32 -0800336 "::grpc::ServerReader< $Request$>* reader, "
nnobleebebb7e2014-12-10 16:31:01 -0800337 "$Response$* response) {\n");
338 printer->Print(
339 " return ::grpc::Status("
340 "::grpc::StatusCode::UNIMPLEMENTED);\n");
341 printer->Print("}\n\n");
342 } else if (ServerOnlyStreaming(method)) {
343 printer->Print(*vars,
344 "::grpc::Status $Service$::Service::$Method$("
yangga4b6f5d2014-12-17 15:53:12 -0800345 "::grpc::ServerContext* context, "
nnobleebebb7e2014-12-10 16:31:01 -0800346 "const $Request$* request, "
Yang Gao1ff11f62015-01-14 11:45:32 -0800347 "::grpc::ServerWriter< $Response$>* writer) {\n");
nnobleebebb7e2014-12-10 16:31:01 -0800348 printer->Print(
349 " return ::grpc::Status("
350 "::grpc::StatusCode::UNIMPLEMENTED);\n");
351 printer->Print("}\n\n");
352 } else if (BidiStreaming(method)) {
353 printer->Print(*vars,
354 "::grpc::Status $Service$::Service::$Method$("
yangga4b6f5d2014-12-17 15:53:12 -0800355 "::grpc::ServerContext* context, "
Yang Gao1ff11f62015-01-14 11:45:32 -0800356 "::grpc::ServerReaderWriter< $Response$, $Request$>* "
nnobleebebb7e2014-12-10 16:31:01 -0800357 "stream) {\n");
358 printer->Print(
359 " return ::grpc::Status("
360 "::grpc::StatusCode::UNIMPLEMENTED);\n");
361 printer->Print("}\n\n");
362 }
363}
364
Craig Tillerecd49342015-01-18 14:36:47 -0800365void PrintSourceService(google::protobuf::io::Printer *printer,
366 const google::protobuf::ServiceDescriptor *service,
367 std::map<std::string, std::string> *vars) {
nnobleebebb7e2014-12-10 16:31:01 -0800368 (*vars)["Service"] = service->name();
Yang Gao5680ff42015-01-14 12:14:21 -0800369 printer->Print(
370 *vars,
371 "$Service$::Stub* $Service$::NewStub("
372 "const std::shared_ptr< ::grpc::ChannelInterface>& channel) {\n"
373 " $Service$::Stub* stub = new $Service$::Stub();\n"
374 " stub->set_channel(channel);\n"
375 " return stub;\n"
376 "};\n\n");
nnobleebebb7e2014-12-10 16:31:01 -0800377 for (int i = 0; i < service->method_count(); ++i) {
378 PrintSourceClientMethod(printer, service->method(i), vars);
379 }
380
381 printer->Print(*vars,
382 "$Service$::Service::~Service() {\n"
383 " delete service_;\n"
384 "}\n\n");
385 for (int i = 0; i < service->method_count(); ++i) {
386 PrintSourceServerMethod(printer, service->method(i), vars);
387 }
388 printer->Print(*vars,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800389 "::grpc::RpcService* $Service$::Service::service() {\n");
nnobleebebb7e2014-12-10 16:31:01 -0800390 printer->Indent();
Craig Tillerb5dcec52015-01-13 11:13:42 -0800391 printer->Print(
392 "if (service_ != nullptr) {\n"
393 " return service_;\n"
394 "}\n");
nnobleebebb7e2014-12-10 16:31:01 -0800395 printer->Print("service_ = new ::grpc::RpcService();\n");
396 for (int i = 0; i < service->method_count(); ++i) {
Craig Tillerecd49342015-01-18 14:36:47 -0800397 const google::protobuf::MethodDescriptor *method = service->method(i);
nnobleebebb7e2014-12-10 16:31:01 -0800398 (*vars)["Method"] = method->name();
399 (*vars)["Request"] =
400 grpc_cpp_generator::ClassName(method->input_type(), true);
401 (*vars)["Response"] =
402 grpc_cpp_generator::ClassName(method->output_type(), true);
403 if (NoStreaming(method)) {
404 printer->Print(
405 *vars,
406 "service_->AddMethod(new ::grpc::RpcServiceMethod(\n"
yangg5bcea0d2015-01-06 10:35:03 -0800407 " \"/$Package$$Service$/$Method$\",\n"
408 " ::grpc::RpcMethod::NORMAL_RPC,\n"
Yang Gao1ff11f62015-01-14 11:45:32 -0800409 " new ::grpc::RpcMethodHandler< $Service$::Service, $Request$, "
nnobleebebb7e2014-12-10 16:31:01 -0800410 "$Response$>(\n"
Yang Gao1ff11f62015-01-14 11:45:32 -0800411 " std::function< ::grpc::Status($Service$::Service*, "
yangga4b6f5d2014-12-17 15:53:12 -0800412 "::grpc::ServerContext*, const $Request$*, $Response$*)>("
nnobleebebb7e2014-12-10 16:31:01 -0800413 "&$Service$::Service::$Method$), this),\n"
414 " new $Request$, new $Response$));\n");
415 } else if (ClientOnlyStreaming(method)) {
416 printer->Print(
417 *vars,
418 "service_->AddMethod(new ::grpc::RpcServiceMethod(\n"
yangg5bcea0d2015-01-06 10:35:03 -0800419 " \"/$Package$$Service$/$Method$\",\n"
420 " ::grpc::RpcMethod::CLIENT_STREAMING,\n"
Yang Gao1ff11f62015-01-14 11:45:32 -0800421 " new ::grpc::ClientStreamingHandler< "
nnobleebebb7e2014-12-10 16:31:01 -0800422 "$Service$::Service, $Request$, $Response$>(\n"
Yang Gao1ff11f62015-01-14 11:45:32 -0800423 " std::function< ::grpc::Status($Service$::Service*, "
yangga4b6f5d2014-12-17 15:53:12 -0800424 "::grpc::ServerContext*, "
Yang Gao1ff11f62015-01-14 11:45:32 -0800425 "::grpc::ServerReader< $Request$>*, $Response$*)>("
nnobleebebb7e2014-12-10 16:31:01 -0800426 "&$Service$::Service::$Method$), this),\n"
427 " new $Request$, new $Response$));\n");
428 } else if (ServerOnlyStreaming(method)) {
429 printer->Print(
430 *vars,
431 "service_->AddMethod(new ::grpc::RpcServiceMethod(\n"
yangg5bcea0d2015-01-06 10:35:03 -0800432 " \"/$Package$$Service$/$Method$\",\n"
433 " ::grpc::RpcMethod::SERVER_STREAMING,\n"
Yang Gao1ff11f62015-01-14 11:45:32 -0800434 " new ::grpc::ServerStreamingHandler< "
nnobleebebb7e2014-12-10 16:31:01 -0800435 "$Service$::Service, $Request$, $Response$>(\n"
Yang Gao1ff11f62015-01-14 11:45:32 -0800436 " std::function< ::grpc::Status($Service$::Service*, "
yangga4b6f5d2014-12-17 15:53:12 -0800437 "::grpc::ServerContext*, "
Yang Gao1ff11f62015-01-14 11:45:32 -0800438 "const $Request$*, ::grpc::ServerWriter< $Response$>*)>("
nnobleebebb7e2014-12-10 16:31:01 -0800439 "&$Service$::Service::$Method$), this),\n"
440 " new $Request$, new $Response$));\n");
441 } else if (BidiStreaming(method)) {
442 printer->Print(
443 *vars,
444 "service_->AddMethod(new ::grpc::RpcServiceMethod(\n"
yangg5bcea0d2015-01-06 10:35:03 -0800445 " \"/$Package$$Service$/$Method$\",\n"
446 " ::grpc::RpcMethod::BIDI_STREAMING,\n"
Yang Gao1ff11f62015-01-14 11:45:32 -0800447 " new ::grpc::BidiStreamingHandler< "
nnobleebebb7e2014-12-10 16:31:01 -0800448 "$Service$::Service, $Request$, $Response$>(\n"
Yang Gao1ff11f62015-01-14 11:45:32 -0800449 " std::function< ::grpc::Status($Service$::Service*, "
yangga4b6f5d2014-12-17 15:53:12 -0800450 "::grpc::ServerContext*, "
Yang Gao1ff11f62015-01-14 11:45:32 -0800451 "::grpc::ServerReaderWriter< $Response$, $Request$>*)>("
nnobleebebb7e2014-12-10 16:31:01 -0800452 "&$Service$::Service::$Method$), this),\n"
453 " new $Request$, new $Response$));\n");
454 }
455 }
456 printer->Print("return service_;\n");
457 printer->Outdent();
458 printer->Print("}\n\n");
459}
460
Craig Tillerecd49342015-01-18 14:36:47 -0800461std::string GetSourceServices(const google::protobuf::FileDescriptor *file) {
Nicolas Noblef5c5d802015-01-15 16:36:13 -0800462 std::string output;
nnobleebebb7e2014-12-10 16:31:01 -0800463 google::protobuf::io::StringOutputStream output_stream(&output);
464 google::protobuf::io::Printer printer(&output_stream, '$');
Nicolas Noblef5c5d802015-01-15 16:36:13 -0800465 std::map<std::string, std::string> vars;
yangg5bcea0d2015-01-06 10:35:03 -0800466 // Package string is empty or ends with a dot. It is used to fully qualify
467 // method names.
468 vars["Package"] = file->package();
469 if (!file->package().empty()) {
470 vars["Package"].append(".");
471 }
nnobleebebb7e2014-12-10 16:31:01 -0800472
473 for (int i = 0; i < file->service_count(); ++i) {
samuelwca9f3592014-12-15 14:22:04 -0800474 PrintSourceService(&printer, file->service(i), &vars);
nnobleebebb7e2014-12-10 16:31:01 -0800475 printer.Print("\n");
476 }
477 return output;
478}
479
480} // namespace grpc_cpp_generator