diff --git a/cpp/logos_api_client.cpp b/cpp/logos_api_client.cpp index d0d06c1..cee77a5 100644 --- a/cpp/logos_api_client.cpp +++ b/cpp/logos_api_client.cpp @@ -15,9 +15,9 @@ LogosAPIClient::~LogosAPIClient() // m_consumer will be deleted automatically as it's a child object } -QObject* LogosAPIClient::requestObject(const QString& objectName, int timeoutMs) +QObject* LogosAPIClient::requestObject(const QString& objectName, Timeout timeout) { - return m_consumer->requestObject(objectName, timeoutMs); + return m_consumer->requestObject(objectName, timeout); } bool LogosAPIClient::isConnected() const @@ -36,7 +36,7 @@ bool LogosAPIClient::reconnect() } QVariant LogosAPIClient::invokeRemoteMethod(const QString& objectName, const QString& methodName, - const QVariantList& args, int timeoutMs) + const QVariantList& args, Timeout timeout) { qDebug() << "LogosAPIClient: invoking remote method" << objectName << methodName << args; @@ -47,7 +47,7 @@ QVariant LogosAPIClient::invokeRemoteMethod(const QString& objectName, const QSt qDebug() << "LogosAPIClient: calling requestModule for" << objectName; LogosAPIConsumer* packageManagerConsumer = new LogosAPIConsumer("capability_module", m_origin_module, m_token_manager, this); QString capabilityToken = getToken("capability_module"); - QVariant result = packageManagerConsumer->invokeRemoteMethod(capabilityToken, "capability_module", "requestModule", QVariantList() << m_origin_module << objectName, timeoutMs); + QVariant result = packageManagerConsumer->invokeRemoteMethod(capabilityToken, "capability_module", "requestModule", QVariantList() << m_origin_module << objectName, timeout); qDebug() << "================================================"; qDebug() << "================================================"; qDebug() << "================================================"; @@ -64,39 +64,39 @@ QVariant LogosAPIClient::invokeRemoteMethod(const QString& objectName, const QSt token = result.toString(); } - return m_consumer->invokeRemoteMethod(token, objectName, methodName, args, timeoutMs); + return m_consumer->invokeRemoteMethod(token, objectName, methodName, args, timeout); } QVariant LogosAPIClient::invokeRemoteMethod(const QString& objectName, const QString& methodName, - const QVariant& arg, int timeoutMs) + const QVariant& arg, Timeout timeout) { - return invokeRemoteMethod(objectName, methodName, QVariantList() << arg, timeoutMs); + return invokeRemoteMethod(objectName, methodName, QVariantList() << arg, timeout); } QVariant LogosAPIClient::invokeRemoteMethod(const QString& objectName, const QString& methodName, - const QVariant& arg1, const QVariant& arg2, int timeoutMs) + const QVariant& arg1, const QVariant& arg2, Timeout timeout) { - return invokeRemoteMethod(objectName, methodName, QVariantList() << arg1 << arg2, timeoutMs); + return invokeRemoteMethod(objectName, methodName, QVariantList() << arg1 << arg2, timeout); } QVariant LogosAPIClient::invokeRemoteMethod(const QString& objectName, const QString& methodName, - const QVariant& arg1, const QVariant& arg2, const QVariant& arg3, int timeoutMs) + const QVariant& arg1, const QVariant& arg2, const QVariant& arg3, Timeout timeout) { - return invokeRemoteMethod(objectName, methodName, QVariantList() << arg1 << arg2 << arg3, timeoutMs); + return invokeRemoteMethod(objectName, methodName, QVariantList() << arg1 << arg2 << arg3, timeout); } QVariant LogosAPIClient::invokeRemoteMethod(const QString& objectName, const QString& methodName, const QVariant& arg1, const QVariant& arg2, const QVariant& arg3, - const QVariant& arg4, int timeoutMs) + const QVariant& arg4, Timeout timeout) { - return invokeRemoteMethod(objectName, methodName, QVariantList() << arg1 << arg2 << arg3 << arg4, timeoutMs); + return invokeRemoteMethod(objectName, methodName, QVariantList() << arg1 << arg2 << arg3 << arg4, timeout); } QVariant LogosAPIClient::invokeRemoteMethod(const QString& objectName, const QString& methodName, const QVariant& arg1, const QVariant& arg2, const QVariant& arg3, - const QVariant& arg4, const QVariant& arg5, int timeoutMs) + const QVariant& arg4, const QVariant& arg5, Timeout timeout) { - return invokeRemoteMethod(objectName, methodName, QVariantList() << arg1 << arg2 << arg3 << arg4 << arg5, timeoutMs); + return invokeRemoteMethod(objectName, methodName, QVariantList() << arg1 << arg2 << arg3 << arg4 << arg5, timeout); } void LogosAPIClient::onEvent(QObject* originObject, QObject* destinationObject, const QString& eventName, std::function callback) diff --git a/cpp/logos_api_client.h b/cpp/logos_api_client.h index b218cdf..a744bce 100644 --- a/cpp/logos_api_client.h +++ b/cpp/logos_api_client.h @@ -8,6 +8,8 @@ #include #include +#include "logos_mode.h" + class LogosAPIConsumer; class TokenManager; @@ -40,10 +42,10 @@ public: /** * @brief Request a remote object replica by name * @param objectName The name of the remote object to acquire - * @param timeoutMs Timeout in milliseconds to wait for the replica to be ready + * @param timeout Timeout to wait for the replica to be ready (default 20000ms) * @return QObject* pointer to the replica, or nullptr if failed */ - QObject* requestObject(const QString& objectName, int timeoutMs = 20000); + QObject* requestObject(const QString& objectName, Timeout timeout = Timeout()); /** * @brief Check if the client is connected to the registry @@ -68,22 +70,22 @@ public: * @param objectName The name of the remote object * @param methodName The name of the method to call * @param args Arguments to pass to the method - * @param timeoutMs Timeout in milliseconds to wait for the result + * @param timeout Timeout to wait for the result (default 20000ms) * @return QVariant containing the result, or invalid QVariant if failed */ QVariant invokeRemoteMethod(const QString& objectName, const QString& methodName, - const QVariantList& args = QVariantList(), int timeoutMs = 20000); + const QVariantList& args = QVariantList(), Timeout timeout = Timeout()); /** * @brief Invoke a remote method on a remote object with a single argument * @param objectName The name of the remote object * @param methodName The name of the method to call * @param arg Argument to pass to the method - * @param timeoutMs Timeout in milliseconds to wait for the result + * @param timeout Timeout to wait for the result (default 20000ms) * @return QVariant containing the result, or invalid QVariant if failed */ QVariant invokeRemoteMethod(const QString& objectName, const QString& methodName, - const QVariant& arg, int timeoutMs = 20000); + const QVariant& arg, Timeout timeout = Timeout()); /** * @brief Invoke a remote method on a remote object with two arguments @@ -91,11 +93,11 @@ public: * @param methodName The name of the method to call * @param arg1 First argument to pass to the method * @param arg2 Second argument to pass to the method - * @param timeoutMs Timeout in milliseconds to wait for the result + * @param timeout Timeout to wait for the result (default 20000ms) * @return QVariant containing the result, or invalid QVariant if failed */ QVariant invokeRemoteMethod(const QString& objectName, const QString& methodName, - const QVariant& arg1, const QVariant& arg2, int timeoutMs = 20000); + const QVariant& arg1, const QVariant& arg2, Timeout timeout = Timeout()); /** * @brief Invoke a remote method on a remote object with three arguments @@ -104,11 +106,11 @@ public: * @param arg1 First argument to pass to the method * @param arg2 Second argument to pass to the method * @param arg3 Third argument to pass to the method - * @param timeoutMs Timeout in milliseconds to wait for the result + * @param timeout Timeout to wait for the result (default 20000ms) * @return QVariant containing the result, or invalid QVariant if failed */ QVariant invokeRemoteMethod(const QString& objectName, const QString& methodName, - const QVariant& arg1, const QVariant& arg2, const QVariant& arg3, int timeoutMs = 20000); + const QVariant& arg1, const QVariant& arg2, const QVariant& arg3, Timeout timeout = Timeout()); /** * @brief Invoke a remote method on a remote object with four arguments @@ -118,12 +120,12 @@ public: * @param arg2 Second argument to pass to the method * @param arg3 Third argument to pass to the method * @param arg4 Fourth argument to pass to the method - * @param timeoutMs Timeout in milliseconds to wait for the result + * @param timeout Timeout to wait for the result (default 20000ms) * @return QVariant containing the result, or invalid QVariant if failed */ QVariant invokeRemoteMethod(const QString& objectName, const QString& methodName, const QVariant& arg1, const QVariant& arg2, const QVariant& arg3, - const QVariant& arg4, int timeoutMs = 20000); + const QVariant& arg4, Timeout timeout = Timeout()); /** * @brief Invoke a remote method on a remote object with five arguments @@ -134,12 +136,12 @@ public: * @param arg3 Third argument to pass to the method * @param arg4 Fourth argument to pass to the method * @param arg5 Fifth argument to pass to the method - * @param timeoutMs Timeout in milliseconds to wait for the result + * @param timeout Timeout to wait for the result (default 20000ms) * @return QVariant containing the result, or invalid QVariant if failed */ QVariant invokeRemoteMethod(const QString& objectName, const QString& methodName, const QVariant& arg1, const QVariant& arg2, const QVariant& arg3, - const QVariant& arg4, const QVariant& arg5, int timeoutMs = 20000); + const QVariant& arg4, const QVariant& arg5, Timeout timeout = Timeout()); /** * @brief Register an event listener for the specified event name diff --git a/cpp/logos_api_consumer.cpp b/cpp/logos_api_consumer.cpp index da18318..03d5b90 100644 --- a/cpp/logos_api_consumer.cpp +++ b/cpp/logos_api_consumer.cpp @@ -41,7 +41,7 @@ LogosAPIConsumer::~LogosAPIConsumer() // QRemoteObjectNode will be deleted automatically as it's a child object } -QObject* LogosAPIConsumer::requestObject(const QString& objectName, int timeoutMs) +QObject* LogosAPIConsumer::requestObject(const QString& objectName, Timeout timeout) { qDebug() << "LogosAPIConsumer: Requesting object:" << objectName << "at" << QTime::currentTime().toString("hh:mm:ss.zzz"); @@ -75,7 +75,7 @@ QObject* LogosAPIConsumer::requestObject(const QString& objectName, int timeoutM } // Wait for the replica to be initialized - if (!replica->waitForSource(timeoutMs)) { + if (!replica->waitForSource(timeout.ms)) { qWarning() << "LogosAPIConsumer: Timeout waiting for object replica to be ready:" << objectName; delete replica; return nullptr; @@ -151,13 +151,13 @@ bool LogosAPIConsumer::connectToRegistry() QVariant LogosAPIConsumer::invokeRemoteMethod(const QString& authToken, const QString& objectName, const QString& methodName, - const QVariantList& args, int timeoutMs) + const QVariantList& args, Timeout timeout) { - qDebug() << "LogosAPIConsumer: Calling invokeRemoteMethod with params:" << authToken << objectName << methodName << args << timeoutMs; + qDebug() << "LogosAPIConsumer: Calling invokeRemoteMethod with params:" << authToken << objectName << methodName << args << timeout.ms; // This method handles both ModuleProxy-wrapped modules (template_module, package_manager) // and direct remote object calls for other modules - QObject* plugin = requestObject(objectName, timeoutMs); + QObject* plugin = requestObject(objectName, timeout); if (!plugin) { qWarning() << "LogosAPIConsumer: Failed to acquire plugin/replica for object:" << objectName; return QVariant(); @@ -196,7 +196,7 @@ QVariant LogosAPIConsumer::invokeRemoteMethod(const QString& authToken, const QS } // Wait for the result - pendingCall.waitForFinished(timeoutMs); + pendingCall.waitForFinished(timeout.ms); delete plugin; if (!pendingCall.isFinished() || pendingCall.error() != QRemoteObjectPendingCall::NoError) { @@ -264,7 +264,7 @@ bool LogosAPIConsumer::informModuleToken(const QString& authToken, const QString { qDebug() << "LogosAPIConsumer: Informing module token for module:" << moduleName << "with token:" << token; - QObject* plugin = requestObject("capability_module", 20000); + QObject* plugin = requestObject("capability_module", Timeout(20000)); if (!plugin) { qWarning() << "LogosAPIConsumer: Failed to acquire plugin/replica for object: capability_module"; return false; @@ -320,7 +320,7 @@ bool LogosAPIConsumer::informModuleToken_module(const QString& authToken, const { qDebug() << "LogosAPIConsumer: Informing module token for module:" << moduleName << "with token:" << token; - QObject* plugin = requestObject(originModule, 20000); + QObject* plugin = requestObject(originModule, Timeout(20000)); if (!plugin) { qWarning() << "LogosAPIConsumer: Failed to acquire plugin/replica for object:" << originModule; return false; diff --git a/cpp/logos_api_consumer.h b/cpp/logos_api_consumer.h index 9716ecc..77b5e8e 100644 --- a/cpp/logos_api_consumer.h +++ b/cpp/logos_api_consumer.h @@ -45,10 +45,10 @@ public: /** * @brief Request a remote object replica by name * @param objectName The name of the remote object to acquire - * @param timeoutMs Timeout in milliseconds to wait for the replica to be ready + * @param timeout Timeout to wait for the replica to be ready (default 20000ms) * @return QObject* pointer to the replica, or nullptr if failed */ - QObject* requestObject(const QString& objectName, int timeoutMs = 20000); + QObject* requestObject(const QString& objectName, Timeout timeout = Timeout()); /** * @brief Check if the consumer is connected to the registry @@ -74,11 +74,11 @@ public: * @param objectName The name of the remote object * @param methodName The name of the method to call * @param args Arguments to pass to the method - * @param timeoutMs Timeout in milliseconds to wait for the result + * @param timeout Timeout to wait for the result (default 20000ms) * @return QVariant containing the result, or invalid QVariant if failed */ QVariant invokeRemoteMethod(const QString& authToken, const QString& objectName, const QString& methodName, - const QVariantList& args = QVariantList(), int timeoutMs = 20000); + const QVariantList& args = QVariantList(), Timeout timeout = Timeout()); /** * @brief Register an event listener for the specified event name diff --git a/cpp/logos_mode.h b/cpp/logos_mode.h index 1511e18..e3fde1a 100644 --- a/cpp/logos_mode.h +++ b/cpp/logos_mode.h @@ -14,6 +14,20 @@ enum class LogosMode { Local // Use in-process PluginRegistry }; +/** + * @brief Timeout provides a strongly-typed wrapper for timeout values + * + * This prevents implicit conversion from int, avoiding ambiguity with + * QVariant parameters in method overloads. + * + * Example usage: + * invokeRemoteMethod("module", "method", arg1, arg2, Timeout(5000)); + */ +struct Timeout { + int ms; + explicit Timeout(int milliseconds = 20000) : ms(milliseconds) {} +}; + /** * @brief LogosModeConfig provides global mode configuration *