Ocean
Loading...
Searching...
No Matches
Maintenance.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_BASE_MAINTENANCE_H
9#define META_OCEAN_BASE_MAINTENANCE_H
10
11#include "ocean/base/Base.h"
12#include "ocean/base/Lock.h"
13#include "ocean/base/RandomI.h"
16
17#include <queue>
18#include <vector>
19
20namespace Ocean
21{
22
23/**
24 * This class implements a maintenance manager.
25 * The maintenance manager allows to transport maintenance data, maintenance information or maintenance messages from an arbitrary component to a central component handling or forwarding the data.<br>
26 * Further, the maintenance manager can receive data, information or massages from a connector that receives arbitrary maintenance information from a remote component so that it can be distributed by this maintenance manager.<br>
27 * An application can have at most one instance of a maintenance manager.<br>
28 * Beware: The maintenance manager accepts data only if the manager is active (the manager is deactivated by default).<br>
29 * Due to performance issues: Check whether the manager is active before preparing maintenance information to save computational time.
30 * @ingroup base
31 */
32class OCEAN_BASE_EXPORT Maintenance : public Singleton<Maintenance>
33{
34 friend class Singleton<Maintenance>;
35
36 public:
37
38 /**
39 * Definition of a vector holding bytes.
40 */
41 using Buffer = std::vector<uint8_t>;
42
43 /**
44 * This class is the base class for all maintenance connectors.
45 * A maintenance connector connects a local maintenance manager with a remote maintenance manager to transmit the information.<br>
46 * The actual implementation of any maintenance connector must be done in a derived class, thus individual connectors with individual capabilities can be implemented.<br>
47 */
48 class OCEAN_BASE_EXPORT Connector
49 {
50 protected:
51
52 /**
53 * Creates a new connector.
54 */
55 inline Connector();
56
57 /**
58 * Explicitly places a maintenance data, information, message into the local maintenance manager.
59 * Explicit maintenance data can be set even if the manager is not active.<br>
60 * @param name The name of the maintenance manager belonging to the data which will be placed
61 * @param id The id of the maintenance manager belonging to the data which will be placed
62 * @param tag The tag of the maintenance data
63 * @param buffer The buffer of the maintenance data, which will be moved
64 * @param timestamp The timestamp of the maintenance data
65 * @return True, if succeeded
66 * @see Maintenance::place().
67 */
68 static bool place(const std::string& name, const uint64_t id, const std::string& tag, Buffer&& buffer, const Timestamp timestamp);
69
70 /**
71 * Encodes a maintenance data to one combined package.
72 * The resulting buffer holds the optional header followed by the payload, all fields in host byte order:
73 * @code
74 * |<- reservedHeaderSize ->|<-------------------------------------------------- payload -------------------------------------------------->|
75 * +------------------------+-----------+------------+-----------------+--------+-----------+----------------+------------+-----------------+
76 * | header, left untouched | timestamp | nameLength | name | id | tagLength | tag | bufferSize | buffer |
77 * | | 8 byte | 8 byte | nameLength byte | 8 byte | 8 byte | tagLength byte | 8 byte | bufferSize byte |
78 * +------------------------+-----------+------------+-----------------+--------+-----------+----------------+------------+-----------------+
79 * @endcode
80 * `timestamp` is the timestamp of the maintenance data, a Timestamp and therefore a double.
81 * `name` is the readable name of the maintenance manager the data originated from, e.g. the application name.
82 * `id` is the random 64 bit id of that manager, which distinguishes two managers that happen to share a name.
83 * `tag` describes what the payload is and is what a receiver dispatches on.
84 * `buffer` is the maintenance payload itself.
85 * `nameLength`, `tagLength` and `bufferSize` each give the byte count of the variable length field which follows it.
86 * None of the strings is null terminated on the wire.
87 * The first `reservedHeaderSize` bytes are left untouched for a transport to write its own header into.
88 * This is how MaintenanceTCPConnector places its length prefix without copying the payload again.
89 * @param name The name of the maintenance manager providing the data
90 * @param id The id of the maintenance manager providing the data
91 * @param tag The tag of the maintenance data
92 * @param buffer The maintenance data as buffer
93 * @param timestamp The timestamp of the maintenance data
94 * @param reservedHeaderSize The number of bytes which will be reserved for the header, so that the resulting buffer has an optional header followed by the payload data
95 * @param encodedBuffer The resulting encoded package
96 */
97 static void encodeData(const std::string& name, const uint64_t id, const std::string& tag, const Buffer& buffer, const Timestamp timestamp, const size_t reservedHeaderSize, Buffer& encodedBuffer);
98
99 /**
100 * Decodes a package buffer to maintenance data with corresponding information.
101 * @param encodedBuffer The encoded network package buffer
102 * @param encodedBufferSize The size of the encoded buffer in bytes
103 * @param name The resulting name of the maintenance manager to which the data has been sent originally
104 * @param id The result id of the maintenance manager to which the data has been sent originally
105 * @param tag The tag of the maintenance data
106 * @param buffer The buffer of the maintenance data
107 * @param timestamp The timestamp of the maintenance data
108 * @return True, if succeeded
109 */
110 static bool decodeData(const void* encodedBuffer, const size_t encodedBufferSize, std::string& name, uint64_t& id, std::string& tag, Buffer& buffer, Timestamp& timestamp);
111 };
112
113 protected:
114
115 /**
116 * This class implements a maintenance data element.
117 */
119 {
120 public:
121
122 /**
123 * Creates a new empty element.
124 */
125 Element() = default;
126
127 /**
128 * Copy constructor.
129 * @param element The element to be copied
130 */
131 Element(const Element& element) = default;
132
133 /**
134 * Move constructor.
135 * @param element The element to be moved
136 */
137 Element(Element&& element) = default;
138
139 /**
140 * Creates a new maintenance element.
141 * @param name The name of the maintenance manager to which the data has been sent
142 * @param id The id of the maintenance manager to which the data has been set
143 * @param timestamp The timestamp of the maintenance data
144 * @param tag The tag of the maintenance data
145 * @param buffer The maintenance data as buffer
146 */
147 inline Element(const std::string& name, const uint64_t id, const Timestamp timestamp, const std::string& tag, const Buffer& buffer);
148
149 /**
150 * Creates a new maintenance element.
151 * @param name The name of the maintenance manager to which the data has been sent
152 * @param id The id of the maintenance manager to which the data has been set
153 * @param timestamp The timestamp of the maintenance data
154 * @param tag The tag of the maintenance data
155 * @param buffer The maintenance data as buffer, will be moved
156 */
157 inline Element(const std::string& name, const uint64_t id, const Timestamp timestamp, const std::string& tag, Buffer&& buffer);
158
159 /**
160 * Returns the name of the maintenance manager to which the maintenance of this element data has been sent.
161 * @return The readable name of the maintenance manager
162 */
163 inline const std::string& name() const;
164
165 /**
166 * Returns the name of the maintenance manager to which the maintenance of this element data has been sent.
167 * @return The readable name of the maintenance manager
168 */
169 inline std::string& name();
170
171 /**
172 * Return sthe id of the maintenance manager to which the maintenance data of this element has been sent.
173 * @return The id of the maintenance manager
174 */
175 inline uint64_t id() const;
176
177 /**
178 * The timestamp of the maintenance data of this element.
179 * @return The timestamp of the maintenance data
180 */
181 inline Timestamp timestamp() const;
182
183 /**
184 * Returns the tag of the maintenance data of this element.
185 * @return The tag of the maintenance data
186 */
187 inline const std::string& tag() const;
188
189 /**
190 * Returns the tag of the maintenance data of this element.
191 * @return The tag of the maintenance data
192 */
193 inline std::string& tag();
194
195 /**
196 * Returns the buffer of the maintenance data of this element.
197 * @return The buffer of the maintenance data
198 */
199 inline const Buffer& buffer() const;
200
201 /**
202 * Returns the buffer of the maintenance data of this element.
203 * @return The buffer of the maintenance data
204 */
205 inline Buffer& buffer();
206
207 /**
208 * Assign operator.
209 * @param element The second element to be copied
210 * @return Reference to this element
211 */
212 Element& operator=(const Element& element) = default;
213
214 /**
215 * Assign operator.
216 * @param element The second element to be moved
217 * @return Reference to this element
218 */
219 Element& operator=(Element&& element) = default;
220
221 protected:
222
223 /// The name of the manager to which the data has been sent.
224 std::string name_;
225
226 /// The id of the manager to which the data has been sent.
227 uint64_t id_ = 0ull;
228
229 /// The timestamp of the data.
230 Timestamp timestamp_ = Timestamp(false);
231
232 /// The tag of the data.
233 std::string tag_;
234
235 /// The buffer of the data.
237 };
238
239 /**
240 * Definition of a vector holding maintenance data elements.
241 */
242 using ElementQueue = std::queue<Element>;
243
244 public:
245
246 /**
247 * Returns whether the maintenance manager is active or not.
248 * Check whether the manager is active before preparing information which will be forwarded to this manager.<br>
249 * @return True, if so
250 * @see setActive().
251 */
252 inline bool isActive() const;
253
254 /**
255 * Returns the name of this maintenance manager.
256 * @return The readable name of this manager
257 * @see setName().
258 */
259 inline std::string name() const;
260
261 /**
262 * Returns a random id of this maintenance manager.
263 * The id provides a random 64 bit number allowing to distinguish between individual maintenance managers with same name (e.g., distributed in a large system connected by a network).
264 * @return The random id of this manager
265 */
266 inline uint64_t id() const;
267
268 /**
269 * Returns whether this maintenance manager is currently empty (does not hold any maintenance data, information or messages).
270 * @return True, if so
271 */
272 inline bool isEmpty() const;
273
274 /**
275 * Returns the number of maintenance data, information or messages which are currently stored in this manager.
276 * @return The number of maintenance messages
277 */
278 inline size_t size() const;
279
280 /**
281 * Activates or deactivates this maintenance manager.
282 * By default this manager is deactivates and thus will not accept any data, information or messages.<br>
283 * @param state True, to activate this manager; False; to deactivate this manager
284 * @see active().
285 */
286 inline void setActive(const bool state);
287
288 /**
289 * Sets the name of this maintenance manager which should be a readable name of the application in which this manager is used.
290 * The name should be set once at application start.<br>
291 * @param name The readable name to set
292 * @see name().
293 */
294 inline void setName(const std::string& name);
295
296 /**
297 * Sends new maintenance data to this manager.
298 * Check whether this manager is active before.<br>
299 * @param tag A tag specifying purpose of the provided data
300 * @param data The data to be sent
301 * @param size The size of the data to be sent, in bytes
302 * @param timestamp The timestamp of the maintenance data
303 * @return True, if the data could be sent
304 */
305 bool send(const std::string& tag, const void* data, const size_t size, const Timestamp timestamp = Timestamp(true));
306
307 /**
308 * Sends new maintenance data to this manager.
309 * Check whether this manager is active before.<br>
310 * @param tag A tag specifying purpose of the provided data
311 * @param buffer The data buffer to be sent
312 * @param timestamp The timestamp of the maintenance data
313 * @return True, if the data could be sent
314 */
315 bool send(const std::string& tag, const Buffer& buffer, const Timestamp timestamp = Timestamp(true));
316
317 /**
318 * Sends new maintenance data to this manager.
319 * Check whether this manager is active before.<br>
320 * @param tag A tag specifying purpose of the provided data
321 * @param buffer The data buffer to be sent, which will be moved
322 * @param timestamp The timestamp of the maintenance data
323 * @return True, if the data could be sent
324 */
325 bool send(const std::string& tag, Buffer&& buffer, const Timestamp timestamp = Timestamp(true));
326
327 /**
328 * Receives the oldest maintenance data from this manager and pops it from the manager.
329 * Maintenance data can be received even if the manager is not active.<br>
330 * @param name The name of the maintenance manager to which the data has been sent
331 * @param id The id of the maintenance manager to which the data has been sent
332 * @param tag The tag of the maintenance data
333 * @param buffer The maintenance data as buffer
334 * @param timestamp The timestamp of the maintenance data
335 * @return True, this manager had data which has been received
336 */
337 bool receive(std::string& name, uint64_t& id, std::string& tag, Buffer& buffer, Timestamp& timestamp);
338
339 /**
340 * Combines two buffers.
341 * @param firstBuffer The first buffer
342 * @param secondBuffer The second buffer
343 * @return The resulting combined buffer
344 */
345 static inline Buffer combine(const Buffer& firstBuffer, const Buffer& secondBuffer);
346
347 /**
348 * Appends a second buffer to a first buffer.
349 * @param firstBuffer The first buffer to which the second buffer will be appended
350 * @param secondBuffer The second buffer
351 */
352 static inline void appendBuffer(Buffer& firstBuffer, const Buffer& secondBuffer);
353
354 protected:
355
356 /**
357 * Creates a new maintenance manager object.
358 */
359 inline Maintenance();
360
361 /**
362 * Explicitly places a maintenance data, information, message into this manager.
363 * Explicit maintenance data can be set even if the manager is not active.<br>
364 * @param name The name of the maintenance manager belonging to the data which will be placed
365 * @param id The id of the maintenance manager belonging to the data which will be placed
366 * @param tag The tag of the maintenance data
367 * @param buffer The buffer of the maintenance data, which will be moved
368 * @param timestamp The timestamp of the maintenance data
369 * @return True, if succeeded
370 * @see Connector::place().
371 */
372 bool place(const std::string& name, const uint64_t id, const std::string& tag, Buffer&& buffer, const Timestamp timestamp);
373
374 protected:
375
376 /// The activation statement of this manager.
378
379 /// The readable name of this manager.
380 std::string maintenanceName;
381
382 /// The random id of this manager.
384
385 /// The maintenance element queue.
387
388 /// The maintenance lock.
390};
391
393{
394 // nothing to do here
395}
396
397inline Maintenance::Element::Element(const std::string& name, const uint64_t id, const Timestamp timestamp, const std::string& tag, const Buffer& buffer) :
398 name_(name),
399 id_(id),
400 timestamp_(timestamp),
401 tag_(tag),
402 buffer_(buffer)
403{
404 // nothing to do here
405}
406
407inline Maintenance::Element::Element(const std::string& name, const uint64_t id, const Timestamp timestamp, const std::string& tag, Buffer&& buffer) :
408 name_(name),
409 id_(id),
410 timestamp_(timestamp),
411 tag_(tag),
412 buffer_(std::move(buffer))
413{
414 // nothing to do here
415}
416
417inline const std::string& Maintenance::Element::name() const
418{
419 return name_;
420}
421
422inline std::string& Maintenance::Element::name()
423{
424 return name_;
425}
426
427inline uint64_t Maintenance::Element::id() const
428{
429 return id_;
430}
431
433{
434 return timestamp_;
435}
436
437inline const std::string& Maintenance::Element::tag() const
438{
439 return tag_;
440}
441
442inline std::string& Maintenance::Element::tag()
443{
444 return tag_;
445}
446
448{
449 return buffer_;
450}
451
453{
454 return buffer_;
455}
456
458 maintenanceActive(false),
459 maintenanceId(RandomI::random64())
460{
461 while (maintenanceId == 0ull)
462 {
464 }
465}
466
467inline bool Maintenance::isActive() const
468{
469 const ScopedLock scopedLock(maintenanceLock);
470 return maintenanceActive;
471}
472
473inline std::string Maintenance::name() const
474{
475 const ScopedLock scopedLock(maintenanceLock);
476 return maintenanceName;
477}
478
479inline uint64_t Maintenance::id() const
480{
481 const ScopedLock scopedLock(maintenanceLock);
482 return maintenanceId;
483}
484
485inline bool Maintenance::isEmpty() const
486{
487 const ScopedLock scopedLock(maintenanceLock);
488 return maintenanceElementQueue.empty();
489}
490
491inline size_t Maintenance::size() const
492{
493 const ScopedLock scopedLock(maintenanceLock);
494 return maintenanceElementQueue.size();
495}
496
497inline void Maintenance::setActive(const bool state)
498{
499 const ScopedLock scopedLock(maintenanceLock);
500 maintenanceActive = state;
501}
502
503inline void Maintenance::setName(const std::string& name)
504{
505 const ScopedLock scopedLock(maintenanceLock);
507}
508
509inline Maintenance::Buffer Maintenance::combine(const Buffer& firstBuffer, const Buffer& secondBuffer)
510{
511 Buffer result(firstBuffer.size() + secondBuffer.size());
512 memcpy(result.data(), firstBuffer.data(), firstBuffer.size());
513 memcpy(result.data() + firstBuffer.size(), secondBuffer.data(), secondBuffer.size());
514
515 return result;
516}
517
518inline void Maintenance::appendBuffer(Buffer& firstBuffer, const Buffer& secondBuffer)
519{
520 const size_t firstBufferSize = firstBuffer.size();
521
522 firstBuffer.resize(firstBuffer.size() + secondBuffer.size());
523 memcpy(firstBuffer.data() + firstBufferSize, secondBuffer.data(), secondBuffer.size());
524}
525
526}
527
528#endif // META_OCEAN_BASE_MAINTENANCE_H
This class implements a recursive lock object.
Definition Lock.h:31
This class is the base class for all maintenance connectors.
Definition Maintenance.h:49
static bool decodeData(const void *encodedBuffer, const size_t encodedBufferSize, std::string &name, uint64_t &id, std::string &tag, Buffer &buffer, Timestamp &timestamp)
Decodes a package buffer to maintenance data with corresponding information.
static void encodeData(const std::string &name, const uint64_t id, const std::string &tag, const Buffer &buffer, const Timestamp timestamp, const size_t reservedHeaderSize, Buffer &encodedBuffer)
Encodes a maintenance data to one combined package.
static bool place(const std::string &name, const uint64_t id, const std::string &tag, Buffer &&buffer, const Timestamp timestamp)
Explicitly places a maintenance data, information, message into the local maintenance manager.
Connector()
Creates a new connector.
Definition Maintenance.h:392
This class implements a maintenance data element.
Definition Maintenance.h:119
const std::string & tag() const
Returns the tag of the maintenance data of this element.
Definition Maintenance.h:437
std::string tag_
The tag of the data.
Definition Maintenance.h:233
uint64_t id() const
Return sthe id of the maintenance manager to which the maintenance data of this element has been sent...
Definition Maintenance.h:427
std::string name_
The name of the manager to which the data has been sent.
Definition Maintenance.h:224
Element(Element &&element)=default
Move constructor.
Element()=default
Creates a new empty element.
const Buffer & buffer() const
Returns the buffer of the maintenance data of this element.
Definition Maintenance.h:447
Buffer buffer_
The buffer of the data.
Definition Maintenance.h:236
Element & operator=(const Element &element)=default
Assign operator.
Element(const Element &element)=default
Copy constructor.
const std::string & name() const
Returns the name of the maintenance manager to which the maintenance of this element data has been se...
Definition Maintenance.h:417
Element & operator=(Element &&element)=default
Assign operator.
Timestamp timestamp() const
The timestamp of the maintenance data of this element.
Definition Maintenance.h:432
This class implements a maintenance manager.
Definition Maintenance.h:33
uint64_t maintenanceId
The random id of this manager.
Definition Maintenance.h:383
bool place(const std::string &name, const uint64_t id, const std::string &tag, Buffer &&buffer, const Timestamp timestamp)
Explicitly places a maintenance data, information, message into this manager.
Maintenance()
Creates a new maintenance manager object.
Definition Maintenance.h:457
bool isActive() const
Returns whether the maintenance manager is active or not.
Definition Maintenance.h:467
bool receive(std::string &name, uint64_t &id, std::string &tag, Buffer &buffer, Timestamp &timestamp)
Receives the oldest maintenance data from this manager and pops it from the manager.
std::queue< Element > ElementQueue
Definition of a vector holding maintenance data elements.
Definition Maintenance.h:242
bool send(const std::string &tag, Buffer &&buffer, const Timestamp timestamp=Timestamp(true))
Sends new maintenance data to this manager.
std::vector< uint8_t > Buffer
Definition of a vector holding bytes.
Definition Maintenance.h:41
bool isEmpty() const
Returns whether this maintenance manager is currently empty (does not hold any maintenance data,...
Definition Maintenance.h:485
std::string maintenanceName
The readable name of this manager.
Definition Maintenance.h:380
bool maintenanceActive
The activation statement of this manager.
Definition Maintenance.h:377
size_t size() const
Returns the number of maintenance data, information or messages which are currently stored in this ma...
Definition Maintenance.h:491
static Buffer combine(const Buffer &firstBuffer, const Buffer &secondBuffer)
Combines two buffers.
Definition Maintenance.h:509
void setName(const std::string &name)
Sets the name of this maintenance manager which should be a readable name of the application in which...
Definition Maintenance.h:503
uint64_t id() const
Returns a random id of this maintenance manager.
Definition Maintenance.h:479
bool send(const std::string &tag, const Buffer &buffer, const Timestamp timestamp=Timestamp(true))
Sends new maintenance data to this manager.
static void appendBuffer(Buffer &firstBuffer, const Buffer &secondBuffer)
Appends a second buffer to a first buffer.
Definition Maintenance.h:518
Lock maintenanceLock
The maintenance lock.
Definition Maintenance.h:389
bool send(const std::string &tag, const void *data, const size_t size, const Timestamp timestamp=Timestamp(true))
Sends new maintenance data to this manager.
void setActive(const bool state)
Activates or deactivates this maintenance manager.
Definition Maintenance.h:497
std::string name() const
Returns the name of this maintenance manager.
Definition Maintenance.h:473
ElementQueue maintenanceElementQueue
The maintenance element queue.
Definition Maintenance.h:386
This class provides base random functions and several random functions for integer data types.
Definition RandomI.h:29
static uint64_t random64()
Returns one random integer number with range [0x00000000 00000000, 0xFFFFFFFF FFFFFFFF].
This class implements a scoped lock object for recursive lock objects.
Definition Lock.h:147
This template class is the base class for all singleton objects.
Definition Singleton.h:71
This class implements a timestamp.
Definition Timestamp.h:64
The namespace covering the entire Ocean framework.
Definition Accessor.h:15