feat: register tasks outside of main

This commit is contained in:
2025-08-22 07:30:47 +02:00
parent 5ccd3ec956
commit c0efba8667
4 changed files with 130 additions and 63 deletions

View File

@@ -1,6 +1,6 @@
#include "ClusterManager.h"
ClusterManager::ClusterManager(NodeContext& ctx) : ctx(ctx) {
ClusterManager::ClusterManager(NodeContext& ctx, TaskManager& taskMgr) : ctx(ctx), taskManager(taskMgr) {
// Register callback for node_discovered event
ctx.on("node_discovered", [this](void* data) {
NodeInfo* node = static_cast<NodeInfo*>(data);
@@ -8,6 +8,27 @@ ClusterManager::ClusterManager(NodeContext& ctx) : ctx(ctx) {
});
}
void ClusterManager::registerTasks() {
// Register all cluster-related tasks using std::bind
taskManager.registerTask("discovery_send", ctx.config.discovery_interval_ms,
std::bind(&ClusterManager::sendDiscovery, this));
taskManager.registerTask("discovery_listen", ctx.config.discovery_interval_ms / 10,
std::bind(&ClusterManager::listenForDiscovery, this));
taskManager.registerTask("status_update", ctx.config.status_update_interval_ms,
std::bind([](ClusterManager* cm) {
cm->updateAllNodeStatuses();
cm->removeDeadNodes();
}, this));
taskManager.registerTask("print_members", ctx.config.print_interval_ms,
std::bind(&ClusterManager::printMemberList, this));
taskManager.registerTask("heartbeat", ctx.config.heartbeat_interval_ms,
std::bind(&ClusterManager::heartbeatTaskCallback, this));
taskManager.registerTask("update_members_info", ctx.config.member_info_update_interval_ms,
std::bind(&ClusterManager::updateAllMembersInfoTaskCallback, this));
Serial.println("[ClusterManager] Registered all cluster tasks");
}
void ClusterManager::sendDiscovery() {
//Serial.println("[Cluster] Sending discovery packet...");
ctx.udp->beginPacket("255.255.255.255", ctx.config.udp_port);