example.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * example.cpp - qmqtt example
  3. *
  4. * Copyright (c) 2013 Ery Lee <ery.lee at gmail dot com>
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * * Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * * Neither the name of mqttc nor the names of its contributors may be used
  16. * to endorse or promote products derived from this software without
  17. * specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. */
  32. #include <qmqtt.h>
  33. #include <QCoreApplication>
  34. #include <QTimer>
  35. const QHostAddress EXAMPLE_HOST = QHostAddress::LocalHost;
  36. const quint16 EXAMPLE_PORT = 1883;
  37. const QString EXAMPLE_TOPIC = "qmqtt/exampletopic";
  38. class Publisher : public QMQTT::Client
  39. {
  40. Q_OBJECT
  41. public:
  42. explicit Publisher(const QHostAddress& host = EXAMPLE_HOST,
  43. const quint16 port = EXAMPLE_PORT,
  44. QObject* parent = NULL)
  45. : QMQTT::Client(host, port, parent)
  46. , _number(0)
  47. {
  48. connect(this, &Publisher::connected, this, &Publisher::onConnected);
  49. connect(&_timer, &QTimer::timeout, this, &Publisher::onTimeout);
  50. connect(this, &Publisher::disconnected, this, &Publisher::onDisconnected);
  51. }
  52. virtual ~Publisher() {}
  53. QTimer _timer;
  54. quint16 _number;
  55. public slots:
  56. void onConnected()
  57. {
  58. subscribe(EXAMPLE_TOPIC, 0);
  59. _timer.start(1000);
  60. }
  61. void onTimeout()
  62. {
  63. QMQTT::Message message(_number, EXAMPLE_TOPIC,
  64. QString("Number is %1").arg(_number).toUtf8());
  65. publish(message);
  66. _number++;
  67. if(_number >= 10)
  68. {
  69. _timer.stop();
  70. disconnectFromHost();
  71. }
  72. }
  73. void onDisconnected()
  74. {
  75. QTimer::singleShot(0, qApp, &QCoreApplication::quit);
  76. }
  77. };
  78. class Subscriber : public QMQTT::Client
  79. {
  80. Q_OBJECT
  81. public:
  82. explicit Subscriber(const QHostAddress& host = EXAMPLE_HOST,
  83. const quint16 port = EXAMPLE_PORT,
  84. QObject* parent = NULL)
  85. : QMQTT::Client(host, port, parent)
  86. , _qout(stdout)
  87. {
  88. connect(this, &Subscriber::connected, this, &Subscriber::onConnected);
  89. connect(this, &Subscriber::subscribed, this, &Subscriber::onSubscribed);
  90. connect(this, &Subscriber::received, this, &Subscriber::onReceived);
  91. }
  92. virtual ~Subscriber() {}
  93. QTextStream _qout;
  94. public slots:
  95. void onConnected()
  96. {
  97. _qout << "connected" << endl;
  98. subscribe(EXAMPLE_TOPIC, 0);
  99. }
  100. void onSubscribed(const QString& topic)
  101. {
  102. _qout << "subscribed " << topic << endl;
  103. }
  104. void onReceived(const QMQTT::Message& message)
  105. {
  106. _qout << "publish received: \"" << QString::fromUtf8(message.payload())
  107. << "\"" << endl;
  108. }
  109. };
  110. int main(int argc, char** argv)
  111. {
  112. QCoreApplication app(argc, argv);
  113. Subscriber subscriber;
  114. subscriber.connectToHost();
  115. Publisher publisher;
  116. publisher.connectToHost();
  117. return app.exec();
  118. }
  119. #include "example.moc"