#ifndef LOGOS_API_H #define LOGOS_API_H #include "logos_mode.h" #include "logos_transport_config.h" #include "logos_types.h" #include #include #include #include #include #include #include class LogosAPIClient; class LogosAPIProvider; class TokenManager; // qHash for LogosTransportConfig — combined with operator== from // logos_transport_config.h, this lets QHash use it as a key. Lives here // rather than in logos_transport_config.h so that header stays Qt-free // (the SDK is being de-Qt'd; only Qt-using consumers like the cache // here pull in the QHash adapter). // // Every field that distinguishes one explicit-transport client from // another contributes to the hash; otherwise two callers with different // TLS or codec settings could land on the same bucket and cache-alias // onto a single client. inline size_t qHash(const LogosTransportConfig& cfg, size_t seed = 0) noexcept { return qHashMulti(seed, static_cast(cfg.protocol), std::hash{}(cfg.host), cfg.port, std::hash{}(cfg.caFile), std::hash{}(cfg.certFile), std::hash{}(cfg.keyFile), cfg.verifyPeer, static_cast(cfg.codec)); } // LogosAPIClient cache key. Mirrors the factory's transport-resolution // rule so two callers that would observe the same connection share a // cached client: // // - Mock / Local mode → transport is ignored at construction; key // ignores it too. Switching mode changes the // key (so cached clients don't bleed across // mode switches in tests). // - Remote mode → cfg picks the wire endpoint; key includes // the full LogosTransportConfig. // // Without the mode-aware comparison, calling // `getClient(x, tcp)` and `getClient(x, tcp_ssl)` in Mock mode would // allocate two clients pointing at functionally identical // MockTransportConnections. struct LogosAPIClientCacheKey { QString target; LogosMode mode; LogosTransportConfig transport; // only compared when mode == Remote }; inline bool operator==(const LogosAPIClientCacheKey& a, const LogosAPIClientCacheKey& b) noexcept { if (a.target != b.target) return false; if (a.mode != b.mode) return false; return a.mode == LogosMode::Remote ? a.transport == b.transport : true; } inline size_t qHash(const LogosAPIClientCacheKey& k, size_t seed = 0) noexcept { if (k.mode == LogosMode::Remote) { return qHashMulti(seed, k.target, static_cast(k.mode), k.transport); } // Mock / Local: transport is irrelevant — leave it out of the hash // so it can't bias which bucket the key lands in. return qHashMulti(seed, k.target, static_cast(k.mode)); } /** * @brief LogosAPI provides a unified interface to the Logos SDK * * This class initializes and keeps instances of the client provider and token manager. */ class LogosAPI : public QObject { Q_OBJECT public: /** * @brief Construct a new LogosAPI instance * @param module_name The name of this module * @param parent Parent QObject */ explicit LogosAPI(const QString& module_name, QObject *parent = nullptr); /** * @brief Construct a new LogosAPI with an explicit transport set. * * `transports` is empty ⇒ use the process-global default (back-compat). * Non-empty ⇒ provider publishes on every configured transport * (e.g. a daemon listing both LocalSocket and TCP+SSL so the CLI has * a fast in-process path *and* remote clients have a secure path). */ LogosAPI(const QString& module_name, LogosTransportSet transports, QObject *parent = nullptr); /** * @brief Construct a new LogosAPI instance (const char* overload — resolves ambiguity) */ explicit LogosAPI(const char* module_name, QObject *parent = nullptr) : LogosAPI(QString(module_name), parent) {} /** * @brief Construct with const char* and explicit transport set. */ LogosAPI(const char* module_name, LogosTransportSet transports, QObject *parent = nullptr) : LogosAPI(QString(module_name), std::move(transports), parent) {} /** * @brief Construct a new LogosAPI instance (std::string overload) */ explicit LogosAPI(const std::string& module_name, QObject *parent = nullptr); /** * @brief Construct with std::string and explicit transport set. */ LogosAPI(const std::string& module_name, LogosTransportSet transports, QObject *parent = nullptr) : LogosAPI(QString::fromStdString(module_name), std::move(transports), parent) {} /** * @brief Destructor */ ~LogosAPI(); /** * @brief Get the client provider instance * @return LogosAPIProvider* Pointer to the provider */ LogosAPIProvider* getProvider() const; /** * @brief Get the client instance for communicating with a module * @param target_module The module to communicate with * @return LogosAPIClient* Pointer to the client */ LogosAPIClient* getClient(const QString& target_module) const; /** * @brief Get the client instance — const char* overload (resolves ambiguity) */ LogosAPIClient* getClient(const char* target_module) const { return getClient(QString(target_module)); } /** * @brief Get the client instance for communicating with a module (std::string overload) */ LogosAPIClient* getClient(const std::string& target_module) const; /** * @brief Get a client that uses an *explicit* transport instead of * the process-global default. * * Use this when the caller needs to dial one module over a * particular protocol without side-effecting the rest of the * process. Canonical case: a CLI that talks only to `core_service` * over tcp_ssl — using `LogosTransportConfigGlobal::setDefault` for * that would also flip the same process's `LogosAPIProvider` into * trying to bind a tcp_ssl server, which the CLI has no cert for. * * Cached per (target_module, full LogosTransportConfig) — repeat * calls with the same target *and* the same transport return the * same client. The cache key covers every config field that can * distinguish two clients (protocol, host, port, codec, all TLS * settings), via the operator== / qHash defined alongside * LogosTransportConfig, so two callers with different TLS or codec * settings always get separate clients — no risk of silently * reusing an insecure connection where a secure one was asked for. */ LogosAPIClient* getClient(const QString& target_module, const LogosTransportConfig& transport) const; /** * @brief Get the token manager instance * @return TokenManager* Pointer to the token manager */ TokenManager* getTokenManager() const; /** * @brief Set the transport used by the SDK's auto-`requestModule` * token-fetch flow (inside LogosAPIClient::invokeRemoteMethod{,Async}). * * That flow always dials `capability_module` to fetch a per-target * token, regardless of which module is the actual call target. * Without an explicit transport it falls through to * LogosTransportConfigGlobal::getDefault() (LocalSocket), which * times out 20 s when capability_module is reachable only on TCP * (e.g. CLI on host, daemon in container). * * Callers that have read the daemon's per-module advertised * transports (e.g. from logoscore's daemon.json) should register * capability_module's transport here so getClient builds each * LogosAPIClient with the right capability_consumer. * * The setting only affects clients constructed *after* this call * — clients already in the cache keep whatever capability transport * they were built with. */ void setCapabilityModuleTransport(const LogosTransportConfig& transport); using QObject::setProperty; /** * @brief Set a dynamic property from a UTF-8 std::string (delegates to QVariant + QString). */ bool setProperty(const char* name, const std::string& value); private: QString m_module_name; LogosAPIProvider* m_provider; // Single cache for both getClient overloads. Keyed by a // mode-aware composite (LogosAPIClientCacheKey above) so that: // - Mock/Local mode buckets ignore transport (the factory does too) // - Remote mode keys include the full LogosTransportConfig // - the no-transport overload resolves to the same key as an // explicit caller passing LogosTransportConfigGlobal::getDefault() // - mode switches don't return stale clients from the previous mode mutable QHash m_clients; TokenManager* m_token_manager; // ABI note: this private layout is consumed by every plugin that // statically links libsdk. Inserting a field above m_token_manager // shifts its offset and SILENTLY breaks plugins compiled before // the change — they read garbage where m_token_manager used to // live, getClient() then constructs LogosAPIClients with a bogus // TokenManager*, and the first cross-process call segfaults. // Append new private members at the END only. (Long-term cure: // pimpl this class so sizeof / offsets become opaque.) // // Optional override for the capability_module transport used by // each LogosAPIClient's pre-built m_capability_consumer. Set via // setCapabilityModuleTransport(). nullopt = use the global default. std::optional m_capabilityModuleTransport; }; #endif // LOGOS_API_H