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 typedef std::vector<unsigned char> Buffer;
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 unsigned long long id, const std::string& tag, Buffer&& buffer, const Timestamp timestamp);
69
70 /**
71 * Encodes a maintenance data to one combined package.
72 * @param name The name of the maintenance manager providing the data
73 * @param id The id of the maintenance manager providing the data
74 * @param tag The tag of the maintenance data
75 * @param buffer The maintenance data as buffer
76 * @param timestamp The timestamp of the maintenance data
77 * @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
78 * @param encodedBuffer The resulting encoded package
79 */
80 static void encodeData(const std::string& name, const unsigned long long id, const std::string& tag, const Buffer& buffer, const Timestamp timestamp, const size_t reservedHeaderSize, Buffer& encodedBuffer);
81
82 /**
83 * Decodes a package buffer to maintenance data with corresponding information.
84 * @param encodedBuffer The encoded network package buffer
85 * @param encodedBufferSize The size of the encoded buffer in bytes
86 * @param name The resulting name of the maintenance manager to which the data has been sent originally
87 * @param id The result id of the maintenance manager to which the data has been sent originally
88 * @param tag The tag of the maintenance data
89 * @param buffer The buffer of the maintenance data
90 * @param timestamp The timestamp of the maintenance data
91 * @return True, if succeeded
92 */
93 static bool decodeData(const void* encodedBuffer, const size_t encodedBufferSize, std::string& name, unsigned long long& id, std::string& tag, Buffer& buffer, Timestamp& timestamp);
94 };
95
96 protected:
97
98 /**
99 * This class implements a maintenance data element.
100 */
102 {
103 public:
104
105 /**
106 * Creates a new empty element.
107 */
108 inline Element();
109
110 /**
111 * Copy constructor.
112 * @param element The element to be copied
113 */
114 inline Element(const Element& element);
115
116 /**
117 * Move constructor.
118 * @param element The element to be moved
119 */
120 inline Element(Element&& element);
121
122 /**
123 * Creates a new maintenance element.
124 * @param name The name of the maintenance manager to which the data has been sent
125 * @param id The id of the maintenance manager to which the data has been set
126 * @param timestamp The timestamp of the maintenance data
127 * @param tag The tag of the maintenance data
128 * @param buffer The maintenance data as buffer
129 */
130 inline Element(const std::string& name, const unsigned long long id, const Timestamp timestamp, const std::string& tag, const Buffer& buffer);
131
132 /**
133 * Creates a new maintenance element.
134 * @param name The name of the maintenance manager to which the data has been sent
135 * @param id The id of the maintenance manager to which the data has been set
136 * @param timestamp The timestamp of the maintenance data
137 * @param tag The tag of the maintenance data
138 * @param buffer The maintenance data as buffer, will be moved
139 */
140 inline Element(const std::string& name, const unsigned long long id, const Timestamp timestamp, const std::string& tag, Buffer&& buffer);
141
142 /**
143 * Returns the name of the maintenance manager to which the maintenance of this element data has been sent.
144 * @return The readable name of the maintenance manager
145 */
146 inline const std::string& name() const;
147
148 /**
149 * Returns the name of the maintenance manager to which the maintenance of this element data has been sent.
150 * @return The readable name of the maintenance manager
151 */
152 inline std::string& name();
153
154 /**
155 * Return sthe id of the maintenance manager to which the maintenance data of this element has been sent.
156 * @return The id of the maintenance manager
157 */
158 inline unsigned long long id() const;
159
160 /**
161 * The timestamp of the maintenance data of this element.
162 * @return The timestamp of the maintenance data
163 */
164 inline Timestamp timestamp() const;
165
166 /**
167 * Returns the tag of the maintenance data of this element.
168 * @return The tag of the maintenance data
169 */
170 inline const std::string& tag() const;
171
172 /**
173 * Returns the tag of the maintenance data of this element.
174 * @return The tag of the maintenance data
175 */
176 inline std::string& tag();
177
178 /**
179 * Returns the buffer of the maintenance data of this element.
180 * @return The buffer of the maintenance data
181 */
182 inline const Buffer& buffer() const;
183
184 /**
185 * Returns the buffer of the maintenance data of this element.
186 * @return The buffer of the maintenance data
187 */
188 inline Buffer& buffer();
189
190 /**
191 * Assign operator.
192 * @param element The second element to be copied
193 * @return Reference to this element
194 */
195 inline Element& operator=(const Element& element);
196
197 /**
198 * Assign operator.
199 * @param element The second element to be moved
200 * @return Reference to this element
201 */
202 inline Element& operator=(Element&& element);
203
204 protected:
205
206 /// The name of the manager to which the data has been sent.
207 std::string elementName;
208
209 /// The id of the manager to which the data has been sent.
210 unsigned long long elementId;
211
212 /// The timestamp of the data.
214
215 /// The tag of the data.
216 std::string elementTag;
217
218 /// The buffer of the data.
220 };
221
222 /**
223 * Definition of a vector holding maintenance data elements.
224 */
225 typedef std::queue<Element> ElementQueue;
226
227 public:
228
229 /**
230 * Returns whether the maintenance manager is active or not.
231 * Check whether the manager is active before preparing information which will be forwarded to this manager.<br>
232 * @return True, if so
233 * @see setActive().
234 */
235 inline bool isActive() const;
236
237 /**
238 * Returns the name of this maintenance manager.
239 * @return The readable name of this manager
240 * @see setName().
241 */
242 inline std::string name() const;
243
244 /**
245 * Returns a random id of this maintenance manager.
246 * 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).
247 * @return The random id of this manager
248 */
249 inline unsigned long long id() const;
250
251 /**
252 * Returns whether this maintenance manager is currently empty (does not hold any maintenance data, information or messages).
253 * @return True, if so
254 */
255 inline bool isEmpty() const;
256
257 /**
258 * Returns the number of maintenance data, information or messages which are currently stored in this manager.
259 * @return The number of maintenance messages
260 */
261 inline size_t size() const;
262
263 /**
264 * Activates or deactivates this maintenance manager.
265 * By default this manager is deactivates and thus will not accept any data, information or messages.<br>
266 * @param state True, to activate this manager; False; to deactivate this manager
267 * @see active().
268 */
269 inline void setActive(const bool state);
270
271 /**
272 * Sets the name of this maintenance manager which should be a readable name of the application in which this manager is used.
273 * The name should be set once at application start.<br>
274 * @param name The readable name to set
275 * @see name().
276 */
277 inline void setName(const std::string& name);
278
279 /**
280 * Sends new maintenance data to this manager.
281 * Check whether this manager is active before.<br>
282 * @param tag A tag specifying purpose of the provided data
283 * @param data The data to be sent
284 * @param size The size of the data to be sent, in bytes
285 * @param timestamp The timestamp of the maintenance data
286 * @return True, if the data could be sent
287 */
288 bool send(const std::string& tag, const void* data, const size_t size, const Timestamp timestamp = Timestamp(true));
289
290 /**
291 * Sends new maintenance data to this manager.
292 * Check whether this manager is active before.<br>
293 * @param tag A tag specifying purpose of the provided data
294 * @param buffer The data buffer to be sent
295 * @param timestamp The timestamp of the maintenance data
296 * @return True, if the data could be sent
297 */
298 bool send(const std::string& tag, const Buffer& buffer, const Timestamp timestamp = Timestamp(true));
299
300 /**
301 * Sends new maintenance data to this manager.
302 * Check whether this manager is active before.<br>
303 * @param tag A tag specifying purpose of the provided data
304 * @param buffer The data buffer to be sent, which will be moved
305 * @param timestamp The timestamp of the maintenance data
306 * @return True, if the data could be sent
307 */
308 bool send(const std::string& tag, Buffer&& buffer, const Timestamp timestamp = Timestamp(true));
309
310 /**
311 * Receives the oldest maintenance data from this manager and pops it from the manager.
312 * Maintenance data can be received even if the manager is not active.<br>
313 * @param name The name of the maintenance manager to which the data has been sent
314 * @param id The id of the maintenance manager to which the data has been sent
315 * @param tag The tag of the maintenance data
316 * @param buffer The maintenance data as buffer
317 * @param timestamp The timestamp of the maintenance data
318 * @return True, this manager had data which has been received
319 */
320 bool receive(std::string& name, unsigned long long& id, std::string& tag, Buffer& buffer, Timestamp& timestamp);
321
322 /**
323 * Combines two buffers.
324 * @param firstBuffer The first buffer
325 * @param secondBuffer The second buffer
326 * @return The resulting combined buffer
327 */
328 static inline Buffer combine(const Buffer& firstBuffer, const Buffer& secondBuffer);
329
330 /**
331 * Appends a second buffer to a first buffer.
332 * @param firstBuffer The first buffer to which the second buffer will be appended
333 * @param secondBuffer The second buffer
334 */
335 static inline void appendBuffer(Buffer& firstBuffer, const Buffer& secondBuffer);
336
337 protected:
338
339 /**
340 * Creates a new maintenance manager object.
341 */
342 inline Maintenance();
343
344 /**
345 * Explicitly places a maintenance data, information, message into this manager.
346 * Explicit maintenance data can be set even if the manager is not active.<br>
347 * @param name The name of the maintenance manager belonging to the data which will be placed
348 * @param id The id of the maintenance manager belonging to the data which will be placed
349 * @param tag The tag of the maintenance data
350 * @param buffer The buffer of the maintenance data, which will be moved
351 * @param timestamp The timestamp of the maintenance data
352 * @return True, if succeeded
353 * @see Connector::place().
354 */
355 bool place(const std::string& name, const unsigned long long id, const std::string& tag, Buffer&& buffer, const Timestamp timestamp);
356
357 protected:
358
359 /// The activation statement of this manager.
361
362 /// The readable name of this manager.
363 std::string maintenanceName;
364
365 /// The random id of this manager.
366 unsigned long long maintenanceId;
367
368 /// The maintenance element queue.
370
371 /// The maintenance lock.
373};
374
376{
377 // nothing to do here
378}
379
381 elementId(0ull),
382 elementTimestamp(false)
383{
384 // nothing to do here
385}
386
388 elementName(element.elementName),
389 elementId(element.elementId),
390 elementTimestamp(element.elementTimestamp),
391 elementTag(element.elementTag),
392 elementBuffer(element.elementBuffer)
393{
394 // nothing to do here
395}
396
398 elementName(std::move(element.elementName)),
399 elementId(element.elementId),
400 elementTimestamp(element.elementTimestamp),
401 elementTag(std::move(element.elementTag)),
402 elementBuffer(std::move(element.elementBuffer))
403{
404 element.elementId = 0ull;
405 element.elementTimestamp.toInvalid();
406}
407
408inline Maintenance::Element::Element(const std::string& name, const unsigned long long id, const Timestamp timestamp, const std::string& tag, const Buffer& buffer) :
409 elementName(name),
410 elementId(id),
411 elementTimestamp(timestamp),
412 elementTag(tag),
413 elementBuffer(buffer)
414{
415 // nothing to do here
416}
417
418inline Maintenance::Element::Element(const std::string& name, const unsigned long long id, const Timestamp timestamp, const std::string& tag, Buffer&& buffer) :
419 elementName(name),
420 elementId(id),
421 elementTimestamp(timestamp),
422 elementTag(tag),
423 elementBuffer(std::move(buffer))
424{
425 // nothing to do here
426}
427
428inline const std::string& Maintenance::Element::name() const
429{
430 return elementName;
431}
432
433inline std::string& Maintenance::Element::name()
434{
435 return elementName;
436}
437
438inline unsigned long long Maintenance::Element::id() const
439{
440 return elementId;
441}
442
444{
445 return elementTimestamp;
446}
447
448inline const std::string& Maintenance::Element::tag() const
449{
450 return elementTag;
451}
452
453inline std::string& Maintenance::Element::tag()
454{
455 return elementTag;
456}
457
459{
460 return elementBuffer;
461}
462
464{
465 return elementBuffer;
466}
467
469{
470 elementName = element.elementName;
471 elementId = element.elementId;
472 elementTimestamp = element.elementTimestamp;
473 elementTag = element.elementTag;
474 elementBuffer = element.elementBuffer;
475
476 return *this;
477}
478
480{
481 if (this != &element)
482 {
483 elementName = std::move(element.elementName);
484 elementId = element.elementId;
485 elementTimestamp = element.elementTimestamp;
486 elementTag = std::move(element.elementTag);
487 elementBuffer = std::move(element.elementBuffer);
488
489 element.elementId = 0ull;
490 element.elementTimestamp.toInvalid();
491 }
492
493 return *this;
494}
495
497 maintenanceActive(false),
498 maintenanceId(RandomI::random64())
499{
500 while (maintenanceId == 0ull)
502}
503
504inline bool Maintenance::isActive() const
505{
506 const ScopedLock scopedLock(maintenanceLock);
507 return maintenanceActive;
508}
509
510inline std::string Maintenance::name() const
511{
512 const ScopedLock scopedLock(maintenanceLock);
513 return maintenanceName;
514}
515
516inline unsigned long long Maintenance::id() const
517{
518 const ScopedLock scopedLock(maintenanceLock);
519 return maintenanceId;
520}
521
522inline bool Maintenance::isEmpty() const
523{
524 const ScopedLock scopedLock(maintenanceLock);
525 return maintenanceElementQueue.empty();
526}
527
528inline size_t Maintenance::size() const
529{
530 const ScopedLock scopedLock(maintenanceLock);
531 return maintenanceElementQueue.size();
532}
533
534inline void Maintenance::setActive(const bool state)
535{
536 const ScopedLock scopedLock(maintenanceLock);
537 maintenanceActive = state;
538}
539
540inline void Maintenance::setName(const std::string& name)
541{
542 const ScopedLock scopedLock(maintenanceLock);
544}
545
546inline Maintenance::Buffer Maintenance::combine(const Buffer& firstBuffer, const Buffer& secondBuffer)
547{
548 Buffer result(firstBuffer.size() + secondBuffer.size());
549 memcpy(result.data(), firstBuffer.data(), firstBuffer.size());
550 memcpy(result.data() + firstBuffer.size(), secondBuffer.data(), secondBuffer.size());
551
552 return result;
553}
554
555inline void Maintenance::appendBuffer(Buffer& firstBuffer, const Buffer& secondBuffer)
556{
557 const size_t firstBufferSize = firstBuffer.size();
558
559 firstBuffer.resize(firstBuffer.size() + secondBuffer.size());
560 memcpy(firstBuffer.data() + firstBufferSize, secondBuffer.data(), secondBuffer.size());
561}
562
563}
564
565#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 void encodeData(const std::string &name, const unsigned long long 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 unsigned long long id, const std::string &tag, Buffer &&buffer, const Timestamp timestamp)
Explicitly places a maintenance data, information, message into the local maintenance manager.
static bool decodeData(const void *encodedBuffer, const size_t encodedBufferSize, std::string &name, unsigned long long &id, std::string &tag, Buffer &buffer, Timestamp &timestamp)
Decodes a package buffer to maintenance data with corresponding information.
Connector()
Creates a new connector.
Definition Maintenance.h:375
This class implements a maintenance data element.
Definition Maintenance.h:102
const std::string & tag() const
Returns the tag of the maintenance data of this element.
Definition Maintenance.h:448
Buffer elementBuffer
The buffer of the data.
Definition Maintenance.h:219
Element & operator=(const Element &element)
Assign operator.
Definition Maintenance.h:468
std::string elementName
The name of the manager to which the data has been sent.
Definition Maintenance.h:207
const Buffer & buffer() const
Returns the buffer of the maintenance data of this element.
Definition Maintenance.h:458
unsigned long long elementId
The id of the manager to which the data has been sent.
Definition Maintenance.h:210
Timestamp elementTimestamp
The timestamp of the data.
Definition Maintenance.h:213
unsigned long long id() const
Return sthe id of the maintenance manager to which the maintenance data of this element has been sent...
Definition Maintenance.h:438
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:428
Timestamp timestamp() const
The timestamp of the maintenance data of this element.
Definition Maintenance.h:443
Element()
Creates a new empty element.
Definition Maintenance.h:380
std::string elementTag
The tag of the data.
Definition Maintenance.h:216
This class implements a maintenance manager.
Definition Maintenance.h:33
Maintenance()
Creates a new maintenance manager object.
Definition Maintenance.h:496
bool isActive() const
Returns whether the maintenance manager is active or not.
Definition Maintenance.h:504
unsigned long long id() const
Returns a random id of this maintenance manager.
Definition Maintenance.h:516
bool send(const std::string &tag, Buffer &&buffer, const Timestamp timestamp=Timestamp(true))
Sends new maintenance data to this manager.
unsigned long long maintenanceId
The random id of this manager.
Definition Maintenance.h:366
std::queue< Element > ElementQueue
Definition of a vector holding maintenance data elements.
Definition Maintenance.h:225
bool isEmpty() const
Returns whether this maintenance manager is currently empty (does not hold any maintenance data,...
Definition Maintenance.h:522
std::string maintenanceName
The readable name of this manager.
Definition Maintenance.h:363
bool maintenanceActive
The activation statement of this manager.
Definition Maintenance.h:360
bool place(const std::string &name, const unsigned long long id, const std::string &tag, Buffer &&buffer, const Timestamp timestamp)
Explicitly places a maintenance data, information, message into this manager.
size_t size() const
Returns the number of maintenance data, information or messages which are currently stored in this ma...
Definition Maintenance.h:528
static Buffer combine(const Buffer &firstBuffer, const Buffer &secondBuffer)
Combines two buffers.
Definition Maintenance.h:546
std::vector< unsigned char > Buffer
Definition of a vector holding bytes.
Definition Maintenance.h:41
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:540
bool receive(std::string &name, unsigned long long &id, std::string &tag, Buffer &buffer, Timestamp &timestamp)
Receives the oldest maintenance data from this manager and pops it from the manager.
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:555
Lock maintenanceLock
The maintenance lock.
Definition Maintenance.h:372
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:534
std::string name() const
Returns the name of this maintenance manager.
Definition Maintenance.h:510
ElementQueue maintenanceElementQueue
The maintenance element queue.
Definition Maintenance.h:369
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:135
This template class is the base class for all singleton objects.
Definition Singleton.h:71
This class implements a timestamp.
Definition Timestamp.h:36
The namespace covering the entire Ocean framework.
Definition Accessor.h:15