diff options
author | Alexei Rousskikh <ext-alexei.rousskikh@nokia.com> | 2012-03-08 09:09:34 -0500 |
---|---|---|
committer | Andrew Christian <andrew.christian@nokia.com> | 2012-03-08 15:49:28 +0100 |
commit | 29c5cdf8c23f13d7aa7400e2fd45f57babb01eff (patch) | |
tree | 4c03157cbcb604ffc9aa287f38ea9d7b35d427dd | |
parent | 74b599f4f4f21878adcb3a349ff9b3a1e9a91536 (diff) |
PIMPL implementation for major classes
Change-Id: I18d9771f952dc980130034eb42d8f325402db280
Reviewed-by: Andrew Christian <andrew.christian@nokia.com>
-rw-r--r-- | src/jsonclient.cpp | 64 | ||||
-rw-r--r-- | src/jsonclient.h | 7 | ||||
-rw-r--r-- | src/jsonpipe.cpp | 148 | ||||
-rw-r--r-- | src/jsonpipe.h | 11 | ||||
-rw-r--r-- | src/jsonserver.cpp | 139 | ||||
-rw-r--r-- | src/jsonserver.h | 20 | ||||
-rw-r--r-- | src/jsonserverclient.cpp | 89 | ||||
-rw-r--r-- | src/jsonserverclient.h | 10 | ||||
-rw-r--r-- | src/jsonstream.cpp | 74 | ||||
-rw-r--r-- | src/jsonstream.h | 9 |
10 files changed, 357 insertions, 214 deletions
diff --git a/src/jsonclient.cpp b/src/jsonclient.cpp index c7af355..b5cdd24 100644 --- a/src/jsonclient.cpp +++ b/src/jsonclient.cpp @@ -40,6 +40,7 @@ ****************************************************************************/ #include "jsonclient.h" +#include "jsonstream.h" #include <QLocalSocket> #include <QTcpSocket> @@ -49,6 +50,27 @@ QT_BEGIN_NAMESPACE_JSONSTREAM +/****************************************************************************/ + +/*! + \internal +*/ +class JsonClientPrivate +{ +public: + JsonClientPrivate() + : mStream(0) {} + + JsonClientPrivate(const QJsonObject& message) + : mRegistrationMessage(message) + , mStream(0) {} + + QJsonObject mRegistrationMessage; + JsonStream mStream; +}; + +/****************************************************************************/ + /*! \class JsonClient \brief The JsonClient class is used to send jsons to the JsonServer. @@ -63,9 +85,10 @@ QT_BEGIN_NAMESPACE_JSONSTREAM */ JsonClient::JsonClient(const QString& registration, QObject* parent) : QObject(parent), - mStream(0) + d_ptr(new JsonClientPrivate()) { - mRegistrationMessage.insert(QStringLiteral("token"), registration); + Q_D(JsonClient); + d->mRegistrationMessage.insert(QStringLiteral("token"), registration); } /*! @@ -73,8 +96,7 @@ JsonClient::JsonClient(const QString& registration, QObject* parent) */ JsonClient::JsonClient(const QJsonObject& message, QObject *parent) : QObject(parent), - mRegistrationMessage(message), - mStream(0) + d_ptr(new JsonClientPrivate(message)) { } @@ -83,7 +105,7 @@ JsonClient::JsonClient(const QJsonObject& message, QObject *parent) */ JsonClient::JsonClient(QObject *parent) : QObject(parent), - mStream(0) + d_ptr(new JsonClientPrivate()) { } @@ -93,8 +115,9 @@ JsonClient::JsonClient(QObject *parent) JsonClient::~JsonClient() { // Variant streams don't own the socket - QIODevice *device = mStream.device(); - mStream.setDevice(0); + Q_D(JsonClient); + QIODevice *device = d->mStream.device(); + d->mStream.setDevice(0); if (device) delete device; } @@ -111,11 +134,12 @@ bool JsonClient::connectTCP(const QString& hostname, int port) if (socket->waitForConnected()) { connect(socket, SIGNAL(disconnected()), SLOT(handleSocketDisconnected())); - mStream.setDevice(socket); - connect(&mStream, SIGNAL(receive(const QJsonObject&)), + Q_D(JsonClient); + d->mStream.setDevice(socket); + connect(&d->mStream, SIGNAL(receive(const QJsonObject&)), this, SIGNAL(receive(const QJsonObject&))); - mStream.send(mRegistrationMessage); + d->mStream.send(d->mRegistrationMessage); return true; } @@ -139,11 +163,12 @@ bool JsonClient::connectLocal(const QString& socketname) if (socket->waitForConnected()) { connect(socket, SIGNAL(disconnected()), SLOT(handleSocketDisconnected())); - mStream.setDevice(socket); - connect(&mStream, SIGNAL(messageReceived(const QJsonObject&)), + Q_D(JsonClient); + d->mStream.setDevice(socket); + connect(&d->mStream, SIGNAL(messageReceived(const QJsonObject&)), this, SIGNAL(messageReceived(const QJsonObject&))); // qDebug() << "Sending local socket registration message" << mRegistrationMessage; - mStream.send(mRegistrationMessage); + d->mStream.send(d->mRegistrationMessage); return true; } delete socket; @@ -156,8 +181,9 @@ bool JsonClient::connectLocal(const QString& socketname) void JsonClient::send(const QJsonObject &message) { - if (mStream.isOpen()) { - mStream << message; + Q_D(JsonClient); + if (d->mStream.isOpen()) { + d->mStream << message; } else { qCritical() << Q_FUNC_INFO << "stream socket is not available"; } @@ -170,7 +196,8 @@ void JsonClient::send(const QJsonObject &message) void JsonClient::setFormat( EncodingFormat format ) { - mStream.setFormat(format); + Q_D(JsonClient); + d->mStream.setFormat(format); } /*! @@ -178,11 +205,12 @@ void JsonClient::setFormat( EncodingFormat format ) */ void JsonClient::handleSocketDisconnected() { - QIODevice *device = mStream.device(); + Q_D(JsonClient); + QIODevice *device = d->mStream.device(); if (!device) return; - mStream.setDevice(0); + d->mStream.setDevice(0); device->deleteLater(); emit disconnected(); } diff --git a/src/jsonclient.h b/src/jsonclient.h index 49012cc..70e8e88 100644 --- a/src/jsonclient.h +++ b/src/jsonclient.h @@ -44,12 +44,13 @@ #include <QObject> #include <QVariant> +#include <QJsonObject> -#include "jsonstream.h" #include "jsonstream-global.h" QT_BEGIN_NAMESPACE_JSONSTREAM +class JsonClientPrivate; class Q_ADDON_JSONSTREAM_EXPORT JsonClient : public QObject { Q_OBJECT @@ -76,8 +77,8 @@ private slots: void handleSocketDisconnected(); private: - QJsonObject mRegistrationMessage; - JsonStream mStream; + Q_DECLARE_PRIVATE(JsonClient) + QScopedPointer<JsonClientPrivate> d_ptr; }; QT_END_NAMESPACE_JSONSTREAM diff --git a/src/jsonpipe.cpp b/src/jsonpipe.cpp index 257e103..cf76e0f 100644 --- a/src/jsonpipe.cpp +++ b/src/jsonpipe.cpp @@ -59,6 +59,26 @@ QT_BEGIN_NAMESPACE_JSONSTREAM /****************************************************************************/ /*! + \internal +*/ +class JsonPipePrivate +{ +public: + JsonPipePrivate() + : mIn(0) + , mOut(0) + , mFormat(FormatUndefined) {} + + JsonBuffer *mInBuffer; + QByteArray mOutBuffer; + QSocketNotifier *mIn; + QSocketNotifier *mOut; + EncodingFormat mFormat; +}; + +/****************************************************************************/ + +/*! \class JsonPipe \brief The JsonPipe class serializes JSON data. @@ -73,12 +93,11 @@ QT_BEGIN_NAMESPACE_JSONSTREAM JsonPipe::JsonPipe(QObject *parent) : QObject(parent) - , mIn(0) - , mOut(0) - , mFormat(FormatUndefined) + , d_ptr(new JsonPipePrivate()) { - mInBuffer = new JsonBuffer(this); - connect(mInBuffer, SIGNAL(objectReceived(const QJsonObject&)), + Q_D(JsonPipe); + d->mInBuffer = new JsonBuffer(this); + connect(d->mInBuffer, SIGNAL(objectReceived(const QJsonObject&)), SLOT(objectReceived(const QJsonObject&))); } @@ -96,7 +115,8 @@ JsonPipe::~JsonPipe() bool JsonPipe::writeEnabled() const { - return (mOut != NULL); + Q_D(const JsonPipe); + return (d->mOut != NULL); } /*! @@ -105,7 +125,8 @@ bool JsonPipe::writeEnabled() const bool JsonPipe::readEnabled() const { - return (mIn != NULL); + Q_D(const JsonPipe); + return (d->mIn != NULL); } /*! @@ -114,31 +135,33 @@ bool JsonPipe::readEnabled() const void JsonPipe::setFds(int in_fd, int out_fd) { - if (mIn) - delete mIn; - if (mOut) - delete mOut; - - mIn = new QSocketNotifier(in_fd, QSocketNotifier::Read, this); - mOut = new QSocketNotifier(out_fd, QSocketNotifier::Write, this); - connect(mIn, SIGNAL(activated(int)), SLOT(inReady(int))); - connect(mOut, SIGNAL(activated(int)), SLOT(outReady(int))); - mIn->setEnabled(true); - mOut->setEnabled(mOutBuffer.size() > 0); + Q_D(JsonPipe); + if (d->mIn) + delete d->mIn; + if (d->mOut) + delete d->mOut; + + d->mIn = new QSocketNotifier(in_fd, QSocketNotifier::Read, this); + d->mOut = new QSocketNotifier(out_fd, QSocketNotifier::Write, this); + connect(d->mIn, SIGNAL(activated(int)), SLOT(inReady(int))); + connect(d->mOut, SIGNAL(activated(int)), SLOT(outReady(int))); + d->mIn->setEnabled(true); + d->mOut->setEnabled(d->mOutBuffer.size() > 0); } void JsonPipe::inReady(int fd) { - mIn->setEnabled(false); - int n = mInBuffer->copyFromFd(fd); + Q_D(JsonPipe); + d->mIn->setEnabled(false); + int n = d->mInBuffer->copyFromFd(fd); if (n <= 0) { - mInBuffer->clear(); - mIn->deleteLater(); - mIn = NULL; + d->mInBuffer->clear(); + d->mIn->deleteLater(); + d->mIn = NULL; emit error( (n < 0) ? ReadFailed : ReadAtEnd ); } else - mIn->setEnabled(true); + d->mIn->setEnabled(true); } /*! @@ -148,32 +171,34 @@ void JsonPipe::inReady(int fd) */ int JsonPipe::writeInternal(int fd) { - if (!mOutBuffer.size()) + Q_D(JsonPipe); + if (!d->mOutBuffer.size()) return 0; - int n = ::write(fd, mOutBuffer.data(), mOutBuffer.size()); + int n = ::write(fd, d->mOutBuffer.data(), d->mOutBuffer.size()); if (n <= 0) { - mOut->deleteLater(); - mOut = NULL; + d->mOut->deleteLater(); + d->mOut = NULL; // ### TODO: This emits errors in the middle of waitForBytesWritten. // ### This could cause problems 'cause it gets called in destructors emit error(n < 0 ? WriteFailed : WriteAtEnd); } - else if (n < mOutBuffer.size()) - mOutBuffer = mOutBuffer.mid(n); + else if (n < d->mOutBuffer.size()) + d->mOutBuffer = d->mOutBuffer.mid(n); else - mOutBuffer.clear(); + d->mOutBuffer.clear(); return n; } void JsonPipe::outReady(int) { - Q_ASSERT(mOut); - mOut->setEnabled(false); - if (mOutBuffer.size()) { - writeInternal(mOut->socket()); - if (mOut && !mOutBuffer.isEmpty()) - mOut->setEnabled(true); + Q_D(JsonPipe); + Q_ASSERT(d->mOut); + d->mOut->setEnabled(false); + if (d->mOutBuffer.size()) { + writeInternal(d->mOut->socket()); + if (d->mOut && !d->mOutBuffer.isEmpty()) + d->mOut->setEnabled(true); } } @@ -183,31 +208,32 @@ void JsonPipe::outReady(int) bool JsonPipe::send(const QJsonObject& object) { - if (!mOut) + Q_D(JsonPipe); + if (!d->mOut) return false; QJsonDocument document(object); - switch (mFormat) { + switch (d->mFormat) { case FormatUndefined: - mFormat = FormatQBJS; + d->mFormat = FormatQBJS; // Deliberate fall through case FormatQBJS: - mOutBuffer.append(document.toBinaryData()); + d->mOutBuffer.append(document.toBinaryData()); break; case FormatUTF8: - mOutBuffer.append(document.toJson()); + d->mOutBuffer.append(document.toJson()); break; case FormatBSON: { BsonObject bson(document.toVariant().toMap()); - mOutBuffer.append("bson"); - mOutBuffer.append(bson.data()); + d->mOutBuffer.append("bson"); + d->mOutBuffer.append(bson.data()); break; } } - if (mOutBuffer.size()) - mOut->setEnabled(true); + if (d->mOutBuffer.size()) + d->mOut->setEnabled(true); return true; } @@ -218,8 +244,9 @@ bool JsonPipe::send(const QJsonObject& object) void JsonPipe::objectReceived(const QJsonObject& object) { - if (mFormat == FormatUndefined) - mFormat = mInBuffer->format(); + Q_D(JsonPipe); + if (d->mFormat == FormatUndefined) + d->mFormat = d->mInBuffer->format(); emit messageReceived(object); } @@ -229,7 +256,8 @@ void JsonPipe::objectReceived(const QJsonObject& object) EncodingFormat JsonPipe::format() const { - return mFormat; + Q_D(const JsonPipe); + return d->mFormat; } /*! @@ -238,7 +266,8 @@ EncodingFormat JsonPipe::format() const void JsonPipe::setFormat( EncodingFormat format ) { - mFormat = format; + Q_D(JsonPipe); + d->mFormat = format; } /* @@ -249,18 +278,19 @@ void JsonPipe::setFormat( EncodingFormat format ) bool JsonPipe::waitForBytesWritten(int msecs) { - if (!mOut || mOutBuffer.isEmpty()) + Q_D(JsonPipe); + if (!d->mOut || d->mOutBuffer.isEmpty()) return false; - mOut->setEnabled(false); + d->mOut->setEnabled(false); QElapsedTimer stopWatch; stopWatch.start(); - while (mOut && !mOutBuffer.isEmpty()) { + while (d->mOut && !d->mOutBuffer.isEmpty()) { fd_set wfds; FD_ZERO(&wfds); - FD_SET(mOut->socket(),&wfds); + FD_SET(d->mOut->socket(),&wfds); int timeout = msecs - stopWatch.elapsed(); struct timeval tv; @@ -270,17 +300,17 @@ bool JsonPipe::waitForBytesWritten(int msecs) tv.tv_usec = (timeout % 1000) * 1000; } - int retval = ::select(mOut->socket() + 1, NULL, &wfds, NULL, tvptr); + int retval = ::select(d->mOut->socket() + 1, NULL, &wfds, NULL, tvptr); if (retval == -1 && errno == EINTR) continue; if (retval <= 0) break; - writeInternal(mOut->socket()); + writeInternal(d->mOut->socket()); } - if (mOut && !mOutBuffer.isEmpty()) - mOut->setEnabled(true); - return mOutBuffer.isEmpty(); + if (d->mOut && !d->mOutBuffer.isEmpty()) + d->mOut->setEnabled(true); + return d->mOutBuffer.isEmpty(); } /*! diff --git a/src/jsonpipe.h b/src/jsonpipe.h index c7df9a6..ff2ac93 100644 --- a/src/jsonpipe.h +++ b/src/jsonpipe.h @@ -42,9 +42,8 @@ #ifndef _JSON_PIPE_H #define _JSON_PIPE_H -#include <QVariant> +#include <QObject> #include <QJsonObject> -#include <QJsonDocument> #include "jsonstream-global.h" class QSocketNotifier; @@ -53,6 +52,7 @@ QT_BEGIN_NAMESPACE_JSONSTREAM class JsonBuffer; +class JsonPipePrivate; class Q_ADDON_JSONSTREAM_EXPORT JsonPipe : public QObject { Q_OBJECT @@ -91,11 +91,8 @@ private: int writeInternal(int fd); private: - JsonBuffer *mInBuffer; - QByteArray mOutBuffer; - QSocketNotifier *mIn; - QSocketNotifier *mOut; - EncodingFormat mFormat; + Q_DECLARE_PRIVATE(JsonPipe) + QScopedPointer<JsonPipePrivate> d_ptr; }; JsonPipe& operator<<( JsonPipe&, const QJsonObject& ); diff --git a/src/jsonserver.cpp b/src/jsonserver.cpp index d5ed561..0c2be1d 100644 --- a/src/jsonserver.cpp +++ b/src/jsonserver.cpp @@ -65,6 +65,34 @@ inline bool canValidate(JsonServer::ValidatorFlags flags, SchemaValidator *valid return flags != JsonServer::NoValidation && validator && !validator->isEmpty(); } +class JsonServerPrivate +{ +public: + JsonServerPrivate() + : m_inboundValidator(0) + , m_outboundValidator(0) {} + + ~JsonServerPrivate() + { + qDeleteAll(m_identifierToClient); + foreach (QLocalServer *server, m_localServers.keys()) + delete server; + + if (m_inboundValidator) + delete m_inboundValidator; + + if (m_outboundValidator) + delete m_outboundValidator; + } + + QMap<QLocalServer *, JsonAuthority *> m_localServers; + QMultiMap<QString, JsonServerClient *> m_identifierToClient; + QMap<QString, QList<QJsonObject> > m_messageQueues; + QSet<QString> m_multipleConnections; + JsonServer::ValidatorFlags m_validatorFlags; + SchemaValidator *m_inboundValidator; + SchemaValidator *m_outboundValidator; +}; /**************************************************************************************************/ @@ -115,8 +143,7 @@ inline bool canValidate(JsonServer::ValidatorFlags flags, SchemaValidator *valid */ JsonServer::JsonServer(QObject *parent) : QObject(parent) - , m_inboundValidator(0) - , m_outboundValidator(0) + , d_ptr(new JsonServerPrivate()) { initSchemaValidation(); // initialize validation if defined by environment } @@ -128,9 +155,6 @@ JsonServer::JsonServer(QObject *parent) */ JsonServer::~JsonServer() { - qDeleteAll(m_identifierToClient); - foreach (QLocalServer *server, m_localServers.keys()) - delete server; } /*! @@ -156,11 +180,12 @@ bool JsonServer::listen(const QString &socketname, JsonAuthority *authority) { QLocalServer::removeServer(socketname); QLocalServer *server = new QLocalServer(this); - m_localServers.insert(server, authority); + Q_D(JsonServer); + d->m_localServers.insert(server, authority); QObject::connect(server, SIGNAL(newConnection()), this, SLOT(handleLocalConnection())); if (!server->listen(socketname)) { qCritical() << Q_FUNC_INFO << "Unable to listen on socket:" << socketname; - m_localServers.remove(server); + d->m_localServers.remove(server); return false; } return true; @@ -178,7 +203,8 @@ void JsonServer::handleLocalConnection() if (QLocalSocket *socket = server->nextPendingConnection()) { socket->setReadBufferSize(64*1024); - JsonAuthority *authority = m_localServers.value(server); + Q_D(JsonServer); + JsonAuthority *authority = d->m_localServers.value(server); JsonServerClient *client = new JsonServerClient(this); client->setAuthority(authority); client->setSocket(socket); @@ -205,15 +231,16 @@ void JsonServer::handleLocalConnection() void JsonServer::handleClientAuthorized(const QString &identifier) { JsonServerClient *client = qobject_cast<JsonServerClient *>(sender()); - bool exists = m_identifierToClient.contains(identifier); + Q_D(JsonServer); + bool exists = d->m_identifierToClient.contains(identifier); - if (exists && !m_multipleConnections.contains(identifier)) { + if (exists && !d->m_multipleConnections.contains(identifier)) { qWarning() << "Error: Multiple disallowed connections for" << identifier; client->stop(); } else { - m_identifierToClient.insert(identifier, client); - QList<QJsonObject> messageQueue = m_messageQueues.take(identifier); + d->m_identifierToClient.insert(identifier, client); + QList<QJsonObject> messageQueue = d->m_messageQueues.take(identifier); foreach (const QJsonObject& message, messageQueue) client->send(message); if (!exists) @@ -233,7 +260,8 @@ void JsonServer::clientDisconnected(const QString &identifier) { return; // Only emit the connectionRemoved signal if this was a valid connection - if (!m_identifierToClient.remove(identifier, client)) + Q_D(JsonServer); + if (!d->m_identifierToClient.remove(identifier, client)) qWarning() << "Error: Mismatched client for" << identifier; else emit connectionRemoved(identifier); @@ -257,11 +285,12 @@ void JsonServer::handleAuthorizationFailed() void JsonServer::receiveMessage(const QString &identifier, const QJsonObject &message) { // do JSON schema validation if required - if (canValidate(validatorFlags(), m_inboundValidator)) { - if (!m_inboundValidator->validateSchema(message)) + Q_D(JsonServer); + if (canValidate(validatorFlags(), d->m_inboundValidator)) { + if (!d->m_inboundValidator->validateSchema(message)) { if (validatorFlags().testFlag(WarnIfInvalid)) { - emit inboundMessageValidationFailed(message, m_inboundValidator->getLastError()); + emit inboundMessageValidationFailed(message, d->m_inboundValidator->getLastError()); } if (validatorFlags().testFlag(DropIfInvalid)) { return; @@ -277,7 +306,8 @@ void JsonServer::receiveMessage(const QString &identifier, const QJsonObject &me */ bool JsonServer::hasConnection(const QString &identifier) const { - return m_identifierToClient.contains(identifier); + Q_D(const JsonServer); + return d->m_identifierToClient.contains(identifier); } /*! @@ -286,7 +316,8 @@ bool JsonServer::hasConnection(const QString &identifier) const QStringList JsonServer::connections() const { - return m_identifierToClient.uniqueKeys(); + Q_D(const JsonServer); + return d->m_identifierToClient.uniqueKeys(); } /*! @@ -300,8 +331,9 @@ void JsonServer::enableQueuing(const QString &identifier) if (identifier.isEmpty()) return; - if (!m_messageQueues.contains(identifier)) - m_messageQueues.insert(identifier, QList<QJsonObject>()); + Q_D(JsonServer); + if (!d->m_messageQueues.contains(identifier)) + d->m_messageQueues.insert(identifier, QList<QJsonObject>()); } /*! @@ -312,15 +344,17 @@ void JsonServer::disableQueuing(const QString &identifier) if (identifier.isEmpty()) return; - m_messageQueues.remove(identifier); + Q_D(JsonServer); + d->m_messageQueues.remove(identifier); } /*! Returns whether queuing of messages is enabled for client identified by \a identifier. */ -bool JsonServer::isQueuingEnabled(const QString &identifier) +bool JsonServer::isQueuingEnabled(const QString &identifier) const { - return m_messageQueues.contains(identifier); + Q_D(const JsonServer); + return d->m_messageQueues.contains(identifier); } /*! @@ -333,8 +367,9 @@ void JsonServer::clearQueue(const QString &identifier) if (identifier.isEmpty()) return; - if (m_messageQueues.contains(identifier)) - m_messageQueues.insert(identifier, QList<QJsonObject>()); + Q_D(JsonServer); + if (d->m_messageQueues.contains(identifier)) + d->m_messageQueues.insert(identifier, QList<QJsonObject>()); } /*! @@ -345,7 +380,8 @@ void JsonServer::clearQueue(const QString &identifier) void JsonServer::enableMultipleConnections(const QString& identifier) { - m_multipleConnections.insert(identifier); + Q_D(JsonServer); + d->m_multipleConnections.insert(identifier); } /*! @@ -354,7 +390,8 @@ void JsonServer::enableMultipleConnections(const QString& identifier) void JsonServer::disableMultipleConnections(const QString& identifier) { - m_multipleConnections.remove(identifier); + Q_D(JsonServer); + d->m_multipleConnections.remove(identifier); } @@ -369,11 +406,12 @@ void JsonServer::disableMultipleConnections(const QString& identifier) bool JsonServer::send(const QString &identifier, const QJsonObject &message) { // do JSON schema validation if required - if (canValidate(validatorFlags(), m_outboundValidator)) { - if (!m_outboundValidator->validateSchema(message)) + Q_D(JsonServer); + if (canValidate(validatorFlags(), d->m_outboundValidator)) { + if (!d->m_outboundValidator->validateSchema(message)) { if (validatorFlags().testFlag(WarnIfInvalid)) { - emit outboundMessageValidationFailed(message, m_outboundValidator->getLastError()); + emit outboundMessageValidationFailed(message, d->m_outboundValidator->getLastError()); } if (validatorFlags().testFlag(DropIfInvalid)) { return false; @@ -382,13 +420,13 @@ bool JsonServer::send(const QString &identifier, const QJsonObject &message) } if (isQueuingEnabled(identifier)) { - QList<QJsonObject> queue = m_messageQueues.value(identifier); + QList<QJsonObject> queue = d->m_messageQueues.value(identifier); queue << message; - m_messageQueues.insert(identifier, queue); + d->m_messageQueues.insert(identifier, queue); return true; } - QList<JsonServerClient*> clients = m_identifierToClient.values(identifier); + QList<JsonServerClient*> clients = d->m_identifierToClient.values(identifier); foreach (JsonServerClient *client, clients) { Q_ASSERT(client); client->send(message); @@ -403,11 +441,12 @@ bool JsonServer::send(const QString &identifier, const QJsonObject &message) void JsonServer::broadcast(const QJsonObject &message) { // do JSON schema validation if required - if (canValidate(validatorFlags(), m_outboundValidator)) { - if (!m_outboundValidator->validateSchema(message)) + Q_D(JsonServer); + if (canValidate(validatorFlags(), d->m_outboundValidator)) { + if (!d->m_outboundValidator->validateSchema(message)) { if (validatorFlags().testFlag(WarnIfInvalid)) { - emit outboundMessageValidationFailed(message, m_outboundValidator->getLastError()); + emit outboundMessageValidationFailed(message, d->m_outboundValidator->getLastError()); } if (validatorFlags().testFlag(DropIfInvalid)) { return; @@ -416,7 +455,7 @@ void JsonServer::broadcast(const QJsonObject &message) } // ### No JsonServerClient should be repeated in this list - QList<JsonServerClient*> clients = m_identifierToClient.values(); + QList<JsonServerClient*> clients = d->m_identifierToClient.values(); foreach (JsonServerClient *client, clients) { Q_ASSERT(client); client->send(message); @@ -433,7 +472,8 @@ void JsonServer::broadcast(const QJsonObject &message) */ void JsonServer::removeConnection(const QString &identifier) { - QList<JsonServerClient*> clients = m_identifierToClient.values(identifier); + Q_D(JsonServer); + QList<JsonServerClient*> clients = d->m_identifierToClient.values(identifier); while (!clients.isEmpty()) { JsonServerClient *client = clients.takeFirst(); Q_ASSERT(client); @@ -458,16 +498,21 @@ void JsonServer::removeConnection(const QString &identifier) */ /*! - \fn ValidatorFlags JsonServer::validatorFlags() const Return the current ValidatorFlags */ +JsonServer::ValidatorFlags JsonServer::validatorFlags() const +{ + Q_D(const JsonServer); + return d->m_validatorFlags; +} /*! Sets validatorFlags property to \a flags. */ void JsonServer::setValidatorFlags(ValidatorFlags flags) { - m_validatorFlags = flags; + Q_D(JsonServer); + d->m_validatorFlags = flags; } /*! @@ -475,10 +520,11 @@ void JsonServer::setValidatorFlags(ValidatorFlags flags) */ SchemaValidator *JsonServer::inboundValidator() { - if (!m_inboundValidator) { - m_inboundValidator = new SchemaValidator(this); + Q_D(JsonServer); + if (!d->m_inboundValidator) { + d->m_inboundValidator = new SchemaValidator(this); } - return m_inboundValidator; + return d->m_inboundValidator; } /*! @@ -486,10 +532,11 @@ SchemaValidator *JsonServer::inboundValidator() */ SchemaValidator *JsonServer::outboundValidator() { - if (!m_outboundValidator) { - m_outboundValidator = new SchemaValidator(this); + Q_D(JsonServer); + if (!d->m_outboundValidator) { + d->m_outboundValidator = new SchemaValidator(this); } - return m_outboundValidator; + return d->m_outboundValidator; } /*! diff --git a/src/jsonserver.h b/src/jsonserver.h index 0657b3c..f7e1c92 100644 --- a/src/jsonserver.h +++ b/src/jsonserver.h @@ -43,22 +43,17 @@ #define _JSON_SERVER_H #include <QObject> -#include <QMap> #include <QJsonObject> -#include <QSet> - -class QLocalServer; #include "jsonstream-global.h" #include "schemaerror.h" QT_BEGIN_NAMESPACE_JSONSTREAM -class JsonStream; class JsonAuthority; -class JsonServerClient; class SchemaValidator; +class JsonServerPrivate; class Q_ADDON_JSONSTREAM_EXPORT JsonServer : public QObject { Q_OBJECT @@ -75,7 +70,7 @@ public: void enableQueuing(const QString &identifier); void disableQueuing(const QString &identifier); - bool isQueuingEnabled(const QString &identifier); + bool isQueuingEnabled(const QString &identifier) const; void clearQueue(const QString &identifier); void enableMultipleConnections(const QString& identifier); @@ -90,7 +85,7 @@ public: }; Q_DECLARE_FLAGS(ValidatorFlags, ValidatorFlag) - ValidatorFlags validatorFlags() const { return m_validatorFlags; } + ValidatorFlags validatorFlags() const; void setValidatorFlags(ValidatorFlags); SchemaValidator *inboundValidator(); @@ -124,13 +119,8 @@ private: void initSchemaValidation(); private: - QMap<QLocalServer *, JsonAuthority *> m_localServers; - QMultiMap<QString, JsonServerClient *> m_identifierToClient; - QMap<QString, QList<QJsonObject> > m_messageQueues; - QSet<QString> m_multipleConnections; - ValidatorFlags m_validatorFlags; - SchemaValidator *m_inboundValidator; - SchemaValidator *m_outboundValidator; + Q_DECLARE_PRIVATE(JsonServer) + QScopedPointer<JsonServerPrivate> d_ptr; }; Q_DECLARE_OPERATORS_FOR_FLAGS(JsonServer::ValidatorFlags) diff --git a/src/jsonserverclient.cpp b/src/jsonserverclient.cpp index f9e21e6..09e4c55 100644 --- a/src/jsonserverclient.cpp +++ b/src/jsonserverclient.cpp @@ -48,10 +48,28 @@ QT_BEGIN_NAMESPACE_JSONSTREAM + +/*! + \internal +*/ +class JsonServerClientPrivate +{ +public: + JsonServerClientPrivate() + : m_socket(0) + , m_stream(0) {} + + QString m_identifier; + QLocalSocket *m_socket; + JsonStream *m_stream; + QWeakPointer<JsonAuthority> m_authority; +}; + +/****************************************************************************/ + JsonServerClient::JsonServerClient(QObject *parent) : QObject(parent) - , m_socket(0) - , m_stream(0) + , d_ptr(new JsonServerClientPrivate()) { } @@ -66,44 +84,49 @@ JsonServerClient::~JsonServerClient() */ void JsonServerClient::setAuthority(JsonAuthority *authority) { - m_authority = authority; + Q_D(JsonServerClient); + d->m_authority = authority; } const QLocalSocket *JsonServerClient::socket() const { - return m_socket; + Q_D(const JsonServerClient); + return d->m_socket; } void JsonServerClient::setSocket(QLocalSocket *socket) { - m_socket = socket; - m_socket->setParent(this); + Q_D(JsonServerClient); + d->m_socket = socket; + d->m_socket->setParent(this); if (socket) { - m_stream = new JsonStream(socket); - m_stream->setParent(this); + d->m_stream = new JsonStream(socket); + d->m_stream->setParent(this); connect(socket, SIGNAL(disconnected()), this, SLOT(handleDisconnect())); - connect(m_stream, SIGNAL(messageReceived(const QJsonObject&)), + connect(d->m_stream, SIGNAL(messageReceived(const QJsonObject&)), this, SLOT(received(const QJsonObject&))); } } QString JsonServerClient::identifier() const { - return m_identifier; + Q_D(const JsonServerClient); + return d->m_identifier; } void JsonServerClient::start() { - if (!m_authority) { - m_identifier = QUuid::createUuid().toString(); - emit authorized(m_identifier); + Q_D(JsonServerClient); + if (!d->m_authority) { + d->m_identifier = QUuid::createUuid().toString(); + emit authorized(d->m_identifier); } else { - AuthorizationRecord authRecord = m_authority.data()->clientConnected(m_stream); + AuthorizationRecord authRecord = d->m_authority.data()->clientConnected(d->m_stream); switch (authRecord.state) { case JsonAuthority::StateAuthorized: - m_identifier = authRecord.identifier; - emit authorized(m_identifier); + d->m_identifier = authRecord.identifier; + emit authorized(d->m_identifier); break; case JsonAuthority::StateNotAuthorized: emit authorizationFailed(); @@ -122,25 +145,27 @@ void JsonServerClient::start() void JsonServerClient::stop() { // qDebug() << Q_FUNC_INFO; - disconnect(m_stream, SIGNAL(messageReceived(const QJsonObject&)), + Q_D(JsonServerClient); + disconnect(d->m_stream, SIGNAL(messageReceived(const QJsonObject&)), this, SLOT(received(const QJsonObject&))); - m_socket->disconnectFromServer(); + d->m_socket->disconnectFromServer(); // qDebug() << Q_FUNC_INFO << "done"; } void JsonServerClient::received(const QJsonObject& message) { - if (m_identifier.isEmpty()) { - if (m_authority.isNull()) { + Q_D(JsonServerClient); + if (d->m_identifier.isEmpty()) { + if (d->m_authority.isNull()) { emit authorizationFailed(); stop(); return; } else { - AuthorizationRecord authRecord = m_authority.data()->messageReceived(m_stream, message); + AuthorizationRecord authRecord = d->m_authority.data()->messageReceived(d->m_stream, message); switch (authRecord.state) { case JsonAuthority::StateAuthorized: - m_identifier = authRecord.identifier; - emit authorized(m_identifier); + d->m_identifier = authRecord.identifier; + emit authorized(d->m_identifier); break; case JsonAuthority::StateNotAuthorized: emit authorizationFailed(); @@ -152,26 +177,28 @@ void JsonServerClient::received(const QJsonObject& message) } } else { - emit messageReceived(m_identifier, message); + emit messageReceived(d->m_identifier, message); } } void JsonServerClient::send(const QJsonObject &message) { - if (m_stream) { + Q_D(JsonServerClient); + if (d->m_stream) { // qDebug() << "Sending message" << message; - m_stream->send(message); + d->m_stream->send(message); } } void JsonServerClient::handleDisconnect() { // qDebug() << Q_FUNC_INFO; - m_stream->setDevice(0); - if (m_authority.data()) - m_authority.data()->clientDisconnected(m_stream); - if (!m_identifier.isEmpty()) - emit disconnected(m_identifier); + Q_D(JsonServerClient); + d->m_stream->setDevice(0); + if (d->m_authority.data()) + d->m_authority.data()->clientDisconnected(d->m_stream); + if (!d->m_identifier.isEmpty()) + emit disconnected(d->m_identifier); // qDebug() << Q_FUNC_INFO << "done"; } diff --git a/src/jsonserverclient.h b/src/jsonserverclient.h index 13b689d..7ee167d 100644 --- a/src/jsonserverclient.h +++ b/src/jsonserverclient.h @@ -43,9 +43,7 @@ #define _JSON_SERVER_CLIENT_H #include <QObject> -#include <QTimer> #include <QJsonObject> -#include <QWeakPointer> class QLocalSocket; @@ -53,9 +51,9 @@ class QLocalSocket; QT_BEGIN_NAMESPACE_JSONSTREAM -class JsonStream; class JsonAuthority; +class JsonServerClientPrivate; class Q_ADDON_JSONSTREAM_EXPORT JsonServerClient : public QObject { Q_OBJECT @@ -89,10 +87,8 @@ private slots: void handleDisconnect(); private: - QString m_identifier; - QLocalSocket *m_socket; - JsonStream *m_stream; - QWeakPointer<JsonAuthority> m_authority; + Q_DECLARE_PRIVATE(JsonServerClient) + QScopedPointer<JsonServerClientPrivate> d_ptr; }; QT_END_NAMESPACE_JSONSTREAM diff --git a/src/jsonstream.cpp b/src/jsonstream.cpp index 5ee611d..e11673e 100644 --- a/src/jsonstream.cpp +++ b/src/jsonstream.cpp @@ -66,6 +66,24 @@ static QByteArray bsonToByteArray(BsonObject &bson) return byteArray; } +/****************************************************************************/ + +/*! + \internal +*/ +class JsonStreamPrivate +{ +public: + JsonStreamPrivate() + : mDevice(0) + , mFormat(FormatUndefined) {} + + QIODevice *mDevice; + JsonBuffer *mBuffer; + EncodingFormat mFormat; +}; + +/****************************************************************************/ /*! \class JsonStream @@ -82,11 +100,11 @@ static QByteArray bsonToByteArray(BsonObject &bson) JsonStream::JsonStream(QIODevice *device) : QObject(0) - , mDevice(0) - , mFormat(FormatUndefined) + , d_ptr(new JsonStreamPrivate()) { - mBuffer = new JsonBuffer(this); - connect(mBuffer, SIGNAL(objectReceived(const QJsonObject&)), + Q_D(JsonStream); + d->mBuffer = new JsonBuffer(this); + connect(d->mBuffer, SIGNAL(objectReceived(const QJsonObject&)), SLOT(objectReceived(const QJsonObject&))); setDevice(device); } @@ -107,7 +125,8 @@ JsonStream::~JsonStream() bool JsonStream::atEnd() const { - return (mDevice && mDevice->atEnd()); + Q_D(const JsonStream); + return (d->mDevice && d->mDevice->atEnd()); } /*! @@ -117,7 +136,8 @@ bool JsonStream::atEnd() const bool JsonStream::isOpen() const { - return (mDevice && mDevice->isOpen()); + Q_D(const JsonStream); + return (d->mDevice && d->mDevice->isOpen()); } /*! @@ -126,7 +146,8 @@ bool JsonStream::isOpen() const QIODevice * JsonStream::device() const { - return mDevice; + Q_D(const JsonStream); + return d->mDevice; } /*! @@ -138,11 +159,12 @@ QIODevice * JsonStream::device() const */ void JsonStream::setDevice( QIODevice *device ) { - if (mDevice) { - disconnect(mDevice, SIGNAL(readyRead()), this, SLOT(dataReadyOnSocket())); - disconnect(mDevice, SIGNAL(aboutToClose()), this, SIGNAL(aboutToClose())); + Q_D(JsonStream); + if (d->mDevice) { + disconnect(d->mDevice, SIGNAL(readyRead()), this, SLOT(dataReadyOnSocket())); + disconnect(d->mDevice, SIGNAL(aboutToClose()), this, SIGNAL(aboutToClose())); } - mDevice = device; + d->mDevice = device; if (device) { connect(device, SIGNAL(readyRead()), this, SLOT(dataReadyOnSocket())); connect(device, SIGNAL(aboutToClose()), this, SIGNAL(aboutToClose())); @@ -157,9 +179,10 @@ void JsonStream::send(const QJsonObject& object) { QJsonDocument document(object); - switch (mFormat) { + Q_D(JsonStream); + switch (d->mFormat) { case FormatUndefined: - mFormat = FormatQBJS; + d->mFormat = FormatQBJS; // Deliberate fall through case FormatQBJS: sendInternal( document.toBinaryData() ); @@ -183,18 +206,19 @@ void JsonStream::send(const QJsonObject& object) void JsonStream::sendInternal(const QByteArray& byteArray) { - if (!mDevice) { + Q_D(JsonStream); + if (!d->mDevice) { qWarning() << Q_FUNC_INFO << "No device in JsonStream"; return; } - int nBytes = mDevice->write( byteArray.data(), byteArray.size() ); - if (QLocalSocket *socket = qobject_cast<QLocalSocket*>(mDevice)) + int nBytes = d->mDevice->write( byteArray.data(), byteArray.size() ); + if (QLocalSocket *socket = qobject_cast<QLocalSocket*>(d->mDevice)) socket->flush(); - else if (QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(mDevice)) + else if (QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(d->mDevice)) socket->flush(); else - qWarning() << Q_FUNC_INFO << "Unknown socket type:" << mDevice->metaObject()->className(); + qWarning() << Q_FUNC_INFO << "Unknown socket type:" << d->mDevice->metaObject()->className(); if (nBytes != byteArray.size()) qCritical() << Q_FUNC_INFO << __LINE__ @@ -208,8 +232,9 @@ void JsonStream::sendInternal(const QByteArray& byteArray) void JsonStream::objectReceived(const QJsonObject& object) { - if (mFormat == FormatUndefined) - mFormat = mBuffer->format(); + Q_D(JsonStream); + if (d->mFormat == FormatUndefined) + d->mFormat = d->mBuffer->format(); emit messageReceived(object); } @@ -220,7 +245,8 @@ void JsonStream::objectReceived(const QJsonObject& object) void JsonStream::dataReadyOnSocket() { - mBuffer->append( mDevice->readAll()); + Q_D(JsonStream); + d->mBuffer->append( d->mDevice->readAll()); } /*! @@ -229,7 +255,8 @@ void JsonStream::dataReadyOnSocket() EncodingFormat JsonStream::format() const { - return mFormat; + Q_D(const JsonStream); + return d->mFormat; } /*! @@ -238,7 +265,8 @@ EncodingFormat JsonStream::format() const void JsonStream::setFormat( EncodingFormat format ) { - mFormat = format; + Q_D(JsonStream); + d->mFormat = format; } diff --git a/src/jsonstream.h b/src/jsonstream.h index 2428b98..3acb204 100644 --- a/src/jsonstream.h +++ b/src/jsonstream.h @@ -42,15 +42,15 @@ #ifndef _JSON_STREAM_H #define _JSON_STREAM_H -#include <QVariant> +#include <QIODevice> #include <QJsonObject> -#include <QJsonDocument> #include "jsonstream-global.h" QT_BEGIN_NAMESPACE_JSONSTREAM class JsonBuffer; +class JsonStreamPrivate; class Q_ADDON_JSONSTREAM_EXPORT JsonStream : public QObject { Q_OBJECT @@ -81,9 +81,8 @@ protected: void sendInternal(const QByteArray& byteArray); private: - QIODevice *mDevice; - JsonBuffer *mBuffer; - EncodingFormat mFormat; + Q_DECLARE_PRIVATE(JsonStream) + QScopedPointer<JsonStreamPrivate> d_ptr; }; JsonStream& operator<<( JsonStream&, const QJsonObject& ); |