forked from bloomberg/ntf-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ntci_interface.h
335 lines (292 loc) · 14.8 KB
/
ntci_interface.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Copyright 2020-2023 Bloomberg Finance L.P.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef INCLUDED_NTCI_INTERFACE
#define INCLUDED_NTCI_INTERFACE
#include <bsls_ident.h>
BSLS_IDENT("$Id: $")
#include <ntca_datagramsocketoptions.h>
#include <ntca_encryptioncertificateoptions.h>
#include <ntca_encryptionclientoptions.h>
#include <ntca_encryptionkeyoptions.h>
#include <ntca_encryptionserveroptions.h>
#include <ntca_listenersocketoptions.h>
#include <ntca_streamsocketoptions.h>
#include <ntccfg_platform.h>
#include <ntci_datagramsocket.h>
#include <ntci_datagramsocketfactory.h>
#include <ntci_datagramsocketmanager.h>
#include <ntci_datagramsocketsession.h>
#include <ntci_datapool.h>
#include <ntci_encryptioncertificate.h>
#include <ntci_encryptioncertificategenerator.h>
#include <ntci_encryptionclient.h>
#include <ntci_encryptionclientfactory.h>
#include <ntci_encryptionkey.h>
#include <ntci_encryptionkeygenerator.h>
#include <ntci_encryptionserver.h>
#include <ntci_encryptionserverfactory.h>
#include <ntci_executor.h>
#include <ntci_listenersocket.h>
#include <ntci_listenersocketfactory.h>
#include <ntci_listenersocketmanager.h>
#include <ntci_listenersocketsession.h>
#include <ntci_proactor.h>
#include <ntci_ratelimiter.h>
#include <ntci_ratelimiterfactory.h>
#include <ntci_reactor.h>
#include <ntci_resolver.h>
#include <ntci_strand.h>
#include <ntci_strandfactory.h>
#include <ntci_streamsocket.h>
#include <ntci_streamsocketfactory.h>
#include <ntci_streamsocketmanager.h>
#include <ntci_streamsocketsession.h>
#include <ntci_thread.h>
#include <ntci_threadpool.h>
#include <ntci_timer.h>
#include <ntci_timerfactory.h>
#include <ntci_waiter.h>
#include <ntcscm_version.h>
#include <ntsa_error.h>
#include <ntsi_channel.h>
#include <bdlbb_blob.h>
#include <bsl_functional.h>
#include <bsl_memory.h>
namespace BloombergLP {
namespace ntci {
/// Provide an interface to create asynchronous, multiplexed sockets and timers
/// using reactors or proactors driven by a pool of threads.
///
/// @details
/// Provide a mechanism that runs a pool of threads to automatically block on a
/// single reactor or proactor (if dynamically load balancing) or set of
/// reactors or proactors (if statically load balancing) to implement the
/// asynchronous behavior required by sockets and timers.
///
/// @par Thread Safety
/// This class is thread safe.
///
/// @ingroup module_ntci_runtime
class Interface : public ntci::DatagramSocketFactory,
public ntci::ListenerSocketFactory,
public ntci::StreamSocketFactory,
public ntci::TimerFactory,
public ntci::StrandFactory,
public ntci::RateLimiterFactory,
public ntci::EncryptionClientFactory,
public ntci::EncryptionServerFactory,
public ntci::EncryptionCertificateGenerator,
public ntci::EncryptionKeyGenerator,
public ntci::Executor,
public ntci::ThreadPool,
public ntci::DataPool
{
public:
/// Destroy this object.
virtual ~Interface();
/// Start each thread managed by the interface. Return the error.
virtual ntsa::Error start() = 0;
/// Begin stopping each thread managed by the interface.
virtual void shutdown() = 0;
/// Wait for each thread managed by this interface to stop.
virtual void linger() = 0;
/// Close all sockets and timers.
virtual ntsa::Error closeAll() = 0;
/// Create a new datagram socket with the specified 'options'.
/// Optionally specify a 'basicAllocator' used to supply memory. If
/// 'basicAllocator' is 0, the currently installed default allocator is
/// used.
virtual bsl::shared_ptr<ntci::DatagramSocket> createDatagramSocket(
const ntca::DatagramSocketOptions& options,
bslma::Allocator* basicAllocator = 0) = 0;
/// Create a new listener socket with the specified 'options'.
/// Optionally specify a 'basicAllocator' used to supply memory. If
/// 'basicAllocator' is 0, the currently installed default allocator is
/// used.
virtual bsl::shared_ptr<ntci::ListenerSocket> createListenerSocket(
const ntca::ListenerSocketOptions& options,
bslma::Allocator* basicAllocator = 0) = 0;
/// Create a new stream socket with the specified 'options'. Optionally
/// specify a 'basicAllocator' used to supply memory. If
/// 'basicAllocator' is 0, the currently installed default allocator is
/// used.
virtual bsl::shared_ptr<ntci::StreamSocket> createStreamSocket(
const ntca::StreamSocketOptions& options,
bslma::Allocator* basicAllocator = 0) = 0;
/// Create a new timer according to the specified 'options' that invokes
/// the specified 'session' for each timer event on this object's
/// 'strand()', if defined, or on an unspecified thread otherwise.
/// Optionally specify a 'basicAllocator' used to supply memory. If
/// 'basicAllocator' is 0, the currently installed default allocator is
/// used.
virtual bsl::shared_ptr<ntci::Timer> createTimer(
const ntca::TimerOptions& options,
const bsl::shared_ptr<ntci::TimerSession>& session,
bslma::Allocator* basicAllocator = 0) = 0;
/// Create a new timer according to the specified 'options' that invokes
/// the specified 'callback' for each timer event on this object's
/// 'strand()', if defined, or on an unspecified thread otherwise.
/// Optionally specify a 'basicAllocator' used to supply memory. If
/// 'basicAllocator' is 0, the currently installed default allocator is
/// used.
virtual bsl::shared_ptr<ntci::Timer> createTimer(
const ntca::TimerOptions& options,
const ntci::TimerCallback& callback,
bslma::Allocator* basicAllocator = 0) = 0;
/// Create a new strand to serialize execution of functors. Optionally
/// specify a 'basicAllocator' used to supply memory. If
/// 'basicAllocator' is 0, the currently installed default allocator is
/// used.
virtual bsl::shared_ptr<ntci::Strand> createStrand(
bslma::Allocator* basicAllocator = 0) = 0;
/// Return a shared pointer to a data container suitable for storing
/// incoming data. The resulting data container is is automatically
/// returned to this pool when its reference count reaches zero.
virtual bsl::shared_ptr<ntsa::Data> createIncomingData() = 0;
/// Return a shared pointer to a data container suitable for storing
/// outgoing data. The resulting data container is is automatically
/// returned to this pool when its reference count reaches zero.
virtual bsl::shared_ptr<ntsa::Data> createOutgoingData() = 0;
/// Return a shared pointer to a blob suitable for storing incoming
/// data. The resulting blob is is automatically returned to this pool
/// when its reference count reaches zero.
virtual bsl::shared_ptr<bdlbb::Blob> createIncomingBlob() = 0;
/// Return a shared pointer to a blob suitable for storing incoming
/// data. The resulting blob is is automatically returned to this pool
/// when its reference count reaches zero.
virtual bsl::shared_ptr<bdlbb::Blob> createOutgoingBlob() = 0;
/// Load into the specified 'blobBuffer' the data and size of a new
/// buffer allocated from the incoming blob buffer factory.
virtual void createIncomingBlobBuffer(bdlbb::BlobBuffer* blobBuffer) = 0;
/// Load into the specified 'blobBuffer' the data and size of a new
/// buffer allocated from the outgoing blob buffer factory.
virtual void createOutgoingBlobBuffer(bdlbb::BlobBuffer* blobBuffer) = 0;
/// Create a new rate limiter with the specified 'configuration'.
/// Optionally specify a 'basicAllocator' used to supply memory. If
/// 'basicAllocator' is 0, the currently installed default allocator is
/// used. Return the error.
virtual bsl::shared_ptr<ntci::RateLimiter> createRateLimiter(
const ntca::RateLimiterConfig& configuration,
bslma::Allocator* basicAllocator = 0) = 0;
/// Load into the specified 'result' a new encryption client with the
/// specified 'options'. Optionally specify a 'basicAllocator' used to
/// supply memory. If 'basicAllocator' is 0, the currently installed
/// default allocator is used. Return the error.
virtual ntsa::Error createEncryptionClient(
bsl::shared_ptr<ntci::EncryptionClient>* result,
const ntca::EncryptionClientOptions& options,
bslma::Allocator* basicAllocator = 0) = 0;
/// Load into the specified 'result' a new encryption client with the
/// specified 'options'. Allocate blob buffers using the specified
/// 'blobBufferFactory'. Optionally specify a 'basicAllocator' used to
/// supply memory. If 'basicAllocator' is 0, the currently installed
/// default allocator is used. Return the error.
virtual ntsa::Error createEncryptionClient(
bsl::shared_ptr<ntci::EncryptionClient>* result,
const ntca::EncryptionClientOptions& options,
const bsl::shared_ptr<bdlbb::BlobBufferFactory>& blobBufferFactory,
bslma::Allocator* basicAllocator = 0) = 0;
/// Load into the specified 'result' a new encryption client with the
/// specified 'options'. Allocate data containers using the specified
/// 'dataPool'. Optionally specify a 'basicAllocator' used to
/// supply memory. If 'basicAllocator' is 0, the currently installed
/// default allocator is used. Return the error.
virtual ntsa::Error createEncryptionClient(
bsl::shared_ptr<ntci::EncryptionClient>* result,
const ntca::EncryptionClientOptions& options,
const bsl::shared_ptr<ntci::DataPool>& dataPool,
bslma::Allocator* basicAllocator = 0) = 0;
/// Load into the specified 'result' a new encryption client with the
/// specified 'options'. Optionally specify a 'basicAllocator' used to
/// supply memory. If 'basicAllocator' is 0, the currently installed
/// default allocator is used. Return the error.
virtual ntsa::Error createEncryptionServer(
bsl::shared_ptr<ntci::EncryptionServer>* result,
const ntca::EncryptionServerOptions& options,
bslma::Allocator* basicAllocator = 0) = 0;
/// Load into the specified 'result' a new encryption client with the
/// specified 'options'. Allocate blob buffers using the specified
/// 'blobBufferFactory'. Optionally specify a 'basicAllocator' used to
/// supply memory. If 'basicAllocator' is 0, the currently installed
/// default allocator is used. Return the error.
virtual ntsa::Error createEncryptionServer(
bsl::shared_ptr<ntci::EncryptionServer>* result,
const ntca::EncryptionServerOptions& options,
const bsl::shared_ptr<bdlbb::BlobBufferFactory>& blobBufferFactory,
bslma::Allocator* basicAllocator = 0) = 0;
/// Load into the specified 'result' a new encryption client with the
/// specified 'options'. Allocate data containers using the specified
/// 'dataPool'. Optionally specify a 'basicAllocator' used to
/// supply memory. If 'basicAllocator' is 0, the currently installed
/// default allocator is used. Return the error.
virtual ntsa::Error createEncryptionServer(
bsl::shared_ptr<ntci::EncryptionServer>* result,
const ntca::EncryptionServerOptions& options,
const bsl::shared_ptr<ntci::DataPool>& dataPool,
bslma::Allocator* basicAllocator = 0) = 0;
/// Defer the execution of the specified 'functor'.
virtual void execute(const Functor& functor) = 0;
/// Atomically defer the execution of the specified 'functorSequence'
/// immediately followed by the specified 'functor', then clear the
/// 'functorSequence'.
virtual void moveAndExecute(FunctorSequence* functorSequence,
const Functor& functor) = 0;
/// Return the resolver used by this interface.
virtual const bsl::shared_ptr<ntci::Resolver>& resolver() const = 0;
/// Return the incoming blob buffer factory.
virtual const bsl::shared_ptr<bdlbb::BlobBufferFactory>&
incomingBlobBufferFactory() const = 0;
/// Return the outgoing blob buffer factory.
virtual const bsl::shared_ptr<bdlbb::BlobBufferFactory>&
outgoingBlobBufferFactory() const = 0;
/// Return the strand on which this object's functions should be called.
virtual const bsl::shared_ptr<ntci::Strand>& strand() const = 0;
/// Load into the specified 'result' the executor driven by the thread
/// identified by the specified 'threadHandle'. Return true if such an
/// executor exists, and false otherwise.
virtual bool lookupByThreadHandle(
bsl::shared_ptr<ntci::Executor>* result,
bslmt::ThreadUtil::Handle threadHandle) const = 0;
/// Load into the specified 'result' the executor driven by the thread
/// identified by the specified 'threadIndex'. Return true if such a
/// thread exists, and false otherwise.
virtual bool lookupByThreadIndex(bsl::shared_ptr<ntci::Executor>* result,
bsl::size_t threadIndex) const = 0;
/// Return the current elapsed time since the Unix epoch.
virtual bsls::TimeInterval currentTime() const = 0;
};
/// Provide a guard to automatically stop an interface and
/// block until the interface is asynchronously stopped.
class InterfaceStopGuard
{
bsl::shared_ptr<ntci::Interface> d_interface_sp;
private:
InterfaceStopGuard(const InterfaceStopGuard&) BSLS_KEYWORD_DELETED;
InterfaceStopGuard& operator=(const InterfaceStopGuard&)
BSLS_KEYWORD_DELETED;
public:
/// Create a new interface stop guard for the specified 'interface', if
/// any.
explicit InterfaceStopGuard(
const bsl::shared_ptr<ntci::Interface>& interface);
/// Destroy this object: stop the managed interface, if any, and block
/// until the interface is asynchronously stopped.
~InterfaceStopGuard();
/// Release the guard and return the guarded object.
bsl::shared_ptr<ntci::Interface> release();
};
} // close package namespace
} // close enterprise namespace
#endif