## Breaking Change
IIO API redesigned from manual pull+if-forest to callback dispatch.
All modules must update their subscribe() calls to pass handlers.
### Before (OLD API)
```cpp
io->subscribe("input:mouse");
void process(...) {
while (io->hasMessages()) {
auto msg = io->pullMessage();
if (msg.topic == "input:mouse") {
handleMouse(msg);
} else if (msg.topic == "input:keyboard") {
handleKeyboard(msg);
}
}
}
```
### After (NEW API)
```cpp
io->subscribe("input:mouse", [this](const Message& msg) {
handleMouse(msg);
});
void process(...) {
while (io->hasMessages()) {
io->pullAndDispatch(); // Callbacks invoked automatically
}
}
```
## Changes
**Core API (include/grove/IIO.h)**
- Added: `using MessageHandler = std::function<void(const Message&)>`
- Changed: `subscribe()` now requires `MessageHandler` callback parameter
- Changed: `subscribeLowFreq()` now requires `MessageHandler` callback
- Removed: `pullMessage()`
- Added: `pullAndDispatch()` - pulls and auto-dispatches to handlers
**Implementation (src/IntraIO.cpp)**
- Store callbacks in `Subscription.handler`
- `pullAndDispatch()` matches topic against ALL subscriptions (not just first)
- Fixed: Regex pattern compilation supports both wildcards (*) and regex (.*)
- Performance: ~1000 msg/s throughput (unchanged from before)
**Files Updated**
- 31 test/module files migrated to callback API (via parallel agents)
- 8 documentation files updated (DEVELOPER_GUIDE, USER_GUIDE, module READMEs)
## Bugs Fixed During Migration
1. **pullAndDispatch() early return bug**: Was only calling FIRST matching handler
- Fix: Loop through ALL subscriptions, invoke all matching handlers
2. **Regex pattern compilation bug**: Pattern "player:.*" failed to match
- Fix: Detect ".*" in pattern → use as regex, otherwise escape and convert wildcards
## Testing
✅ test_11_io_system: PASSED (IIO pub/sub, pattern matching, batching)
✅ test_threaded_module_system: 6/6 PASSED
✅ test_threaded_stress: 5/5 PASSED (50 modules, 100x reload, concurrent ops)
✅ test_12_datanode: PASSED
✅ 10 TopicTree scenarios: 10/10 PASSED
✅ benchmark_e2e: ~1000 msg/s throughput
Total: 23+ tests passing
## Performance Impact
No performance regression from callback dispatch:
- IIO throughput: ~1000 msg/s (same as before)
- ThreadedModuleSystem: Speedup ~1.0x (barrier pattern expected)
## Migration Guide
For all modules using IIO:
1. Update subscribe() calls to include handler lambda
2. Replace pullMessage() loops with pullAndDispatch()
3. Move topic-specific logic from if-forest into callbacks
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
257 lines
8.9 KiB
C++
257 lines
8.9 KiB
C++
/**
|
|
* TestControllerModule - Simulates game logic for integration tests
|
|
*
|
|
* This module:
|
|
* - Subscribes to UI events (clicks, actions, value changes)
|
|
* - Responds to UI interactions
|
|
* - Updates UI state via IIO messages
|
|
* - Demonstrates bidirectional UI ↔ Game communication
|
|
*/
|
|
|
|
#include <grove/IModule.h>
|
|
#include <grove/IIO.h>
|
|
#include <grove/JsonDataNode.h>
|
|
|
|
#include <memory>
|
|
#include <string>
|
|
#include <iostream>
|
|
|
|
extern "C" {
|
|
|
|
class TestControllerModule : public grove::IModule {
|
|
public:
|
|
TestControllerModule() = default;
|
|
~TestControllerModule() override = default;
|
|
|
|
void setConfiguration(const grove::IDataNode& config, grove::IIO* io, grove::ITaskScheduler* scheduler) override {
|
|
m_io = io;
|
|
m_uiInstanceId = config.getString("uiInstanceId", "ui_module");
|
|
|
|
std::cout << "[TestController] Initializing...\n";
|
|
|
|
// Subscribe to UI events with callbacks
|
|
if (m_io) {
|
|
m_io->subscribe("ui:click", [this](const grove::Message& msg) {
|
|
handleClick(*msg.data);
|
|
});
|
|
|
|
m_io->subscribe("ui:action", [this](const grove::Message& msg) {
|
|
handleAction(*msg.data);
|
|
});
|
|
|
|
m_io->subscribe("ui:value_changed", [this](const grove::Message& msg) {
|
|
handleValueChanged(*msg.data);
|
|
});
|
|
|
|
m_io->subscribe("ui:text_changed", [this](const grove::Message& msg) {
|
|
handleTextChanged(*msg.data);
|
|
});
|
|
|
|
m_io->subscribe("ui:text_submit", [this](const grove::Message& msg) {
|
|
handleTextSubmit(*msg.data);
|
|
});
|
|
|
|
m_io->subscribe("ui:hover", [this](const grove::Message& msg) {
|
|
handleHover(*msg.data);
|
|
});
|
|
|
|
m_io->subscribe("ui:focus_gained", [this](const grove::Message& msg) {
|
|
handleFocusGained(*msg.data);
|
|
});
|
|
|
|
m_io->subscribe("ui:focus_lost", [this](const grove::Message& msg) {
|
|
handleFocusLost(*msg.data);
|
|
});
|
|
}
|
|
|
|
std::cout << "[TestController] Subscribed to UI events\n";
|
|
}
|
|
|
|
void process(const grove::IDataNode& input) override {
|
|
if (!m_io) return;
|
|
|
|
m_frameCount++;
|
|
|
|
// Pull and dispatch all pending messages (callbacks invoked automatically)
|
|
while (m_io->hasMessages() > 0) {
|
|
m_io->pullAndDispatch();
|
|
}
|
|
|
|
// Simulate some game logic
|
|
if (m_frameCount % 120 == 0) { // Every 2 seconds at 60fps
|
|
// Could update UI here
|
|
std::cout << "[TestController] Heartbeat at frame " << m_frameCount << "\n";
|
|
}
|
|
}
|
|
|
|
const grove::IDataNode& getConfiguration() override {
|
|
if (!m_config) {
|
|
m_config = std::make_unique<grove::JsonDataNode>("config");
|
|
}
|
|
return *m_config;
|
|
}
|
|
|
|
std::unique_ptr<grove::IDataNode> getHealthStatus() override {
|
|
auto health = std::make_unique<grove::JsonDataNode>("health");
|
|
health->setString("status", "healthy");
|
|
health->setString("module", "TestControllerModule");
|
|
health->setInt("frameCount", static_cast<int>(m_frameCount));
|
|
health->setInt("clicksReceived", m_clickCount);
|
|
health->setInt("actionsReceived", m_actionCount);
|
|
return health;
|
|
}
|
|
|
|
void shutdown() override {
|
|
std::cout << "[TestController] Shutting down...\n";
|
|
std::cout << " Total frames: " << m_frameCount << "\n";
|
|
std::cout << " Clicks received: " << m_clickCount << "\n";
|
|
std::cout << " Actions received: " << m_actionCount << "\n";
|
|
std::cout << " Value changes: " << m_valueChangeCount << "\n";
|
|
m_io = nullptr;
|
|
}
|
|
|
|
std::unique_ptr<grove::IDataNode> getState() override {
|
|
auto state = std::make_unique<grove::JsonDataNode>("state");
|
|
state->setInt("frameCount", static_cast<int>(m_frameCount));
|
|
state->setInt("clickCount", m_clickCount);
|
|
state->setInt("actionCount", m_actionCount);
|
|
state->setInt("valueChangeCount", m_valueChangeCount);
|
|
return state;
|
|
}
|
|
|
|
void setState(const grove::IDataNode& state) override {
|
|
m_frameCount = static_cast<uint64_t>(state.getInt("frameCount", 0));
|
|
m_clickCount = state.getInt("clickCount", 0);
|
|
m_actionCount = state.getInt("actionCount", 0);
|
|
m_valueChangeCount = state.getInt("valueChangeCount", 0);
|
|
}
|
|
|
|
std::string getType() const override {
|
|
return "TestControllerModule";
|
|
}
|
|
|
|
bool isIdle() const override {
|
|
return true;
|
|
}
|
|
|
|
private:
|
|
grove::IIO* m_io = nullptr;
|
|
std::string m_uiInstanceId;
|
|
std::unique_ptr<grove::JsonDataNode> m_config;
|
|
|
|
// Stats
|
|
uint64_t m_frameCount = 0;
|
|
int m_clickCount = 0;
|
|
int m_actionCount = 0;
|
|
int m_valueChangeCount = 0;
|
|
int m_textChangeCount = 0;
|
|
|
|
void handleClick(const grove::IDataNode& data) {
|
|
m_clickCount++;
|
|
std::string widgetId = data.getString("widgetId", "");
|
|
double x = data.getDouble("x", 0.0);
|
|
double y = data.getDouble("y", 0.0);
|
|
|
|
std::cout << "[TestController] Click on '" << widgetId
|
|
<< "' at (" << x << ", " << y << ")\n";
|
|
|
|
// Example: Update UI visibility based on click
|
|
if (widgetId == "btn_toggle") {
|
|
auto visibilityMsg = std::make_unique<grove::JsonDataNode>("set_visible");
|
|
visibilityMsg->setString("id", "hidden_panel");
|
|
visibilityMsg->setBool("visible", true);
|
|
m_io->publish("ui:set_visible", std::move(visibilityMsg));
|
|
}
|
|
}
|
|
|
|
void handleAction(const grove::IDataNode& data) {
|
|
m_actionCount++;
|
|
std::string action = data.getString("action", "");
|
|
std::string widgetId = data.getString("widgetId", "");
|
|
|
|
std::cout << "[TestController] Action: '" << action
|
|
<< "' from '" << widgetId << "'\n";
|
|
|
|
// Example: Handle game actions
|
|
if (action == "game:start") {
|
|
std::cout << " → Starting game...\n";
|
|
}
|
|
else if (action == "game:quit") {
|
|
std::cout << " → Quitting game...\n";
|
|
}
|
|
else if (action == "settings:volume") {
|
|
double volume = data.getDouble("value", 50.0);
|
|
std::cout << " → Setting volume to " << volume << "\n";
|
|
}
|
|
}
|
|
|
|
void handleValueChanged(const grove::IDataNode& data) {
|
|
m_valueChangeCount++;
|
|
std::string widgetId = data.getString("widgetId", "");
|
|
|
|
if (data.hasChild("value")) {
|
|
double value = data.getDouble("value", 0.0);
|
|
std::cout << "[TestController] Value changed: '" << widgetId
|
|
<< "' = " << value << "\n";
|
|
}
|
|
else if (data.hasChild("checked")) {
|
|
bool checked = data.getBool("checked", false);
|
|
std::cout << "[TestController] Checkbox changed: '" << widgetId
|
|
<< "' = " << (checked ? "checked" : "unchecked") << "\n";
|
|
}
|
|
}
|
|
|
|
void handleTextChanged(const grove::IDataNode& data) {
|
|
m_textChangeCount++;
|
|
std::string widgetId = data.getString("widgetId", "");
|
|
std::string text = data.getString("text", "");
|
|
|
|
std::cout << "[TestController] Text changed: '" << widgetId
|
|
<< "' = \"" << text << "\"\n";
|
|
}
|
|
|
|
void handleTextSubmit(const grove::IDataNode& data) {
|
|
std::string widgetId = data.getString("widgetId", "");
|
|
std::string text = data.getString("text", "");
|
|
|
|
std::cout << "[TestController] Text submitted: '" << widgetId
|
|
<< "' = \"" << text << "\"\n";
|
|
|
|
// Example: Process username input
|
|
if (widgetId == "username_input") {
|
|
std::cout << " → Username entered: " << text << "\n";
|
|
}
|
|
}
|
|
|
|
void handleHover(const grove::IDataNode& data) {
|
|
std::string widgetId = data.getString("widgetId", "");
|
|
bool enter = data.getBool("enter", false);
|
|
|
|
if (enter && !widgetId.empty()) {
|
|
// Only log enter events to reduce spam
|
|
// std::cout << "[TestController] Hover: '" << widgetId << "'\n";
|
|
}
|
|
}
|
|
|
|
void handleFocusGained(const grove::IDataNode& data) {
|
|
std::string widgetId = data.getString("widgetId", "");
|
|
std::cout << "[TestController] Focus gained: '" << widgetId << "'\n";
|
|
}
|
|
|
|
void handleFocusLost(const grove::IDataNode& data) {
|
|
std::string widgetId = data.getString("widgetId", "");
|
|
std::cout << "[TestController] Focus lost: '" << widgetId << "'\n";
|
|
}
|
|
};
|
|
|
|
// Module factory functions (standard GroveEngine interface)
|
|
grove::IModule* createModule() {
|
|
return new TestControllerModule();
|
|
}
|
|
|
|
void destroyModule(grove::IModule* module) {
|
|
delete module;
|
|
}
|
|
|
|
} // extern "C"
|