Vladimir Bershov
Vladimir Bershov

Reputation: 2832

Make Peer-to-Peer communication on Qt Remote Objects

I want to make simple communication example on Qt Remote Objects. I want to make the communication peer-to-peer, therefore I'm trying to merge both Source and Replica of the same remote object functionality in one application (REPC_MERGED tool used to generate Source and Replica base classes).

#include <QtCore/QCoreApplication>

#include "MyPeerHost.h"
#include "Client.h"

#include <QDebug>

static QString peer_node_name(int number)
{
    QString ret = QString("peer_%1").arg(number);

    return ret;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    MyPeerHost peerHost; // just inherits auto-generated MyPeerSimpleSource

    QUrl thisAddress = "local:" + peer_node_name(0);

    QRemoteObjectHost sourceNode(thisAddress);

    if(sourceNode.enableRemoting(&peerHost))
    {
        qInfo() << "Source remoting enabled successfully" << thisAddress;

        QUrl remoteAddress = "local:" + peer_node_name(1);

        QSharedPointer<MyPeerReplica> replica;

        QRemoteObjectNode replicaNode;

        if(replicaNode.connectToNode(remoteAddress))
        {
            qInfo() << "Replica connected to the address" << remoteAddress << "successfully";

            replica.reset(replicaNode.acquire<MyPeerReplica>());

            QString sourceClassName = peerHost.staticMetaObject.className();

            qDebug() << "Replica wait for Source" << sourceClassName << "...";

            if(replica->waitForSource(1000))
            {
                qInfo() << "Replica object completely initialized";

                Client client;
                client.setReplicaObject(replica);

                client.sendMessage("AAA");
            }
            else
            {
                qCritical() << "Replica wait for Source" << sourceClassName << "FAILED" << replicaNode.lastError();
            }
        }
        else
        {
            qCritical() << "Replica connect to the address" << remoteAddress << "FAILED" << replicaNode.lastError();
        }
    }
    else
    {
        qCritical() << "Source remoting enable FAILED" << sourceNode.lastError();
    }

    return a.exec();
}

Application output:

Source remoting enabled successfully QUrl("local:peer_0")
Replica connected to the address QUrl("local:peer_1") successfully
Replica wait for Source "MyPeerHost" ...
Replica wait for Source "MyPeerHost" FAILED QRemoteObjectNode::NoError

As you see, replicaNode successfully connected to the non-existent node QUrl("local:peer_1").
What I am doing wrong?

Upvotes: 3

Views: 768

Answers (1)

Brett Stottlemyer
Brett Stottlemyer

Reputation: 2852

You don't have valid Qt code.

Qt relies on an event loop to handle asynchronous behavior, which is started by the a.exec() at the end of your main() routine. Qt Remote Objects, in turn, relies on the event loop for all of its communication.

In your code, you create your objects on the stack, but in code blocks that go out of scope before you start the event loop. They will therefore be destructed before the event loop is kicked off.

I'd recommend starting with some of the examples, make sure they work, then grow what you are trying to do from there.

Upvotes: 0

Related Questions