Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Sree Kuchibhotla | 0190712 | 2016-04-21 15:09:13 -0700 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -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 | 1ff52d5 | 2015-03-01 05:24:36 +0100 | [diff] [blame] | 34 | #ifndef GRPCXX_SERVER_BUILDER_H |
| 35 | #define GRPCXX_SERVER_BUILDER_H |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 36 | |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 37 | #include <map> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 38 | #include <memory> |
| 39 | #include <vector> |
| 40 | |
yang-g | a23f17b | 2015-11-25 10:21:05 -0800 | [diff] [blame] | 41 | #include <grpc++/impl/server_builder_option.h> |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 42 | #include <grpc++/impl/server_builder_plugin.h> |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 43 | #include <grpc++/support/config.h> |
yang-g | a23f17b | 2015-11-25 10:21:05 -0800 | [diff] [blame] | 44 | #include <grpc/compression.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 45 | |
Craig Tiller | db1a5cc | 2016-09-28 14:22:12 -0700 | [diff] [blame^] | 46 | struct grpc_buffer_pool; |
| 47 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 48 | namespace grpc { |
| 49 | |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 50 | class AsyncGenericService; |
Craig Tiller | db1a5cc | 2016-09-28 14:22:12 -0700 | [diff] [blame^] | 51 | class BufferPool; |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 52 | class CompletionQueue; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 53 | class RpcService; |
| 54 | class Server; |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 55 | class ServerCompletionQueue; |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 56 | class ServerCredentials; |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 57 | class Service; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 58 | |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 59 | namespace testing { |
| 60 | class ServerBuilderPluginTest; |
| 61 | } // namespace testing |
| 62 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 63 | /// A builder class for the creation and startup of \a grpc::Server instances. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 64 | class ServerBuilder { |
| 65 | public: |
| 66 | ServerBuilder(); |
Craig Tiller | db1a5cc | 2016-09-28 14:22:12 -0700 | [diff] [blame^] | 67 | ~ServerBuilder(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 68 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 69 | /// Register a service. This call does not take ownership of the service. |
| 70 | /// The service must exist for the lifetime of the \a Server instance returned |
| 71 | /// by \a BuildAndStart(). |
| 72 | /// Matches requests with any :authority |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 73 | ServerBuilder& RegisterService(Service* service); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 74 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 75 | /// Register a generic service. |
| 76 | /// Matches requests with any :authority |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 77 | ServerBuilder& RegisterAsyncGenericService(AsyncGenericService* service); |
Craig Tiller | db57c4f | 2015-02-24 10:34:47 -0800 | [diff] [blame] | 78 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 79 | /// Register a service. This call does not take ownership of the service. |
| 80 | /// The service must exist for the lifetime of the \a Server instance returned |
| 81 | /// by BuildAndStart(). |
| 82 | /// Only matches requests with :authority \a host |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 83 | ServerBuilder& RegisterService(const grpc::string& host, Service* service); |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 84 | |
Mark D. Roth | 6980362 | 2016-09-06 08:15:36 -0700 | [diff] [blame] | 85 | /// Set max receive message size in bytes. |
| 86 | ServerBuilder& SetMaxReceiveMessageSize(int max_receive_message_size) { |
| 87 | max_receive_message_size_ = max_receive_message_size; |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 88 | return *this; |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Mark D. Roth | 6980362 | 2016-09-06 08:15:36 -0700 | [diff] [blame] | 91 | /// Set max send message size in bytes. |
| 92 | ServerBuilder& SetMaxSendMessageSize(int max_send_message_size) { |
| 93 | max_send_message_size_ = max_send_message_size; |
| 94 | return *this; |
| 95 | } |
| 96 | |
Mark D. Roth | 0e25610 | 2016-09-15 09:01:05 -0700 | [diff] [blame] | 97 | /// \deprecated For backward compatibility. |
Mark D. Roth | 6980362 | 2016-09-06 08:15:36 -0700 | [diff] [blame] | 98 | ServerBuilder& SetMaxMessageSize(int max_message_size) { |
| 99 | return SetMaxReceiveMessageSize(max_message_size); |
| 100 | } |
| 101 | |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 102 | /// Set the support status for compression algorithms. All algorithms are |
| 103 | /// enabled by default. |
| 104 | /// |
| 105 | /// Incoming calls compressed with an unsupported algorithm will fail with |
| 106 | /// GRPC_STATUS_UNIMPLEMENTED. |
| 107 | ServerBuilder& SetCompressionAlgorithmSupportStatus( |
| 108 | grpc_compression_algorithm algorithm, bool enabled); |
David Garcia Quintas | 49dd250 | 2015-09-08 16:01:09 -0700 | [diff] [blame] | 109 | |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 110 | /// The default compression level to use for all channel calls in the |
| 111 | /// absence of a call-specific level. |
| 112 | ServerBuilder& SetDefaultCompressionLevel(grpc_compression_level level); |
| 113 | |
| 114 | /// The default compression algorithm to use for all channel calls in the |
| 115 | /// absence of a call-specific level. Note that it overrides any compression |
| 116 | /// level set by \a SetDefaultCompressionLevel. |
| 117 | ServerBuilder& SetDefaultCompressionAlgorithm( |
| 118 | grpc_compression_algorithm algorithm); |
| 119 | |
Craig Tiller | db1a5cc | 2016-09-28 14:22:12 -0700 | [diff] [blame^] | 120 | /// Set the attached buffer pool for this server |
| 121 | ServerBuilder& SetBufferPool(const BufferPool& buffer_pool); |
| 122 | |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 123 | ServerBuilder& SetOption(std::unique_ptr<ServerBuilderOption> option); |
yang-g | a23f17b | 2015-11-25 10:21:05 -0800 | [diff] [blame] | 124 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 125 | /// Tries to bind \a server to the given \a addr. |
| 126 | /// |
| 127 | /// It can be invoked multiple times. |
| 128 | /// |
| 129 | /// \param addr The address to try to bind to the server (eg, localhost:1234, |
| 130 | /// 192.168.1.1:31416, [::1]:27182, etc.). |
| 131 | /// \params creds The credentials associated with the server. |
| 132 | /// \param selected_port[out] Upon success, updated to contain the port |
| 133 | /// number. \a nullptr otherwise. |
| 134 | /// |
| 135 | // TODO(dgq): the "port" part seems to be a misnomer. |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 136 | ServerBuilder& AddListeningPort(const grpc::string& addr, |
| 137 | std::shared_ptr<ServerCredentials> creds, |
| 138 | int* selected_port = nullptr); |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 139 | |
David Garcia Quintas | 058c9de | 2016-06-13 19:04:43 -0700 | [diff] [blame] | 140 | /// Add a completion queue for handling asynchronous services. |
| 141 | /// |
| 142 | /// Caller is required to shutdown the server prior to shutting down the |
| 143 | /// returned completion queue. A typical usage scenario: |
| 144 | /// |
| 145 | /// // While building the server: |
| 146 | /// ServerBuilder builder; |
| 147 | /// ... |
| 148 | /// cq_ = builder.AddCompletionQueue(); |
| 149 | /// server_ = builder.BuildAndStart(); |
| 150 | /// |
| 151 | /// // While shutting down the server; |
| 152 | /// server_->Shutdown(); |
| 153 | /// cq_->Shutdown(); // Always *after* the associated server's Shutdown()! |
Sree Kuchibhotla | 1f5e262 | 2016-04-21 12:28:09 -0700 | [diff] [blame] | 154 | /// |
| 155 | /// \param is_frequently_polled This is an optional parameter to inform GRPC |
| 156 | /// library about whether this completion queue would be frequently polled |
| 157 | /// (i.e by calling Next() or AsyncNext()). The default value is 'true' and is |
| 158 | /// the recommended setting. Setting this to 'false' (i.e not polling the |
| 159 | /// completion queue frequently) will have a significantly negative |
| 160 | /// performance impact and hence should not be used in production use cases. |
| 161 | std::unique_ptr<ServerCompletionQueue> AddCompletionQueue( |
| 162 | bool is_frequently_polled = true); |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 163 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 164 | /// Return a running server which is ready for processing calls. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 165 | std::unique_ptr<Server> BuildAndStart(); |
| 166 | |
Yuchen Zeng | 7d099a5 | 2016-05-06 13:21:36 -0700 | [diff] [blame] | 167 | /// For internal use only: Register a ServerBuilderPlugin factory function. |
Yuchen Zeng | 3b8f335 | 2016-05-03 12:18:13 -0700 | [diff] [blame] | 168 | static void InternalAddPluginFactory( |
| 169 | std::unique_ptr<ServerBuilderPlugin> (*CreatePlugin)()); |
| 170 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 171 | private: |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 172 | friend class ::grpc::testing::ServerBuilderPluginTest; |
| 173 | |
Craig Tiller | 42bc87c | 2015-02-23 08:50:19 -0800 | [diff] [blame] | 174 | struct Port { |
| 175 | grpc::string addr; |
| 176 | std::shared_ptr<ServerCredentials> creds; |
| 177 | int* selected_port; |
| 178 | }; |
| 179 | |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 180 | typedef std::unique_ptr<grpc::string> HostString; |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 181 | struct NamedService { |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 182 | explicit NamedService(Service* s) : service(s) {} |
| 183 | NamedService(const grpc::string& h, Service* s) |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 184 | : host(new grpc::string(h)), service(s) {} |
| 185 | HostString host; |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 186 | Service* service; |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 187 | }; |
| 188 | |
Mark D. Roth | 6980362 | 2016-09-06 08:15:36 -0700 | [diff] [blame] | 189 | int max_receive_message_size_; |
| 190 | int max_send_message_size_; |
yang-g | a23f17b | 2015-11-25 10:21:05 -0800 | [diff] [blame] | 191 | std::vector<std::unique_ptr<ServerBuilderOption>> options_; |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 192 | std::vector<std::unique_ptr<NamedService>> services_; |
Craig Tiller | 42bc87c | 2015-02-23 08:50:19 -0800 | [diff] [blame] | 193 | std::vector<Port> ports_; |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 194 | std::vector<ServerCompletionQueue*> cqs_; |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 195 | std::shared_ptr<ServerCredentials> creds_; |
Vijay Pai | 15855f3 | 2016-06-10 12:25:32 -0700 | [diff] [blame] | 196 | std::vector<std::unique_ptr<ServerBuilderPlugin>> plugins_; |
Craig Tiller | db1a5cc | 2016-09-28 14:22:12 -0700 | [diff] [blame^] | 197 | grpc_buffer_pool* buffer_pool_; |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 198 | AsyncGenericService* generic_service_; |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 199 | struct { |
| 200 | bool is_set; |
| 201 | grpc_compression_level level; |
| 202 | } maybe_default_compression_level_; |
| 203 | struct { |
| 204 | bool is_set; |
| 205 | grpc_compression_algorithm algorithm; |
| 206 | } maybe_default_compression_algorithm_; |
| 207 | uint32_t enabled_compression_algorithms_bitset_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | } // namespace grpc |
| 211 | |
Nicolas "Pixel" Noble | 1ff52d5 | 2015-03-01 05:24:36 +0100 | [diff] [blame] | 212 | #endif // GRPCXX_SERVER_BUILDER_H |