ifw-ccf 6.0.0
 
Loading...
Searching...
No Matches
messageBus.hpp
Go to the documentation of this file.
1
6
7#pragma once
8
9#include <string>
10#include <queue>
11#include <iostream>
12
13// TODO: Use C++20 std::counting_semaphore, when available.
14//#include <boost/interprocess/sync/interprocess_semaphore.hpp>
15
16#include <ifw/core/utils/base/base.hpp>
17#include <ifw/core/utils/time/time.hpp>
18
21
22
23// TODO: Dummy implementation. Should use C++20 counting semaphore.
25 public:
26
28 m_count = 0;
29 };
30
32 // Release all blocked on the semaphore?
33 };
34
35 void release() {
36 m_count_mutex.lock(); {
37 m_count++;
38 } m_count_mutex.unlock();
39 };
40
41 void acquire() {
42 m_acquire_mutex.lock(); {
43
44 while (m_count == 0) {
45 ifw::core::utils::time::Sleep(0.0001);
46 }
47
48 m_count_mutex.lock(); {
49 m_count--;
50 } m_count_mutex.unlock();
51
52 } m_acquire_mutex.unlock();
53 };
54
55 bool try_acquire_for(const double time) {
56 m_acquire_mutex.lock(); {
57
58 double start_time = ifw::core::utils::time::Time();
59 while ((m_count == 0) && ((ifw::core::utils::time::Time() - start_time) < time)) {
60 ifw::core::utils::time::Sleep(0.0001);
61 }
62 if ((ifw::core::utils::time::Time() - start_time) >= time) {
63 m_acquire_mutex.unlock();
64 return false;
65 }
66
67 m_count_mutex.lock(); {
68 m_count--;
69 } m_count_mutex.unlock();
70
71 } m_acquire_mutex.unlock();
72
73 return true;
74 };
75
76 private:
77 std::mutex m_acquire_mutex;
78 std::mutex m_count_mutex;
79 int32_t m_count;
80};
81
82
83namespace ifw::ccf::mptk {
84
85 void SplitDblTime(const double time,
86 int64_t& secs,
87 int64_t& nano_secs);
88
92 class MessageBus {
93 public:
94
95 MessageBus();
96
98
100 void Reset();
101
103 MessageBus& RegisterThread(const std::string& thread_name);
104
106 bool ThreadRegistered(const std::string& thread_name) const;
107
109 void SendMessage(const Message& message);
110
119 bool ReceiveMessage(const std::string& receiver_thread_name,
120 const double timeout,
121 bool& timed_out,
122 Message& message);
123
125 void SendResponse(Response& response);
126
129 bool ReceiveResponse(const std::string& msg_sender_thread_name,
130 const double timeout,
131 bool& timed_out,
132 Response& response);
133
136 bool ReceiveResponseByMsgId(const std::string& msg_id,
137 const double timeout,
138 bool& timed_out,
139 Response& response);
140
142 std::string ToString() const;
143
144 private:
145 std::string m_id; // Message Bus ID.
146
147 // Message Receiver Thread ID to message objects list.
148 std::map<std::string, std::list<Message>> m_message_registry;
149 // Message Receiver Registry counting semaphores.
150 // boost: std::map<std::string, boost::interprocess::interprocess_semaphore*> m_message_registry_sem;
151 std::map<std::string, TmpCountingSem*> m_message_registry_sem;
152
153 // Response receiver (= sender of msg) Thread ID to response objects list.
154 std::map<std::string, std::list<Response>> m_response_registry;
155 // Response Receiver Registry counting semaphores.
156 // boost: std::map<std::string, boost::interprocess::interprocess_semaphore*> m_response_registry_sem;
157 std::map<std::string, TmpCountingSem*> m_response_registry_sem;
158
159 // Message ID to response objects list.
160 std::map<std::string, Response*> m_response_msg_id_registry;
161
162 std::vector<std::string> m_threads;
163 };
164
165}
TmpCountingSem()
Definition messageBus.hpp:27
void release()
Definition messageBus.hpp:35
bool try_acquire_for(const double time)
Definition messageBus.hpp:55
~TmpCountingSem()
Definition messageBus.hpp:31
void acquire()
Definition messageBus.hpp:41
std::string ToString() const
Generate ASCII output providing a status of the object (to the extend possible).
void SendMessage(const Message &message)
Send a message on the Message Bus.
Definition messageBus.cpp:83
MessageBus()
Definition messageBus.cpp:28
bool ThreadRegistered(const std::string &thread_name) const
Check if thread has been registered as participant in the Message Bus instance.
Definition messageBus.cpp:57
~MessageBus()
Definition messageBus.cpp:36
void Reset()
Reset the internal message queues and other objects.
Definition messageBus.cpp:46
MessageBus & RegisterThread(const std::string &thread_name)
Register thread which will send/receive messages on the Message Bus.
Definition messageBus.cpp:65
bool ReceiveMessage(const std::string &receiver_thread_name, const double timeout, bool &timed_out, Message &message)
Check for a message for this thread. Returns true if message available.
Definition messageBus.cpp:101
bool ReceiveResponseByMsgId(const std::string &msg_id, const double timeout, bool &timed_out, Response &response)
Check for a message for this thread. Returns true if message available.
Definition messageBus.cpp:213
void SendResponse(Response &response)
Send a response to a message received on the Message Bus.
Definition messageBus.cpp:150
bool ReceiveResponse(const std::string &msg_sender_thread_name, const double timeout, bool &timed_out, Response &response)
Check for a message for this thread. Returns true if message available.
Definition messageBus.cpp:163
IFW CTD Multiprocessing Toolkit Message class.
Definition message.hpp:25
IFW CTD Multiprocessing Toolkit Response class.
Definition response.hpp:21
Definition logger.hpp:13
void SplitDblTime(const double time, int64_t &secs, int64_t &nano_secs)
Definition messageBus.cpp:21