warfactoryracine/core/include/warfactory/IModuleSystem.h
StillHammer c37f7d245e Complete Phase 1: Finalize all core interfaces with immutable architecture
- **IEngine**: Add run(), step(), loadModules(), socket management, health monitoring
- **IModuleSystem**: Inherit ITaskScheduler, 1:1 module relationship, task delegation
- **IModule**: Service injection (IIO*, ITaskScheduler*), pub/sub communication, void process()
- **IIO**: Pull-based pub/sub with wildcards, low-frequency batching, health monitoring
- **ITaskScheduler**: Task delegation interface for module→execution system

Architecture completed with quadruple interface pattern optimized for:
- Thread-safe pull-based messaging
- Module task delegation to execution systems
- Engine health monitoring of all IIO instances
- Immutable interface foundation for future development

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-24 09:03:56 +08:00

120 lines
3.9 KiB
C++

#pragma once
#include <string>
#include <memory>
#include <nlohmann/json.hpp>
// Forward declarations to avoid circular dependencies
namespace warfactory {
class IModule;
class IIO;
}
using json = nlohmann::json;
namespace warfactory {
enum class ModuleSystemType {
SEQUENTIAL = 0,
THREADED = 1,
THREAD_POOL = 2,
CLUSTER = 3
};
/**
* @brief Task scheduling interface for module delegation
*/
class ITaskScheduler {
public:
virtual ~ITaskScheduler() = default;
/**
* @brief Schedule a task for execution
* @param taskType Type of task (e.g., "pathfinding", "collision_check")
* @param taskData JSON data for the task
*/
virtual void scheduleTask(const std::string& taskType, const json& taskData) = 0;
/**
* @brief Check if completed tasks are available
* @return Number of completed tasks ready to be pulled
*/
virtual int hasCompletedTasks() const = 0;
/**
* @brief Pull and consume one completed task
* @return Task result JSON. Task is removed from completed queue.
*/
virtual json getCompletedTask() = 0;
};
/**
* @brief Module execution strategy interface - swappable performance architecture
*
* The module system manages module lifecycle and execution strategy.
* Different implementations provide different performance characteristics:
*
* - SequentialModuleSystem: Debug/test mode, processes modules one at a time
* - ThreadedModuleSystem: Each module in its own thread
* - MultithreadedModuleSystem: Module tasks distributed across thread pool
* - ClusterModuleSystem: Modules distributed across multiple machines
*
* This enables progressive evolution from debug to production to MMO scale
* without changing any module business logic code.
*
* Inherits from ITaskScheduler to provide task delegation capabilities.
*/
class IModuleSystem : public ITaskScheduler {
public:
virtual ~IModuleSystem() = default;
/**
* @brief Register a module with the system
* @param name Unique identifier for the module
* @param module Module implementation (unique ownership)
*
* The module system takes ownership of the module and manages its lifecycle.
* Modules can be registered at any time and will participate in the next
* processing cycle.
*/
virtual void registerModule(const std::string& name, std::unique_ptr<IModule> module) = 0;
/**
* @brief Process all registered modules
* @param deltaTime Time elapsed since last processing cycle in seconds
*
* This is the core execution method that coordinates all modules according
* to the implemented strategy. Each module's process() method will be called
* with appropriate timing and coordination.
*/
virtual void processModules(float deltaTime) = 0;
/**
* @brief Set the IO layer for inter-module communication
* @param ioLayer Communication transport implementation (unique ownership)
*
* The module system takes ownership of the IO layer and uses it to
* facilitate communication between modules.
*/
virtual void setIOLayer(std::unique_ptr<IIO> ioLayer) = 0;
/**
* @brief Query a specific module directly
* @param name Name of the module to query
* @param input JSON input to send to the module
* @return JSON response from the module
*
* This provides direct access to module functionality for debugging,
* testing, or administrative purposes. The query bypasses normal
* execution flow and calls the module's process() method directly.
*/
virtual json queryModule(const std::string& name, const json& input) = 0;
/**
* @brief Get module system type identifier
* @return Module system type enum value for identification
*/
virtual ModuleSystemType getType() const = 0;
};
} // namespace warfactory