From 14a65f976060a68982b6e17a8cb76fb770579afb Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Mon, 9 Feb 2015 13:13:14 -0800 Subject: [PATCH] Further progress --- include/grpc++/impl/rpc_service_method.h | 2 +- include/grpc++/impl/service_type.h | 54 ++++++++++++++++++++++++ include/grpc++/server_builder.h | 7 ++- src/compiler/cpp_generator.cc | 22 +++++----- src/cpp/server/server_builder.cc | 9 +++- 5 files changed, 80 insertions(+), 14 deletions(-) create mode 100644 include/grpc++/impl/service_type.h diff --git a/include/grpc++/impl/rpc_service_method.h b/include/grpc++/impl/rpc_service_method.h index 620de5e67fb..3077e0af66c 100644 --- a/include/grpc++/impl/rpc_service_method.h +++ b/include/grpc++/impl/rpc_service_method.h @@ -203,7 +203,7 @@ class RpcService { public: // Takes ownership. void AddMethod(RpcServiceMethod* method) { - methods_.push_back(std::unique_ptr(method)); + methods_.emplace_back(method); } RpcServiceMethod* GetMethod(int i) { return methods_[i].get(); } diff --git a/include/grpc++/impl/service_type.h b/include/grpc++/impl/service_type.h new file mode 100644 index 00000000000..6e50c43493c --- /dev/null +++ b/include/grpc++/impl/service_type.h @@ -0,0 +1,54 @@ +/* + * + * Copyright 2014, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef __GRPCPP_IMPL_SERVICE_TYPE_H__ +#define __GRPCPP_IMPL_SERVICE_TYPE_H__ + +namespace grpc { + +class RpcService; + +class SynchronousService { + public: + virtual ~SynchronousService() {} + virtual RpcService *service() = 0; +}; + +class AsynchronousService { + public: + virtual ~AsynchronousService() {} +}; + +} // namespace grpc + +#endif // __GRPCPP_IMPL_SERVICE_TYPE_H__ \ No newline at end of file diff --git a/include/grpc++/server_builder.h b/include/grpc++/server_builder.h index cf274520107..f9a40b302d3 100644 --- a/include/grpc++/server_builder.h +++ b/include/grpc++/server_builder.h @@ -41,9 +41,11 @@ namespace grpc { +class AsynchronousService; class RpcService; class Server; class ServerCredentials; +class SynchronousService; class ThreadPoolInterface; class ServerBuilder { @@ -53,7 +55,9 @@ class ServerBuilder { // Register a service. This call does not take ownership of the service. // The service must exist for the lifetime of the Server instance returned by // BuildAndStart(). - void RegisterService(RpcService* service); + void RegisterService(SynchronousService* service); + + void ReigsterAsyncService(AsynchronousService *service); // Add a listening port. Can be called multiple times. void AddPort(const grpc::string& addr); @@ -71,6 +75,7 @@ class ServerBuilder { private: std::vector services_; + std::vector async_services_; std::vector ports_; std::shared_ptr creds_; ThreadPoolInterface* thread_pool_; diff --git a/src/compiler/cpp_generator.cc b/src/compiler/cpp_generator.cc index 37cde1af9a9..ecae429af50 100644 --- a/src/compiler/cpp_generator.cc +++ b/src/compiler/cpp_generator.cc @@ -108,8 +108,9 @@ bool HasBidiStreaming(const google::protobuf::FileDescriptor *file) { std::string GetHeaderIncludes(const google::protobuf::FileDescriptor *file) { std::string temp = - "#include \"grpc++/impl/internal_stub.h\"\n" - "#include \"grpc++/status.h\"\n" + "#include \n" + "#include \n" + "#include \n" "\n" "namespace grpc {\n" "class ChannelInterface;\n" @@ -147,6 +148,7 @@ std::string GetSourceIncludes() { return "#include \n" "#include \n" "#include \n" + "#include \n" "#include \n"; } @@ -165,8 +167,8 @@ void PrintHeaderClientMethod(google::protobuf::io::Printer *printer, printer->Print(*vars, "void $Method$(::grpc::ClientContext* context, " "const $Request$& request, $Response$* response, " - "Status *status, " - "CompletionQueue *cq, void *tag);\n"); + "::grpc::Status *status, " + "::grpc::CompletionQueue *cq, void *tag);\n"); } else if (ClientOnlyStreaming(method)) { printer->Print(*vars, "::grpc::ClientWriter< $Request$>* $Method$(" @@ -174,8 +176,8 @@ void PrintHeaderClientMethod(google::protobuf::io::Printer *printer, printer->Print(*vars, "::grpc::ClientWriter< $Request$>* $Method$(" "::grpc::ClientContext* context, $Response$* response, " - "Status *status, " - "CompletionQueue *cq, void *tag);\n"); + "::grpc::Status *status, " + "::grpc::CompletionQueue *cq, void *tag);\n"); } else if (ServerOnlyStreaming(method)) { printer->Print( *vars, @@ -184,7 +186,7 @@ void PrintHeaderClientMethod(google::protobuf::io::Printer *printer, printer->Print(*vars, "::grpc::ClientReader< $Response$>* $Method$(" "::grpc::ClientContext* context, const $Request$* request, " - "CompletionQueue *cq, void *tag);\n"); + "::grpc::CompletionQueue *cq, void *tag);\n"); } else if (BidiStreaming(method)) { printer->Print(*vars, "::grpc::ClientReaderWriter< $Request$, $Response$>* " @@ -192,7 +194,7 @@ void PrintHeaderClientMethod(google::protobuf::io::Printer *printer, printer->Print(*vars, "::grpc::ClientReaderWriter< $Request$, $Response$>* " "$Method$(::grpc::ClientContext* context, " - "CompletionQueue *cq, void *tag);\n"); + "::grpc::CompletionQueue *cq, void *tag);\n"); } } @@ -297,7 +299,7 @@ void PrintHeaderService(google::protobuf::io::Printer *printer, // Server side - Synchronous printer->Print( - "class Service {\n" + "class Service : public ::grpc::SynchronousService {\n" " public:\n"); printer->Indent(); printer->Print("Service() : service_(nullptr) {}\n"); @@ -314,7 +316,7 @@ void PrintHeaderService(google::protobuf::io::Printer *printer, // Server side - Asynchronous printer->Print( - "class AsyncService final {\n" + "class AsyncService final : public ::grpc::AsynchronousService {\n" " public:\n"); printer->Indent(); printer->Print("AsyncService() : service_(nullptr) {}\n"); diff --git a/src/cpp/server/server_builder.cc b/src/cpp/server/server_builder.cc index add22cc3d86..66e2055af09 100644 --- a/src/cpp/server/server_builder.cc +++ b/src/cpp/server/server_builder.cc @@ -34,14 +34,19 @@ #include #include +#include #include namespace grpc { ServerBuilder::ServerBuilder() : thread_pool_(nullptr) {} -void ServerBuilder::RegisterService(RpcService *service) { - services_.push_back(service); +void ServerBuilder::RegisterService(SynchronousService *service) { + services_.push_back(service->service()); +} + +void ServerBuilder::RegisterAsyncService(AsynchronousService *service) { + async_services_.push_back(service); } void ServerBuilder::AddPort(const grpc::string &addr) {