CommRaT 2.0.0
C++20 Real-Time Messaging Framework
Loading...
Searching...
No Matches
work_loop_handler.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <iostream>
6#include <type_traits>
7
8namespace commrat {
9
23template<typename ModuleType>
25protected:
37 void work_loop() {
38 auto& module = static_cast<ModuleType&>(*this);
39
40 // Just print a simple message - the address details aren't critical for the log
41 std::cout << "[" << module.config_.name << "] work_loop started on WORK mailbox\n" << std::flush;
42
43 while (module.running_) {
44 // Use receive_any with visitor pattern on work_mailbox
45 // BLOCKING receive - waits indefinitely for subscription messages
46 std::cout << "[" << module.config_.name << "] work_loop: waiting for message...\n" << std::flush;
47
48 auto visitor = [&module](auto&& tims_msg) {
49 // tims_msg is TimsMessage<PayloadT>, extract payload
50 auto& msg = tims_msg.payload;
51 using MsgType = std::decay_t<decltype(msg)>;
52
53 // Handle subscription protocol
54 if constexpr (std::is_same_v<MsgType, SubscribeRequestType>) {
55 std::cout << "[" << module.config_.name << "] Handling SubscribeRequest\n";
56 module.handle_subscribe_request(msg);
57 } else if constexpr (std::is_same_v<MsgType, SubscribeReplyType>) {
58 std::cout << "[" << module.config_.name << "] Handling SubscribeReply\n";
59 module.handle_subscribe_reply(msg);
60 } else if constexpr (std::is_same_v<MsgType, UnsubscribeRequestType>) {
61 std::cout << "[" << module.config_.name << "] Handling UnsubscribeRequest\n";
62 module.handle_unsubscribe_request(msg);
63 }
64 };
65
66 // BLOCKING receive on work mailbox (no timeout)
67 module.work_mailbox().receive_any(visitor);
68 }
69
70 std::cout << "[" << module.config_.name << "] work_loop ended\n";
71 }
72};
73
74} // namespace commrat
Phase 7: Work Loop Handler CRTP Mixin.
void work_loop()
Main work loop - handles subscription protocol messages.
CommRaT - Modern C++ Real-Time Communication Framework.