五 Example 3: Connections to Remote Nodes using a Registry

QRemoteObjectRegistry类有什么用?可以参考下图:
在这里插入图片描述
在某些应用程序中,需要有多个源,而为每个副本分别连接到每个源则显得很冗余,因此QRemoteObjectRegistryHost的任务是为多个源提供一个连接点,并通过它连接每个副本。

QRemoteObjectRegistry类型的registry属性。而它有两个关键信号:

void remoteObjectAdded(const QRemoteObjectSourceLocation &entry);
void remoteObjectRemoved(const QRemoteObjectSourceLocation &entry);

当带有Registry的QtRO网络中有新的Source被enableRemoting出来,则每个接入的Node的registry属性都会发出remoteObjectAdded这个信号;当有Source被disableRemoting之后,每个接入的Node的registry都会发出remoteObjectRemoved信号。

Source 端代码

simpleswitch.h 和 simpleswitch.cpp 不做修改, 不同之处在于主机节点的创建和连接方式:
main.cpp

  #include <QCoreApplication>
  #include "simpleswitch.h"

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

      SimpleSwitch srcSwitch; // create SimpleSwitch

      QRemoteObjectRegistryHost regNode(QUrl(QStringLiteral("local:registry"))); // create node that hosts registry
      QRemoteObjectHost srcNode(QUrl(QStringLiteral("local:switch")), QUrl(QStringLiteral("local:registry"))); // create node that will host source and connect to registry
      //Note, you can add srcSwitch directly to regNode if desired.
      //We use two Nodes here, as the regNode could easily be in a third process.

      srcNode.enableRemoting(&srcSwitch); // enable remoting of source object

      return a.exec();
  }

Replica 端代码

本例中使用的请求者对象是示例2中讨论的动态副本客户端。

唯一的修改是主要的。cpp:创建注册表节点以获取副本:

QRemoteObjectNode repNode(QUrl(QStringLiteral("local:registry")));

可参考https://zhuanlan.zhihu.com/p/68383336

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值