| 1 |
#ifndef PROTON_CONTAINER_HPP |
| 2 |
#define PROTON_CONTAINER_HPP |
| 3 |
|
| 4 |
/* |
| 5 |
* |
| 6 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 7 |
* or more contributor license agreements. See the NOTICE file |
| 8 |
* distributed with this work for additional information |
| 9 |
* regarding copyright ownership. The ASF licenses this file |
| 10 |
* to you under the Apache License, Version 2.0 (the |
| 11 |
* "License"); you may not use this file except in compliance |
| 12 |
* with the License. You may obtain a copy of the License at |
| 13 |
* |
| 14 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 15 |
* |
| 16 |
* Unless required by applicable law or agreed to in writing, |
| 17 |
* software distributed under the License is distributed on an |
| 18 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 19 |
* KIND, either express or implied. See the License for the |
| 20 |
* specific language governing permissions and limitations |
| 21 |
* under the License. |
| 22 |
* |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "./fwd.hpp" |
| 26 |
#include "./returned.hpp" |
| 27 |
#include "./types_fwd.hpp" |
| 28 |
|
| 29 |
#include "./internal/export.hpp" |
| 30 |
|
| 31 |
#include <memory> |
| 32 |
#include <string> |
| 33 |
|
| 34 |
/// @file |
| 35 |
/// @copybrief proton::container |
| 36 |
|
| 37 |
namespace proton { |
| 38 |
|
| 39 |
/// A top-level container of connections, sessions, and links. |
| 40 |
/// |
| 41 |
/// A container gives a unique identity to each communicating peer. It |
| 42 |
/// is often a process-level object. |
| 43 |
/// |
| 44 |
/// It also serves as an entry point to the API, allowing connections, |
| 45 |
/// senders, and receivers to be established. It can be supplied with |
| 46 |
/// an event handler in order to intercept important messaging events, |
| 47 |
/// such as newly received messages or newly issued credit for sending |
| 48 |
/// messages. |
| 49 |
class PN_CPP_CLASS_EXTERN container { |
| 50 |
public: |
| 51 |
/// Create a container with a global handler for messaging events. |
| 52 |
/// |
| 53 |
/// **Thread safety** - in a multi-threaded container this handler will be |
| 54 |
/// called concurrently. You can use locks to make that safe, or use a |
| 55 |
/// separate handler for each connection. See @ref mt_page. |
| 56 |
/// |
| 57 |
/// @param handler global handler, called for events on all connections |
| 58 |
/// managed by the container. |
| 59 |
/// |
| 60 |
/// @param id sets the container's unique identity. |
| 61 |
PN_CPP_EXTERN container(messaging_handler& handler, const std::string& id); |
| 62 |
|
| 63 |
/// Create a container with a global handler for messaging events. |
| 64 |
/// |
| 65 |
/// **Thread safety** - in a multi-threaded container this handler will be |
| 66 |
/// called concurrently. You can use locks to make that safe, or use a |
| 67 |
/// separate handler for each connection. See @ref mt_page. |
| 68 |
/// |
| 69 |
/// @param handler global handler, called for events on all connections |
| 70 |
/// managed by the container. |
| 71 |
PN_CPP_EXTERN container(messaging_handler& handler); |
| 72 |
|
| 73 |
/// Create a container. |
| 74 |
/// @param id sets the container's unique identity. |
| 75 |
PN_CPP_EXTERN container(const std::string& id); |
| 76 |
|
| 77 |
/// Create a container. |
| 78 |
/// |
| 79 |
/// This will create a default random identity |
| 80 |
PN_CPP_EXTERN container(); |
| 81 |
|
| 82 |
/// Destroy a container. |
| 83 |
/// |
| 84 |
/// A container must not be destroyed while a call to run() is in progress, |
| 85 |
/// in particular it must not be destroyed from a @ref messaging_handler |
| 86 |
/// callback. |
| 87 |
/// |
| 88 |
/// **Thread safety** - in a multi-threaded application, run() must return |
| 89 |
/// in all threads that call it before destroying the container. |
| 90 |
/// |
| 91 |
PN_CPP_EXTERN ~container(); |
| 92 |
|
| 93 |
/// Connect to `conn_url` and send an open request to the remote |
| 94 |
/// peer. |
| 95 |
/// |
| 96 |
/// Options are applied to the connection as follows. |
| 97 |
/// |
| 98 |
/// 1. client_connection_options() |
| 99 |
/// 2. Options passed to connect() |
| 100 |
/// |
| 101 |
/// Values in later options override earlier ones. The handler in |
| 102 |
/// the composed options is used to call |
| 103 |
/// `messaging_handler::on_connection_open()` when the open |
| 104 |
/// response is received from the remote peer. |
| 105 |
/// |
| 106 |
/// @copydetails returned |
| 107 |
PN_CPP_EXTERN returned<connection> connect(const std::string& conn_url, |
| 108 |
const connection_options& conn_opts); |
| 109 |
|
| 110 |
/// @copybrief connect() |
| 111 |
/// |
| 112 |
/// @copydetails returned |
| 113 |
PN_CPP_EXTERN returned<connection> connect(const std::string& conn_url); |
| 114 |
|
| 115 |
/// Connect using the default @ref connect-config file. |
| 116 |
/// |
| 117 |
/// @copydetails returned |
| 118 |
PN_CPP_EXTERN returned<connection> connect(); |
| 119 |
|
| 120 |
/// Listen for new connections on `listen_url`. |
| 121 |
/// |
| 122 |
/// If the listener opens successfully, listen_handler::on_open() is called. |
| 123 |
/// If it fails to open, listen_handler::on_error() then listen_handler::close() |
| 124 |
/// are called. |
| 125 |
/// |
| 126 |
/// listen_handler::on_accept() is called for each incoming connection to determine |
| 127 |
/// the @ref connection_options to use, including the @ref messaging_handler. |
| 128 |
/// |
| 129 |
/// **Thread safety** - Calls to `listen_handler` methods are serialized for |
| 130 |
/// this listener, but handlers attached to separate listeners may be called |
| 131 |
/// concurrently. |
| 132 |
PN_CPP_EXTERN listener listen(const std::string& listen_url, |
| 133 |
listen_handler& handler); |
| 134 |
|
| 135 |
/// @copybrief listen |
| 136 |
/// |
| 137 |
/// Use a fixed set of options for all accepted connections. See |
| 138 |
/// listen(const std::string&, listen_handler&). |
| 139 |
/// |
| 140 |
/// **Thread safety** - for multi-threaded applications we recommend using a |
| 141 |
/// @ref listen_handler to create a new @ref messaging_handler for each connection. |
| 142 |
/// See listen(const std::string&, listen_handler&) and @ref mt_page |
| 143 |
PN_CPP_EXTERN listener listen(const std::string& listen_url, |
| 144 |
const connection_options& conn_opts); |
| 145 |
|
| 146 |
/// @copybrief listen |
| 147 |
/// |
| 148 |
/// New connections will use the handler from |
| 149 |
/// `server_connection_options()`. See listen(const std::string&, const |
| 150 |
/// connection_options&); |
| 151 |
PN_CPP_EXTERN listener listen(const std::string& listen_url); |
| 152 |
|
| 153 |
/// Run the container in the current thread. |
| 154 |
/// |
| 155 |
/// The call returns when the container stops. See `auto_stop()` |
| 156 |
/// and `stop()`. |
| 157 |
/// |
| 158 |
/// **C++ versions** - With C++11 or later, you can call `run()` |
| 159 |
/// in multiple threads to create a thread pool. See also |
| 160 |
/// `run(int count)`. |
| 161 |
PN_CPP_EXTERN void run(); |
| 162 |
|
| 163 |
/// Run the container with a pool of `count` threads, including the current thread. |
| 164 |
/// |
| 165 |
/// **C++ versions** - Available with C++11 or later. |
| 166 |
/// |
| 167 |
/// The call returns when the container stops. See `auto_stop()` |
| 168 |
/// and `stop()`. |
| 169 |
PN_CPP_EXTERN void run(int count); |
| 170 |
|
| 171 |
/// Enable or disable automatic container stop. It is enabled by |
| 172 |
/// default. |
| 173 |
/// |
| 174 |
/// If true, the container stops when all active connections and |
| 175 |
/// listeners are closed. If false, the container keeps running |
| 176 |
/// until `stop()` is called. |
| 177 |
PN_CPP_EXTERN void auto_stop(bool enabled); |
| 178 |
|
| 179 |
/// Stop the container with error condition `err`. |
| 180 |
/// |
| 181 |
/// @copydetails stop() |
| 182 |
PN_CPP_EXTERN void stop(const error_condition& err); |
| 183 |
|
| 184 |
/// Stop the container with an empty error condition. |
| 185 |
/// |
| 186 |
/// This function initiates shutdown and immediately returns. The |
| 187 |
/// shutdown process has the following steps. |
| 188 |
/// |
| 189 |
/// - Abort all open connections and listeners. |
| 190 |
/// - Process final handler events and queued work. |
| 191 |
/// - If `!err.empty()`, fire `messaging_handler::on_transport_error`. |
| 192 |
/// |
| 193 |
/// When the process is complete, `run()` returns in all threads. |
| 194 |
/// |
| 195 |
/// **Thread safety** - It is safe to call this method in any thread. |
| 196 |
PN_CPP_EXTERN void stop(); |
| 197 |
|
| 198 |
/// Open a connection and sender for `addr_url`. |
| 199 |
/// |
| 200 |
/// @copydetails returned |
| 201 |
PN_CPP_EXTERN returned<sender> open_sender(const std::string& addr_url); |
| 202 |
|
| 203 |
/// Open a connection and sender for `addr_url`. |
| 204 |
/// |
| 205 |
/// Supplied sender options will override the container's |
| 206 |
/// template options. |
| 207 |
/// |
| 208 |
/// @copydetails returned |
| 209 |
PN_CPP_EXTERN returned<sender> open_sender(const std::string& addr_url, |
| 210 |
const proton::sender_options& snd_opts); |
| 211 |
|
| 212 |
/// Open a connection and sender for `addr_url`. |
| 213 |
/// |
| 214 |
/// Supplied connection options will override the |
| 215 |
/// container's template options. |
| 216 |
/// |
| 217 |
/// @copydetails returned |
| 218 |
PN_CPP_EXTERN returned<sender> open_sender(const std::string& addr_url, |
| 219 |
const connection_options& conn_opts); |
| 220 |
|
| 221 |
/// Open a connection and sender for `addr_url`. |
| 222 |
/// |
| 223 |
/// Supplied sender or connection options will override the |
| 224 |
/// container's template options. |
| 225 |
/// |
| 226 |
/// @copydetails returned |
| 227 |
PN_CPP_EXTERN returned<sender> open_sender(const std::string& addr_url, |
| 228 |
const proton::sender_options& snd_opts, |
| 229 |
const connection_options& conn_opts); |
| 230 |
|
| 231 |
/// Open a connection and receiver for `addr_url`. |
| 232 |
/// |
| 233 |
/// @copydetails returned |
| 234 |
PN_CPP_EXTERN returned<receiver> open_receiver(const std::string& addr_url); |
| 235 |
|
| 236 |
|
| 237 |
/// Open a connection and receiver for `addr_url`. |
| 238 |
/// |
| 239 |
/// Supplied receiver options will override the container's |
| 240 |
/// template options. |
| 241 |
/// |
| 242 |
/// @copydetails returned |
| 243 |
PN_CPP_EXTERN returned<receiver> open_receiver(const std::string& addr_url, |
| 244 |
const proton::receiver_options& rcv_opts); |
| 245 |
|
| 246 |
/// Open a connection and receiver for `addr_url`. |
| 247 |
/// |
| 248 |
/// Supplied receiver or connection options will override the |
| 249 |
/// container's template options. |
| 250 |
/// |
| 251 |
/// @copydetails returned |
| 252 |
PN_CPP_EXTERN returned<receiver> open_receiver(const std::string& addr_url, |
| 253 |
const connection_options& conn_opts); |
| 254 |
|
| 255 |
/// Open a connection and receiver for `addr_url`. |
| 256 |
/// |
| 257 |
/// Supplied receiver or connection options will override the |
| 258 |
/// container's template options. |
| 259 |
/// |
| 260 |
/// @copydetails returned |
| 261 |
PN_CPP_EXTERN returned<receiver> open_receiver(const std::string& addr_url, |
| 262 |
const proton::receiver_options& rcv_opts, |
| 263 |
const connection_options& conn_opts); |
| 264 |
|
| 265 |
/// A unique identifier for the container. |
| 266 |
PN_CPP_EXTERN std::string id() const; |
| 267 |
|
| 268 |
/// Connection options applied to outgoing connections. These are |
| 269 |
/// applied first and then overridden by any options provided in |
| 270 |
/// `connect()` or `messaging_handler::on_connection_open()`. |
| 271 |
PN_CPP_EXTERN void client_connection_options(const connection_options& conn_opts); |
| 272 |
|
| 273 |
/// @copydoc client_connection_options |
| 274 |
PN_CPP_EXTERN connection_options client_connection_options() const; |
| 275 |
|
| 276 |
/// Connection options applied to incoming connections. These are |
| 277 |
/// applied first and then overridden by any options provided in |
| 278 |
/// `listen()`, `listen_handler::on_accept()`, or |
| 279 |
/// `messaging_handler::on_connection_open()`. |
| 280 |
PN_CPP_EXTERN void server_connection_options(const connection_options& conn_opts); |
| 281 |
|
| 282 |
/// @copydoc server_connection_options |
| 283 |
PN_CPP_EXTERN connection_options server_connection_options() const; |
| 284 |
|
| 285 |
/// Sender options applied to senders created by this |
| 286 |
/// container. They are applied before |
| 287 |
/// `messaging_handler::on_sender_open()` and can be overridden. |
| 288 |
PN_CPP_EXTERN void sender_options(const class sender_options& snd_opts); |
| 289 |
|
| 290 |
/// @copydoc sender_options |
| 291 |
PN_CPP_EXTERN class sender_options sender_options() const; |
| 292 |
|
| 293 |
/// Receiver options applied to receivers created by this |
| 294 |
/// container. They are applied before |
| 295 |
/// `messaging_handler::on_receiver_open()` and can be overridden. |
| 296 |
PN_CPP_EXTERN void receiver_options(const class receiver_options& rcv_opts); |
| 297 |
|
| 298 |
/// @copydoc receiver_options |
| 299 |
PN_CPP_EXTERN class receiver_options receiver_options() const; |
| 300 |
|
| 301 |
/// Schedule `fn` for execution after a duration. The piece of |
| 302 |
/// work can be created from a function object. |
| 303 |
/// |
| 304 |
/// **C++ versions** - With C++11 and later, use a |
| 305 |
/// `std::function<void()>` type for the `fn` parameter. |
| 306 |
PN_CPP_EXTERN void schedule(duration dur, work fn); |
| 307 |
|
| 308 |
/// **Deprecated** - Use `container::schedule(duration, work)`. |
| 309 |
PN_CPP_EXTERN PN_CPP_DEPRECATED("Use 'container::schedule(duration, work)'") void schedule(duration dur, void_function0& fn); |
| 310 |
|
| 311 |
private: |
| 312 |
/// Declare both v03 and v11 if compiling with c++11 as the library contains both. |
| 313 |
/// A C++11 user should never call the v03 overload so it is private in this case |
| 314 |
PN_CPP_EXTERN void schedule(duration dur, internal::v03::work fn); |
| 315 |
class impl; |
| 316 |
std::unique_ptr<impl> impl_; |
| 317 |
|
| 318 |
/// @cond INTERNAL |
| 319 |
friend class connection_options; |
| 320 |
friend class session_options; |
| 321 |
friend class receiver_options; |
| 322 |
friend class sender_options; |
| 323 |
friend class work_queue; |
| 324 |
/// @endcond |
| 325 |
}; |
| 326 |
|
| 327 |
} // proton |
| 328 |
|
| 329 |
#endif // PROTON_CONTAINER_HPP |