mirror of
https://github.com/logos-co/logos-protocol.git
synced 2026-08-30 21:41:10 +00:00
Acquiring a QtRO replica per call (acquireDynamic + waitForSource) is expensive:
under a tight loop — e.g. a proxy forwarding every method to its target, or a UI
backend driving a whole surface — it dominates and can even starve the nested
synchronous calls. Cache the LogosObject handle per object name in m_objectCache
and reuse it across calls (both the sync invokeRemoteMethod and the async
invokeRemoteMethodAsync paths); no per-call release(). A stale handle (source
went away — module unloaded / transport dropped) is detected via a new
LogosObject::isValid() (QtRO replica state == Valid) and transparently
re-acquired. The cache is released in clearObjectCache() from the destructor and
before reconnect().
- logos_object.h: add virtual bool isValid() (default true).
- qt_remote/remote_transport.{h,cpp}: RemoteLogosObject::isValid() (replica
Valid state) + a process-wide acquireCount() test hook.
- logos_api_consumer.{h,cpp}: m_objectCache + acquireCachedObject()/
clearObjectCache(); sync + async reuse the cached handle; async keeps the
QPointer guard and never releases the shared handle from its callback.
Test: RemoteEventTest.ConsumerReusesCachedHandleAcrossSyncAndAsyncCalls publishes
a provider over the qt_remote host, does 12 sync + 12 async echo calls, and
asserts every result is correct AND acquireCount() == 1 (one replica for all 24
calls). 164/164 green.
130 lines
4.6 KiB
C++
130 lines
4.6 KiB
C++
#ifndef LOGOS_OBJECT_H
|
|
#define LOGOS_OBJECT_H
|
|
|
|
#include <QString>
|
|
#include <QVariant>
|
|
#include <QVariantList>
|
|
#include <QJsonArray>
|
|
#include <functional>
|
|
#include <cstdint>
|
|
|
|
/**
|
|
* @brief Abstract interface for a module object handle.
|
|
*
|
|
* LogosObject decouples callers from the underlying transport mechanism.
|
|
* Each transport (local/Qt Remote Objects/mock/JSON-RPC/...) provides its
|
|
* own concrete subclass. Callers interact exclusively through this
|
|
* interface and never need to know the implementation type.
|
|
*/
|
|
class LogosObject {
|
|
public:
|
|
virtual ~LogosObject() = default;
|
|
|
|
/**
|
|
* @brief Invoke a method on the remote/local module.
|
|
* @param authToken Authentication token for the operation
|
|
* @param methodName Method to call on the underlying module
|
|
* @param args Arguments for the method
|
|
* @param timeoutMs Maximum time to wait for the result
|
|
* @return The method result, or an invalid QVariant on failure
|
|
*/
|
|
virtual QVariant callMethod(const QString& authToken,
|
|
const QString& methodName,
|
|
const QVariantList& args,
|
|
int timeoutMs) = 0;
|
|
|
|
using AsyncResultCallback = std::function<void(QVariant)>;
|
|
|
|
/**
|
|
* @brief Invoke a method asynchronously; result is delivered via callback.
|
|
*
|
|
* Returns immediately. The callback is always invoked on a subsequent
|
|
* event-loop iteration, never synchronously inside this call.
|
|
*
|
|
* @param authToken Authentication token for the operation
|
|
* @param methodName Method to call on the underlying module
|
|
* @param args Arguments for the method
|
|
* @param timeoutMs Maximum time to wait for the result
|
|
* @param callback Called with the result (invalid QVariant on failure/timeout)
|
|
*/
|
|
virtual void callMethodAsync(const QString& authToken,
|
|
const QString& methodName,
|
|
const QVariantList& args,
|
|
int timeoutMs,
|
|
AsyncResultCallback callback) = 0;
|
|
|
|
/**
|
|
* @brief Deliver a module token to the underlying module.
|
|
* @param authToken Authentication token for the operation
|
|
* @param moduleName Target module name
|
|
* @param token The token to deliver
|
|
* @param timeoutMs Maximum time to wait for the result
|
|
* @return true if the token was delivered successfully
|
|
*/
|
|
virtual bool informModuleToken(const QString& authToken,
|
|
const QString& moduleName,
|
|
const QString& token,
|
|
int timeoutMs) = 0;
|
|
|
|
using EventCallback = std::function<void(const QString&, const QVariantList&)>;
|
|
|
|
/**
|
|
* @brief Subscribe to events from this object.
|
|
*
|
|
* Qt-based implementations use QObject::connect internally;
|
|
* other implementations may use a different mechanism.
|
|
*
|
|
* @param eventName The event name to listen for
|
|
* @param callback Called when the event fires
|
|
*/
|
|
virtual void onEvent(const QString& eventName, EventCallback callback) = 0;
|
|
|
|
/**
|
|
* @brief Remove all event subscriptions made via onEvent().
|
|
*/
|
|
virtual void disconnectEvents() = 0;
|
|
|
|
/**
|
|
* @brief Emit an event on this object.
|
|
*
|
|
* For Qt-based implementations this triggers the underlying
|
|
* QObject signal so that Qt Remote Objects can replicate it.
|
|
*
|
|
* @param eventName The event name
|
|
* @param data Event payload
|
|
*/
|
|
virtual void emitEvent(const QString& eventName, const QVariantList& data) = 0;
|
|
|
|
/**
|
|
* @brief Return introspection data for the methods exposed by
|
|
* the underlying module.
|
|
*/
|
|
virtual QJsonArray getMethods() = 0;
|
|
|
|
/**
|
|
* @brief Release resources associated with this handle.
|
|
*
|
|
* After calling release() the object must not be used again.
|
|
* Implementations that own the underlying resource (e.g. a
|
|
* QRemoteObjectReplica) will delete it here.
|
|
*/
|
|
virtual void release() = 0;
|
|
|
|
/**
|
|
* @brief Stable identity value suitable for use as a hash key.
|
|
*/
|
|
virtual quintptr id() const = 0;
|
|
|
|
/**
|
|
* @brief Whether this handle is still usable for calls.
|
|
*
|
|
* A cached handle can go stale (e.g. its QRemoteObjectReplica lost its
|
|
* source when the target module unloaded). Callers that keep a handle
|
|
* across calls should re-acquire when this returns false. Non-owning or
|
|
* always-live implementations may keep the default.
|
|
*/
|
|
virtual bool isValid() const { return true; }
|
|
};
|
|
|
|
#endif // LOGOS_OBJECT_H
|