diff --git a/qmqtt_client.cpp b/qmqtt_client.cpp index 569607c..668d56f 100644 --- a/qmqtt_client.cpp +++ b/qmqtt_client.cpp @@ -36,14 +36,18 @@ namespace QMQTT { Client::Client(const QString & host, quint32 port, QObject * parent /* =0 */) - :pd_ptr(new ClientPrivate(this)) +:d_ptr(new ClientPrivate(this)) + { - pd_func()->init(host, port, parent); + Q_D(Client); + d->init(host, port, parent); } Client::~Client() { //? + Q_D(Client); + delete d; } /*---------------------------------------------------------------- @@ -51,102 +55,126 @@ Client::~Client() ----------------------------------------------------------------*/ QString Client::host() const { - return pd_func()->host; + Q_D(const Client); +// QString* str = d->host; +// return const_cast(d->host); +// return d->host; + return d->host; } void Client::setHost(const QString & host) { - pd_func()->host = host; +// d->host = host; + Q_D(Client); + d->host = host; } quint32 Client::port() const { - return pd_func()->port; + Q_D(const Client); + return d->port; } void Client::setPort(quint32 port) { - pd_func()->port = port; + Q_D(Client); + d->port = port; } QString Client::clientId() const { - return pd_func()->clientId; + Q_D(const Client); + return d->clientId; } void Client::setClientId(const QString &clientId) { - pd_func()->clientId = clientId; + Q_D(Client); + d->clientId = clientId; } QString Client::username() const { - return pd_func()->username; + Q_D(const Client); + return d->username; } void Client::setUsername(const QString & username) { - pd_func()->username = username; + Q_D(Client); + d->username = username; } QString Client::password() const { - return pd_func()->password; + Q_D(const Client); + return d->password; } void Client::setPassword(const QString & password) { - pd_func()->password = password; + Q_D(Client); + d->password = password; } int Client::keepalive() { - return pd_func()->keepalive; + Q_D( Client); + return d->keepalive; } void Client::setKeepAlive(int keepalive) { - pd_func()->keepalive = keepalive; + Q_D(Client); + d->keepalive = keepalive; } bool Client::cleansess() { - return pd_func()->cleansess; + Q_D(Client); + return d->cleansess; } void Client::setCleansess(bool cleansess) { - pd_func()->cleansess = cleansess; + Q_D(Client); + d->cleansess = cleansess; } bool Client::autoReconnect() const { - return pd_func()->network->autoReconnect(); + Q_D(const Client); + return d->network->autoReconnect(); } void Client::setAutoReconnect(bool value) { - pd_func()->network->setAutoReconnect(value); + Q_D(Client); + d->network->setAutoReconnect(value); } Will *Client::will() { - return pd_func()->will; + Q_D(Client); + return d->will; } void Client::setWill(Will *will) { - pd_func()->will = will; + Q_D(Client); + d->will = will; } State Client::state() const { - return pd_func()->state; + Q_D(const Client); + return d->state; } bool Client::isConnected() { - return pd_func()->network->isConnected(); + Q_D(Client); + return d->network->isConnected(); } @@ -155,56 +183,65 @@ bool Client::isConnected() ----------------------------------------------------------------*/ void Client::connect() { - pd_func()->sockConnect(); + Q_D(Client); + d->sockConnect(); } void Client::onConnected() { + Q_D(Client); qCDebug(client) << "Sock Connected...."; - pd_func()->sendConnect(); - pd_func()->startKeepalive(); + d->sendConnect(); + d->startKeepalive(); emit connected(); } quint16 Client::publish(Message &message) { - quint16 msgid = pd_func()->sendPublish(message); + Q_D(Client); + quint16 msgid = d->sendPublish(message); emit published(message); return msgid; } void Client::puback(quint8 type, quint16 msgid) { - pd_func()->sendPuback(type, msgid); + Q_D(Client); + d->sendPuback(type, msgid); emit pubacked(type, msgid); } quint16 Client::subscribe(const QString &topic, quint8 qos) { - quint16 msgid = pd_func()->sendSubscribe(topic, qos); + Q_D(Client); + quint16 msgid = d->sendSubscribe(topic, qos); emit subscribed(topic); return msgid; } void Client::unsubscribe(const QString &topic) { - pd_func()->sendUnsubscribe(topic); + Q_D(Client); + d->sendUnsubscribe(topic); emit unsubscribed(topic); } void Client::ping() { - pd_func()->sendPing(); + Q_D(Client); + d->sendPing(); } void Client::disconnect() { - pd_func()->disconnect(); + Q_D(Client); + d->disconnect(); } void Client::onDisconnected() { - pd_func()->stopKeepalive(); + Q_D(Client); + d->stopKeepalive(); emit disconnected(); } @@ -274,20 +311,22 @@ void Client::handleConnack(quint8 ack) void Client::handlePublish(Message & message) { + Q_D(Client); if(message.qos() == MQTT_QOS1) { - pd_func()->sendPuback(PUBACK, message.id()); + d->sendPuback(PUBACK, message.id()); } else if(message.qos() == MQTT_QOS2) { - pd_func()->sendPuback(PUBREC, message.id()); + d->sendPuback(PUBREC, message.id()); } emit received(message); } void Client::handlePuback(quint8 type, quint16 msgid) { + Q_D(Client); if(type == PUBREC) { - pd_func()->sendPuback(PUBREL, msgid); + d->sendPuback(PUBREL, msgid); } else if (type == PUBREL) { - pd_func()->sendPuback(PUBCOMP, msgid); + d->sendPuback(PUBCOMP, msgid); } emit pubacked(type, msgid); } diff --git a/qmqtt_client.h b/qmqtt_client.h index d81f8ae..ab8b2ec 100644 --- a/qmqtt_client.h +++ b/qmqtt_client.h @@ -88,11 +88,8 @@ class QMQTTSHARED_EXPORT Client : public QObject Q_PROPERTY(int keepalive READ keepalive WRITE setKeepAlive) Q_PROPERTY(bool autoReconnect READ autoReconnect WRITE setAutoReconnect) - Q_DISABLE_COPY(Client) - - P_DECLARE_PRIVATE(QMQTT::Client) - //friend class ClientPrivate; +// friend class ClientPrivate; public: Client(const QString &host = "localhost", quint32 port = 1883, QObject * parent = 0); @@ -169,8 +166,11 @@ private slots: void handleConnack(quint8 ack); void handlePuback(quint8 type, quint16 msgid); -protected: - ClientPrivate * const pd_ptr; +private: + ClientPrivate *const d_ptr; + + Q_DISABLE_COPY(Client) + Q_DECLARE_PRIVATE(Client) }; diff --git a/qmqtt_client_p.cpp b/qmqtt_client_p.cpp index 43f73c4..556ef0d 100644 --- a/qmqtt_client_p.cpp +++ b/qmqtt_client_p.cpp @@ -37,11 +37,11 @@ namespace QMQTT { Q_LOGGING_CATEGORY(client, "qmqtt.client") -ClientPrivate::ClientPrivate(Client *q) : +ClientPrivate::ClientPrivate(Client *qt) : host("localhost"), port(1883), keepalive(300), - pq_ptr(q) + q_ptr(qt) { gmid= 1; } @@ -53,19 +53,20 @@ ClientPrivate::~ClientPrivate() void ClientPrivate::init(QObject * parent) { - pq_func()->setParent(parent); + Q_Q(Client); + q->setParent(parent); if(!timer) { - timer = new QTimer(pq_func()); + timer = new QTimer(q); } - QObject::connect(timer, SIGNAL(timeout()), pq_func(), SLOT(ping())); + QObject::connect(timer, SIGNAL(timeout()), q, SLOT(ping())); if(!network){ - network = new Network(pq_func()); + network = new Network(q); } //TODO: FIXME LATER, how to handle socket error? - QObject::connect(network, SIGNAL(connected()), pq_func(), SLOT(onConnected())); - QObject::connect(network, SIGNAL(error(QAbstractSocket::SocketError)), pq_func(), SIGNAL(error(QAbstractSocket::SocketError))); - QObject::connect(network, SIGNAL(disconnected()), pq_func(), SLOT(onDisconnected())); - QObject::connect(network, SIGNAL(received(Frame &)), pq_func(), SLOT(onReceived(Frame &))); + QObject::connect(network, SIGNAL(connected()), q, SLOT(onConnected())); + QObject::connect(network, SIGNAL(error(QAbstractSocket::SocketError)), q, SIGNAL(error(QAbstractSocket::SocketError))); + QObject::connect(network, SIGNAL(disconnected()), q, SLOT(onDisconnected())); + QObject::connect(network, SIGNAL(received(Frame &)), q, SLOT(onReceived(Frame &))); } void ClientPrivate::init(const QString &host, int port, QObject * parent) diff --git a/qmqtt_client_p.h b/qmqtt_client_p.h index aaa2735..7875353 100644 --- a/qmqtt_client_p.h +++ b/qmqtt_client_p.h @@ -42,18 +42,16 @@ #include "qmqtt_global.h" #include "qmqtt_message.h" #include "qmqtt_will.h" - +#include "qmqtt_network.h" namespace QMQTT { Q_DECLARE_LOGGING_CATEGORY(client) - class ClientPrivate { - P_DECLARE_PUBLIC(QMQTT::Client) - + Q_DECLARE_PUBLIC(Client) public: - explicit ClientPrivate(Client * q); + ClientPrivate(Client * qt); ~ClientPrivate(); void init(QObject * parent = 0); void init(const QString & host, int port, QObject *parent = 0); @@ -74,7 +72,7 @@ class ClientPrivate QPointer network; QPointer timer; - Client * const pq_ptr; + public slots: void sockConnect(); @@ -92,6 +90,9 @@ public slots: private: QString randomClientId(); quint16 nextmid(); + Client * const q_ptr; + + }; diff --git a/qmqtt_global.h b/qmqtt_global.h index bfc464d..5cfdaf0 100644 --- a/qmqtt_global.h +++ b/qmqtt_global.h @@ -32,8 +32,7 @@ #ifndef QMQTT_GLOBAL_H #define QMQTT_GLOBAL_H -#include - +//#include #if defined(QMQTT_LIBRARY) # define QMQTTSHARED_EXPORT Q_DECL_EXPORT #else @@ -42,6 +41,7 @@ #define QMQTT_VERSION "0.2.0" +/* #define P_DECLARE_PRIVATE(Class) \ friend class Class##Private; \ inline Class##Private* pd_func() { return reinterpret_cast(this->pd_ptr); } \ @@ -54,5 +54,6 @@ #define P_D(Class) Class##Private * const d = this->pd_func() #define P_Q(Class) Class * const q = this->pq_func() - +*/ #endif // QMQTT_GLOBAL_H + diff --git a/qmqtt_message.h b/qmqtt_message.h index 8ff276a..c5348d4 100644 --- a/qmqtt_message.h +++ b/qmqtt_message.h @@ -35,10 +35,10 @@ #include #include #include - +#include "qmqtt_global.h" namespace QMQTT { -class Message +class QMQTTSHARED_EXPORT Message { public: Message(); diff --git a/qmqtt_will.h b/qmqtt_will.h index 9ecf60b..f4e2edb 100644 --- a/qmqtt_will.h +++ b/qmqtt_will.h @@ -34,10 +34,10 @@ #include #include - +#include "qmqtt_global.h" namespace QMQTT { -class Will : public QObject +class QMQTTSHARED_EXPORT Will : public QObject { Q_OBJECT