## 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>
136 lines
4.1 KiB
C++
136 lines
4.1 KiB
C++
#include "EconomyModule.h"
|
|
#include <iostream>
|
|
|
|
namespace grove {
|
|
|
|
EconomyModule::EconomyModule() {
|
|
std::cout << "[EconomyModule] Constructor" << std::endl;
|
|
}
|
|
|
|
EconomyModule::~EconomyModule() {
|
|
std::cout << "[EconomyModule] Destructor" << std::endl;
|
|
}
|
|
|
|
void EconomyModule::process(const IDataNode& input) {
|
|
// Pull and dispatch all pending messages (callbacks invoked automatically)
|
|
if (io) {
|
|
while (io->hasMessages() > 0) {
|
|
io->pullAndDispatch();
|
|
}
|
|
}
|
|
}
|
|
|
|
void EconomyModule::setConfiguration(const IDataNode& configNode, IIO* ioPtr, ITaskScheduler* schedulerPtr) {
|
|
std::cout << "[EconomyModule] setConfiguration called" << std::endl;
|
|
|
|
this->io = ioPtr;
|
|
this->scheduler = schedulerPtr;
|
|
|
|
// Store config
|
|
config = std::make_unique<JsonDataNode>("config", nlohmann::json::object());
|
|
|
|
// Subscribe to player events with callback
|
|
if (io) {
|
|
io->subscribe("player:*", [this](const Message& msg) {
|
|
playerEventsProcessed++;
|
|
handlePlayerEvent(msg.topic, msg.data.get());
|
|
});
|
|
}
|
|
}
|
|
|
|
const IDataNode& EconomyModule::getConfiguration() {
|
|
if (!config) {
|
|
config = std::make_unique<JsonDataNode>("config", nlohmann::json::object());
|
|
}
|
|
return *config;
|
|
}
|
|
|
|
std::unique_ptr<IDataNode> EconomyModule::getHealthStatus() {
|
|
nlohmann::json health = {
|
|
{"status", "healthy"},
|
|
{"totalBonusesApplied", totalBonusesApplied},
|
|
{"playerEventsProcessed", playerEventsProcessed}
|
|
};
|
|
return std::make_unique<JsonDataNode>("health", health);
|
|
}
|
|
|
|
void EconomyModule::shutdown() {
|
|
std::cout << "[EconomyModule] Shutdown - Processed " << playerEventsProcessed << " player events" << std::endl;
|
|
}
|
|
|
|
std::unique_ptr<IDataNode> EconomyModule::getState() {
|
|
nlohmann::json state = {
|
|
{"totalBonusesApplied", totalBonusesApplied},
|
|
{"playerEventsProcessed", playerEventsProcessed}
|
|
};
|
|
return std::make_unique<JsonDataNode>("state", state);
|
|
}
|
|
|
|
void EconomyModule::setState(const IDataNode& state) {
|
|
totalBonusesApplied = state.getInt("totalBonusesApplied", 0);
|
|
playerEventsProcessed = state.getInt("playerEventsProcessed", 0);
|
|
std::cout << "[EconomyModule] State restored" << std::endl;
|
|
}
|
|
|
|
void EconomyModule::setDataTree(IDataTree* treePtr) {
|
|
this->tree = treePtr;
|
|
}
|
|
|
|
void EconomyModule::handlePlayerEvent(const std::string& topic, IDataNode* data) {
|
|
std::cout << "[EconomyModule] Handling player event: " << topic << std::endl;
|
|
|
|
if (topic == "player:level_up") {
|
|
// Apply economy bonus
|
|
if (data) {
|
|
int goldBonus = data->getInt("goldBonus", 0);
|
|
applyEconomyBonus(goldBonus);
|
|
}
|
|
} else if (topic == "player:gold:updated") {
|
|
// Verify synchronization
|
|
if (data && tree) {
|
|
auto dataRoot = tree->getDataRoot();
|
|
auto player = dataRoot->getChild("player");
|
|
if (player) {
|
|
auto profile = player->getChild("profile");
|
|
if (profile) {
|
|
int goldInData = profile->getInt("gold", 0);
|
|
int goldInMsg = data->getInt("gold", 0);
|
|
|
|
if (goldInData == goldInMsg) {
|
|
std::cout << "[EconomyModule] Sync OK: gold=" << goldInData << std::endl;
|
|
} else {
|
|
std::cout << "[EconomyModule] SYNC ERROR: msg=" << goldInMsg
|
|
<< " data=" << goldInData << std::endl;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void EconomyModule::applyEconomyBonus(int goldBonus) {
|
|
totalBonusesApplied += goldBonus;
|
|
|
|
if (!tree) return;
|
|
|
|
auto dataRoot = tree->getDataRoot();
|
|
|
|
nlohmann::json bonusData = {
|
|
{"levelUpBonus", goldBonus},
|
|
{"totalBonuses", totalBonusesApplied}
|
|
};
|
|
|
|
auto bonuses = std::make_unique<JsonDataNode>("bonuses", bonusData);
|
|
|
|
std::cout << "[EconomyModule] Applied bonus: " << goldBonus << std::endl;
|
|
}
|
|
|
|
} // namespace grove
|
|
|
|
// Export C API
|
|
extern "C" {
|
|
grove::IModule* createModule() {
|
|
return new grove::EconomyModule();
|
|
}
|
|
}
|