mirror of
https://github.com/logos-co/logos-cpp-sdk.git
synced 2026-08-30 09:11:13 +00:00
* refactor: abstract connection/transport; and clearly separate qt remote obj and qt local into separate implementations * abstract qt remote registry * add mock implementation; these serves to further test the abstraction but also useful for testing modules later * use LogosObject instead of QObject * abstract provider side * updates to use new api * re-add async api back --------- Co-authored-by: Logos Workspace <logos@workspace.local>
153 lines
5.8 KiB
C++
153 lines
5.8 KiB
C++
#include "logos_api_consumer.h"
|
|
#include "logos_object.h"
|
|
#include "module_proxy.h"
|
|
#include "logos_api_client.h"
|
|
#include "token_manager.h"
|
|
#include "logos_mode.h"
|
|
#include "logos_instance.h"
|
|
#include "logos_transport.h"
|
|
#include "logos_transport_factory.h"
|
|
#include <QDebug>
|
|
#include <QUrl>
|
|
#include <QMetaObject>
|
|
#include <QTimer>
|
|
#include <QTime>
|
|
|
|
LogosAPIConsumer::LogosAPIConsumer(const QString& module_to_talk_to, const QString& origin_module, TokenManager* token_manager, QObject *parent)
|
|
: QObject(parent)
|
|
, m_registryUrl(LogosInstance::id(module_to_talk_to))
|
|
, m_token_manager(token_manager)
|
|
{
|
|
m_transport = LogosTransportFactory::createConnection(m_registryUrl);
|
|
m_transport->connectToHost();
|
|
}
|
|
|
|
LogosAPIConsumer::~LogosAPIConsumer()
|
|
{
|
|
}
|
|
|
|
LogosObject* LogosAPIConsumer::requestObject(const QString& objectName, Timeout timeout)
|
|
{
|
|
qDebug() << "LogosAPIConsumer: Requesting object:" << objectName << "at" << QTime::currentTime().toString("hh:mm:ss.zzz");
|
|
|
|
if (objectName.isEmpty()) {
|
|
qWarning() << "LogosAPIConsumer: Object name cannot be empty";
|
|
return nullptr;
|
|
}
|
|
|
|
if (!m_transport->isConnected()) {
|
|
qWarning() << "LogosAPIConsumer: Not connected to registry. Cannot request object:" << objectName;
|
|
return nullptr;
|
|
}
|
|
|
|
LogosObject* object = m_transport->requestObject(objectName, timeout.ms);
|
|
if (object) {
|
|
qDebug() << "[LogosObject] LogosAPIConsumer: acquired LogosObject for:" << objectName << "(id:" << object->id() << ")";
|
|
}
|
|
return object;
|
|
}
|
|
|
|
bool LogosAPIConsumer::isConnected() const
|
|
{
|
|
return m_transport->isConnected();
|
|
}
|
|
|
|
QString LogosAPIConsumer::registryUrl() const
|
|
{
|
|
return m_registryUrl;
|
|
}
|
|
|
|
bool LogosAPIConsumer::reconnect()
|
|
{
|
|
qDebug() << "LogosAPIConsumer: Attempting to reconnect to registry:" << m_registryUrl;
|
|
return m_transport->reconnect();
|
|
}
|
|
|
|
QVariant LogosAPIConsumer::invokeRemoteMethod(const QString& authToken, const QString& objectName, const QString& methodName,
|
|
const QVariantList& args, Timeout timeout)
|
|
{
|
|
qDebug() << "LogosAPIConsumer: Calling invokeRemoteMethod:" << objectName << methodName << "args_count:" << args.size() << "timeout:" << timeout.ms;
|
|
|
|
LogosObject* plugin = m_transport->requestObject(objectName, timeout.ms);
|
|
if (!plugin) {
|
|
qWarning() << "LogosAPIConsumer: Failed to acquire plugin/replica for object:" << objectName;
|
|
return QVariant();
|
|
}
|
|
|
|
qDebug() << "[LogosObject] LogosAPIConsumer: calling via LogosObject::callMethod" << methodName;
|
|
QVariant result = plugin->callMethod(authToken, methodName, args, timeout.ms);
|
|
plugin->release();
|
|
return result;
|
|
}
|
|
|
|
void LogosAPIConsumer::invokeRemoteMethodAsync(const QString& authToken, const QString& objectName, const QString& methodName,
|
|
const QVariantList& args,
|
|
AsyncResultCallback callback,
|
|
Timeout timeout)
|
|
{
|
|
if (!callback) {
|
|
qWarning() << "LogosAPIConsumer: invokeRemoteMethodAsync called with null callback";
|
|
return;
|
|
}
|
|
|
|
LogosObject* plugin = m_transport->requestObject(objectName, timeout.ms);
|
|
if (!plugin) {
|
|
qWarning() << "LogosAPIConsumer: Failed to acquire plugin/replica for object:" << objectName;
|
|
QTimer::singleShot(0, this, [callback]() { callback(QVariant()); });
|
|
return;
|
|
}
|
|
|
|
qDebug() << "[LogosObject] LogosAPIConsumer: async calling via LogosObject::callMethod" << methodName;
|
|
QVariant result = plugin->callMethod(authToken, methodName, args, timeout.ms);
|
|
plugin->release();
|
|
QTimer::singleShot(0, this, [callback, result]() { callback(result); });
|
|
}
|
|
|
|
void LogosAPIConsumer::onEvent(LogosObject* originObject, const QString& eventName, std::function<void(const QString&, const QVariantList&)> callback)
|
|
{
|
|
qDebug() << "[LogosObject] LogosAPIConsumer::onEvent registering for:" << eventName << "on LogosObject id:" << originObject;
|
|
|
|
if (!originObject) {
|
|
qWarning() << "LogosAPIConsumer: Cannot register event on null object";
|
|
return;
|
|
}
|
|
|
|
originObject->onEvent(eventName, std::move(callback));
|
|
|
|
qDebug() << "[LogosObject] LogosAPIConsumer: event callback registered for:" << eventName;
|
|
}
|
|
|
|
bool LogosAPIConsumer::informModuleToken(const QString& authToken, const QString& moduleName, const QString& token)
|
|
{
|
|
qDebug() << "LogosAPIConsumer: Informing module token for module:" << moduleName << "with token:" << token;
|
|
|
|
LogosObject* plugin = m_transport->requestObject("capability_module", 20000);
|
|
if (!plugin) {
|
|
qWarning() << "LogosAPIConsumer: Failed to acquire plugin/replica for object: capability_module";
|
|
return false;
|
|
}
|
|
|
|
qDebug() << "[LogosObject] LogosAPIConsumer: calling LogosObject::informModuleToken for" << moduleName;
|
|
bool result = plugin->informModuleToken(authToken, moduleName, token, 20000);
|
|
qDebug() << "LogosAPIConsumer: informModuleToken completed with result:" << result;
|
|
plugin->release();
|
|
return result;
|
|
}
|
|
|
|
bool LogosAPIConsumer::informModuleToken_module(const QString& authToken, const QString& originModule, const QString& moduleName, const QString& token)
|
|
{
|
|
qDebug() << "LogosAPIConsumer: Informing module token for module:" << moduleName << "with token:" << token;
|
|
|
|
LogosObject* plugin = m_transport->requestObject(originModule, 20000);
|
|
if (!plugin) {
|
|
qWarning() << "LogosAPIConsumer: Failed to acquire plugin/replica for object:" << originModule;
|
|
return false;
|
|
}
|
|
|
|
qDebug() << "[LogosObject] LogosAPIConsumer: calling LogosObject::informModuleToken for" << moduleName << "on" << originModule;
|
|
bool result = plugin->informModuleToken(authToken, moduleName, token, 20000);
|
|
qDebug() << "LogosAPIConsumer: informModuleToken completed with result:" << result;
|
|
plugin->release();
|
|
return result;
|
|
}
|