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