【问题标题】:What's different QRemoteObjectRegistryHost and QRemoteObjectHost?QRemoteObjectRegistryHost 和 QRemoteObjectHost 有什么不同?
【发布时间】:2020-01-07 09:47:58
【问题描述】:

我是新来学习QRemoteObjects,我了解Direct Connection with a Dynamic Replica的用法。但我不了解Connections to Remote Nodes using a Registry机制。我对QRemoteObjectRegistryHostQRemoteObjectHostQRemoteObjectNode和@之间的关系感到困惑987654327@,谁能给我简单的解释一下?

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

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

【问题讨论】:

    标签: qt qt5 qtremoteobjects


    【解决方案1】:

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

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

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

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

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

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

    ├── 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_())
    

    然后在不同的 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
    # ...
    

    【讨论】:

    • 是否有任何工具或方法可以编写您所写的console markdown 样式?当我不想粘贴图片来说明某些内容时,它可能很有用。
    • @jie 没有,如果有请告诉我,我都是手动完成的。
    猜你喜欢
    • 2011-05-22
    • 2015-11-04
    • 2012-10-30
    • 2019-10-22
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多