CommRaT 2.0.0
C++20 Real-Time Messaging Framework
Loading...
Searching...
No Matches
command_dispatcher.hpp
Go to the documentation of this file.
1
9#pragma once
10
11#include <iostream>
12#include <string>
13
14namespace commrat {
15
25template<typename ModuleType, typename... CommandTypes>
27protected:
34 void command_loop() {
35 auto& module = static_cast<ModuleType&>(*this);
36 std::cout << "[" << module.config_.name << "] command_loop started\n";
37
38 while (module.running_) {
39 // Use receive_any with visitor pattern on cmd_mailbox
40 // BLOCKING receive - waits indefinitely for user commands
41 auto visitor = [&module](auto&& tims_msg) {
42 // tims_msg is TimsMessage<PayloadT>, extract payload
43 auto& msg = tims_msg.payload;
44 using MsgType = std::decay_t<decltype(msg)>;
45
46 std::cout << "[" << module.config_.name << "] Received command in command_loop\n";
47
48 // Handle user command types only
49 module.handle_user_command(msg);
50 };
51
52 // BLOCKING receive on command mailbox (no timeout)
53 module.cmd_mailbox().receive_any(visitor);
54 }
55
56 std::cout << "[" << module.config_.name << "] command_loop ended\n";
57 }
58
65 template<typename CmdT>
66 void handle_user_command(const CmdT& cmd) {
67 auto& module = static_cast<ModuleType&>(*this);
68
69 // Check if this is one of our declared CommandTypes
70 if constexpr ((std::is_same_v<CmdT, CommandTypes> || ...)) {
71 module.on_command(cmd);
72 }
73 // Otherwise ignore (not in our command list)
74 }
75
88 template<typename CmdT>
89 void on_command(const CmdT& cmd) {
90 // Default: no-op - override in derived classes for specific CommandTypes
91 (void)cmd; // Suppress unused parameter warning
92 }
93};
94
95} // namespace commrat
Command dispatcher mixin.
void handle_user_command(const CmdT &cmd)
Dispatch user command to on_command handler.
void command_loop()
Command loop - receives and dispatches user commands.
void on_command(const CmdT &cmd)
Handle a specific command type (override in derived class)
CommRaT - Modern C++ Real-Time Communication Framework.