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