Files
pip/libs/main/mqtt_client/pimqttclient.h

113 lines
2.8 KiB
C++

//! \~\file pimqttclient.h
//! \~\ingroup MQTT
//! \~\brief
//! \~english
//! \~russian
/*
PIP - Platform Independent Primitives
MQTT Client
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef pimqttclient_h
#define pimqttclient_h
#include "piliterals_time.h"
#include "pimqtttypes.h"
#include "piprotectedvariable.h"
#include "pithread.h"
namespace PIMQTT {
class PIP_EXPORT Client: public PIObject {
PIOBJECT(PIMQTT::Client)
public:
Client();
virtual ~Client();
void setConnectTimeout(PISystemTime time) { connect_timeout = time; }
void connect(const PIString & address, const PIString & client, const PIString & username = {}, const PIString & password = {});
void disconnect();
void subscribe(const PIString & topic, QoS qos = QoS::Level1);
void unsubscribe(const PIString & topic);
void publish(const PIString & topic, const PIByteArray & msg, QoS qos = QoS::Level0);
void unsubscribeAll() { unsubscribe("#"); }
bool isConnecting() const { return m_status == Connecting; }
bool isConnected() const { return m_status == Connected; }
EVENT0(connected);
EVENT1(disconnected, PIMQTT::Error, code);
EVENT1(received, PIMQTT::Message, message);
private:
NO_COPY_CLASS(Client)
PRIVATE_DECLARATION(PIP_EXPORT)
enum Status {
Idle,
Connecting,
Connected,
Disconnecting,
};
struct ConnectInfo {
PIString address;
PIString clientID;
PIString username;
PIString password;
};
struct Subscribe {
PIString topic;
QoS qos;
};
void mqtt_connectionLost();
void mqtt_deliveryComplete(int token);
void mqtt_messageArrived(const Message & msg);
void connectInternal();
void publishInternal(const Message & m);
void subscribeInternal(const Subscribe & sub);
void unsubscribeInternal(const PIString & topic);
void destroy();
void changeStatus(Status s);
void run();
PIProtectedVariable<PIQueue<Message>> pub_queue;
PIProtectedVariable<PIQueue<Subscribe>> sub_queue;
PIProtectedVariable<PIQueue<PIString>> unsub_queue;
PIProtectedVariable<ConnectInfo> connect_info;
PIThreadNotifier notifier;
std::atomic_int m_status = {Idle};
PISystemTime connect_timeout = 10_s;
PIThread * thread = nullptr;
};
}; // namespace PIMQTT
#endif