lintel 0.2.0
Loading...
Searching...
No Matches
MessageQueue.h
Go to the documentation of this file.
1#ifndef LINTEL_MESSAGEQUEUE_H
2#define LINTEL_MESSAGEQUEUE_H
3
6
7#include <atomic>
8#include <cstddef>
9#include <cstdint>
10#include <optional>
11#include <string>
12
13#include <utility>
14
15namespace lintel {
24template<typename T> class MessageQueue
25{
26private:
27 // variables
28 std::string m_processName;
29 std::string m_name;
30 static constexpr std::size_t m_msgSize = sizeof(T);
31 int32_t m_msgCount;
32 MessageQueueCore m_messageQueue;
33 std::shared_ptr<ProcessName> m_realProcessName;
34 std::atomic<std::uint64_t> m_droppedMessages{ 0 };
35
36public:
40 static void removeOf(const std::string &messageQueueName) { MessageQueueCore::removeOf(messageQueueName); }
41
45 [[nodiscard]] bool isOwner() const { return m_processName == m_realProcessName->getProcessName(); }
46
48 [[nodiscard]] const std::string &getProcessName() const { return m_processName; }
49
51 [[nodiscard]] const std::string &getName() const { return m_name; }
52
54 [[nodiscard]] int32_t getMsgCount() const { return m_msgCount; }
55
57 [[nodiscard]] int32_t numberMessagesOf() const { return m_messageQueue.numberMessagesOf(); }
58
65 MessageQueue(std::string processName,
66 std::string name,
68 std::shared_ptr<ProcessName> realProcessName)
69 : m_processName(std::move(processName)), m_name(std::move(name)), m_msgCount(msgCount),
70 m_messageQueue(m_name, msgCount, m_msgSize), m_realProcessName(std::move(realProcessName))
71 {}
72
77 void sendOf(const T &message) { m_messageQueue.sendOf(&message, m_msgSize); }
78
84 bool trySendOf(const T &message) { return m_messageQueue.trySendOf(&message, m_msgSize); }
85
93 bool sendOfWithTimeout(const T &message, const std::chrono::milliseconds &timeout)
94 {
95 const bool success = m_messageQueue.sendOfWithTimeout(&message, m_msgSize, timeout);
96 if (!success) { m_droppedMessages.fetch_add(1u, std::memory_order_relaxed); }
97 return success;
98 }
99
104 [[nodiscard]] std::uint64_t droppedMessagesOf() const { return m_droppedMessages.load(std::memory_order_relaxed); }
105
112 {
113 T temp{};
114 m_messageQueue.receiveOf(&temp, m_msgSize);
115 return temp;
116 }
117
123 std::optional<T> tryReceiveOf()
124 {
125 T temp{};
126 if (!m_messageQueue.tryReceiveOf(&temp, m_msgSize)) { return std::nullopt; }
127 return std::make_optional(temp);
128 }
129
131 bool operator<(const MessageQueue &rhs) const
132 {
133 if (m_processName < rhs.m_processName) return true;
134 if (rhs.m_processName < m_processName) return false;
135 return m_name < rhs.m_name;
136 }
137
139 bool operator>(const MessageQueue &rhs) const { return rhs < *this; }
140
142 bool operator<=(const MessageQueue &rhs) const { return !(rhs < *this); }
143
145 bool operator>=(const MessageQueue &rhs) const { return !(*this < rhs); }
146
148 bool operator==(const MessageQueue &rhs) const { return m_processName == rhs.m_processName && m_name == rhs.m_name; }
149
151 bool operator!=(const MessageQueue &rhs) const { return !(*this == rhs); }
152};
153
154}// namespace lintel
155#endif// LINTEL_MESSAGEQUEUE_H
Definition MessageQueueCore.h:18
bool trySendOf(const void *message, std::size_t size)
int32_t numberMessagesOf() const
void receiveOf(void *message, std::size_t size)
bool tryReceiveOf(void *message, std::size_t size)
void sendOf(const void *message, std::size_t size)
static void removeOf(const std::string &messageQueueName)
bool sendOfWithTimeout(const void *message, std::size_t size, const std::chrono::milliseconds &timeout)
Definition MessageQueue.h:25
std::uint64_t droppedMessagesOf() const
Definition MessageQueue.h:104
bool isOwner() const
Definition MessageQueue.h:45
bool operator!=(const MessageQueue &rhs) const
Definition MessageQueue.h:151
int32_t numberMessagesOf() const
Definition MessageQueue.h:57
bool operator>(const MessageQueue &rhs) const
Definition MessageQueue.h:139
void sendOf(const T &message)
Definition MessageQueue.h:77
bool operator>=(const MessageQueue &rhs) const
Definition MessageQueue.h:145
int32_t getMsgCount() const
Definition MessageQueue.h:54
T receiveOf()
Definition MessageQueue.h:111
const std::string & getName() const
Definition MessageQueue.h:51
MessageQueue(std::string processName, std::string name, int32_t msgCount, std::shared_ptr< ProcessName > realProcessName)
Definition MessageQueue.h:65
static void removeOf(const std::string &messageQueueName)
Definition MessageQueue.h:40
bool trySendOf(const T &message)
Definition MessageQueue.h:84
bool operator==(const MessageQueue &rhs) const
Definition MessageQueue.h:148
bool sendOfWithTimeout(const T &message, const std::chrono::milliseconds &timeout)
Definition MessageQueue.h:93
bool operator<=(const MessageQueue &rhs) const
Definition MessageQueue.h:142
bool operator<(const MessageQueue &rhs) const
Definition MessageQueue.h:131
std::optional< T > tryReceiveOf()
Definition MessageQueue.h:123
const std::string & getProcessName() const
Definition MessageQueue.h:48
Definition BaseFeature.h:6
constexpr std::string_view type_name()
Definition TypeName.h:9