BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
MosquittoClient.h
1 #pragma once
2 
3 #include <list>
4 #include <queue>
5 #include <set>
6 #include <string>
7 
8 #include <Poco/AtomicCounter.h>
9 #include <Poco/Event.h>
10 #include <Poco/Mutex.h>
11 #include <Poco/SharedPtr.h>
12 #include <Poco/Timespan.h>
13 
14 #include <mosquittopp.h>
15 
16 #include "loop/StoppableRunnable.h"
17 #include "net/MqttClient.h"
18 #include "util/Loggable.h"
19 
20 namespace BeeeOn {
21 
32  Loggable,
33  protected mosqpp::mosquittopp,
34  public StoppableRunnable,
35  public MqttClient {
36 public:
37  typedef Poco::SharedPtr<MosquittoClient> Ptr;
38 
40  ~MosquittoClient();
41 
47  void run() override;
48  void stop() override;
49 
50  void setHost(const std::string &host);
51  void setPort(int port);
52 
56  void setSubTopics(const std::list<std::string> &subTopics);
57 
62  void setReconnectTimeout(const Poco::Timespan &timeout);
63 
64  void setClientID(const std::string &id);
65  std::string clientID() const;
66 
70  void publish(const MqttMessage &msq) override;
71 
85  MqttMessage receive(const Poco::Timespan &timeout) override;
86 
87 protected:
88  virtual std::string buildClientID() const;
89 
94  void connect();
95 
96 private:
101  bool initConnection();
102 
103  void subscribeToAll();
104 
105  void throwMosquittoError(int returnCode) const;
106 
113  void on_message(const struct mosquitto_message *message) override;
114 
118  MqttMessage nextMessage();
119 
120 private:
121  std::string m_clientID;
122  std::string m_host;
123  Poco::Timespan m_reconnectTimeout;
124  int m_port;
125  std::set<std::string> m_subTopics;
126  Poco::AtomicCounter m_stop;
127  Poco::Event m_receiveEvent;
128  Poco::Event m_reconnectEvent;
129  Poco::FastMutex m_queueMutex;
130  std::queue<MqttMessage> m_msgQueue;
131 };
132 
133 }
void run() override
Definition: MosquittoClient.cpp:157
void setReconnectTimeout(const Poco::Timespan &timeout)
Definition: MosquittoClient.cpp:219
Definition: MqttClient.h:10
Definition: MqttMessage.h:7
void connect()
Definition: MosquittoClient.cpp:85
void setSubTopics(const std::list< std::string > &subTopics)
Definition: MosquittoClient.cpp:184
void stop() override
Definition: MosquittoClient.cpp:177
void publish(const MqttMessage &msq) override
Definition: MosquittoClient.cpp:72
Definition: MosquittoClient.h:31
Definition: Loggable.h:19
Definition: StoppableRunnable.h:8
MqttMessage receive(const Poco::Timespan &timeout) override
Definition: MosquittoClient.cpp:108