chore: rename ClusterContext to NodeContext

This commit is contained in:
2025-08-21 17:39:43 +02:00
parent 81bc70fa83
commit 5870695465
10 changed files with 40 additions and 26 deletions

View File

@@ -14,14 +14,28 @@ SProcket ORchestration Engine
- ESP8266
## Configuration
## Architecture
### Auto Discovery
A node periodically executes 2 tasks responsible for auto discovers:
- send discovery: send UDP packet on broadcast address to discover nodes
- listen for discovery: receive UDP packets and send response back to the node who initiated discovery
Discovered nodes are added to the so cluster memberlist.
Another periodic task will then call the `/api/node/status` endpoint over HTTP on each node in the memberlist to get detailed informations about the node (e.g. freeHeap, available API endpoints).
## Develop
### Configuration
Choose one of your nodes as the API node to interact with the cluster and configure it in `.env`:
```sh
export API_NODE=10.0.1.x
```
## Build
### Build
Build the firmware:
@@ -29,14 +43,14 @@ Build the firmware:
./ctl.sh build
```
## Flash
### Flash
Flash firmware to a connected device:
```sh
./ctl.sh flash
```
## OTA
### OTA
Update one nodes:

View File

@@ -1,6 +1,6 @@
#include "ClusterManager.h"
ClusterManager::ClusterManager(ClusterContext& ctx) : ctx(ctx) {
ClusterManager::ClusterManager(NodeContext& ctx) : ctx(ctx) {
// Register callback for node_discovered event
ctx.registerEvent("node_discovered", [this](void* data) {
NodeInfo* node = static_cast<NodeInfo*>(data);

View File

@@ -1,6 +1,6 @@
#pragma once
#include "Globals.h"
#include "ClusterContext.h"
#include "NodeContext.h"
#include "NodeInfo.h"
#include <Arduino.h>
#include <ESP8266WiFi.h>
@@ -9,7 +9,7 @@
class ClusterManager {
public:
ClusterManager(ClusterContext& ctx);
ClusterManager(NodeContext& ctx);
void sendDiscovery();
void listenForDiscovery();
void addOrUpdateNode(const String& nodeHost, IPAddress nodeIP);
@@ -22,5 +22,5 @@ public:
void heartbeatTaskCallback();
void updateAllMembersInfoTaskCallback();
private:
ClusterContext& ctx;
NodeContext& ctx;
};

View File

@@ -3,7 +3,7 @@
const char* STA_SSID = "shroud";
const char* STA_PASS = "th3r31sn0sp00n";
NetworkManager::NetworkManager(ClusterContext& ctx) : ctx(ctx) {}
NetworkManager::NetworkManager(NodeContext& ctx) : ctx(ctx) {}
void NetworkManager::setHostnameFromMac() {
uint8_t mac[6];

View File

@@ -1,12 +1,12 @@
#pragma once
#include "ClusterContext.h"
#include "NodeContext.h"
#include <ESP8266WiFi.h>
class NetworkManager {
public:
NetworkManager(ClusterContext& ctx);
NetworkManager(NodeContext& ctx);
void setupWiFi();
void setHostnameFromMac();
private:
ClusterContext& ctx;
NodeContext& ctx;
};

View File

@@ -1,23 +1,23 @@
#include "ClusterContext.h"
#include "NodeContext.h"
ClusterContext::ClusterContext() {
NodeContext::NodeContext() {
scheduler = new Scheduler();
udp = new WiFiUDP();
memberList = new std::vector<NodeInfo>();
hostname = "";
}
ClusterContext::~ClusterContext() {
NodeContext::~NodeContext() {
delete scheduler;
delete udp;
delete memberList;
}
void ClusterContext::registerEvent(const std::string& event, EventCallback cb) {
void NodeContext::registerEvent(const std::string& event, EventCallback cb) {
eventRegistry[event].push_back(cb);
}
void ClusterContext::triggerEvent(const std::string& event, void* data) {
void NodeContext::triggerEvent(const std::string& event, void* data) {
for (auto& cb : eventRegistry[event]) {
cb(data);
}

View File

@@ -7,10 +7,10 @@
#include <map>
#include <string>
class ClusterContext {
class NodeContext {
public:
ClusterContext();
~ClusterContext();
NodeContext();
~NodeContext();
Scheduler* scheduler;
WiFiUDP* udp;
String hostname;

View File

@@ -1,6 +1,6 @@
#include "RestApiServer.h"
RestApiServer::RestApiServer(ClusterContext& ctx, uint16_t port) : server(port), ctx(ctx) {}
RestApiServer::RestApiServer(NodeContext& ctx, uint16_t port) : server(port), ctx(ctx) {}
void RestApiServer::addEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler) {
serviceRegistry.push_back(std::make_tuple(uri, method));

View File

@@ -7,7 +7,7 @@
#include <vector>
#include <tuple>
#include "ClusterContext.h"
#include "NodeContext.h"
#include "NodeInfo.h"
#define DEBUG_UPDATER 1
@@ -17,14 +17,14 @@ using namespace std::placeholders;
class RestApiServer {
public:
RestApiServer(ClusterContext& ctx, uint16_t port = 80);
RestApiServer(NodeContext& ctx, uint16_t port = 80);
void begin();
void addEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler);
void addEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler,
std::function<void(AsyncWebServerRequest*, const String&, size_t, uint8_t*, size_t, bool)> uploadHandler);
private:
AsyncWebServer server;
ClusterContext& ctx;
NodeContext& ctx;
std::vector<std::tuple<String, int>> serviceRegistry;
void getClusterMembersJson(AsyncWebServerRequest *request);
void methodToStr(const std::tuple<String, int> &endpoint, ArduinoJson::V742PB22::JsonObject &apiObj);

View File

@@ -1,11 +1,11 @@
#include <Arduino.h>
#include "Globals.h"
#include "ClusterContext.h"
#include "NodeContext.h"
#include "NetworkManager.h"
#include "ClusterManager.h"
#include "RestApiServer.h"
ClusterContext ctx;
NodeContext ctx;
NetworkManager network(ctx);
ClusterManager cluster(ctx);
RestApiServer apiServer(ctx);