nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 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( |
Yang Gao | 3a5e549 | 2015-02-18 14:32:38 -0800 | [diff] [blame] | 130 | "template <class OutMessage> class ClientAsyncResponseReader;\n"); |
| 131 | temp.append( |
Craig Tiller | 5ef5db1 | 2015-02-09 12:47:21 -0800 | [diff] [blame] | 132 | "template <class OutMessage> class ServerAsyncResponseWriter;\n"); |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 133 | } |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 134 | if (HasClientOnlyStreaming(file)) { |
| 135 | temp.append("template <class OutMessage> class ClientWriter;\n"); |
| 136 | temp.append("template <class InMessage> class ServerReader;\n"); |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 137 | temp.append("template <class OutMessage> class ClientAsyncWriter;\n"); |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 138 | temp.append("template <class OutMessage, class InMessage> class ServerAsyncReader;\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 139 | } |
| 140 | if (HasServerOnlyStreaming(file)) { |
| 141 | temp.append("template <class InMessage> class ClientReader;\n"); |
| 142 | temp.append("template <class OutMessage> class ServerWriter;\n"); |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 143 | temp.append("template <class OutMessage> class ClientAsyncReader;\n"); |
| 144 | temp.append("template <class InMessage> class ServerAsyncWriter;\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 145 | } |
| 146 | if (HasBidiStreaming(file)) { |
| 147 | temp.append( |
| 148 | "template <class OutMessage, class InMessage>\n" |
| 149 | "class ClientReaderWriter;\n"); |
| 150 | temp.append( |
| 151 | "template <class OutMessage, class InMessage>\n" |
| 152 | "class ServerReaderWriter;\n"); |
Craig Tiller | 225f7be | 2015-02-09 22:32:37 -0800 | [diff] [blame] | 153 | temp.append( |
| 154 | "template <class OutMessage, class InMessage>\n" |
| 155 | "class ClientAsyncReaderWriter;\n"); |
| 156 | temp.append( |
| 157 | "template <class OutMessage, class InMessage>\n" |
| 158 | "class ServerAsyncReaderWriter;\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 159 | } |
| 160 | temp.append("} // namespace grpc\n"); |
| 161 | return temp; |
| 162 | } |
| 163 | |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 164 | std::string GetSourceIncludes() { |
Yang Gao | 3a5e549 | 2015-02-18 14:32:38 -0800 | [diff] [blame] | 165 | return "#include <grpc++/async_unary_call.h>\n" |
| 166 | "#include <grpc++/channel_interface.h>\n" |
Craig Tiller | 80e00a8 | 2015-02-09 20:54:25 -0800 | [diff] [blame] | 167 | "#include <grpc++/impl/client_unary_call.h>\n" |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 168 | "#include <grpc++/impl/rpc_method.h>\n" |
| 169 | "#include <grpc++/impl/rpc_service_method.h>\n" |
Craig Tiller | 14a65f9 | 2015-02-09 13:13:14 -0800 | [diff] [blame] | 170 | "#include <grpc++/impl/service_type.h>\n" |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 171 | "#include <grpc++/stream.h>\n"; |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 174 | void PrintHeaderClientMethod(google::protobuf::io::Printer *printer, |
| 175 | const google::protobuf::MethodDescriptor *method, |
| 176 | std::map<std::string, std::string> *vars) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 177 | (*vars)["Method"] = method->name(); |
| 178 | (*vars)["Request"] = |
| 179 | grpc_cpp_generator::ClassName(method->input_type(), true); |
| 180 | (*vars)["Response"] = |
| 181 | grpc_cpp_generator::ClassName(method->output_type(), true); |
| 182 | if (NoStreaming(method)) { |
| 183 | printer->Print(*vars, |
| 184 | "::grpc::Status $Method$(::grpc::ClientContext* context, " |
Craig Tiller | 5ef5db1 | 2015-02-09 12:47:21 -0800 | [diff] [blame] | 185 | "const $Request$& request, $Response$* response);\n"); |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 186 | printer->Print( |
| 187 | *vars, |
| 188 | "std::unique_ptr< ::grpc::ClientAsyncResponseReader< $Response$>> " |
| 189 | "$Method$(::grpc::ClientContext* context, " |
| 190 | "const $Request$& request, " |
| 191 | "::grpc::CompletionQueue* cq, void* tag);\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 192 | } else if (ClientOnlyStreaming(method)) { |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 193 | printer->Print( |
| 194 | *vars, |
| 195 | "std::unique_ptr< ::grpc::ClientWriter< $Request$>> $Method$(" |
| 196 | "::grpc::ClientContext* context, $Response$* response);\n"); |
| 197 | printer->Print( |
| 198 | *vars, |
| 199 | "std::unique_ptr< ::grpc::ClientAsyncWriter< $Request$>> $Method$(" |
| 200 | "::grpc::ClientContext* context, $Response$* response, " |
| 201 | "::grpc::CompletionQueue* cq, void* tag);\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 202 | } else if (ServerOnlyStreaming(method)) { |
| 203 | printer->Print( |
| 204 | *vars, |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 205 | "std::unique_ptr< ::grpc::ClientReader< $Response$>> $Method$(" |
Yang Gao | 07d8304 | 2015-02-13 14:11:31 -0800 | [diff] [blame] | 206 | "::grpc::ClientContext* context, const $Request$& request);\n"); |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 207 | printer->Print( |
| 208 | *vars, |
| 209 | "std::unique_ptr< ::grpc::ClientAsyncReader< $Response$>> $Method$(" |
| 210 | "::grpc::ClientContext* context, const $Request$& request, " |
| 211 | "::grpc::CompletionQueue* cq, void* tag);\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 212 | } else if (BidiStreaming(method)) { |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 213 | printer->Print( |
| 214 | *vars, |
| 215 | "std::unique_ptr< ::grpc::ClientReaderWriter< $Request$, $Response$>> " |
| 216 | "$Method$(::grpc::ClientContext* context);\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 217 | printer->Print(*vars, |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 218 | "std::unique_ptr< ::grpc::ClientAsyncReaderWriter< " |
| 219 | "$Request$, $Response$>> " |
Craig Tiller | 5ef5db1 | 2015-02-09 12:47:21 -0800 | [diff] [blame] | 220 | "$Method$(::grpc::ClientContext* context, " |
Yang Gao | 068c85b | 2015-02-12 15:21:24 -0800 | [diff] [blame] | 221 | "::grpc::CompletionQueue* cq, void* tag);\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Craig Tiller | 5ef5db1 | 2015-02-09 12:47:21 -0800 | [diff] [blame] | 225 | void PrintHeaderServerMethodSync( |
| 226 | google::protobuf::io::Printer *printer, |
| 227 | const google::protobuf::MethodDescriptor *method, |
| 228 | std::map<std::string, std::string> *vars) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 229 | (*vars)["Method"] = method->name(); |
| 230 | (*vars)["Request"] = |
| 231 | grpc_cpp_generator::ClassName(method->input_type(), true); |
| 232 | (*vars)["Response"] = |
| 233 | grpc_cpp_generator::ClassName(method->output_type(), true); |
| 234 | if (NoStreaming(method)) { |
| 235 | printer->Print(*vars, |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 236 | "virtual ::grpc::Status $Method$(" |
| 237 | "::grpc::ServerContext* context, const $Request$* request, " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 238 | "$Response$* response);\n"); |
| 239 | } else if (ClientOnlyStreaming(method)) { |
| 240 | printer->Print(*vars, |
| 241 | "virtual ::grpc::Status $Method$(" |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 242 | "::grpc::ServerContext* context, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 243 | "::grpc::ServerReader< $Request$>* reader, " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 244 | "$Response$* response);\n"); |
| 245 | } else if (ServerOnlyStreaming(method)) { |
| 246 | printer->Print(*vars, |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 247 | "virtual ::grpc::Status $Method$(" |
| 248 | "::grpc::ServerContext* context, const $Request$* request, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 249 | "::grpc::ServerWriter< $Response$>* writer);\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 250 | } else if (BidiStreaming(method)) { |
Yang Gao | 5680ff4 | 2015-01-14 12:14:21 -0800 | [diff] [blame] | 251 | printer->Print( |
| 252 | *vars, |
| 253 | "virtual ::grpc::Status $Method$(" |
| 254 | "::grpc::ServerContext* context, " |
| 255 | "::grpc::ServerReaderWriter< $Response$, $Request$>* stream);" |
| 256 | "\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
Craig Tiller | 5ef5db1 | 2015-02-09 12:47:21 -0800 | [diff] [blame] | 260 | void PrintHeaderServerMethodAsync( |
| 261 | google::protobuf::io::Printer *printer, |
| 262 | const google::protobuf::MethodDescriptor *method, |
| 263 | std::map<std::string, std::string> *vars) { |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 264 | (*vars)["Method"] = method->name(); |
| 265 | (*vars)["Request"] = |
| 266 | grpc_cpp_generator::ClassName(method->input_type(), true); |
| 267 | (*vars)["Response"] = |
| 268 | grpc_cpp_generator::ClassName(method->output_type(), true); |
| 269 | if (NoStreaming(method)) { |
| 270 | printer->Print(*vars, |
Yang Gao | ca3cb3e | 2015-02-12 00:05:11 -0800 | [diff] [blame] | 271 | "void Request$Method$(" |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 272 | "::grpc::ServerContext* context, $Request$* request, " |
| 273 | "::grpc::ServerAsyncResponseWriter< $Response$>* response, " |
| 274 | "::grpc::CompletionQueue* cq, void *tag);\n"); |
| 275 | } else if (ClientOnlyStreaming(method)) { |
| 276 | printer->Print(*vars, |
Yang Gao | ca3cb3e | 2015-02-12 00:05:11 -0800 | [diff] [blame] | 277 | "void Request$Method$(" |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 278 | "::grpc::ServerContext* context, " |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 279 | "::grpc::ServerAsyncReader< $Response$, $Request$>* reader, " |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 280 | "::grpc::CompletionQueue* cq, void *tag);\n"); |
| 281 | } else if (ServerOnlyStreaming(method)) { |
| 282 | printer->Print(*vars, |
Yang Gao | ca3cb3e | 2015-02-12 00:05:11 -0800 | [diff] [blame] | 283 | "void Request$Method$(" |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 284 | "::grpc::ServerContext* context, $Request$* request, " |
| 285 | "::grpc::ServerAsyncWriter< $Response$>* writer, " |
| 286 | "::grpc::CompletionQueue* cq, void *tag);\n"); |
| 287 | } else if (BidiStreaming(method)) { |
| 288 | printer->Print( |
| 289 | *vars, |
Yang Gao | ca3cb3e | 2015-02-12 00:05:11 -0800 | [diff] [blame] | 290 | "void Request$Method$(" |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 291 | "::grpc::ServerContext* context, " |
Craig Tiller | 225f7be | 2015-02-09 22:32:37 -0800 | [diff] [blame] | 292 | "::grpc::ServerAsyncReaderWriter< $Response$, $Request$>* stream, " |
Craig Tiller | 5ef5db1 | 2015-02-09 12:47:21 -0800 | [diff] [blame] | 293 | "::grpc::CompletionQueue* cq, void *tag);\n"); |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 297 | void PrintHeaderService(google::protobuf::io::Printer *printer, |
| 298 | const google::protobuf::ServiceDescriptor *service, |
| 299 | std::map<std::string, std::string> *vars) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 300 | (*vars)["Service"] = service->name(); |
| 301 | |
| 302 | printer->Print(*vars, |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 303 | "class $Service$ final {\n" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 304 | " public:\n"); |
| 305 | printer->Indent(); |
| 306 | |
| 307 | // Client side |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 308 | printer->Print( |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 309 | "class Stub final : public ::grpc::InternalStub {\n" |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 310 | " public:\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 311 | printer->Indent(); |
| 312 | for (int i = 0; i < service->method_count(); ++i) { |
| 313 | PrintHeaderClientMethod(printer, service->method(i), vars); |
| 314 | } |
| 315 | printer->Outdent(); |
| 316 | printer->Print("};\n"); |
| 317 | printer->Print( |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 318 | "static std::unique_ptr<Stub> NewStub(const std::shared_ptr< " |
| 319 | "::grpc::ChannelInterface>& " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 320 | "channel);\n"); |
| 321 | |
| 322 | printer->Print("\n"); |
| 323 | |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 324 | // Server side - Synchronous |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 325 | printer->Print( |
Craig Tiller | 14a65f9 | 2015-02-09 13:13:14 -0800 | [diff] [blame] | 326 | "class Service : public ::grpc::SynchronousService {\n" |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 327 | " public:\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 328 | printer->Indent(); |
| 329 | printer->Print("Service() : service_(nullptr) {}\n"); |
| 330 | printer->Print("virtual ~Service();\n"); |
| 331 | for (int i = 0; i < service->method_count(); ++i) { |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 332 | PrintHeaderServerMethodSync(printer, service->method(i), vars); |
| 333 | } |
Craig Tiller | 40fcdaf | 2015-02-09 15:43:34 -0800 | [diff] [blame] | 334 | printer->Print("::grpc::RpcService* service() override final;\n"); |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 335 | printer->Outdent(); |
| 336 | printer->Print( |
| 337 | " private:\n" |
| 338 | " ::grpc::RpcService* service_;\n"); |
| 339 | printer->Print("};\n"); |
| 340 | |
| 341 | // Server side - Asynchronous |
| 342 | printer->Print( |
Craig Tiller | 14a65f9 | 2015-02-09 13:13:14 -0800 | [diff] [blame] | 343 | "class AsyncService final : public ::grpc::AsynchronousService {\n" |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 344 | " public:\n"); |
| 345 | printer->Indent(); |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 346 | (*vars)["MethodCount"] = as_string(service->method_count()); |
| 347 | printer->Print("explicit AsyncService(::grpc::CompletionQueue* cq);\n"); |
Craig Tiller | 0220cf1 | 2015-02-12 17:39:26 -0800 | [diff] [blame] | 348 | printer->Print("~AsyncService() {};\n"); |
Craig Tiller | 2dff17d | 2015-02-09 12:42:23 -0800 | [diff] [blame] | 349 | for (int i = 0; i < service->method_count(); ++i) { |
| 350 | PrintHeaderServerMethodAsync(printer, service->method(i), vars); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 351 | } |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 352 | printer->Outdent(); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 353 | printer->Print("};\n"); |
| 354 | |
| 355 | printer->Outdent(); |
| 356 | printer->Print("};\n"); |
| 357 | } |
| 358 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 359 | std::string GetHeaderServices(const google::protobuf::FileDescriptor *file) { |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 360 | std::string output; |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 361 | google::protobuf::io::StringOutputStream output_stream(&output); |
| 362 | google::protobuf::io::Printer printer(&output_stream, '$'); |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 363 | std::map<std::string, std::string> vars; |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 364 | |
| 365 | for (int i = 0; i < file->service_count(); ++i) { |
samuelw | ca9f359 | 2014-12-15 14:22:04 -0800 | [diff] [blame] | 366 | PrintHeaderService(&printer, file->service(i), &vars); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 367 | printer.Print("\n"); |
| 368 | } |
| 369 | return output; |
| 370 | } |
| 371 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 372 | void PrintSourceClientMethod(google::protobuf::io::Printer *printer, |
| 373 | const google::protobuf::MethodDescriptor *method, |
| 374 | std::map<std::string, std::string> *vars) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 375 | (*vars)["Method"] = method->name(); |
| 376 | (*vars)["Request"] = |
| 377 | grpc_cpp_generator::ClassName(method->input_type(), true); |
| 378 | (*vars)["Response"] = |
| 379 | grpc_cpp_generator::ClassName(method->output_type(), true); |
| 380 | if (NoStreaming(method)) { |
| 381 | printer->Print(*vars, |
| 382 | "::grpc::Status $Service$::Stub::$Method$(" |
| 383 | "::grpc::ClientContext* context, " |
| 384 | "const $Request$& request, $Response$* response) {\n"); |
| 385 | printer->Print(*vars, |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 386 | " return ::grpc::BlockingUnaryCall(channel()," |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 387 | "::grpc::RpcMethod($Service$_method_names[$Idx$]), " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 388 | "context, request, response);\n" |
| 389 | "}\n\n"); |
Yang Gao | 5680ff4 | 2015-01-14 12:14:21 -0800 | [diff] [blame] | 390 | printer->Print( |
| 391 | *vars, |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 392 | "std::unique_ptr< ::grpc::ClientAsyncResponseReader< $Response$>> " |
| 393 | "$Service$::Stub::$Method$(::grpc::ClientContext* context, " |
| 394 | "const $Request$& request, " |
| 395 | "::grpc::CompletionQueue* cq, void* tag) {\n"); |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 396 | printer->Print(*vars, |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 397 | " return std::unique_ptr< " |
| 398 | "::grpc::ClientAsyncResponseReader< $Response$>>(new " |
| 399 | "::grpc::ClientAsyncResponseReader< $Response$>(" |
| 400 | "channel(), cq, " |
| 401 | "::grpc::RpcMethod($Service$_method_names[$Idx$]), " |
| 402 | "context, request, tag));\n" |
| 403 | "}\n\n"); |
| 404 | } else if (ClientOnlyStreaming(method)) { |
| 405 | printer->Print(*vars, |
| 406 | "std::unique_ptr< ::grpc::ClientWriter< $Request$>> " |
| 407 | "$Service$::Stub::$Method$(" |
| 408 | "::grpc::ClientContext* context, $Response$* response) {\n"); |
| 409 | printer->Print(*vars, |
| 410 | " return std::unique_ptr< ::grpc::ClientWriter< " |
| 411 | "$Request$>>(new ::grpc::ClientWriter< $Request$>(" |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 412 | "channel()," |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 413 | "::grpc::RpcMethod($Service$_method_names[$Idx$], " |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 414 | "::grpc::RpcMethod::RpcType::CLIENT_STREAMING), " |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 415 | "context, response));\n" |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 416 | "}\n\n"); |
Yang Gao | 068c85b | 2015-02-12 15:21:24 -0800 | [diff] [blame] | 417 | printer->Print(*vars, |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 418 | "std::unique_ptr< ::grpc::ClientAsyncWriter< $Request$>> " |
| 419 | "$Service$::Stub::$Method$(" |
| 420 | "::grpc::ClientContext* context, $Response$* response, " |
| 421 | "::grpc::CompletionQueue* cq, void* tag) {\n"); |
| 422 | printer->Print(*vars, |
| 423 | " return std::unique_ptr< ::grpc::ClientAsyncWriter< " |
| 424 | "$Request$>>(new ::grpc::ClientAsyncWriter< $Request$>(" |
Yang Gao | 068c85b | 2015-02-12 15:21:24 -0800 | [diff] [blame] | 425 | "channel(), cq, " |
| 426 | "::grpc::RpcMethod($Service$_method_names[$Idx$], " |
| 427 | "::grpc::RpcMethod::RpcType::CLIENT_STREAMING), " |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 428 | "context, response, tag));\n" |
Yang Gao | 068c85b | 2015-02-12 15:21:24 -0800 | [diff] [blame] | 429 | "}\n\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 430 | } else if (ServerOnlyStreaming(method)) { |
| 431 | printer->Print( |
| 432 | *vars, |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 433 | "std::unique_ptr< ::grpc::ClientReader< $Response$>> " |
| 434 | "$Service$::Stub::$Method$(" |
Yang Gao | 07d8304 | 2015-02-13 14:11:31 -0800 | [diff] [blame] | 435 | "::grpc::ClientContext* context, const $Request$& request) {\n"); |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 436 | printer->Print(*vars, |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 437 | " return std::unique_ptr< ::grpc::ClientReader< " |
| 438 | "$Response$>>(new ::grpc::ClientReader< $Response$>(" |
Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 439 | "channel()," |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 440 | "::grpc::RpcMethod($Service$_method_names[$Idx$], " |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 441 | "::grpc::RpcMethod::RpcType::SERVER_STREAMING), " |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 442 | "context, request));\n" |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 443 | "}\n\n"); |
Yang Gao | 068c85b | 2015-02-12 15:21:24 -0800 | [diff] [blame] | 444 | printer->Print(*vars, |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 445 | "std::unique_ptr< ::grpc::ClientAsyncReader< $Response$>> " |
| 446 | "$Service$::Stub::$Method$(" |
| 447 | "::grpc::ClientContext* context, const $Request$& request, " |
| 448 | "::grpc::CompletionQueue* cq, void* tag) {\n"); |
| 449 | printer->Print(*vars, |
| 450 | " return std::unique_ptr< ::grpc::ClientAsyncReader< " |
| 451 | "$Response$>>(new ::grpc::ClientAsyncReader< $Response$>(" |
Yang Gao | 068c85b | 2015-02-12 15:21:24 -0800 | [diff] [blame] | 452 | "channel(), cq, " |
| 453 | "::grpc::RpcMethod($Service$_method_names[$Idx$], " |
| 454 | "::grpc::RpcMethod::RpcType::SERVER_STREAMING), " |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 455 | "context, request, tag));\n" |
Yang Gao | 068c85b | 2015-02-12 15:21:24 -0800 | [diff] [blame] | 456 | "}\n\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 457 | } else if (BidiStreaming(method)) { |
| 458 | printer->Print( |
| 459 | *vars, |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 460 | "std::unique_ptr< ::grpc::ClientReaderWriter< $Request$, $Response$>> " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 461 | "$Service$::Stub::$Method$(::grpc::ClientContext* context) {\n"); |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 462 | printer->Print(*vars, |
| 463 | " return std::unique_ptr< ::grpc::ClientReaderWriter< " |
| 464 | "$Request$, $Response$>>(new ::grpc::ClientReaderWriter< " |
| 465 | "$Request$, $Response$>(" |
| 466 | "channel()," |
| 467 | "::grpc::RpcMethod($Service$_method_names[$Idx$], " |
| 468 | "::grpc::RpcMethod::RpcType::BIDI_STREAMING), " |
| 469 | "context));\n" |
| 470 | "}\n\n"); |
| 471 | printer->Print(*vars, |
| 472 | "std::unique_ptr< ::grpc::ClientAsyncReaderWriter< " |
| 473 | "$Request$, $Response$>> " |
| 474 | "$Service$::Stub::$Method$(::grpc::ClientContext* context, " |
| 475 | "::grpc::CompletionQueue* cq, void* tag) {\n"); |
| 476 | printer->Print(*vars, |
| 477 | " return std::unique_ptr< ::grpc::ClientAsyncReaderWriter< " |
| 478 | "$Request$, $Response$>>(new " |
| 479 | "::grpc::ClientAsyncReaderWriter< $Request$, $Response$>(" |
| 480 | "channel(), cq, " |
| 481 | "::grpc::RpcMethod($Service$_method_names[$Idx$], " |
| 482 | "::grpc::RpcMethod::RpcType::BIDI_STREAMING), " |
| 483 | "context, tag));\n" |
| 484 | "}\n\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 488 | void PrintSourceServerMethod(google::protobuf::io::Printer *printer, |
| 489 | const google::protobuf::MethodDescriptor *method, |
| 490 | std::map<std::string, std::string> *vars) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 491 | (*vars)["Method"] = method->name(); |
| 492 | (*vars)["Request"] = |
| 493 | grpc_cpp_generator::ClassName(method->input_type(), true); |
| 494 | (*vars)["Response"] = |
| 495 | grpc_cpp_generator::ClassName(method->output_type(), true); |
| 496 | if (NoStreaming(method)) { |
| 497 | printer->Print(*vars, |
| 498 | "::grpc::Status $Service$::Service::$Method$(" |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 499 | "::grpc::ServerContext* context, " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 500 | "const $Request$* request, $Response$* response) {\n"); |
| 501 | printer->Print( |
| 502 | " return ::grpc::Status(" |
| 503 | "::grpc::StatusCode::UNIMPLEMENTED);\n"); |
| 504 | printer->Print("}\n\n"); |
| 505 | } else if (ClientOnlyStreaming(method)) { |
| 506 | printer->Print(*vars, |
| 507 | "::grpc::Status $Service$::Service::$Method$(" |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 508 | "::grpc::ServerContext* context, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 509 | "::grpc::ServerReader< $Request$>* reader, " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 510 | "$Response$* response) {\n"); |
| 511 | printer->Print( |
| 512 | " return ::grpc::Status(" |
| 513 | "::grpc::StatusCode::UNIMPLEMENTED);\n"); |
| 514 | printer->Print("}\n\n"); |
| 515 | } else if (ServerOnlyStreaming(method)) { |
| 516 | printer->Print(*vars, |
| 517 | "::grpc::Status $Service$::Service::$Method$(" |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 518 | "::grpc::ServerContext* context, " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 519 | "const $Request$* request, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 520 | "::grpc::ServerWriter< $Response$>* writer) {\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 521 | printer->Print( |
| 522 | " return ::grpc::Status(" |
| 523 | "::grpc::StatusCode::UNIMPLEMENTED);\n"); |
| 524 | printer->Print("}\n\n"); |
| 525 | } else if (BidiStreaming(method)) { |
| 526 | printer->Print(*vars, |
| 527 | "::grpc::Status $Service$::Service::$Method$(" |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 528 | "::grpc::ServerContext* context, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 529 | "::grpc::ServerReaderWriter< $Response$, $Request$>* " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 530 | "stream) {\n"); |
| 531 | printer->Print( |
| 532 | " return ::grpc::Status(" |
| 533 | "::grpc::StatusCode::UNIMPLEMENTED);\n"); |
| 534 | printer->Print("}\n\n"); |
| 535 | } |
| 536 | } |
| 537 | |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 538 | void PrintSourceServerAsyncMethod( |
| 539 | google::protobuf::io::Printer *printer, |
| 540 | const google::protobuf::MethodDescriptor *method, |
| 541 | std::map<std::string, std::string> *vars) { |
Craig Tiller | 225f7be | 2015-02-09 22:32:37 -0800 | [diff] [blame] | 542 | (*vars)["Method"] = method->name(); |
| 543 | (*vars)["Request"] = |
| 544 | grpc_cpp_generator::ClassName(method->input_type(), true); |
| 545 | (*vars)["Response"] = |
| 546 | grpc_cpp_generator::ClassName(method->output_type(), true); |
| 547 | if (NoStreaming(method)) { |
| 548 | printer->Print(*vars, |
Yang Gao | ca3cb3e | 2015-02-12 00:05:11 -0800 | [diff] [blame] | 549 | "void $Service$::AsyncService::Request$Method$(" |
Craig Tiller | 225f7be | 2015-02-09 22:32:37 -0800 | [diff] [blame] | 550 | "::grpc::ServerContext* context, " |
| 551 | "$Request$* request, " |
| 552 | "::grpc::ServerAsyncResponseWriter< $Response$>* response, " |
| 553 | "::grpc::CompletionQueue* cq, void* tag) {\n"); |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 554 | printer->Print( |
| 555 | *vars, |
| 556 | " AsynchronousService::RequestAsyncUnary($Idx$, context, request, response, cq, tag);\n"); |
Craig Tiller | 225f7be | 2015-02-09 22:32:37 -0800 | [diff] [blame] | 557 | printer->Print("}\n\n"); |
| 558 | } else if (ClientOnlyStreaming(method)) { |
| 559 | printer->Print(*vars, |
Yang Gao | ca3cb3e | 2015-02-12 00:05:11 -0800 | [diff] [blame] | 560 | "void $Service$::AsyncService::Request$Method$(" |
Craig Tiller | 225f7be | 2015-02-09 22:32:37 -0800 | [diff] [blame] | 561 | "::grpc::ServerContext* context, " |
Yang Gao | 005f18a | 2015-02-13 10:22:33 -0800 | [diff] [blame] | 562 | "::grpc::ServerAsyncReader< $Response$, $Request$>* reader, " |
Craig Tiller | 225f7be | 2015-02-09 22:32:37 -0800 | [diff] [blame] | 563 | "::grpc::CompletionQueue* cq, void* tag) {\n"); |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 564 | printer->Print( |
| 565 | *vars, |
| 566 | " AsynchronousService::RequestClientStreaming($Idx$, context, reader, cq, tag);\n"); |
Craig Tiller | 225f7be | 2015-02-09 22:32:37 -0800 | [diff] [blame] | 567 | printer->Print("}\n\n"); |
| 568 | } else if (ServerOnlyStreaming(method)) { |
| 569 | printer->Print(*vars, |
Yang Gao | ca3cb3e | 2015-02-12 00:05:11 -0800 | [diff] [blame] | 570 | "void $Service$::AsyncService::Request$Method$(" |
Craig Tiller | 225f7be | 2015-02-09 22:32:37 -0800 | [diff] [blame] | 571 | "::grpc::ServerContext* context, " |
| 572 | "$Request$* request, " |
| 573 | "::grpc::ServerAsyncWriter< $Response$>* writer, " |
| 574 | "::grpc::CompletionQueue* cq, void* tag) {\n"); |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 575 | printer->Print( |
| 576 | *vars, |
| 577 | " AsynchronousService::RequestServerStreaming($Idx$, context, request, writer, cq, tag);\n"); |
Craig Tiller | 225f7be | 2015-02-09 22:32:37 -0800 | [diff] [blame] | 578 | printer->Print("}\n\n"); |
| 579 | } else if (BidiStreaming(method)) { |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 580 | printer->Print( |
| 581 | *vars, |
| 582 | "void $Service$::AsyncService::Request$Method$(" |
| 583 | "::grpc::ServerContext* context, " |
| 584 | "::grpc::ServerAsyncReaderWriter< $Response$, $Request$>* stream, " |
| 585 | "::grpc::CompletionQueue* cq, void *tag) {\n"); |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 586 | printer->Print( |
| 587 | *vars, |
| 588 | " AsynchronousService::RequestBidiStreaming($Idx$, context, stream, cq, tag);\n"); |
Craig Tiller | 225f7be | 2015-02-09 22:32:37 -0800 | [diff] [blame] | 589 | printer->Print("}\n\n"); |
| 590 | } |
| 591 | } |
| 592 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 593 | void PrintSourceService(google::protobuf::io::Printer *printer, |
| 594 | const google::protobuf::ServiceDescriptor *service, |
| 595 | std::map<std::string, std::string> *vars) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 596 | (*vars)["Service"] = service->name(); |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 597 | |
| 598 | printer->Print(*vars, "static const char* $Service$_method_names[] = {\n"); |
| 599 | for (int i = 0; i < service->method_count(); ++i) { |
| 600 | (*vars)["Method"] = service->method(i)->name(); |
| 601 | printer->Print(*vars, " \"/$Package$$Service$/$Method$\",\n"); |
| 602 | } |
| 603 | printer->Print(*vars, "};\n\n"); |
| 604 | |
Yang Gao | 5680ff4 | 2015-01-14 12:14:21 -0800 | [diff] [blame] | 605 | printer->Print( |
| 606 | *vars, |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 607 | "std::unique_ptr< $Service$::Stub> $Service$::NewStub(" |
Yang Gao | 5680ff4 | 2015-01-14 12:14:21 -0800 | [diff] [blame] | 608 | "const std::shared_ptr< ::grpc::ChannelInterface>& channel) {\n" |
Craig Tiller | fd1b49b | 2015-02-23 12:53:39 -0800 | [diff] [blame] | 609 | " std::unique_ptr< $Service$::Stub> stub(new $Service$::Stub());\n" |
Yang Gao | 5680ff4 | 2015-01-14 12:14:21 -0800 | [diff] [blame] | 610 | " stub->set_channel(channel);\n" |
| 611 | " return stub;\n" |
| 612 | "};\n\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 613 | for (int i = 0; i < service->method_count(); ++i) { |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 614 | (*vars)["Idx"] = as_string(i); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 615 | PrintSourceClientMethod(printer, service->method(i), vars); |
| 616 | } |
| 617 | |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 618 | (*vars)["MethodCount"] = as_string(service->method_count()); |
| 619 | printer->Print( |
| 620 | *vars, |
| 621 | "$Service$::AsyncService::AsyncService(::grpc::CompletionQueue* cq) : " |
| 622 | "::grpc::AsynchronousService(cq, $Service$_method_names, $MethodCount$) " |
| 623 | "{}\n\n"); |
| 624 | |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 625 | printer->Print(*vars, |
| 626 | "$Service$::Service::~Service() {\n" |
| 627 | " delete service_;\n" |
| 628 | "}\n\n"); |
| 629 | for (int i = 0; i < service->method_count(); ++i) { |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 630 | (*vars)["Idx"] = as_string(i); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 631 | PrintSourceServerMethod(printer, service->method(i), vars); |
Craig Tiller | 225f7be | 2015-02-09 22:32:37 -0800 | [diff] [blame] | 632 | PrintSourceServerAsyncMethod(printer, service->method(i), vars); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 633 | } |
| 634 | printer->Print(*vars, |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 635 | "::grpc::RpcService* $Service$::Service::service() {\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 636 | printer->Indent(); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 637 | printer->Print( |
| 638 | "if (service_ != nullptr) {\n" |
| 639 | " return service_;\n" |
| 640 | "}\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 641 | printer->Print("service_ = new ::grpc::RpcService();\n"); |
| 642 | for (int i = 0; i < service->method_count(); ++i) { |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 643 | const google::protobuf::MethodDescriptor *method = service->method(i); |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 644 | (*vars)["Idx"] = as_string(i); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 645 | (*vars)["Method"] = method->name(); |
| 646 | (*vars)["Request"] = |
| 647 | grpc_cpp_generator::ClassName(method->input_type(), true); |
| 648 | (*vars)["Response"] = |
| 649 | grpc_cpp_generator::ClassName(method->output_type(), true); |
| 650 | if (NoStreaming(method)) { |
| 651 | printer->Print( |
| 652 | *vars, |
| 653 | "service_->AddMethod(new ::grpc::RpcServiceMethod(\n" |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 654 | " $Service$_method_names[$Idx$],\n" |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 655 | " ::grpc::RpcMethod::NORMAL_RPC,\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 656 | " new ::grpc::RpcMethodHandler< $Service$::Service, $Request$, " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 657 | "$Response$>(\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 658 | " std::function< ::grpc::Status($Service$::Service*, " |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 659 | "::grpc::ServerContext*, const $Request$*, $Response$*)>(" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 660 | "&$Service$::Service::$Method$), this),\n" |
| 661 | " new $Request$, new $Response$));\n"); |
| 662 | } else if (ClientOnlyStreaming(method)) { |
| 663 | printer->Print( |
| 664 | *vars, |
| 665 | "service_->AddMethod(new ::grpc::RpcServiceMethod(\n" |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 666 | " $Service$_method_names[$Idx$],\n" |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 667 | " ::grpc::RpcMethod::CLIENT_STREAMING,\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 668 | " new ::grpc::ClientStreamingHandler< " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 669 | "$Service$::Service, $Request$, $Response$>(\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 670 | " std::function< ::grpc::Status($Service$::Service*, " |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 671 | "::grpc::ServerContext*, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 672 | "::grpc::ServerReader< $Request$>*, $Response$*)>(" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 673 | "&$Service$::Service::$Method$), this),\n" |
| 674 | " new $Request$, new $Response$));\n"); |
| 675 | } else if (ServerOnlyStreaming(method)) { |
| 676 | printer->Print( |
| 677 | *vars, |
| 678 | "service_->AddMethod(new ::grpc::RpcServiceMethod(\n" |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 679 | " $Service$_method_names[$Idx$],\n" |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 680 | " ::grpc::RpcMethod::SERVER_STREAMING,\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 681 | " new ::grpc::ServerStreamingHandler< " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 682 | "$Service$::Service, $Request$, $Response$>(\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 683 | " std::function< ::grpc::Status($Service$::Service*, " |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 684 | "::grpc::ServerContext*, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 685 | "const $Request$*, ::grpc::ServerWriter< $Response$>*)>(" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 686 | "&$Service$::Service::$Method$), this),\n" |
| 687 | " new $Request$, new $Response$));\n"); |
| 688 | } else if (BidiStreaming(method)) { |
| 689 | printer->Print( |
| 690 | *vars, |
| 691 | "service_->AddMethod(new ::grpc::RpcServiceMethod(\n" |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 692 | " $Service$_method_names[$Idx$],\n" |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 693 | " ::grpc::RpcMethod::BIDI_STREAMING,\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 694 | " new ::grpc::BidiStreamingHandler< " |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 695 | "$Service$::Service, $Request$, $Response$>(\n" |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 696 | " std::function< ::grpc::Status($Service$::Service*, " |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 697 | "::grpc::ServerContext*, " |
Yang Gao | 1ff11f6 | 2015-01-14 11:45:32 -0800 | [diff] [blame] | 698 | "::grpc::ServerReaderWriter< $Response$, $Request$>*)>(" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 699 | "&$Service$::Service::$Method$), this),\n" |
| 700 | " new $Request$, new $Response$));\n"); |
| 701 | } |
| 702 | } |
| 703 | printer->Print("return service_;\n"); |
| 704 | printer->Outdent(); |
| 705 | printer->Print("}\n\n"); |
| 706 | } |
| 707 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 708 | std::string GetSourceServices(const google::protobuf::FileDescriptor *file) { |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 709 | std::string output; |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 710 | google::protobuf::io::StringOutputStream output_stream(&output); |
| 711 | google::protobuf::io::Printer printer(&output_stream, '$'); |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 712 | std::map<std::string, std::string> vars; |
yangg | 5bcea0d | 2015-01-06 10:35:03 -0800 | [diff] [blame] | 713 | // Package string is empty or ends with a dot. It is used to fully qualify |
| 714 | // method names. |
| 715 | vars["Package"] = file->package(); |
| 716 | if (!file->package().empty()) { |
| 717 | vars["Package"].append("."); |
| 718 | } |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 719 | |
| 720 | for (int i = 0; i < file->service_count(); ++i) { |
samuelw | ca9f359 | 2014-12-15 14:22:04 -0800 | [diff] [blame] | 721 | PrintSourceService(&printer, file->service(i), &vars); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 722 | printer.Print("\n"); |
| 723 | } |
| 724 | return output; |
| 725 | } |
| 726 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 727 | } // namespace grpc_cpp_generator |