Ocean
MessageQueue.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 FACEBOOK_NETWORK_MESSAGE_QUEUE_H
9 #define FACEBOOK_NETWORK_MESSAGE_QUEUE_H
10 
11 #include "ocean/network/Network.h"
12 
13 #include "ocean/base/Lock.h"
14 
15 #include <map>
16 #include <queue>
17 
18 namespace Ocean
19 {
20 
21 namespace Network
22 {
23 
24 /**
25  * This class implements a message queue.
26  * @ingroup network
27  */
28 class OCEAN_NETWORK_EXPORT MessageQueue
29 {
30  public:
31 
32  /**
33  * Definition of a message id.
34  */
35  typedef unsigned int Id;
36 
37  /**
38  * Returns an invalid message id.
39  * @return Invalid message id
40  */
41  static constexpr Id invalidId();
42 
43  private:
44 
45  /**
46  * Definition of a message pair.
47  */
48  typedef std::pair<std::string, std::string> Message;
49 
50  /**
51  * Definition of a queue holding message pairs.
52  */
53  typedef std::queue<Message> Queue;
54 
55  /**
56  * Definition of a map mapping message ids to message queues.
57  */
58  typedef std::unordered_map<Id, Queue> MessageMap;
59 
60  public:
61 
62  /**
63  * Creates a new message queue.
64  */
65  MessageQueue() = default;
66 
67  /**
68  * Move constructor.
69  * @param messageQueue The message queue object to be moved
70  */
71  MessageQueue(MessageQueue&& messageQueue);
72 
73  /**
74  * Creates a new message queue.
75  * @param oldMessageAge The capacity of the queue, with range [1, infinity)
76  */
77  explicit MessageQueue(const unsigned int oldMessageAge);
78 
79  /**
80  * Destructs a message queue.
81  */
83 
84  /**
85  * Pushes a new message.
86  * Beware: To avoid a message overflow all messages older than the defined 'age' will be released.
87  * @param id Message id
88  * @param message The message to push
89  * @param value Optional message value to push
90  * @return True, if succeeded
91  */
92  bool push(const Id id, const std::string& message, const std::string& value);
93 
94  /**
95  * Gets the front message for a given message id.
96  * @param id Message id to get the front message for
97  * @param message Resulting message
98  * @param value Resulting optional value
99  * @return True, if succeeded
100  */
101  bool front(const Id id, std::string& message, std::string& value);
102 
103  /**
104  * Gets the front message for a given message id and waits a specified time if no message is available.
105  * @param id Message id to get the front message for
106  * @param timeout Time to wait in seconds
107  * @param message Resulting message
108  * @param value Resulting optional value
109  * @return True, if a message could be returned within the specified wait time
110  */
111  bool front(const Id id, const double timeout, std::string& message, std::string& value);
112 
113  /**
114  * Pops the front message with a specified message id from the queue.
115  * @param id Message id to pop the front message for
116  */
117  void pop(const Id id);
118 
119  /**
120  * Returns and pops the front message for a given message id and waits a specified time if no message is available.
121  * @param id Message id to get the front message for
122  * @param timeout Time to wait in seconds
123  * @return Resulting message, if any
124  */
125  std::string pop(const Id id, const double timeout);
126 
127  /**
128  * Returns and pops the front message for a given message id and waits a specified time if no message is available.
129  * @param id Message id to get the front message for
130  * @param timeout Time to wait in seconds
131  * @param message Resulting message
132  * @param value Resulting optional value
133  * @return True, if a message could be popped within the specified wait time
134  */
135  bool pop(const Id id, const double timeout, std::string& message, std::string& value);
136 
137  /**
138  * Clears the entire message queue.
139  */
140  void clear();
141 
142  /**
143  * Clears all messages with a specified message id.
144  * @param id Message id to clear all messages for
145  */
146  void clear(const Id id);
147 
148  /**
149  * Returns a unique message id within this message queue.
150  * @return Unique message id
151  */
153 
154  /**
155  * Returns the last unique message id again.
156  * Beware: Do no use this id directly, use it for control purpose only!
157  * @return Last unique id
158  */
160 
161  /**
162  * Move operator.
163  * @param messageQueue The message queue to be moved
164  * @return Reference to this object
165  */
167 
168  protected:
169 
170  /// Map holding all messages.
172 
173  /// Definition of an message age of old and therefore unused messages.
174  unsigned int oldMessageAge_ = 500u;
175 
176  /// Message id counter.
177  Id idCounter_ = invalidId();
178 
179  /// Map lock.
181 };
182 
184 {
185  return Id(0);
186 }
187 
188 }
189 
190 }
191 
192 #endif // FACEBOOK_NETWORK_MESSAGE_QUEUE_H
This class implements a recursive lock object.
Definition: Lock.h:31
This class implements a message queue.
Definition: MessageQueue.h:29
Lock lock_
Map lock.
Definition: MessageQueue.h:180
MessageQueue()=default
Creates a new message queue.
bool front(const Id id, std::string &message, std::string &value)
Gets the front message for a given message id.
void clear()
Clears the entire message queue.
bool push(const Id id, const std::string &message, const std::string &value)
Pushes a new message.
MessageMap messageMap_
Map holding all messages.
Definition: MessageQueue.h:171
std::string pop(const Id id, const double timeout)
Returns and pops the front message for a given message id and waits a specified time if no message is...
std::pair< std::string, std::string > Message
Definition of a message pair.
Definition: MessageQueue.h:48
void pop(const Id id)
Pops the front message with a specified message id from the queue.
std::unordered_map< Id, Queue > MessageMap
Definition of a map mapping message ids to message queues.
Definition: MessageQueue.h:58
Id uniqueId()
Returns a unique message id within this message queue.
~MessageQueue()
Destructs a message queue.
static constexpr Id invalidId()
Returns an invalid message id.
Definition: MessageQueue.h:183
MessageQueue(MessageQueue &&messageQueue)
Move constructor.
Id lastUniqueId()
Returns the last unique message id again.
MessageQueue(const unsigned int oldMessageAge)
Creates a new message queue.
bool pop(const Id id, const double timeout, std::string &message, std::string &value)
Returns and pops the front message for a given message id and waits a specified time if no message is...
std::queue< Message > Queue
Definition of a queue holding message pairs.
Definition: MessageQueue.h:53
void clear(const Id id)
Clears all messages with a specified message id.
MessageQueue & operator=(MessageQueue &&messageQueue)
Move operator.
bool front(const Id id, const double timeout, std::string &message, std::string &value)
Gets the front message for a given message id and waits a specified time if no message is available.
unsigned int Id
Definition of a message id.
Definition: MessageQueue.h:35
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15