QRemoteObjectRegistryHost 和 QRemoteObjectHost 有什么不同?

What's different QRemoteObjectRegistryHost and QRemoteObjectHost?

我是新手QRemoteObjects,我了解Direct Connection with a Dynamic Replica.But i don't understand Connections to Remote Nodes using a Registry的用法 mechanism.I 搞不清楚QRemoteObjectRegistryHostQRemoteObjectHost、[=15之间的关系=]和QRemoteObjectReplica,谁能给我简单的解释一下?

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

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')) 在这种方法中是多余的。

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

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

例如下面的方案展示了它的用法:

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

可以通过QRemoteObjectHost注册多个节点,QRemoteObjectHost注册在QRemoteObjectRegistryHost中,这样任何QRemoteObjectNode都可以获得[=20=的副本] 节点通过 QRemoteObjectRegistryHost.

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

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

register.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_())

replica.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_())

source.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_())

然后运行不同CMDs/terminals以下命令:

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/terminal 控制台中,您将看到以下内容:

# ...
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
# ...