Collapsed individual reply signals to single reply signal that emits reference to Reply.

This commit is contained in:
Nathan Osman 2013-08-01 15:43:58 -07:00
parent d42dc66f4e
commit fa702fbe15
7 changed files with 25 additions and 92 deletions

View File

@ -18,6 +18,7 @@ namespace QRedis
* @brief Reply types * @brief Reply types
*/ */
enum Type { enum Type {
/** /**
* @brief A status reply * @brief A status reply
* *
@ -25,6 +26,7 @@ namespace QRedis
* by the server as a QString. * by the server as a QString.
*/ */
Status, Status,
/** /**
* @brief An error reply * @brief An error reply
* *
@ -32,6 +34,7 @@ namespace QRedis
* the server as a QString. * the server as a QString.
*/ */
Error, Error,
/** /**
* @brief An integer reply * @brief An integer reply
* *
@ -39,6 +42,7 @@ namespace QRedis
* the server as a qlonglong. * the server as a qlonglong.
*/ */
Integer, Integer,
/** /**
* @brief A bulk reply * @brief A bulk reply
* *
@ -46,6 +50,7 @@ namespace QRedis
* the server as a QByteArray. * the server as a QByteArray.
*/ */
Bulk, Bulk,
/** /**
* @brief A multi-bulk reply * @brief A multi-bulk reply
* *
@ -73,7 +78,7 @@ namespace QRedis
* @brief Returns the value of the reply * @brief Returns the value of the reply
* @return the reply value * @return the reply value
*/ */
QVariant & value() const { return _value; } const QVariant & value() const { return _value; }
private: private:

View File

@ -5,6 +5,7 @@
#include <QScopedPointer> #include <QScopedPointer>
#include <QVariantList> #include <QVariantList>
#include <qredis/reply.h>
#include "qredis_export.h" #include "qredis_export.h"
namespace QRedis namespace QRedis
@ -41,40 +42,10 @@ namespace QRedis
Q_SIGNALS: Q_SIGNALS:
/** /**
* @brief Emitted when a status reply is received * @brief Emitted when a reply is received
* @param message a descriptive status message * @param reply the reply received
*/ */
void status(const QString & message); void reply(Reply & reply);
/**
* @brief Emitted when an error reply is received
* @param generic a generic error identifer
* @param specific a more specific error message
*/
void error(const QString & generic, const QString & specific);
/**
* @brief Emitted when an integer reply is received
* @param value the integer value
*/
void integer(qlonglong value);
/**
* @brief Emitted when a bulk reply is received
* @param value the value as a byte array
*/
void bulk(const QByteArray & value);
/**
* @brief Emitted when a multi-bulk reply is received
* @param a list of bulk values
*/
void multiBulk(const QVariantList & values);
/**
* @brief Emitted when any reply is received
*/
void reply();
private: private:

View File

@ -8,37 +8,12 @@ ClientPrivate::ClientPrivate(Client * client)
{ {
connect(&socket, SIGNAL(connected()), client, SIGNAL(connected())); connect(&socket, SIGNAL(connected()), client, SIGNAL(connected()));
connect(&socket, SIGNAL(disconnected()), client, SIGNAL(disconnected())); connect(&socket, SIGNAL(disconnected()), client, SIGNAL(disconnected()));
connect(&parser, SIGNAL(reply(QRedis::Reply&)), SLOT(sendReply(Reply&)));
connect(&parser, SIGNAL(status(QString)), SLOT(sendStatus(QString)));
connect(&parser, SIGNAL(error(QString,QString)), SLOT(sendError(QString,QString)));
connect(&parser, SIGNAL(integer(qlonglong)), SLOT(sendInteger(qlonglong)));
connect(&parser, SIGNAL(bulk(QByteArray)), SLOT(sendBulk(QByteArray)));
connect(&parser, SIGNAL(multiBulk(QVariantList)), SLOT(sendMultiBulk(QVariantList)));
} }
void ClientPrivate::sendStatus(const QString & message) void ClientPrivate::sendReply(Reply & reply)
{ {
emit queue.dequeue()->status(message); emit queue.dequeue()->reply(reply);
}
void ClientPrivate::sendError(const QString & generic, const QString & specific)
{
emit queue.dequeue()->error(generic, specific);
}
void ClientPrivate::sendInteger(qlonglong value)
{
emit queue.dequeue()->integer(value);
}
void ClientPrivate::sendBulk(const QByteArray & value)
{
emit queue.dequeue()->bulk(value);
}
void ClientPrivate::sendMultiBulk(const QVariantList & values)
{
emit queue.dequeue()->multiBulk(values);
} }
Client::Client(QObject * parent) Client::Client(QObject * parent)

View File

@ -6,6 +6,7 @@
#include <QTcpSocket> #include <QTcpSocket>
#include <qredis/client.h> #include <qredis/client.h>
#include <qredis/reply.h>
#include <qredis/request.h> #include <qredis/request.h>
#include "lexer.h" #include "lexer.h"
#include "parser.h" #include "parser.h"
@ -28,11 +29,7 @@ namespace QRedis
private Q_SLOTS: private Q_SLOTS:
void sendStatus(const QString &); void sendReply(Reply &);
void sendError(const QString &, const QString &);
void sendInteger(qlonglong);
void sendBulk(const QByteArray &);
void sendMultiBulk(const QVariantList &);
}; };
} }

View File

@ -37,14 +37,14 @@ void Parser::readUnsafeString(const QString & value)
stack.removeLast(); stack.removeLast();
if(action == Task::ReadStatus) if(action == Task::ReadStatus)
emit status(value); ;//emit status(value);
else if(action == Task::ReadError) else if(action == Task::ReadError)
{ {
int pos = value.indexOf(' '); int pos = value.indexOf(' ');
emit error(value.left(pos), value.right(pos + 1)); ;//emit error(value.left(pos), value.right(pos + 1));
} }
else if(!stack.count()) else if(!stack.count())
emit integer(value.toLongLong()); ;//emit integer(value.toLongLong());
else else
{ {
tos().value.toList().append(value); tos().value.toList().append(value);
@ -58,7 +58,7 @@ void Parser::readSafeString(const QByteArray & value)
stack.removeLast(); stack.removeLast();
if(!stack.count()) if(!stack.count())
emit bulk(value); ;//emit bulk(value);
else else
{ {
tos().value.toList().append(value); tos().value.toList().append(value);
@ -75,7 +75,7 @@ void Parser::descend()
if(stack.count() == 1) if(stack.count() == 1)
{ {
emit multiBulk(tos().value.toList()); ;//emit multiBulk(tos().value.toList());
stack.removeLast(); stack.removeLast();
break; break;
} }

View File

@ -6,6 +6,7 @@
#include <QVariant> #include <QVariant>
#include <QVariantList> #include <QVariantList>
#include <qredis/reply.h>
#include "lexer.h" #include "lexer.h"
class Parser : public QObject class Parser : public QObject
@ -19,11 +20,7 @@ class Parser : public QObject
Q_SIGNALS: Q_SIGNALS:
void status(const QString &); void reply(QRedis::Reply &);
void error(const QString &, const QString &);
void integer(qlonglong);
void bulk(const QByteArray &);
void multiBulk(const QVariantList &);
private Q_SLOTS: private Q_SLOTS:

View File

@ -13,19 +13,7 @@ void RequestPrivate::quitEventLoop()
Request::Request(QObject * parent) Request::Request(QObject * parent)
: QObject(parent), d(new RequestPrivate) : QObject(parent), d(new RequestPrivate)
{ {
/* connect(this, SIGNAL(reply(Reply&)), SLOT(deleteLater()));
* Each of these signals also causes the generic reply() signal to be emitted.
*/
connect(this, SIGNAL(bulk(QByteArray)), SIGNAL(reply()));
connect(this, SIGNAL(error(QString,QString)), SIGNAL(reply()));
connect(this, SIGNAL(integer(qlonglong)), SIGNAL(reply()));
connect(this, SIGNAL(multiBulk(QVariantList)), SIGNAL(reply()));
connect(this, SIGNAL(status(QString)), SIGNAL(reply()));
/*
* We also need to ensure that this object is deleted when the reply is received.
*/
connect(this, SIGNAL(reply()), SLOT(deleteLater()));
} }
Request::~Request() Request::~Request()
@ -38,8 +26,8 @@ bool Request::waitForReply(int msecs)
timer.setInterval(msecs); timer.setInterval(msecs);
timer.setSingleShot(true); timer.setSingleShot(true);
connect(&timer, SIGNAL(timeout()), &d->loop, SLOT(quit())); connect(&timer, SIGNAL(timeout()), &d->loop, SLOT(quit()));
connect(this, SIGNAL(reply()), d.data(), SLOT(quitEventLoop())); connect(this, SIGNAL(reply(Reply&)), d.data(), SLOT(quitEventLoop()));
/* /*
* If the timer fires, the return value will be 0. * If the timer fires, the return value will be 0.