From a9ab0d6b75004d1aca86ac965b79cf5efea1f7c0 Mon Sep 17 00:00:00 2001 From: faumaray Date: Mon, 22 Jun 2026 22:25:15 +0300 Subject: [PATCH] Make the service registry a real read path for commands MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The DI registry was write-only — populated in Core::initialize() but never resolved through. Thread it into command execution so commands resolve shared services by interface instead of constructing throwaways: - CommandContext gains a non-owning ServiceRegistry* plus an ergonomic service() resolver. - CommandManager carries the registry (Core::setServices(®istry_)) and stamps it onto every CommandContext it builds (async + sync paths). - RpcInfoCommand's getPublicIpAddress() now reuses the shared, initialized INetworkClient from the registry, falling back to a throwaway client only when none is registered. Core itself (the composition root) keeps direct member wiring and commands keep explicit MusicManager injection — converting those to a service locator would only hide dependencies; the registry earns its keep at the genuine seam where a command needs a service it isn't handed. Co-Authored-By: Claude Opus 4.8 --- include/echoion/core/CommandManager.hpp | 11 +++++++++++ include/echoion/core/commands/Command.hpp | 13 ++++++++++++ src/CommandManager.cpp | 2 ++ src/Core.cpp | 4 ++++ src/commands/RpcInfoCommand.cpp | 24 +++++++++++++++-------- 5 files changed, 46 insertions(+), 8 deletions(-) diff --git a/include/echoion/core/CommandManager.hpp b/include/echoion/core/CommandManager.hpp index 3f65c96..483f64b 100644 --- a/include/echoion/core/CommandManager.hpp +++ b/include/echoion/core/CommandManager.hpp @@ -43,6 +43,13 @@ namespace Echoion::Core // Shutdown the command manager (stops thread pool) void shutdown(); + // Provide the service registry that command contexts resolve + // shared services from (non-owning; must outlive this manager). + void setServices(const Void::Service::ServiceRegistry* services) + { + services_ = services; + } + // Disable copy CommandManager(const CommandManager&) = delete; CommandManager& operator=(const CommandManager&) = delete; @@ -141,6 +148,10 @@ namespace Echoion::Core std::string prefix_ = "!"; + // Non-owning service registry threaded into each CommandContext + // so commands can resolve shared services by interface. + const Void::Service::ServiceRegistry* services_ = nullptr; + // Map for fast lookup: "play" -> PlayCommand std::map> commands_; diff --git a/include/echoion/core/commands/Command.hpp b/include/echoion/core/commands/Command.hpp index 4f20e1d..fa642bd 100644 --- a/include/echoion/core/commands/Command.hpp +++ b/include/echoion/core/commands/Command.hpp @@ -3,6 +3,7 @@ #include "echoion/core/MusicManager.hpp" #include #include +#include #include #include #include @@ -47,6 +48,10 @@ namespace Echoion::Core // Dependencies std::weak_ptr musicManager; + // Service registry for resolving shared services by interface + // (non-owning; outlives the command). Populated by CommandManager. + const Void::Service::ServiceRegistry* services = nullptr; + // Reply callback - set by CommandManager std::function replyCallback; @@ -57,6 +62,14 @@ namespace Echoion::Core replyCallback(message); } } + + // Resolve a shared service by interface, or nullptr if the + // registry is absent / the service was not provided. + template + [[nodiscard]] std::shared_ptr service() const + { + return services ? services->get() : nullptr; + } }; /** diff --git a/src/CommandManager.cpp b/src/CommandManager.cpp index cef2588..fe6669c 100644 --- a/src/CommandManager.cpp +++ b/src/CommandManager.cpp @@ -224,6 +224,7 @@ namespace Echoion::Core .args = std::move(argsStr), .invokerName = task.invokerName, .musicManager = task.musicManager, + .services = services_, .replyCallback = std::move(task.replyFunc)}; command->execute(ctx); @@ -366,6 +367,7 @@ namespace Echoion::Core .args = std::move(argsStr), .invokerName = invokerName, .musicManager = musicManager, + .services = services_, .replyCallback = std::move(replyFunc)}; try { diff --git a/src/Core.cpp b/src/Core.cpp index 417a274..51d817f 100644 --- a/src/Core.cpp +++ b/src/Core.cpp @@ -119,6 +119,10 @@ namespace Echoion::Core commandManager_ = std::make_unique(); commandManager_->setPrefix(config_.bot.commandPrefix); commandManager_->setServerConfig(config_); + // Let commands resolve shared services (e.g. the network client) by + // interface from the registry composed below — the pointer is stable + // and entries are populated before any command dispatches. + commandManager_->setServices(®istry_); // Set up permission check function commandManager_->setPermissionCheckFunc( diff --git a/src/commands/RpcInfoCommand.cpp b/src/commands/RpcInfoCommand.cpp index 4f92148..2c1bb7f 100644 --- a/src/commands/RpcInfoCommand.cpp +++ b/src/commands/RpcInfoCommand.cpp @@ -1,6 +1,7 @@ #include "echoion/core/NetworkClient.hpp" #include "echoion/core/commands/RpcInfoCommand.hpp" #include +#include #include #include #include @@ -8,7 +9,7 @@ namespace Echoion::Core { - std::string getPublicIpAddress() + std::string getPublicIpAddress(Api::INetworkClient* shared) { // Try to get public IP from external services static std::string cachedIp; @@ -27,11 +28,16 @@ namespace Echoion::Core "https://api.ipify.org", "https://checkip.amazonaws.com", "https://icanhazip.com", "https://ifconfig.me/ip"}; - // Create a temporary network client for IP lookup - auto networkClient = std::make_shared(); - auto initResult = networkClient->initialize(); - if (!initResult) { - return cachedIp.empty() ? "127.0.0.1" : cachedIp; + // Prefer the shared, already-initialized network client resolved from + // the service registry; only spin up a throwaway one as a fallback. + std::shared_ptr owned; + Api::INetworkClient* networkClient = shared; + if (!networkClient) { + owned = std::make_shared(); + if (!owned->initialize()) { + return cachedIp.empty() ? "127.0.0.1" : cachedIp; + } + networkClient = owned.get(); } for (const auto& service : ipServices) { @@ -73,8 +79,10 @@ namespace Echoion::Core uint16_t rpcPort = networkConfig.rpcPort; std::string rpcBindAddress = networkConfig.rpcBindAddress; - // Determine the public IP to show - std::string publicIp = getPublicIpAddress(); + // Determine the public IP to show, reusing the shared network client + // from the registry when available. + auto sharedNet = ctx.service(); + std::string publicIp = getPublicIpAddress(sharedNet.get()); // If bind address is not localhost, use it if (rpcBindAddress != "127.0.0.1" && rpcBindAddress != "localhost") {