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 | |
Nicolas "Pixel" Noble | 36f5323 | 2015-01-16 06:39:58 +0100 | [diff] [blame] | 34 | #include <string> |
| 35 | #include <map> |
| 36 | |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 37 | #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 | |
| 45 | namespace grpc_cpp_generator { |
| 46 | namespace { |
| 47 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 48 | bool NoStreaming(const google::protobuf::MethodDescriptor *method) { |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 49 | return !method->client_streaming() && !method->server_streaming(); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 52 | bool ClientOnlyStreaming(const google::protobuf::MethodDescriptor *method) { |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 53 | return method->client_streaming() && !method->server_streaming(); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 56 | bool ServerOnlyStreaming(const google::protobuf::MethodDescriptor *method) { |
yangg | 1b15109 | 2015-01-09 15:31:05 -0800 | [diff] [blame] | 57 | return !method->client_streaming() && method->server_streaming(); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 60 | bool BidiStreaming(const google::protobuf::MethodDescriptor *method) { |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 61 | return method->client_streaming() && method->server_streaming(); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 64 | bool HasClientOnlyStreaming(const google::protobuf::FileDescriptor *file) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 65 | 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 Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 75 | bool HasServerOnlyStreaming(const google::protobuf::FileDescriptor *file) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 76 | 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 Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 86 | bool HasBidiStreaming(const google::protobuf::FileDescriptor *file) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 87 | 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 Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 98 | std::string GetHeaderIncludes(const google::protobuf::FileDescriptor *file) { |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 99 | std::string temp = |
yangg | 1b15109 | 2015-01-09 15:31:05 -0800 | [diff] [blame] | 100 | "#include \"grpc++/impl/internal_stub.h\"\n" |
temiola | 33a2168 | 2014-12-11 15:13:40 -0800 | [diff] [blame] | 101 | "#include \"grpc++/status.h\"\n" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 102 | "\n" |
| 103 | "namespace grpc {\n" |
| 104 | "class ChannelInterface;\n" |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 105 | "class RpcService;\n" |
| 106 | "class ServerContext;\n"; |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 107 | 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 Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 127 | std::string GetSourceIncludes() { |
yangg | 1b15109 | 2015-01-09 15:31:05 -0800 | [diff] [blame] | 128 | return "#include \"grpc++/channel_interface.h\"\n" |
| 129 | "#include \"grpc++/impl/rpc_method.h\"\n" |
| 130 | "#include \"grpc++/impl/rpc_service_method.h\"\n" |
temiola | 33a2168 | 2014-12-11 15:13:40 -0800 | [diff] [blame] | 131 | "#include \"grpc++/stream.h\"\n"; |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 134 | void PrintHeaderClientMethod(google::protobuf::io::Printer *printer, |
| 135 | const google::protobuf::MethodDescriptor *method, |
| 136 | std::map<std::string, std::string> *vars) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 137 | (*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 Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 149 | "::grpc::ClientWriter< $Request$>* $Method$(" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 150 | "::grpc::ClientContext* context, $Response$* response);\n\n"); |
| 151 | } else if (ServerOnlyStreaming(method)) { |
| 152 | printer->Print( |
| 153 | *vars, |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 154 | "::grpc::ClientReader< $Response$>* $Method$(" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 155 | "::grpc::ClientContext* context, const $Request$* request);\n\n"); |
| 156 | } else if (BidiStreaming(method)) { |
| 157 | printer->Print(*vars, |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 158 | "::grpc::ClientReaderWriter< $Request$, $Response$>* " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 159 | "$Method$(::grpc::ClientContext* context);\n\n"); |
| 160 | } |
| 161 | } |
| 162 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 163 | void PrintHeaderServerMethod(google::protobuf::io::Printer *printer, |
| 164 | const google::protobuf::MethodDescriptor *method, |
| 165 | std::map<std::string, std::string> *vars) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 166 | (*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, |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 173 | "virtual ::grpc::Status $Method$(" |
| 174 | "::grpc::ServerContext* context, const $Request$* request, " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 175 | "$Response$* response);\n"); |
| 176 | } else if (ClientOnlyStreaming(method)) { |
| 177 | printer->Print(*vars, |
| 178 | "virtual ::grpc::Status $Method$(" |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 179 | "::grpc::ServerContext* context, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 180 | "::grpc::ServerReader< $Request$>* reader, " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 181 | "$Response$* response);\n"); |
| 182 | } else if (ServerOnlyStreaming(method)) { |
| 183 | printer->Print(*vars, |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 184 | "virtual ::grpc::Status $Method$(" |
| 185 | "::grpc::ServerContext* context, const $Request$* request, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 186 | "::grpc::ServerWriter< $Response$>* writer);\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 187 | } else if (BidiStreaming(method)) { |
Yang Gao | 5680ff4 | 2015-01-14 12:14:21 -0800 | [diff] [blame] | 188 | printer->Print( |
| 189 | *vars, |
| 190 | "virtual ::grpc::Status $Method$(" |
| 191 | "::grpc::ServerContext* context, " |
| 192 | "::grpc::ServerReaderWriter< $Response$, $Request$>* stream);" |
| 193 | "\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 197 | void PrintHeaderService(google::protobuf::io::Printer *printer, |
| 198 | const google::protobuf::ServiceDescriptor *service, |
| 199 | std::map<std::string, std::string> *vars) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 200 | (*vars)["Service"] = service->name(); |
| 201 | |
| 202 | printer->Print(*vars, |
| 203 | "class $Service$ {\n" |
| 204 | " public:\n"); |
| 205 | printer->Indent(); |
| 206 | |
| 207 | // Client side |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 208 | printer->Print( |
| 209 | "class Stub : public ::grpc::InternalStub {\n" |
| 210 | " public:\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 211 | 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 Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 218 | "static Stub* NewStub(const std::shared_ptr< ::grpc::ChannelInterface>& " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 219 | "channel);\n"); |
| 220 | |
| 221 | printer->Print("\n"); |
| 222 | |
| 223 | // Server side |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 224 | printer->Print( |
| 225 | "class Service {\n" |
| 226 | " public:\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 227 | 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 Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 235 | printer->Print( |
| 236 | " private:\n" |
| 237 | " ::grpc::RpcService* service_;\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 238 | printer->Print("};\n"); |
| 239 | |
| 240 | printer->Outdent(); |
| 241 | printer->Print("};\n"); |
| 242 | } |
| 243 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 244 | std::string GetHeaderServices(const google::protobuf::FileDescriptor *file) { |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 245 | std::string output; |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 246 | google::protobuf::io::StringOutputStream output_stream(&output); |
| 247 | google::protobuf::io::Printer printer(&output_stream, '$'); |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 248 | std::map<std::string, std::string> vars; |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 249 | |
| 250 | for (int i = 0; i < file->service_count(); ++i) { |
samuelw | ca9f359 | 2014-12-15 14:22:04 -0800 | [diff] [blame] | 251 | PrintHeaderService(&printer, file->service(i), &vars); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 252 | printer.Print("\n"); |
| 253 | } |
| 254 | return output; |
| 255 | } |
| 256 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 257 | void PrintSourceClientMethod(google::protobuf::io::Printer *printer, |
| 258 | const google::protobuf::MethodDescriptor *method, |
| 259 | std::map<std::string, std::string> *vars) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 260 | (*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(" |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 272 | "::grpc::RpcMethod(\"/$Package$$Service$/$Method$\"), " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 273 | "context, request, response);\n" |
| 274 | "}\n\n"); |
| 275 | } else if (ClientOnlyStreaming(method)) { |
Yang Gao | 5680ff4 | 2015-01-14 12:14:21 -0800 | [diff] [blame] | 276 | printer->Print( |
| 277 | *vars, |
| 278 | "::grpc::ClientWriter< $Request$>* $Service$::Stub::$Method$(" |
| 279 | "::grpc::ClientContext* context, $Response$* response) {\n"); |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 280 | printer->Print(*vars, |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 281 | " return new ::grpc::ClientWriter< $Request$>(" |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 282 | "channel()->CreateStream(" |
| 283 | "::grpc::RpcMethod(\"/$Package$$Service$/$Method$\", " |
| 284 | "::grpc::RpcMethod::RpcType::CLIENT_STREAMING), " |
| 285 | "context, nullptr, response));\n" |
| 286 | "}\n\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 287 | } else if (ServerOnlyStreaming(method)) { |
| 288 | printer->Print( |
| 289 | *vars, |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 290 | "::grpc::ClientReader< $Response$>* $Service$::Stub::$Method$(" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 291 | "::grpc::ClientContext* context, const $Request$* request) {\n"); |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 292 | printer->Print(*vars, |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 293 | " return new ::grpc::ClientReader< $Response$>(" |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 294 | "channel()->CreateStream(" |
| 295 | "::grpc::RpcMethod(\"/$Package$$Service$/$Method$\", " |
| 296 | "::grpc::RpcMethod::RpcType::SERVER_STREAMING), " |
| 297 | "context, request, nullptr));\n" |
| 298 | "}\n\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 299 | } else if (BidiStreaming(method)) { |
| 300 | printer->Print( |
| 301 | *vars, |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 302 | "::grpc::ClientReaderWriter< $Request$, $Response$>* " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 303 | "$Service$::Stub::$Method$(::grpc::ClientContext* context) {\n"); |
| 304 | printer->Print( |
| 305 | *vars, |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 306 | " return new ::grpc::ClientReaderWriter< $Request$, $Response$>(" |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 307 | "channel()->CreateStream(" |
| 308 | "::grpc::RpcMethod(\"/$Package$$Service$/$Method$\", " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 309 | "::grpc::RpcMethod::RpcType::BIDI_STREAMING), " |
| 310 | "context, nullptr, nullptr));\n" |
| 311 | "}\n\n"); |
| 312 | } |
| 313 | } |
| 314 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 315 | void PrintSourceServerMethod(google::protobuf::io::Printer *printer, |
| 316 | const google::protobuf::MethodDescriptor *method, |
| 317 | std::map<std::string, std::string> *vars) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 318 | (*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$(" |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 326 | "::grpc::ServerContext* context, " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 327 | "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$(" |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 335 | "::grpc::ServerContext* context, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 336 | "::grpc::ServerReader< $Request$>* reader, " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 337 | "$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$(" |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 345 | "::grpc::ServerContext* context, " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 346 | "const $Request$* request, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 347 | "::grpc::ServerWriter< $Response$>* writer) {\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 348 | 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$(" |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 355 | "::grpc::ServerContext* context, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 356 | "::grpc::ServerReaderWriter< $Response$, $Request$>* " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 357 | "stream) {\n"); |
| 358 | printer->Print( |
| 359 | " return ::grpc::Status(" |
| 360 | "::grpc::StatusCode::UNIMPLEMENTED);\n"); |
| 361 | printer->Print("}\n\n"); |
| 362 | } |
| 363 | } |
| 364 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 365 | void PrintSourceService(google::protobuf::io::Printer *printer, |
| 366 | const google::protobuf::ServiceDescriptor *service, |
| 367 | std::map<std::string, std::string> *vars) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 368 | (*vars)["Service"] = service->name(); |
Yang Gao | 5680ff4 | 2015-01-14 12:14:21 -0800 | [diff] [blame] | 369 | 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"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 377 | 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 Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 389 | "::grpc::RpcService* $Service$::Service::service() {\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 390 | printer->Indent(); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 391 | printer->Print( |
| 392 | "if (service_ != nullptr) {\n" |
| 393 | " return service_;\n" |
| 394 | "}\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 395 | printer->Print("service_ = new ::grpc::RpcService();\n"); |
| 396 | for (int i = 0; i < service->method_count(); ++i) { |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 397 | const google::protobuf::MethodDescriptor *method = service->method(i); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 398 | (*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" |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 407 | " \"/$Package$$Service$/$Method$\",\n" |
| 408 | " ::grpc::RpcMethod::NORMAL_RPC,\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 409 | " new ::grpc::RpcMethodHandler< $Service$::Service, $Request$, " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 410 | "$Response$>(\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 411 | " std::function< ::grpc::Status($Service$::Service*, " |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 412 | "::grpc::ServerContext*, const $Request$*, $Response$*)>(" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 413 | "&$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" |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 419 | " \"/$Package$$Service$/$Method$\",\n" |
| 420 | " ::grpc::RpcMethod::CLIENT_STREAMING,\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 421 | " new ::grpc::ClientStreamingHandler< " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 422 | "$Service$::Service, $Request$, $Response$>(\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 423 | " std::function< ::grpc::Status($Service$::Service*, " |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 424 | "::grpc::ServerContext*, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 425 | "::grpc::ServerReader< $Request$>*, $Response$*)>(" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 426 | "&$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" |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 432 | " \"/$Package$$Service$/$Method$\",\n" |
| 433 | " ::grpc::RpcMethod::SERVER_STREAMING,\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 434 | " new ::grpc::ServerStreamingHandler< " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 435 | "$Service$::Service, $Request$, $Response$>(\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 436 | " std::function< ::grpc::Status($Service$::Service*, " |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 437 | "::grpc::ServerContext*, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 438 | "const $Request$*, ::grpc::ServerWriter< $Response$>*)>(" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 439 | "&$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" |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 445 | " \"/$Package$$Service$/$Method$\",\n" |
| 446 | " ::grpc::RpcMethod::BIDI_STREAMING,\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 447 | " new ::grpc::BidiStreamingHandler< " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 448 | "$Service$::Service, $Request$, $Response$>(\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 449 | " std::function< ::grpc::Status($Service$::Service*, " |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 450 | "::grpc::ServerContext*, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 451 | "::grpc::ServerReaderWriter< $Response$, $Request$>*)>(" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 452 | "&$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 Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 461 | std::string GetSourceServices(const google::protobuf::FileDescriptor *file) { |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 462 | std::string output; |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 463 | google::protobuf::io::StringOutputStream output_stream(&output); |
| 464 | google::protobuf::io::Printer printer(&output_stream, '$'); |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 465 | std::map<std::string, std::string> vars; |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 466 | // 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 | } |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 472 | |
| 473 | for (int i = 0; i < file->service_count(); ++i) { |
samuelw | ca9f359 | 2014-12-15 14:22:04 -0800 | [diff] [blame] | 474 | PrintSourceService(&printer, file->service(i), &vars); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 475 | printer.Print("\n"); |
| 476 | } |
| 477 | return output; |
| 478 | } |
| 479 | |
| 480 | } // namespace grpc_cpp_generator |