Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -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 | |
| 34 | #ifndef __GRPCPP_SERVER_H__ |
| 35 | #define __GRPCPP_SERVER_H__ |
| 36 | |
| 37 | #include <condition_variable> |
| 38 | #include <map> |
| 39 | #include <memory> |
| 40 | #include <mutex> |
| 41 | |
| 42 | #include <grpc++/completion_queue.h> |
| 43 | #include <grpc++/config.h> |
| 44 | #include <grpc++/status.h> |
| 45 | |
| 46 | struct grpc_server; |
| 47 | |
| 48 | namespace google { |
| 49 | namespace protobuf { |
| 50 | class Message; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | namespace grpc { |
| 55 | class AsyncServerContext; |
| 56 | class RpcService; |
| 57 | class RpcServiceMethod; |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 58 | class ServerCredentials; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 59 | class ThreadPoolInterface; |
| 60 | |
| 61 | // Currently it only supports handling rpcs in a single thread. |
| 62 | class Server { |
| 63 | public: |
| 64 | ~Server(); |
| 65 | |
| 66 | // Shutdown the server, block until all rpc processing finishes. |
| 67 | void Shutdown(); |
| 68 | |
| 69 | private: |
| 70 | friend class ServerBuilder; |
| 71 | |
| 72 | // ServerBuilder use only |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 73 | Server(ThreadPoolInterface* thread_pool, ServerCredentials* creds); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 74 | Server(); |
| 75 | // Register a service. This call does not take ownership of the service. |
| 76 | // The service must exist for the lifetime of the Server instance. |
| 77 | void RegisterService(RpcService* service); |
| 78 | // Add a listening port. Can be called multiple times. |
| 79 | void AddPort(const grpc::string& addr); |
| 80 | // Start the server. |
| 81 | void Start(); |
| 82 | |
| 83 | void AllowOneRpc(); |
| 84 | void HandleQueueClosed(); |
| 85 | void RunRpc(); |
| 86 | void ScheduleCallback(); |
| 87 | |
| 88 | // Completion queue. |
| 89 | CompletionQueue cq_; |
| 90 | |
| 91 | // Sever status |
| 92 | std::mutex mu_; |
| 93 | bool started_; |
| 94 | bool shutdown_; |
| 95 | // The number of threads which are running callbacks. |
| 96 | int num_running_cb_; |
| 97 | std::condition_variable callback_cv_; |
| 98 | |
| 99 | // Pointer to the c grpc server. |
| 100 | grpc_server* server_; |
| 101 | |
| 102 | // A map for all method information. |
| 103 | std::map<grpc::string, RpcServiceMethod*> method_map_; |
| 104 | |
| 105 | ThreadPoolInterface* thread_pool_; |
| 106 | // Whether the thread pool is created and owned by the server. |
| 107 | bool thread_pool_owned_; |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 108 | // Whether the server is created with credentials. |
| 109 | bool secure_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | } // namespace grpc |
| 113 | |
| 114 | #endif // __GRPCPP_SERVER_H__ |