50 lines
1.6 KiB
C++
50 lines
1.6 KiB
C++
#pragma once
|
|
#include "spore/core/NodeContext.h"
|
|
#include "spore/types/NodeInfo.h"
|
|
#include "spore/core/TaskManager.h"
|
|
#include <Arduino.h>
|
|
#include <ESP8266WiFi.h>
|
|
#include <ArduinoJson.h>
|
|
#include <ESP8266HTTPClient.h>
|
|
#include <map>
|
|
#include <vector>
|
|
#include <functional>
|
|
|
|
class ClusterManager {
|
|
public:
|
|
ClusterManager(NodeContext& ctx, TaskManager& taskMgr);
|
|
void registerTasks();
|
|
void listen();
|
|
void addOrUpdateNode(const String& nodeHost, IPAddress nodeIP);
|
|
void updateAllNodeStatuses();
|
|
void removeDeadNodes();
|
|
void printMemberList();
|
|
const std::map<String, NodeInfo>& getMemberList() const { return *ctx.memberList; }
|
|
void fetchNodeInfo(const IPAddress& ip);
|
|
void updateLocalNodeResources();
|
|
void heartbeatTaskCallback();
|
|
void updateAllMembersInfoTaskCallback();
|
|
void broadcastNodeUpdate();
|
|
private:
|
|
NodeContext& ctx;
|
|
TaskManager& taskManager;
|
|
struct MessageHandler {
|
|
bool (*predicate)(const char*);
|
|
std::function<void(const char*)> handle;
|
|
const char* name;
|
|
};
|
|
void initMessageHandlers();
|
|
void handleIncomingMessage(const char* incoming);
|
|
static bool isHeartbeatMsg(const char* msg);
|
|
static bool isNodeUpdateMsg(const char* msg);
|
|
static bool isClusterEventMsg(const char* msg);
|
|
static bool isRawMsg(const char* msg);
|
|
void onHeartbeat(const char* msg);
|
|
void onNodeUpdate(const char* msg);
|
|
void onClusterEvent(const char* msg);
|
|
void onRawMessage(const char* msg);
|
|
void sendNodeInfo(const String& hostname, const IPAddress& targetIP);
|
|
unsigned long lastHeartbeatSentAt = 0;
|
|
std::vector<MessageHandler> messageHandlers;
|
|
};
|