2

我是新手QRemoteObjects,我了解直接连接与动态副本的用法。但我不了解使用注册表机制连接到远程节点QRemoteObjectRegistryHost。我对、和之间的关系感到困惑QRemoteObjectHost,谁能给我简单的解释?QRemoteObjectNodeQRemoteObjectReplica

在注册表方法
服务器中使用这样的代码

regNode = QRemoteObjectRegistryHost(QUrl('local:registry'))
srcNode = QRemoteObjectHost(QUrl('local:replica'), QUrl('local:registry'))
#is there will create two Local Socket  server?

客户使用

repNode = QRemoteObjectNode(QUrl('local:registry'))

QUrl('local:registry')和有什么区别QUrl('local:replica')
我认为QRemoteObjectHost(QUrl('local:replica'), QUrl('local:registry'))这种方法是多余的。

4

1 回答 1

2

在您提供的示例中,没有观察到优势,因此您认为它是多余的。

在某些应用程序中,需要有多个源,并且副本必须连接到每个源是多余的,因此 QRemoteObjectRegistryHost 的任务是为多个源提供一个连接点,并通过它连接副本。

例如,以下方案显示了它的用途:

 ┌-------------------┐                ┌-------------------┐
 | QRemoteObjectHost |                | QRemoteObjectHost |
 └--------┬----------┘                └-------┬-----------┘
          |                                   |
          |                                   |
     ┌----┴-----------------------------------┴----┐ 
     |          QRemoteObjectRegistryHost          |
     └--┬-------------------┬-----------------┬----┘
        |                   |                 |
        |                   |                 |
┌-------┴----- ---┐ ┌-------┴---------┐ ┌-----┴------- ---┐
|QRemoteObjectNode| |QRemoteObjectNode| |QRemoteObjectNode|
└-----------------┘ └-----------------┘ └-----------------┘

通过 可以注册多个节点QRemoteObjectHost,并在QRemoteObjectHost中注册,QRemoteObjectRegistryHost以便任何人都可以通过 获取节点QRemoteObjectNode的副本。QRemoteObjectHostQRemoteObjectRegistryHost

为了说明我创建了以下示例的功能:

├── register.py
├── replica.py
└── source.py

注册.py

from PyQt5 import QtCore, QtRemoteObjects


if __name__ == "__main__":
    import sys

    app = QtCore.QCoreApplication(sys.argv)

    regNode = QtRemoteObjects.QRemoteObjectRegistryHost(
        QtCore.QUrl("tcp://127.0.0.1:5557")
    )

    sys.exit(app.exec_())

副本.py

from functools import partial
import sys

from PyQt5 import QtCore, QtRemoteObjects


if __name__ == "__main__":

    app = QtCore.QCoreApplication(sys.argv)

    node = QtRemoteObjects.QRemoteObjectNode(QtCore.QUrl("tcp://127.0.0.1:5557"))

    replicas = []

    def on_remoteObjectAdded(info):
        name, url = info
        print("object added", name, url)
        replica = node.acquireDynamic(name)

        wrapper = partial(on_initialized, replica, name)
        replica.initialized.connect(wrapper)
        replicas.append(replica)

    node.registry().remoteObjectAdded.connect(on_remoteObjectAdded)

    def on_initialized(replica, name):
        wrapper = partial(print, name)
        replica.dataChanged.connect(wrapper)

    sys.exit(app.exec_())

源码.py

import sys

from PyQt5 import QtCore, QtRemoteObjects


class Node(QtCore.QObject):
    dataChanged = QtCore.pyqtSignal(str)


if __name__ == "__main__":

    app = QtCore.QCoreApplication(sys.argv)

    parser = QtCore.QCommandLineParser()
    parser.addPositionalArgument("url", "Host URL different to tcp://127.0.0.1:5557")
    parser.addPositionalArgument("name", "Name of node")
    parser.process(app)
    args = parser.positionalArguments()

    if len(args) != 2:
        print("only url and name is required")
        sys.exit(-1)

    url, name = args

    if QtCore.QUrl("tcp://127.0.0.1:5557") == QtCore.QUrl(url):
        print("url different tcp://127.0.0.1:5557")
        sys.exit(-1)

    node = Node()
    srcNode = QtRemoteObjects.QRemoteObjectHost(
        QtCore.QUrl(url), QtCore.QUrl("tcp://127.0.0.1:5557")
    )
    srcNode.enableRemoting(node, name)

    def on_timeout():
        data = QtCore.QDateTime.currentDateTime().toString()
        node.dataChanged.emit(data)

    timer = QtCore.QTimer(interval=1000, timeout=on_timeout)
    timer.start()

    sys.exit(app.exec_())

然后在不同的 CMD/终端上运行以下命令:

python register.py
python replica.py
python source.py tcp://127.0.0.1:5558 node1
python source.py tcp://127.0.0.1:5559 node2

在 replica.py 的 CMD/终端控制台中,您将看到以下内容:

# ...
node1 Tue Jan 7 22:32:09 2020
node2 Tue Jan 7 22:32:09 2020
node1 Tue Jan 7 22:32:10 2020
node2 Tue Jan 7 22:32:10 2020
# ...
于 2020-01-08T03:35:44.047 回答