nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 34 | #include "src/compiler/cpp_generator.h" |
| 35 | |
| 36 | #include "src/compiler/cpp_generator_helpers.h" |
| 37 | #include <google/protobuf/descriptor.h> |
| 38 | #include <google/protobuf/descriptor.pb.h> |
| 39 | #include <google/protobuf/io/printer.h> |
| 40 | #include <google/protobuf/io/zero_copy_stream_impl_lite.h> |
| 41 | |
| 42 | namespace grpc_cpp_generator { |
| 43 | namespace { |
| 44 | |
| 45 | bool NoStreaming(const google::protobuf::MethodDescriptor* method) { |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 46 | return !method->client_streaming() && !method->server_streaming(); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | bool ClientOnlyStreaming(const google::protobuf::MethodDescriptor* method) { |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 50 | return method->client_streaming() && !method->server_streaming(); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | bool ServerOnlyStreaming(const google::protobuf::MethodDescriptor* method) { |
yangg | 1b15109 | 2015-01-09 15:31:05 -0800 | [diff] [blame] | 54 | return !method->client_streaming() && method->server_streaming(); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | bool BidiStreaming(const google::protobuf::MethodDescriptor* method) { |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 58 | return method->client_streaming() && method->server_streaming(); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | bool HasClientOnlyStreaming(const google::protobuf::FileDescriptor* file) { |
| 62 | for (int i = 0; i < file->service_count(); i++) { |
| 63 | for (int j = 0; j < file->service(i)->method_count(); j++) { |
| 64 | if (ClientOnlyStreaming(file->service(i)->method(j))) { |
| 65 | return true; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | bool HasServerOnlyStreaming(const google::protobuf::FileDescriptor* file) { |
| 73 | for (int i = 0; i < file->service_count(); i++) { |
| 74 | for (int j = 0; j < file->service(i)->method_count(); j++) { |
| 75 | if (ServerOnlyStreaming(file->service(i)->method(j))) { |
| 76 | return true; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | bool HasBidiStreaming(const google::protobuf::FileDescriptor* file) { |
| 84 | for (int i = 0; i < file->service_count(); i++) { |
| 85 | for (int j = 0; j < file->service(i)->method_count(); j++) { |
| 86 | if (BidiStreaming(file->service(i)->method(j))) { |
| 87 | return true; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | return false; |
| 92 | } |
| 93 | } // namespace |
| 94 | |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame^] | 95 | std::string GetHeaderIncludes(const google::protobuf::FileDescriptor* file) { |
| 96 | std::string temp = |
yangg | 1b15109 | 2015-01-09 15:31:05 -0800 | [diff] [blame] | 97 | "#include \"grpc++/impl/internal_stub.h\"\n" |
temiola | 33a2168 | 2014-12-11 15:13:40 -0800 | [diff] [blame] | 98 | "#include \"grpc++/status.h\"\n" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 99 | "\n" |
| 100 | "namespace grpc {\n" |
| 101 | "class ChannelInterface;\n" |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 102 | "class RpcService;\n" |
| 103 | "class ServerContext;\n"; |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 104 | if (HasClientOnlyStreaming(file)) { |
| 105 | temp.append("template <class OutMessage> class ClientWriter;\n"); |
| 106 | temp.append("template <class InMessage> class ServerReader;\n"); |
| 107 | } |
| 108 | if (HasServerOnlyStreaming(file)) { |
| 109 | temp.append("template <class InMessage> class ClientReader;\n"); |
| 110 | temp.append("template <class OutMessage> class ServerWriter;\n"); |
| 111 | } |
| 112 | if (HasBidiStreaming(file)) { |
| 113 | temp.append( |
| 114 | "template <class OutMessage, class InMessage>\n" |
| 115 | "class ClientReaderWriter;\n"); |
| 116 | temp.append( |
| 117 | "template <class OutMessage, class InMessage>\n" |
| 118 | "class ServerReaderWriter;\n"); |
| 119 | } |
| 120 | temp.append("} // namespace grpc\n"); |
| 121 | return temp; |
| 122 | } |
| 123 | |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame^] | 124 | std::string GetSourceIncludes() { |
yangg | 1b15109 | 2015-01-09 15:31:05 -0800 | [diff] [blame] | 125 | return "#include \"grpc++/channel_interface.h\"\n" |
| 126 | "#include \"grpc++/impl/rpc_method.h\"\n" |
| 127 | "#include \"grpc++/impl/rpc_service_method.h\"\n" |
temiola | 33a2168 | 2014-12-11 15:13:40 -0800 | [diff] [blame] | 128 | "#include \"grpc++/stream.h\"\n"; |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void PrintHeaderClientMethod(google::protobuf::io::Printer* printer, |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 132 | const google::protobuf::MethodDescriptor* method, |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame^] | 133 | std::map<std::string, std::string>* vars) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 134 | (*vars)["Method"] = method->name(); |
| 135 | (*vars)["Request"] = |
| 136 | grpc_cpp_generator::ClassName(method->input_type(), true); |
| 137 | (*vars)["Response"] = |
| 138 | grpc_cpp_generator::ClassName(method->output_type(), true); |
| 139 | if (NoStreaming(method)) { |
| 140 | printer->Print(*vars, |
| 141 | "::grpc::Status $Method$(::grpc::ClientContext* context, " |
| 142 | "const $Request$& request, $Response$* response);\n\n"); |
| 143 | } else if (ClientOnlyStreaming(method)) { |
| 144 | printer->Print( |
| 145 | *vars, |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 146 | "::grpc::ClientWriter< $Request$>* $Method$(" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 147 | "::grpc::ClientContext* context, $Response$* response);\n\n"); |
| 148 | } else if (ServerOnlyStreaming(method)) { |
| 149 | printer->Print( |
| 150 | *vars, |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 151 | "::grpc::ClientReader< $Response$>* $Method$(" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 152 | "::grpc::ClientContext* context, const $Request$* request);\n\n"); |
| 153 | } else if (BidiStreaming(method)) { |
| 154 | printer->Print(*vars, |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 155 | "::grpc::ClientReaderWriter< $Request$, $Response$>* " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 156 | "$Method$(::grpc::ClientContext* context);\n\n"); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void PrintHeaderServerMethod(google::protobuf::io::Printer* printer, |
| 161 | const google::protobuf::MethodDescriptor* method, |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame^] | 162 | std::map<std::string, std::string>* vars) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 163 | (*vars)["Method"] = method->name(); |
| 164 | (*vars)["Request"] = |
| 165 | grpc_cpp_generator::ClassName(method->input_type(), true); |
| 166 | (*vars)["Response"] = |
| 167 | grpc_cpp_generator::ClassName(method->output_type(), true); |
| 168 | if (NoStreaming(method)) { |
| 169 | printer->Print(*vars, |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 170 | "virtual ::grpc::Status $Method$(" |
| 171 | "::grpc::ServerContext* context, const $Request$* request, " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 172 | "$Response$* response);\n"); |
| 173 | } else if (ClientOnlyStreaming(method)) { |
| 174 | printer->Print(*vars, |
| 175 | "virtual ::grpc::Status $Method$(" |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 176 | "::grpc::ServerContext* context, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 177 | "::grpc::ServerReader< $Request$>* reader, " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 178 | "$Response$* response);\n"); |
| 179 | } else if (ServerOnlyStreaming(method)) { |
| 180 | printer->Print(*vars, |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 181 | "virtual ::grpc::Status $Method$(" |
| 182 | "::grpc::ServerContext* context, const $Request$* request, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 183 | "::grpc::ServerWriter< $Response$>* writer);\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 184 | } else if (BidiStreaming(method)) { |
Yang Gao | 5680ff4 | 2015-01-14 12:14:21 -0800 | [diff] [blame] | 185 | printer->Print( |
| 186 | *vars, |
| 187 | "virtual ::grpc::Status $Method$(" |
| 188 | "::grpc::ServerContext* context, " |
| 189 | "::grpc::ServerReaderWriter< $Response$, $Request$>* stream);" |
| 190 | "\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | void PrintHeaderService(google::protobuf::io::Printer* printer, |
| 195 | const google::protobuf::ServiceDescriptor* service, |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame^] | 196 | std::map<std::string, std::string>* vars) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 197 | (*vars)["Service"] = service->name(); |
| 198 | |
| 199 | printer->Print(*vars, |
| 200 | "class $Service$ {\n" |
| 201 | " public:\n"); |
| 202 | printer->Indent(); |
| 203 | |
| 204 | // Client side |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 205 | printer->Print( |
| 206 | "class Stub : public ::grpc::InternalStub {\n" |
| 207 | " public:\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 208 | printer->Indent(); |
| 209 | for (int i = 0; i < service->method_count(); ++i) { |
| 210 | PrintHeaderClientMethod(printer, service->method(i), vars); |
| 211 | } |
| 212 | printer->Outdent(); |
| 213 | printer->Print("};\n"); |
| 214 | printer->Print( |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 215 | "static Stub* NewStub(const std::shared_ptr< ::grpc::ChannelInterface>& " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 216 | "channel);\n"); |
| 217 | |
| 218 | printer->Print("\n"); |
| 219 | |
| 220 | // Server side |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 221 | printer->Print( |
| 222 | "class Service {\n" |
| 223 | " public:\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 224 | printer->Indent(); |
| 225 | printer->Print("Service() : service_(nullptr) {}\n"); |
| 226 | printer->Print("virtual ~Service();\n"); |
| 227 | for (int i = 0; i < service->method_count(); ++i) { |
| 228 | PrintHeaderServerMethod(printer, service->method(i), vars); |
| 229 | } |
| 230 | printer->Print("::grpc::RpcService* service();\n"); |
| 231 | printer->Outdent(); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 232 | printer->Print( |
| 233 | " private:\n" |
| 234 | " ::grpc::RpcService* service_;\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 235 | printer->Print("};\n"); |
| 236 | |
| 237 | printer->Outdent(); |
| 238 | printer->Print("};\n"); |
| 239 | } |
| 240 | |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame^] | 241 | std::string GetHeaderServices(const google::protobuf::FileDescriptor* file) { |
| 242 | std::string output; |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 243 | google::protobuf::io::StringOutputStream output_stream(&output); |
| 244 | google::protobuf::io::Printer printer(&output_stream, '$'); |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame^] | 245 | std::map<std::string, std::string> vars; |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 246 | |
| 247 | for (int i = 0; i < file->service_count(); ++i) { |
samuelw | ca9f359 | 2014-12-15 14:22:04 -0800 | [diff] [blame] | 248 | PrintHeaderService(&printer, file->service(i), &vars); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 249 | printer.Print("\n"); |
| 250 | } |
| 251 | return output; |
| 252 | } |
| 253 | |
| 254 | void PrintSourceClientMethod(google::protobuf::io::Printer* printer, |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 255 | const google::protobuf::MethodDescriptor* method, |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame^] | 256 | std::map<std::string, std::string>* vars) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 257 | (*vars)["Method"] = method->name(); |
| 258 | (*vars)["Request"] = |
| 259 | grpc_cpp_generator::ClassName(method->input_type(), true); |
| 260 | (*vars)["Response"] = |
| 261 | grpc_cpp_generator::ClassName(method->output_type(), true); |
| 262 | if (NoStreaming(method)) { |
| 263 | printer->Print(*vars, |
| 264 | "::grpc::Status $Service$::Stub::$Method$(" |
| 265 | "::grpc::ClientContext* context, " |
| 266 | "const $Request$& request, $Response$* response) {\n"); |
| 267 | printer->Print(*vars, |
| 268 | " return channel()->StartBlockingRpc(" |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 269 | "::grpc::RpcMethod(\"/$Package$$Service$/$Method$\"), " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 270 | "context, request, response);\n" |
| 271 | "}\n\n"); |
| 272 | } else if (ClientOnlyStreaming(method)) { |
Yang Gao | 5680ff4 | 2015-01-14 12:14:21 -0800 | [diff] [blame] | 273 | printer->Print( |
| 274 | *vars, |
| 275 | "::grpc::ClientWriter< $Request$>* $Service$::Stub::$Method$(" |
| 276 | "::grpc::ClientContext* context, $Response$* response) {\n"); |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 277 | printer->Print(*vars, |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 278 | " return new ::grpc::ClientWriter< $Request$>(" |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 279 | "channel()->CreateStream(" |
| 280 | "::grpc::RpcMethod(\"/$Package$$Service$/$Method$\", " |
| 281 | "::grpc::RpcMethod::RpcType::CLIENT_STREAMING), " |
| 282 | "context, nullptr, response));\n" |
| 283 | "}\n\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 284 | } else if (ServerOnlyStreaming(method)) { |
| 285 | printer->Print( |
| 286 | *vars, |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 287 | "::grpc::ClientReader< $Response$>* $Service$::Stub::$Method$(" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 288 | "::grpc::ClientContext* context, const $Request$* request) {\n"); |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 289 | printer->Print(*vars, |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 290 | " return new ::grpc::ClientReader< $Response$>(" |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 291 | "channel()->CreateStream(" |
| 292 | "::grpc::RpcMethod(\"/$Package$$Service$/$Method$\", " |
| 293 | "::grpc::RpcMethod::RpcType::SERVER_STREAMING), " |
| 294 | "context, request, nullptr));\n" |
| 295 | "}\n\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 296 | } else if (BidiStreaming(method)) { |
| 297 | printer->Print( |
| 298 | *vars, |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 299 | "::grpc::ClientReaderWriter< $Request$, $Response$>* " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 300 | "$Service$::Stub::$Method$(::grpc::ClientContext* context) {\n"); |
| 301 | printer->Print( |
| 302 | *vars, |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 303 | " return new ::grpc::ClientReaderWriter< $Request$, $Response$>(" |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 304 | "channel()->CreateStream(" |
| 305 | "::grpc::RpcMethod(\"/$Package$$Service$/$Method$\", " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 306 | "::grpc::RpcMethod::RpcType::BIDI_STREAMING), " |
| 307 | "context, nullptr, nullptr));\n" |
| 308 | "}\n\n"); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | void PrintSourceServerMethod(google::protobuf::io::Printer* printer, |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 313 | const google::protobuf::MethodDescriptor* method, |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame^] | 314 | std::map<std::string, std::string>* vars) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 315 | (*vars)["Method"] = method->name(); |
| 316 | (*vars)["Request"] = |
| 317 | grpc_cpp_generator::ClassName(method->input_type(), true); |
| 318 | (*vars)["Response"] = |
| 319 | grpc_cpp_generator::ClassName(method->output_type(), true); |
| 320 | if (NoStreaming(method)) { |
| 321 | printer->Print(*vars, |
| 322 | "::grpc::Status $Service$::Service::$Method$(" |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 323 | "::grpc::ServerContext* context, " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 324 | "const $Request$* request, $Response$* response) {\n"); |
| 325 | printer->Print( |
| 326 | " return ::grpc::Status(" |
| 327 | "::grpc::StatusCode::UNIMPLEMENTED);\n"); |
| 328 | printer->Print("}\n\n"); |
| 329 | } else if (ClientOnlyStreaming(method)) { |
| 330 | printer->Print(*vars, |
| 331 | "::grpc::Status $Service$::Service::$Method$(" |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 332 | "::grpc::ServerContext* context, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 333 | "::grpc::ServerReader< $Request$>* reader, " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 334 | "$Response$* response) {\n"); |
| 335 | printer->Print( |
| 336 | " return ::grpc::Status(" |
| 337 | "::grpc::StatusCode::UNIMPLEMENTED);\n"); |
| 338 | printer->Print("}\n\n"); |
| 339 | } else if (ServerOnlyStreaming(method)) { |
| 340 | printer->Print(*vars, |
| 341 | "::grpc::Status $Service$::Service::$Method$(" |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 342 | "::grpc::ServerContext* context, " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 343 | "const $Request$* request, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 344 | "::grpc::ServerWriter< $Response$>* writer) {\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 345 | printer->Print( |
| 346 | " return ::grpc::Status(" |
| 347 | "::grpc::StatusCode::UNIMPLEMENTED);\n"); |
| 348 | printer->Print("}\n\n"); |
| 349 | } else if (BidiStreaming(method)) { |
| 350 | printer->Print(*vars, |
| 351 | "::grpc::Status $Service$::Service::$Method$(" |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 352 | "::grpc::ServerContext* context, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 353 | "::grpc::ServerReaderWriter< $Response$, $Request$>* " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 354 | "stream) {\n"); |
| 355 | printer->Print( |
| 356 | " return ::grpc::Status(" |
| 357 | "::grpc::StatusCode::UNIMPLEMENTED);\n"); |
| 358 | printer->Print("}\n\n"); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | void PrintSourceService(google::protobuf::io::Printer* printer, |
| 363 | const google::protobuf::ServiceDescriptor* service, |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame^] | 364 | std::map<std::string, std::string>* vars) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 365 | (*vars)["Service"] = service->name(); |
Yang Gao | 5680ff4 | 2015-01-14 12:14:21 -0800 | [diff] [blame] | 366 | printer->Print( |
| 367 | *vars, |
| 368 | "$Service$::Stub* $Service$::NewStub(" |
| 369 | "const std::shared_ptr< ::grpc::ChannelInterface>& channel) {\n" |
| 370 | " $Service$::Stub* stub = new $Service$::Stub();\n" |
| 371 | " stub->set_channel(channel);\n" |
| 372 | " return stub;\n" |
| 373 | "};\n\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 374 | for (int i = 0; i < service->method_count(); ++i) { |
| 375 | PrintSourceClientMethod(printer, service->method(i), vars); |
| 376 | } |
| 377 | |
| 378 | printer->Print(*vars, |
| 379 | "$Service$::Service::~Service() {\n" |
| 380 | " delete service_;\n" |
| 381 | "}\n\n"); |
| 382 | for (int i = 0; i < service->method_count(); ++i) { |
| 383 | PrintSourceServerMethod(printer, service->method(i), vars); |
| 384 | } |
| 385 | printer->Print(*vars, |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 386 | "::grpc::RpcService* $Service$::Service::service() {\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 387 | printer->Indent(); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 388 | printer->Print( |
| 389 | "if (service_ != nullptr) {\n" |
| 390 | " return service_;\n" |
| 391 | "}\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 392 | printer->Print("service_ = new ::grpc::RpcService();\n"); |
| 393 | for (int i = 0; i < service->method_count(); ++i) { |
| 394 | const google::protobuf::MethodDescriptor* method = service->method(i); |
| 395 | (*vars)["Method"] = method->name(); |
| 396 | (*vars)["Request"] = |
| 397 | grpc_cpp_generator::ClassName(method->input_type(), true); |
| 398 | (*vars)["Response"] = |
| 399 | grpc_cpp_generator::ClassName(method->output_type(), true); |
| 400 | if (NoStreaming(method)) { |
| 401 | printer->Print( |
| 402 | *vars, |
| 403 | "service_->AddMethod(new ::grpc::RpcServiceMethod(\n" |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 404 | " \"/$Package$$Service$/$Method$\",\n" |
| 405 | " ::grpc::RpcMethod::NORMAL_RPC,\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 406 | " new ::grpc::RpcMethodHandler< $Service$::Service, $Request$, " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 407 | "$Response$>(\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 408 | " std::function< ::grpc::Status($Service$::Service*, " |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 409 | "::grpc::ServerContext*, const $Request$*, $Response$*)>(" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 410 | "&$Service$::Service::$Method$), this),\n" |
| 411 | " new $Request$, new $Response$));\n"); |
| 412 | } else if (ClientOnlyStreaming(method)) { |
| 413 | printer->Print( |
| 414 | *vars, |
| 415 | "service_->AddMethod(new ::grpc::RpcServiceMethod(\n" |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 416 | " \"/$Package$$Service$/$Method$\",\n" |
| 417 | " ::grpc::RpcMethod::CLIENT_STREAMING,\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 418 | " new ::grpc::ClientStreamingHandler< " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 419 | "$Service$::Service, $Request$, $Response$>(\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 420 | " std::function< ::grpc::Status($Service$::Service*, " |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 421 | "::grpc::ServerContext*, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 422 | "::grpc::ServerReader< $Request$>*, $Response$*)>(" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 423 | "&$Service$::Service::$Method$), this),\n" |
| 424 | " new $Request$, new $Response$));\n"); |
| 425 | } else if (ServerOnlyStreaming(method)) { |
| 426 | printer->Print( |
| 427 | *vars, |
| 428 | "service_->AddMethod(new ::grpc::RpcServiceMethod(\n" |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 429 | " \"/$Package$$Service$/$Method$\",\n" |
| 430 | " ::grpc::RpcMethod::SERVER_STREAMING,\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 431 | " new ::grpc::ServerStreamingHandler< " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 432 | "$Service$::Service, $Request$, $Response$>(\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 433 | " std::function< ::grpc::Status($Service$::Service*, " |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 434 | "::grpc::ServerContext*, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 435 | "const $Request$*, ::grpc::ServerWriter< $Response$>*)>(" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 436 | "&$Service$::Service::$Method$), this),\n" |
| 437 | " new $Request$, new $Response$));\n"); |
| 438 | } else if (BidiStreaming(method)) { |
| 439 | printer->Print( |
| 440 | *vars, |
| 441 | "service_->AddMethod(new ::grpc::RpcServiceMethod(\n" |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 442 | " \"/$Package$$Service$/$Method$\",\n" |
| 443 | " ::grpc::RpcMethod::BIDI_STREAMING,\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 444 | " new ::grpc::BidiStreamingHandler< " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 445 | "$Service$::Service, $Request$, $Response$>(\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 446 | " std::function< ::grpc::Status($Service$::Service*, " |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 447 | "::grpc::ServerContext*, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 448 | "::grpc::ServerReaderWriter< $Response$, $Request$>*)>(" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 449 | "&$Service$::Service::$Method$), this),\n" |
| 450 | " new $Request$, new $Response$));\n"); |
| 451 | } |
| 452 | } |
| 453 | printer->Print("return service_;\n"); |
| 454 | printer->Outdent(); |
| 455 | printer->Print("}\n\n"); |
| 456 | } |
| 457 | |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame^] | 458 | std::string GetSourceServices(const google::protobuf::FileDescriptor* file) { |
| 459 | std::string output; |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 460 | google::protobuf::io::StringOutputStream output_stream(&output); |
| 461 | google::protobuf::io::Printer printer(&output_stream, '$'); |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame^] | 462 | std::map<std::string, std::string> vars; |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 463 | // Package string is empty or ends with a dot. It is used to fully qualify |
| 464 | // method names. |
| 465 | vars["Package"] = file->package(); |
| 466 | if (!file->package().empty()) { |
| 467 | vars["Package"].append("."); |
| 468 | } |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 469 | |
| 470 | for (int i = 0; i < file->service_count(); ++i) { |
samuelw | ca9f359 | 2014-12-15 14:22:04 -0800 | [diff] [blame] | 471 | PrintSourceService(&printer, file->service(i), &vars); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 472 | printer.Print("\n"); |
| 473 | } |
| 474 | return output; |
| 475 | } |
| 476 | |
| 477 | } // namespace grpc_cpp_generator |