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 | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 64 | bool HasUnaryCalls(const google::protobuf::FileDescriptor *file) { |
| 65 | for (int i = 0; i < file->service_count(); i++) { |
| 66 | for (int j = 0; j < file->service(i)->method_count(); j++) { |
| 67 | if (NoStreaming(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 HasClientOnlyStreaming(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 (ClientOnlyStreaming(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 HasServerOnlyStreaming(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 (ServerOnlyStreaming(file->service(i)->method(j))) { |
| 90 | return true; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | return false; |
| 95 | } |
| 96 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 97 | bool HasBidiStreaming(const google::protobuf::FileDescriptor *file) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 98 | for (int i = 0; i < file->service_count(); i++) { |
| 99 | for (int j = 0; j < file->service(i)->method_count(); j++) { |
| 100 | if (BidiStreaming(file->service(i)->method(j))) { |
| 101 | return true; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | return false; |
| 106 | } |
| 107 | } // namespace |
| 108 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 109 | std::string GetHeaderIncludes(const google::protobuf::FileDescriptor *file) { |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 110 | std::string temp = |
Craig Tiller | 14a65f9 | 2015-02-09 13:13:14 -0800 | [diff] [blame] | 111 | "#include <grpc++/impl/internal_stub.h>\n" |
| 112 | "#include <grpc++/impl/service_type.h>\n" |
| 113 | "#include <grpc++/status.h>\n" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 114 | "\n" |
| 115 | "namespace grpc {\n" |
| 116 | "class ChannelInterface;\n" |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 117 | "class RpcService;\n" |
| 118 | "class ServerContext;\n"; |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 119 | if (HasUnaryCalls(file)) { |
Craig Tiller | 5ef5db1 | 2015-02-09 12:47:21 -0800 | [diff] [blame] | 120 | temp.append( |
| 121 | "template <class OutMessage> class ServerAsyncResponseWriter;\n"); |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 122 | } |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 123 | if (HasClientOnlyStreaming(file)) { |
| 124 | temp.append("template <class OutMessage> class ClientWriter;\n"); |
| 125 | temp.append("template <class InMessage> class ServerReader;\n"); |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 126 | temp.append("template <class OutMessage> class ClientAsyncWriter;\n"); |
| 127 | temp.append("template <class InMessage> class ServerAsyncReader;\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 128 | } |
| 129 | if (HasServerOnlyStreaming(file)) { |
| 130 | temp.append("template <class InMessage> class ClientReader;\n"); |
| 131 | temp.append("template <class OutMessage> class ServerWriter;\n"); |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 132 | temp.append("template <class OutMessage> class ClientAsyncReader;\n"); |
| 133 | temp.append("template <class InMessage> class ServerAsyncWriter;\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 134 | } |
| 135 | if (HasBidiStreaming(file)) { |
| 136 | temp.append( |
| 137 | "template <class OutMessage, class InMessage>\n" |
| 138 | "class ClientReaderWriter;\n"); |
| 139 | temp.append( |
| 140 | "template <class OutMessage, class InMessage>\n" |
| 141 | "class ServerReaderWriter;\n"); |
| 142 | } |
| 143 | temp.append("} // namespace grpc\n"); |
| 144 | return temp; |
| 145 | } |
| 146 | |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 147 | std::string GetSourceIncludes() { |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 148 | return "#include <grpc++/channel_interface.h>\n" |
| 149 | "#include <grpc++/impl/rpc_method.h>\n" |
| 150 | "#include <grpc++/impl/rpc_service_method.h>\n" |
Craig Tiller | 14a65f9 | 2015-02-09 13:13:14 -0800 | [diff] [blame] | 151 | "#include <grpc++/impl/service_type.h>\n" |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 152 | "#include <grpc++/stream.h>\n"; |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 155 | void PrintHeaderClientMethod(google::protobuf::io::Printer *printer, |
| 156 | const google::protobuf::MethodDescriptor *method, |
| 157 | std::map<std::string, std::string> *vars) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 158 | (*vars)["Method"] = method->name(); |
| 159 | (*vars)["Request"] = |
| 160 | grpc_cpp_generator::ClassName(method->input_type(), true); |
| 161 | (*vars)["Response"] = |
| 162 | grpc_cpp_generator::ClassName(method->output_type(), true); |
| 163 | if (NoStreaming(method)) { |
| 164 | printer->Print(*vars, |
| 165 | "::grpc::Status $Method$(::grpc::ClientContext* context, " |
Craig Tiller | 5ef5db1 | 2015-02-09 12:47:21 -0800 | [diff] [blame] | 166 | "const $Request$& request, $Response$* response);\n"); |
| 167 | printer->Print(*vars, |
| 168 | "void $Method$(::grpc::ClientContext* context, " |
| 169 | "const $Request$& request, $Response$* response, " |
Craig Tiller | 14a65f9 | 2015-02-09 13:13:14 -0800 | [diff] [blame] | 170 | "::grpc::Status *status, " |
| 171 | "::grpc::CompletionQueue *cq, void *tag);\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 172 | } else if (ClientOnlyStreaming(method)) { |
Craig Tiller | 5ef5db1 | 2015-02-09 12:47:21 -0800 | [diff] [blame] | 173 | printer->Print(*vars, |
| 174 | "::grpc::ClientWriter< $Request$>* $Method$(" |
| 175 | "::grpc::ClientContext* context, $Response$* response);\n"); |
| 176 | printer->Print(*vars, |
| 177 | "::grpc::ClientWriter< $Request$>* $Method$(" |
| 178 | "::grpc::ClientContext* context, $Response$* response, " |
Craig Tiller | 14a65f9 | 2015-02-09 13:13:14 -0800 | [diff] [blame] | 179 | "::grpc::Status *status, " |
| 180 | "::grpc::CompletionQueue *cq, void *tag);\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 181 | } else if (ServerOnlyStreaming(method)) { |
| 182 | printer->Print( |
| 183 | *vars, |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 184 | "::grpc::ClientReader< $Response$>* $Method$(" |
Craig Tiller | 5ef5db1 | 2015-02-09 12:47:21 -0800 | [diff] [blame] | 185 | "::grpc::ClientContext* context, const $Request$* request);\n"); |
| 186 | printer->Print(*vars, |
| 187 | "::grpc::ClientReader< $Response$>* $Method$(" |
| 188 | "::grpc::ClientContext* context, const $Request$* request, " |
Craig Tiller | 14a65f9 | 2015-02-09 13:13:14 -0800 | [diff] [blame] | 189 | "::grpc::CompletionQueue *cq, void *tag);\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 190 | } else if (BidiStreaming(method)) { |
| 191 | printer->Print(*vars, |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 192 | "::grpc::ClientReaderWriter< $Request$, $Response$>* " |
Craig Tiller | 5ef5db1 | 2015-02-09 12:47:21 -0800 | [diff] [blame] | 193 | "$Method$(::grpc::ClientContext* context);\n"); |
| 194 | printer->Print(*vars, |
| 195 | "::grpc::ClientReaderWriter< $Request$, $Response$>* " |
| 196 | "$Method$(::grpc::ClientContext* context, " |
Craig Tiller | 14a65f9 | 2015-02-09 13:13:14 -0800 | [diff] [blame] | 197 | "::grpc::CompletionQueue *cq, void *tag);\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
Craig Tiller | 5ef5db1 | 2015-02-09 12:47:21 -0800 | [diff] [blame] | 201 | void PrintHeaderServerMethodSync( |
| 202 | google::protobuf::io::Printer *printer, |
| 203 | const google::protobuf::MethodDescriptor *method, |
| 204 | std::map<std::string, std::string> *vars) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 205 | (*vars)["Method"] = method->name(); |
| 206 | (*vars)["Request"] = |
| 207 | grpc_cpp_generator::ClassName(method->input_type(), true); |
| 208 | (*vars)["Response"] = |
| 209 | grpc_cpp_generator::ClassName(method->output_type(), true); |
| 210 | if (NoStreaming(method)) { |
| 211 | printer->Print(*vars, |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 212 | "virtual ::grpc::Status $Method$(" |
| 213 | "::grpc::ServerContext* context, const $Request$* request, " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 214 | "$Response$* response);\n"); |
| 215 | } else if (ClientOnlyStreaming(method)) { |
| 216 | printer->Print(*vars, |
| 217 | "virtual ::grpc::Status $Method$(" |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 218 | "::grpc::ServerContext* context, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 219 | "::grpc::ServerReader< $Request$>* reader, " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 220 | "$Response$* response);\n"); |
| 221 | } else if (ServerOnlyStreaming(method)) { |
| 222 | printer->Print(*vars, |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 223 | "virtual ::grpc::Status $Method$(" |
| 224 | "::grpc::ServerContext* context, const $Request$* request, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 225 | "::grpc::ServerWriter< $Response$>* writer);\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 226 | } else if (BidiStreaming(method)) { |
Yang Gao | 5680ff4 | 2015-01-14 12:14:21 -0800 | [diff] [blame] | 227 | printer->Print( |
| 228 | *vars, |
| 229 | "virtual ::grpc::Status $Method$(" |
| 230 | "::grpc::ServerContext* context, " |
| 231 | "::grpc::ServerReaderWriter< $Response$, $Request$>* stream);" |
| 232 | "\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
Craig Tiller | 5ef5db1 | 2015-02-09 12:47:21 -0800 | [diff] [blame] | 236 | void PrintHeaderServerMethodAsync( |
| 237 | google::protobuf::io::Printer *printer, |
| 238 | const google::protobuf::MethodDescriptor *method, |
| 239 | std::map<std::string, std::string> *vars) { |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 240 | (*vars)["Method"] = method->name(); |
| 241 | (*vars)["Request"] = |
| 242 | grpc_cpp_generator::ClassName(method->input_type(), true); |
| 243 | (*vars)["Response"] = |
| 244 | grpc_cpp_generator::ClassName(method->output_type(), true); |
| 245 | if (NoStreaming(method)) { |
| 246 | printer->Print(*vars, |
| 247 | "void $Method$(" |
| 248 | "::grpc::ServerContext* context, $Request$* request, " |
| 249 | "::grpc::ServerAsyncResponseWriter< $Response$>* response, " |
| 250 | "::grpc::CompletionQueue* cq, void *tag);\n"); |
| 251 | } else if (ClientOnlyStreaming(method)) { |
| 252 | printer->Print(*vars, |
| 253 | "void $Method$(" |
| 254 | "::grpc::ServerContext* context, " |
| 255 | "::grpc::ServerAsyncReader< $Request$>* reader, " |
| 256 | "$Response$* response, " |
| 257 | "::grpc::CompletionQueue* cq, void *tag);\n"); |
| 258 | } else if (ServerOnlyStreaming(method)) { |
| 259 | printer->Print(*vars, |
| 260 | "void $Method$(" |
| 261 | "::grpc::ServerContext* context, $Request$* request, " |
| 262 | "::grpc::ServerAsyncWriter< $Response$>* writer, " |
| 263 | "::grpc::CompletionQueue* cq, void *tag);\n"); |
| 264 | } else if (BidiStreaming(method)) { |
| 265 | printer->Print( |
| 266 | *vars, |
| 267 | "void $Method$(" |
| 268 | "::grpc::ServerContext* context, " |
| 269 | "::grpc::ServerReaderWriter< $Response$, $Request$>* stream, " |
Craig Tiller | 5ef5db1 | 2015-02-09 12:47:21 -0800 | [diff] [blame] | 270 | "::grpc::CompletionQueue* cq, void *tag);\n"); |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 274 | void PrintHeaderService(google::protobuf::io::Printer *printer, |
| 275 | const google::protobuf::ServiceDescriptor *service, |
| 276 | std::map<std::string, std::string> *vars) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 277 | (*vars)["Service"] = service->name(); |
| 278 | |
| 279 | printer->Print(*vars, |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 280 | "class $Service$ final {\n" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 281 | " public:\n"); |
| 282 | printer->Indent(); |
| 283 | |
| 284 | // Client side |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 285 | printer->Print( |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 286 | "class Stub final : public ::grpc::InternalStub {\n" |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 287 | " public:\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 288 | printer->Indent(); |
| 289 | for (int i = 0; i < service->method_count(); ++i) { |
| 290 | PrintHeaderClientMethod(printer, service->method(i), vars); |
| 291 | } |
| 292 | printer->Outdent(); |
| 293 | printer->Print("};\n"); |
| 294 | printer->Print( |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 295 | "static Stub* NewStub(const std::shared_ptr< ::grpc::ChannelInterface>& " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 296 | "channel);\n"); |
| 297 | |
| 298 | printer->Print("\n"); |
| 299 | |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 300 | // Server side - Synchronous |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 301 | printer->Print( |
Craig Tiller | 14a65f9 | 2015-02-09 13:13:14 -0800 | [diff] [blame] | 302 | "class Service : public ::grpc::SynchronousService {\n" |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 303 | " public:\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 304 | printer->Indent(); |
| 305 | printer->Print("Service() : service_(nullptr) {}\n"); |
| 306 | printer->Print("virtual ~Service();\n"); |
| 307 | for (int i = 0; i < service->method_count(); ++i) { |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 308 | PrintHeaderServerMethodSync(printer, service->method(i), vars); |
| 309 | } |
| 310 | printer->Print("::grpc::RpcService* service();\n"); |
| 311 | printer->Outdent(); |
| 312 | printer->Print( |
| 313 | " private:\n" |
| 314 | " ::grpc::RpcService* service_;\n"); |
| 315 | printer->Print("};\n"); |
| 316 | |
| 317 | // Server side - Asynchronous |
| 318 | printer->Print( |
Craig Tiller | 14a65f9 | 2015-02-09 13:13:14 -0800 | [diff] [blame] | 319 | "class AsyncService final : public ::grpc::AsynchronousService {\n" |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 320 | " public:\n"); |
| 321 | printer->Indent(); |
| 322 | printer->Print("AsyncService() : service_(nullptr) {}\n"); |
| 323 | printer->Print("~AsyncService();\n"); |
| 324 | for (int i = 0; i < service->method_count(); ++i) { |
| 325 | PrintHeaderServerMethodAsync(printer, service->method(i), vars); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 326 | } |
| 327 | printer->Print("::grpc::RpcService* service();\n"); |
| 328 | printer->Outdent(); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 329 | printer->Print( |
| 330 | " private:\n" |
| 331 | " ::grpc::RpcService* service_;\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 332 | printer->Print("};\n"); |
| 333 | |
| 334 | printer->Outdent(); |
| 335 | printer->Print("};\n"); |
| 336 | } |
| 337 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 338 | std::string GetHeaderServices(const google::protobuf::FileDescriptor *file) { |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 339 | std::string output; |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 340 | google::protobuf::io::StringOutputStream output_stream(&output); |
| 341 | google::protobuf::io::Printer printer(&output_stream, '$'); |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 342 | std::map<std::string, std::string> vars; |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 343 | |
| 344 | for (int i = 0; i < file->service_count(); ++i) { |
samuelw | ca9f359 | 2014-12-15 14:22:04 -0800 | [diff] [blame] | 345 | PrintHeaderService(&printer, file->service(i), &vars); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 346 | printer.Print("\n"); |
| 347 | } |
| 348 | return output; |
| 349 | } |
| 350 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 351 | void PrintSourceClientMethod(google::protobuf::io::Printer *printer, |
| 352 | const google::protobuf::MethodDescriptor *method, |
| 353 | std::map<std::string, std::string> *vars) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 354 | (*vars)["Method"] = method->name(); |
| 355 | (*vars)["Request"] = |
| 356 | grpc_cpp_generator::ClassName(method->input_type(), true); |
| 357 | (*vars)["Response"] = |
| 358 | grpc_cpp_generator::ClassName(method->output_type(), true); |
| 359 | if (NoStreaming(method)) { |
| 360 | printer->Print(*vars, |
| 361 | "::grpc::Status $Service$::Stub::$Method$(" |
| 362 | "::grpc::ClientContext* context, " |
| 363 | "const $Request$& request, $Response$* response) {\n"); |
| 364 | printer->Print(*vars, |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 365 | "return ::grpc::BlockingUnaryCall(channel()," |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 366 | "::grpc::RpcMethod(\"/$Package$$Service$/$Method$\"), " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 367 | "context, request, response);\n" |
| 368 | "}\n\n"); |
| 369 | } else if (ClientOnlyStreaming(method)) { |
Yang Gao | 5680ff4 | 2015-01-14 12:14:21 -0800 | [diff] [blame] | 370 | printer->Print( |
| 371 | *vars, |
| 372 | "::grpc::ClientWriter< $Request$>* $Service$::Stub::$Method$(" |
| 373 | "::grpc::ClientContext* context, $Response$* response) {\n"); |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 374 | printer->Print(*vars, |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 375 | " return new ::grpc::ClientWriter< $Request$>(" |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 376 | "channel()," |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 377 | "::grpc::RpcMethod(\"/$Package$$Service$/$Method$\", " |
| 378 | "::grpc::RpcMethod::RpcType::CLIENT_STREAMING), " |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 379 | "context, response);\n" |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 380 | "}\n\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 381 | } else if (ServerOnlyStreaming(method)) { |
| 382 | printer->Print( |
| 383 | *vars, |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 384 | "::grpc::ClientReader< $Response$>* $Service$::Stub::$Method$(" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 385 | "::grpc::ClientContext* context, const $Request$* request) {\n"); |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 386 | printer->Print(*vars, |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 387 | " return new ::grpc::ClientReader< $Response$>(" |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 388 | "channel()," |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 389 | "::grpc::RpcMethod(\"/$Package$$Service$/$Method$\", " |
| 390 | "::grpc::RpcMethod::RpcType::SERVER_STREAMING), " |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 391 | "context, *request);\n" |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 392 | "}\n\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 393 | } else if (BidiStreaming(method)) { |
| 394 | printer->Print( |
| 395 | *vars, |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 396 | "::grpc::ClientReaderWriter< $Request$, $Response$>* " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 397 | "$Service$::Stub::$Method$(::grpc::ClientContext* context) {\n"); |
| 398 | printer->Print( |
| 399 | *vars, |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 400 | " return new ::grpc::ClientReaderWriter< $Request$, $Response$>(" |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 401 | "channel()," |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 402 | "::grpc::RpcMethod(\"/$Package$$Service$/$Method$\", " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 403 | "::grpc::RpcMethod::RpcType::BIDI_STREAMING), " |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 404 | "context);\n" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 405 | "}\n\n"); |
| 406 | } |
| 407 | } |
| 408 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 409 | void PrintSourceServerMethod(google::protobuf::io::Printer *printer, |
| 410 | const google::protobuf::MethodDescriptor *method, |
| 411 | std::map<std::string, std::string> *vars) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 412 | (*vars)["Method"] = method->name(); |
| 413 | (*vars)["Request"] = |
| 414 | grpc_cpp_generator::ClassName(method->input_type(), true); |
| 415 | (*vars)["Response"] = |
| 416 | grpc_cpp_generator::ClassName(method->output_type(), true); |
| 417 | if (NoStreaming(method)) { |
| 418 | printer->Print(*vars, |
| 419 | "::grpc::Status $Service$::Service::$Method$(" |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 420 | "::grpc::ServerContext* context, " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 421 | "const $Request$* request, $Response$* response) {\n"); |
| 422 | printer->Print( |
| 423 | " return ::grpc::Status(" |
| 424 | "::grpc::StatusCode::UNIMPLEMENTED);\n"); |
| 425 | printer->Print("}\n\n"); |
| 426 | } else if (ClientOnlyStreaming(method)) { |
| 427 | printer->Print(*vars, |
| 428 | "::grpc::Status $Service$::Service::$Method$(" |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 429 | "::grpc::ServerContext* context, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 430 | "::grpc::ServerReader< $Request$>* reader, " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 431 | "$Response$* response) {\n"); |
| 432 | printer->Print( |
| 433 | " return ::grpc::Status(" |
| 434 | "::grpc::StatusCode::UNIMPLEMENTED);\n"); |
| 435 | printer->Print("}\n\n"); |
| 436 | } else if (ServerOnlyStreaming(method)) { |
| 437 | printer->Print(*vars, |
| 438 | "::grpc::Status $Service$::Service::$Method$(" |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 439 | "::grpc::ServerContext* context, " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 440 | "const $Request$* request, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 441 | "::grpc::ServerWriter< $Response$>* writer) {\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 442 | printer->Print( |
| 443 | " return ::grpc::Status(" |
| 444 | "::grpc::StatusCode::UNIMPLEMENTED);\n"); |
| 445 | printer->Print("}\n\n"); |
| 446 | } else if (BidiStreaming(method)) { |
| 447 | printer->Print(*vars, |
| 448 | "::grpc::Status $Service$::Service::$Method$(" |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 449 | "::grpc::ServerContext* context, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 450 | "::grpc::ServerReaderWriter< $Response$, $Request$>* " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 451 | "stream) {\n"); |
| 452 | printer->Print( |
| 453 | " return ::grpc::Status(" |
| 454 | "::grpc::StatusCode::UNIMPLEMENTED);\n"); |
| 455 | printer->Print("}\n\n"); |
| 456 | } |
| 457 | } |
| 458 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 459 | void PrintSourceService(google::protobuf::io::Printer *printer, |
| 460 | const google::protobuf::ServiceDescriptor *service, |
| 461 | std::map<std::string, std::string> *vars) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 462 | (*vars)["Service"] = service->name(); |
Yang Gao | 5680ff4 | 2015-01-14 12:14:21 -0800 | [diff] [blame] | 463 | printer->Print( |
| 464 | *vars, |
| 465 | "$Service$::Stub* $Service$::NewStub(" |
| 466 | "const std::shared_ptr< ::grpc::ChannelInterface>& channel) {\n" |
| 467 | " $Service$::Stub* stub = new $Service$::Stub();\n" |
| 468 | " stub->set_channel(channel);\n" |
| 469 | " return stub;\n" |
| 470 | "};\n\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 471 | for (int i = 0; i < service->method_count(); ++i) { |
| 472 | PrintSourceClientMethod(printer, service->method(i), vars); |
| 473 | } |
| 474 | |
| 475 | printer->Print(*vars, |
| 476 | "$Service$::Service::~Service() {\n" |
| 477 | " delete service_;\n" |
| 478 | "}\n\n"); |
| 479 | for (int i = 0; i < service->method_count(); ++i) { |
| 480 | PrintSourceServerMethod(printer, service->method(i), vars); |
| 481 | } |
| 482 | printer->Print(*vars, |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 483 | "::grpc::RpcService* $Service$::Service::service() {\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 484 | printer->Indent(); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 485 | printer->Print( |
| 486 | "if (service_ != nullptr) {\n" |
| 487 | " return service_;\n" |
| 488 | "}\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 489 | printer->Print("service_ = new ::grpc::RpcService();\n"); |
| 490 | for (int i = 0; i < service->method_count(); ++i) { |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 491 | const google::protobuf::MethodDescriptor *method = service->method(i); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 492 | (*vars)["Method"] = method->name(); |
| 493 | (*vars)["Request"] = |
| 494 | grpc_cpp_generator::ClassName(method->input_type(), true); |
| 495 | (*vars)["Response"] = |
| 496 | grpc_cpp_generator::ClassName(method->output_type(), true); |
| 497 | if (NoStreaming(method)) { |
| 498 | printer->Print( |
| 499 | *vars, |
| 500 | "service_->AddMethod(new ::grpc::RpcServiceMethod(\n" |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 501 | " \"/$Package$$Service$/$Method$\",\n" |
| 502 | " ::grpc::RpcMethod::NORMAL_RPC,\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 503 | " new ::grpc::RpcMethodHandler< $Service$::Service, $Request$, " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 504 | "$Response$>(\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 505 | " std::function< ::grpc::Status($Service$::Service*, " |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 506 | "::grpc::ServerContext*, const $Request$*, $Response$*)>(" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 507 | "&$Service$::Service::$Method$), this),\n" |
| 508 | " new $Request$, new $Response$));\n"); |
| 509 | } else if (ClientOnlyStreaming(method)) { |
| 510 | printer->Print( |
| 511 | *vars, |
| 512 | "service_->AddMethod(new ::grpc::RpcServiceMethod(\n" |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 513 | " \"/$Package$$Service$/$Method$\",\n" |
| 514 | " ::grpc::RpcMethod::CLIENT_STREAMING,\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 515 | " new ::grpc::ClientStreamingHandler< " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 516 | "$Service$::Service, $Request$, $Response$>(\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 517 | " std::function< ::grpc::Status($Service$::Service*, " |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 518 | "::grpc::ServerContext*, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 519 | "::grpc::ServerReader< $Request$>*, $Response$*)>(" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 520 | "&$Service$::Service::$Method$), this),\n" |
| 521 | " new $Request$, new $Response$));\n"); |
| 522 | } else if (ServerOnlyStreaming(method)) { |
| 523 | printer->Print( |
| 524 | *vars, |
| 525 | "service_->AddMethod(new ::grpc::RpcServiceMethod(\n" |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 526 | " \"/$Package$$Service$/$Method$\",\n" |
| 527 | " ::grpc::RpcMethod::SERVER_STREAMING,\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 528 | " new ::grpc::ServerStreamingHandler< " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 529 | "$Service$::Service, $Request$, $Response$>(\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 530 | " std::function< ::grpc::Status($Service$::Service*, " |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 531 | "::grpc::ServerContext*, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 532 | "const $Request$*, ::grpc::ServerWriter< $Response$>*)>(" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 533 | "&$Service$::Service::$Method$), this),\n" |
| 534 | " new $Request$, new $Response$));\n"); |
| 535 | } else if (BidiStreaming(method)) { |
| 536 | printer->Print( |
| 537 | *vars, |
| 538 | "service_->AddMethod(new ::grpc::RpcServiceMethod(\n" |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 539 | " \"/$Package$$Service$/$Method$\",\n" |
| 540 | " ::grpc::RpcMethod::BIDI_STREAMING,\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 541 | " new ::grpc::BidiStreamingHandler< " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 542 | "$Service$::Service, $Request$, $Response$>(\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 543 | " std::function< ::grpc::Status($Service$::Service*, " |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 544 | "::grpc::ServerContext*, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 545 | "::grpc::ServerReaderWriter< $Response$, $Request$>*)>(" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 546 | "&$Service$::Service::$Method$), this),\n" |
| 547 | " new $Request$, new $Response$));\n"); |
| 548 | } |
| 549 | } |
| 550 | printer->Print("return service_;\n"); |
| 551 | printer->Outdent(); |
| 552 | printer->Print("}\n\n"); |
| 553 | } |
| 554 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 555 | std::string GetSourceServices(const google::protobuf::FileDescriptor *file) { |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 556 | std::string output; |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 557 | google::protobuf::io::StringOutputStream output_stream(&output); |
| 558 | google::protobuf::io::Printer printer(&output_stream, '$'); |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 559 | std::map<std::string, std::string> vars; |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 560 | // Package string is empty or ends with a dot. It is used to fully qualify |
| 561 | // method names. |
| 562 | vars["Package"] = file->package(); |
| 563 | if (!file->package().empty()) { |
| 564 | vars["Package"].append("."); |
| 565 | } |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 566 | |
| 567 | for (int i = 0; i < file->service_count(); ++i) { |
samuelw | ca9f359 | 2014-12-15 14:22:04 -0800 | [diff] [blame] | 568 | PrintSourceService(&printer, file->service(i), &vars); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 569 | printer.Print("\n"); |
| 570 | } |
| 571 | return output; |
| 572 | } |
| 573 | |
| 574 | } // namespace grpc_cpp_generator |