三 、官方示例 directconnectclient(simpleSwitch)


本示例采用了直接连接,并用了静态的Rep文件

Source 代码

1、创建源对象

To create this Source object, first we create the definition file, simpleswitch.rep. This file describes the properties and methods for the object and is input to the Qt Remote Objects Compiler repc. This file only defines interfaces that are necessary to expose to the Replicas.
要创建这个源对象,首先创建定义文件simpleswitch.rep。该文件描述对象的属性和方法,并输入到Qt repc编译器。此文件仅定义Replicas副本所需的接口。

simpleswitch.rep

  class SimpleSwitch
  {
      PROP(bool currState=false);
      SLOT(server_slot(bool clientState));
  };

In simpleswitch.rep,
currState 保存当前switch的开关状态.
server_slot() 链接到 echoSwitchState(bool newstate) 信号。
只有在Pro文件中,添加下面这句话,rep文件才会被Qt的repc编译器处理:

  REPC_SOURCE = simpleswitch.rep

REPC_SOURCE 包含在 Qt Remote Objects模块中:

  QT       += remoteobjects

repc创建rep_SimpleSwitch_source.h头文件。repc创建了三个用于QtRO的相关类。对于本例,我们使用基本的:SimpleSwitchSimpleSource。它是一个抽象类,在rep_SimpleSwitch_source.h中定义, 我们从中派生出SimpleSwitch实现类:

simpleswitch.h

  #ifndef SIMPLESWITCH_H
  #define SIMPLESWITCH_H

  #include "rep_SimpleSwitch_source.h"

  class SimpleSwitch : public SimpleSwitchSimpleSource
  {
      Q_OBJECT
  public:
      SimpleSwitch(QObject *parent = nullptr);
      ~SimpleSwitch();
      virtual void server_slot(bool clientState);
  public Q_SLOTS:
      void timeout_slot();
  private:
      QTimer *stateChangeTimer;
  };

  #endif

在simpleswitch.h中

stateChangeTimer是一个用于切换SimpleSwitch状态的QTimer。
timeout_slot()连接到stateChangeTimer的timeout()信号。
server_slot()——每当任何副本调用时,都会在源上自动调用它
currStateChanged(bool),在repc生成的rep_SimpleSwitch.h, 每当currState切换时都会发出。在本例中,我们忽略源端的信号,然后在副本端处理它。

simpleswitch.cpp

#include "simpleswitch.h"

  // constructor
  SimpleSwitch::SimpleSwitch(QObject *parent) : SimpleSwitchSimpleSource(parent)
  {
      stateChangeTimer = new QTimer(this); // Initialize timer
      QObject::connect(stateChangeTimer, SIGNAL(timeout()), this, SLOT(timeout_slot())); // connect timeout() signal from stateChangeTimer to timeout_slot() of simpleSwitch
      stateChangeTimer->start(2000); // Start timer and set timout to 2 seconds
      qDebug() << "Source Node Started";
  }

  //destructor
  SimpleSwitch::~SimpleSwitch()
  {
      stateChangeTimer->stop();
  }

  void SimpleSwitch::server_slot(bool clientState)
  {
      qDebug() << "Replica state is " << clientState; // print switch state echoed back by client
  }

  void SimpleSwitch::timeout_slot()
  {
      // slot called on timer timeout
      if (currState()) // check if current state is true, currState() is defined in repc generated rep_SimpleSwitch_source.h
          setCurrState(false); // set state to false
      else
          setCurrState(true); // set state to true
      qDebug() << "Source State is "<<currState();

  }

2 创建registry

本例中用到的是直接连接,所以省略此步骤

3 创建主机节点

主机节点的创建过程如下:

  QRemoteObjectHost srcNode(QUrl(QStringLiteral("local:switch")));

4 共享主机节点到QtRo

共享主机节点到QtRo网络:

  SimpleSwitch srcSwitch; // create simple switch
  srcNode.enableRemoting(&srcSwitch); // enable remoting

main.cpp

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

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

      SimpleSwitch srcSwitch; // create simple switch

      QRemoteObjectHost srcNode(QUrl(QStringLiteral("local:switch"))); // create host node without Registry
      srcNode.enableRemoting(&srcSwitch); // enable remoting/sharing

      return a.exec();
  }

编译并运行这个源代码端项目。在不创建任何复制副本的情况下,输出应如下所示,开关状态每两秒在true和false之间切换

在这里插入图片描述

接下来的步骤是创建网络的副本端,在本例中,副本端从源获取switch状态并将其返回给主机节点。

Replica 代码

1 使用repc将副本添加到项目中

我们使用与源端相同的rep文件,SimpleSwitch.rep

  REPC_REPLICA = simpleswitch.rep

2 创建一个节点以连接源的主机节点

实例化网络上的第二个节点,并将其与源主机节点连接:

  QRemoteObjectNode repNode; // create remote object node
  repNode.connectToNode(QUrl(QStringLiteral("local:switch"))); // connect with remote host node

3 调用节点的acquire()来创建指向复制副本的指针

首先,实例化一个 replica:

  QSharedPointer<SimpleSwitchReplica> ptr;
  ptr.reset(repNode.acquire<SimpleSwitchReplica>()); // acquire replica of source from host node

注意:acquire()返回指向复制副本的指针,我们用QSharedPointer或QScopedPointer指向他,以确保始终能够正确删除。

main.cpp

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

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

      QSharedPointer<SimpleSwitchReplica> ptr; // shared pointer to hold source replica

      QRemoteObjectNode repNode; // create remote object node
      repNode.connectToNode(QUrl(QStringLiteral("local:switch"))); // connect with remote host node

      ptr.reset(repNode.acquire<SimpleSwitchReplica>()); // acquire replica of source from host node

      Client rswitch(ptr); // create client switch object and pass reference of replica to it

      return a.exec();
  }

client.h

  #ifndef _CLIENT_H
  #define _CLIENT_H

  #include <QObject>
  #include <QSharedPointer>

  #include "rep_SimpleSwitch_replica.h"

  class Client : public QObject
  {
      Q_OBJECT
  public:
      Client(QSharedPointer<SimpleSwitchReplica> ptr);
      ~Client();
      void initConnections();// Function to connect signals and slots of source and client

  Q_SIGNALS:
      void echoSwitchState(bool switchState);// this signal is connected with server_slot(..) on the source object and echoes back switch state received from source

  public Q_SLOTS:
      void recSwitchState_slot(); // slot to receive source state
  private:
      bool clientSwitchState; // holds received server switch state
      QSharedPointer<SimpleSwitchReplica> reptr;// holds reference to replica

   };

  #endif

client.cpp

 #include "client.h"

  // constructor
  Client::Client(QSharedPointer<SimpleSwitchReplica> ptr) :
      QObject(nullptr),reptr(ptr)
  {
      initConnections();
      //We can connect to SimpleSwitchReplica Signals/Slots
      //directly because our Replica was generated by repc.
  }

  //destructor
  Client::~Client()
  {
  }

  void Client::initConnections()
  {
          // initialize connections between signals and slots

         // connect source replica signal currStateChanged() with client's recSwitchState() slot to receive source's current state
          QObject::connect(reptr.data(), SIGNAL(currStateChanged()), this, SLOT(recSwitchState_slot()));
         // connect client's echoSwitchState(..) signal with replica's server_slot(..) to echo back received state
          QObject::connect(this, SIGNAL(echoSwitchState(bool)),reptr.data(), SLOT(server_slot(bool)));
  }

  void Client::recSwitchState_slot()
  {
      qDebug() << "Received source state "<<reptr.data()->currState();
      clientSwitchState = reptr.data()->currState();
      Q_EMIT echoSwitchState(clientSwitchState); // Emit signal to echo received state back to server
  }

将此示例与源代码端示例一起编译并运行会生成以下输出:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值