Qt IVI Generator Remote Objects Example
#include "processingservicebackend.h"
#include <QDebug>
#include <QSettings>
#include "remotemodule.h"
ProcessingServiceBackend:: ProcessingServiceBackend(QObject * parent)
: ProcessingServiceBackendInterface(parent)
{
RemoteModule:: registerTypes();
}
ProcessingServiceBackend:: ~ ProcessingServiceBackend()
{
delete m_node;
}
void ProcessingServiceBackend:: initialize()
{
QString configPath(QStringLiteral ("./server.conf" ));
if (qEnvironmentVariableIsSet ("SERVER_CONF_PATH" ))
configPath = QString :: fromLocal8Bit(qgetenv("SERVER_CONF_PATH" ));
else
qDebug () < < "Environment variable SERVER_CONF_PATH not defined, using " < < configPath;
QSettings settings(configPath, QSettings :: IniFormat);
settings. beginGroup(QStringLiteral ("remote" ));
QUrl url = QUrl (settings. value(QStringLiteral ("Registry" ), QStringLiteral ("local:remote" )). toString());
m_node = new QRemoteObjectNode (url);
connect(m_node, & QRemoteObjectNode :: error, this , & ProcessingServiceBackend:: onNodeError);
m_replica. reset(m_node- > acquire< ProcessingServiceReplica> (QStringLiteral ("Example.IVI.Remote.ProcessingService" )));
setupConnections();
emit lastMessageChanged(m_replica- > lastMessage());
if (m_replica- > isInitialized())
emit initializationDone();
}
void ProcessingServiceBackend:: setLastMessage(const QString & lastMessage)
{
m_replica- > pushLastMessage(lastMessage);
}
QIviPendingReply < int > ProcessingServiceBackend:: process(const QString & data)
{
if (m_replica- > state() ! = QRemoteObjectReplica :: Valid)
return QIviPendingReply < int > :: createFailedReply();
QIviPendingReply < int > iviReply;
QRemoteObjectPendingReply < int > reply = m_replica- > process(data);
auto watcher = new QRemoteObjectPendingCallWatcher (reply);
connect(watcher, & QRemoteObjectPendingCallWatcher :: finished, this , [ this , iviReply] (QRemoteObjectPendingCallWatcher * self) mutable {
if (self- > error() = = QRemoteObjectPendingCallWatcher :: NoError) {
iviReply. setSuccess(self- > returnValue(). value< int > ());
} else {
iviReply. setFailed();
emit errorChanged(QIviAbstractFeature :: InvalidOperation, QStringLiteral ("ProcessingServiceBackend, remote call of method process failed" ));
}
self- > deleteLater();
});
return iviReply;
}
void ProcessingServiceBackend:: setupConnections()
{
connect(m_replica. data(), & QRemoteObjectReplica :: initialized, this , & QIviFeatureInterface :: initializationDone);
connect(m_replica. data(), & QRemoteObjectReplica :: stateChanged, this , & ProcessingServiceBackend:: onReplicaStateChanged);
connect(m_replica. data(), & ProcessingServiceReplica:: lastMessageChanged, this , & ProcessingServiceBackend:: lastMessageChanged);
}
void ProcessingServiceBackend:: onReplicaStateChanged(QRemoteObjectReplica :: State newState,
QRemoteObjectReplica :: State oldState)
{
if (newState = = QRemoteObjectReplica :: Suspect) {
qDebug () < < "ProcessingServiceBackend, QRemoteObjectReplica error, connection to the source lost" ;
emit errorChanged(QIviAbstractFeature :: Unknown,
"QRemoteObjectReplica error, connection to the source lost" );
} else if (newState = = QRemoteObjectReplica :: SignatureMismatch) {
qDebug () < < "ProcessingServiceBackend, QRemoteObjectReplica error, signature mismatch" ;
emit errorChanged(QIviAbstractFeature :: Unknown,
"QRemoteObjectReplica error, signature mismatch" );
} else if (newState= = QRemoteObjectReplica :: Valid) {
emit errorChanged(QIviAbstractFeature :: NoError, "" );
}
}
void ProcessingServiceBackend:: onNodeError(QRemoteObjectNode :: ErrorCode code)
{
qDebug () < < "ProcessingServiceBackend, QRemoteObjectNode error, code: " < < code;
emit errorChanged(QIviAbstractFeature :: Unknown, "QRemoteObjectNode error, code: " + code);
}