blob: bae83eee3f5bed049064c6721b7a30aee274609e [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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" Noble1ff52d52015-03-01 05:24:36 +010034#ifndef GRPCXX_SERVER_H
35#define GRPCXX_SERVER_H
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036
Craig Tillercbd04852015-02-10 17:39:54 -080037#include <list>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080038#include <memory>
Yuchen Zenga42ec212016-04-29 13:03:06 -070039#include <vector>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080040
41#include <grpc++/completion_queue.h>
Craig Tillerbb5227f2015-02-11 13:34:48 -080042#include <grpc++/impl/call.h>
David Garcia Quintase1300de2016-01-27 18:41:26 -080043#include <grpc++/impl/codegen/grpc_library.h>
44#include <grpc++/impl/codegen/server_interface.h>
Craig Tiller15f383c2016-01-07 12:45:32 -080045#include <grpc++/impl/rpc_service_method.h>
Nicolas "Pixel" Nobleff2828b2015-04-03 03:16:46 +020046#include <grpc++/impl/sync.h>
Julien Boeuf0d471922015-08-30 22:18:50 -070047#include <grpc++/security/server_credentials.h>
yang-ga23f17b2015-11-25 10:21:05 -080048#include <grpc++/support/channel_arguments.h>
yang-g9e2f90c2015-08-21 15:35:03 -070049#include <grpc++/support/config.h>
50#include <grpc++/support/status.h>
yang-ga23f17b2015-11-25 10:21:05 -080051#include <grpc/compression.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080052
Sree Kuchibhotlabb5519f2016-07-19 11:00:39 -070053#include "src/cpp/rpcmanager/grpc_rpc_manager.h"
54
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080055struct grpc_server;
56
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080057namespace grpc {
Craig Tiller50a7a682015-06-04 12:53:40 -070058
Yang Gao005eb882015-03-11 22:17:13 -070059class GenericServerContext;
Yang Gao49996492015-03-12 16:40:19 -070060class AsyncGenericService;
Craig Tiller81fafa82015-06-04 08:51:17 -070061class ServerAsyncStreamingInterface;
Craig Tiller7221d992015-11-24 06:59:33 -080062class ServerContext;
Yuchen Zenga42ec212016-04-29 13:03:06 -070063class ServerInitializer;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080064class ThreadPoolInterface;
65
Craig Tillerd6599a32015-09-03 09:37:02 -070066/// Models a gRPC server.
67///
68/// Servers are configured and started via \a grpc::ServerBuilder.
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -070069class Server GRPC_FINAL : public ServerInterface, private GrpcLibraryCodegen {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080070 public:
71 ~Server();
72
Craig Tillerd6599a32015-09-03 09:37:02 -070073 /// Block waiting for all work to complete.
74 ///
75 /// \warning The server must be either shutting down or some other thread must
76 /// call \a Shutdown for this function to ever return.
David Garcia Quintas1f4e72c2016-01-19 14:45:32 -080077 void Wait() GRPC_OVERRIDE;
Craig Tiller6e57b9e2015-02-24 15:46:22 -080078
Craig Tiller7221d992015-11-24 06:59:33 -080079 /// Global Callbacks
80 ///
81 /// Can be set exactly once per application to install hooks whenever
82 /// a server event occurs
83 class GlobalCallbacks {
84 public:
Craig Tiller8352b9e2015-12-02 11:43:40 -080085 virtual ~GlobalCallbacks() {}
yang-geb62c942016-02-17 15:37:25 -080086 /// Called before server is created.
87 virtual void UpdateArguments(ChannelArguments* args) {}
Craig Tiller8352b9e2015-12-02 11:43:40 -080088 /// Called before application callback for each synchronous server request
Craig Tiller7221d992015-11-24 06:59:33 -080089 virtual void PreSynchronousRequest(ServerContext* context) = 0;
Craig Tiller8352b9e2015-12-02 11:43:40 -080090 /// Called after application callback for each synchronous server request
Craig Tiller7221d992015-11-24 06:59:33 -080091 virtual void PostSynchronousRequest(ServerContext* context) = 0;
92 };
Craig Tiller8352b9e2015-12-02 11:43:40 -080093 /// Set the global callback object. Can only be called once. Does not take
94 /// ownership of callbacks, and expects the pointed to object to be alive
95 /// until all server objects in the process have been destroyed.
Craig Tiller7221d992015-11-24 06:59:33 -080096 static void SetGlobalCallbacks(GlobalCallbacks* callbacks);
97
Adam Michalikb97e2d12016-06-02 12:12:55 -070098 // Returns a \em raw pointer to the underlying grpc_server instance.
99 grpc_server* c_server();
100
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800101 private:
Yang Gao49996492015-03-12 16:40:19 -0700102 friend class AsyncGenericService;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800103 friend class ServerBuilder;
Yuchen Zenga42ec212016-04-29 13:03:06 -0700104 friend class ServerInitializer;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800105
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800106 class SyncRequest;
107 class AsyncRequest;
Craig Tillerbce999f2015-05-27 09:55:51 -0700108 class ShutdownRequest;
Craig Tillercbd04852015-02-10 17:39:54 -0800109
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700110 /// SyncRequestManager is an implementation of GrpcRpcManager. This class is
111 /// responsible for polling for incoming RPCs and calling the RPC handlers.
112 /// This is only used in case of a Sync server (i.e a server exposing a sync
113 /// interface)
114 class SyncRequestManager;
115
David Garcia Quintas44f32492016-01-14 18:00:04 -0800116 class UnimplementedAsyncRequestContext;
117 class UnimplementedAsyncRequest;
118 class UnimplementedAsyncResponse;
119
Craig Tillerd6599a32015-09-03 09:37:02 -0700120 /// Server constructors. To be used by \a ServerBuilder only.
121 ///
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700122 /// \param sync_server_cqs The completion queues to use if the server is a
123 /// synchronous server (or a hybrid server). The server polls for new RPCs on
124 /// these queues
125 ///
Craig Tillerd6599a32015-09-03 09:37:02 -0700126 /// \param max_message_size Maximum message length that the channel can
127 /// receive.
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700128 ///
129 /// \param args The channel args
130 ///
131 /// \param min_pollers The minimum number of polling threads per server
132 /// completion queue (in param sync_server_cqs) to use for listening to
133 /// incoming requests (used only in case of sync server)
134 ///
135 /// \param max_pollers The maximum number of polling threads per server
136 /// completion queue (in param sync_server_cqs) to use for listening to
137 /// incoming requests (used only in case of sync server)
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700138 ///
139 /// \param sync_cq_timeout_msec The timeout to use when calling AsyncNext() on
140 /// server completion queues passed via sync_server_cqs param.
Sree Kuchibhotla4028d2c2016-09-21 10:45:33 -0700141 Server(std::shared_ptr<std::vector<std::unique_ptr<ServerCompletionQueue>>>
142 sync_server_cqs,
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700143 int max_message_size, ChannelArguments* args, int min_pollers,
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700144 int max_pollers, int sync_cq_timeout_msec);
Craig Tillerd6599a32015-09-03 09:37:02 -0700145
146 /// Register a service. This call does not take ownership of the service.
147 /// The service must exist for the lifetime of the Server instance.
David Garcia Quintasf3ddb7c2016-01-20 16:02:22 -0800148 bool RegisterService(const grpc::string* host,
149 Service* service) GRPC_OVERRIDE;
Craig Tillerd6599a32015-09-03 09:37:02 -0700150
151 /// Register a generic service. This call does not take ownership of the
152 /// service. The service must exist for the lifetime of the Server instance.
David Garcia Quintas1f4e72c2016-01-19 14:45:32 -0800153 void RegisterAsyncGenericService(AsyncGenericService* service) GRPC_OVERRIDE;
Craig Tillerd6599a32015-09-03 09:37:02 -0700154
155 /// Tries to bind \a server to the given \a addr.
156 ///
157 /// It can be invoked multiple times.
158 ///
159 /// \param addr The address to try to bind to the server (eg, localhost:1234,
160 /// 192.168.1.1:31416, [::1]:27182, etc.).
161 /// \params creds The credentials associated with the server.
162 ///
163 /// \return bound port number on sucess, 0 on failure.
164 ///
165 /// \warning It's an error to call this method on an already started server.
David Garcia Quintasf3ddb7c2016-01-20 16:02:22 -0800166 int AddListeningPort(const grpc::string& addr,
167 ServerCredentials* creds) GRPC_OVERRIDE;
Craig Tillerd6599a32015-09-03 09:37:02 -0700168
169 /// Start the server.
170 ///
171 /// \param cqs Completion queues for handling asynchronous services. The
172 /// caller is required to keep all completion queues live until the server is
173 /// destroyed.
174 /// \param num_cqs How many completion queues does \a cqs hold.
175 ///
176 /// \return true on a successful shutdown.
David Garcia Quintas1f4e72c2016-01-19 14:45:32 -0800177 bool Start(ServerCompletionQueue** cqs, size_t num_cqs) GRPC_OVERRIDE;
Craig Tillerd6599a32015-09-03 09:37:02 -0700178
179 /// Process one or more incoming calls.
David Garcia Quintas1f4e72c2016-01-19 14:45:32 -0800180 void RunRpc() GRPC_OVERRIDE;
Craig Tillerd6599a32015-09-03 09:37:02 -0700181
182 /// Schedule \a RunRpc to run in the threadpool.
David Garcia Quintas1f4e72c2016-01-19 14:45:32 -0800183 void ScheduleCallback() GRPC_OVERRIDE;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800184
Craig Tillerce40de52015-06-05 07:14:58 -0700185 void PerformOpsOnCall(CallOpSetInterface* ops, Call* call) GRPC_OVERRIDE;
Craig Tillerbb5227f2015-02-11 13:34:48 -0800186
David Garcia Quintas1f4e72c2016-01-19 14:45:32 -0800187 void ShutdownInternal(gpr_timespec deadline) GRPC_OVERRIDE;
Craig Tillere50e5cb2015-08-18 12:44:57 -0700188
Mark D. Roth69803622016-09-06 08:15:36 -0700189 int max_receive_message_size() const GRPC_OVERRIDE {
190 return max_receive_message_size_;
191 };
Craig Tiller50a7a682015-06-04 12:53:40 -0700192
David Garcia Quintas1f4e72c2016-01-19 14:45:32 -0800193 grpc_server* server() GRPC_OVERRIDE { return server_; };
Yang Gao1c402332015-03-05 16:39:25 -0800194
Yuchen Zenga42ec212016-04-29 13:03:06 -0700195 ServerInitializer* initializer();
196
Mark D. Roth69803622016-09-06 08:15:36 -0700197 const int max_receive_message_size_;
Yang Gao3921c562015-04-30 16:07:06 -0700198
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700199 /// The following completion queues are ONLY used in case of Sync API i.e if
200 /// the server has any services with sync methods. The server uses these
201 /// completion queues to poll for new RPCs
Sree Kuchibhotla4028d2c2016-09-21 10:45:33 -0700202 std::shared_ptr<std::vector<std::unique_ptr<ServerCompletionQueue>>>
203 sync_server_cqs_;
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700204
205 /// List of GrpcRpcManager instances (one for each cq in the sync_server_cqs)
206 std::vector<std::unique_ptr<SyncRequestManager>> sync_req_mgrs_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800207
208 // Sever status
Nicolas "Pixel" Nobleff2828b2015-04-03 03:16:46 +0200209 grpc::mutex mu_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800210 bool started_;
211 bool shutdown_;
yang-g6ec11f22016-07-14 14:53:35 -0700212 bool shutdown_notified_;
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700213
214 // TODO (sreek) : Remove num_running_cb_ and callback_cv_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800215 // The number of threads which are running callbacks.
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700216 // int num_running_cb_;
217 // grpc::condition_variable callback_cv_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800218
yang-ge89dc6c2016-07-11 15:48:01 -0700219 grpc::condition_variable shutdown_cv_;
220
Craig Tiller64616492016-01-26 14:16:20 -0800221 std::shared_ptr<GlobalCallbacks> global_callbacks_;
222
Yuchen Zenga42ec212016-04-29 13:03:06 -0700223 std::vector<grpc::string> services_;
yang-g9b7757d2015-08-13 11:15:53 -0700224 bool has_generic_service_;
Craig Tillercbd04852015-02-10 17:39:54 -0800225
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700226 // Pointer to the c core's grpc server.
yang-geb62c942016-02-17 15:37:25 -0800227 grpc_server* server_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800228
Yuchen Zenga42ec212016-04-29 13:03:06 -0700229 std::unique_ptr<ServerInitializer> server_initializer_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800230};
231
232} // namespace grpc
233
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +0100234#endif // GRPCXX_SERVER_H