Added separate signals for each type of reply.
This commit is contained in:
parent
95d1ca09f6
commit
e049215a70
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QScopedPointer>
|
#include <QScopedPointer>
|
||||||
#include <QVariant>
|
#include <QVariantList>
|
||||||
|
|
||||||
#include "qredis_export.h"
|
#include "qredis_export.h"
|
||||||
|
|
||||||
|
|
@ -20,17 +20,6 @@ namespace QRedis
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Types of replies received from the Redis server
|
|
||||||
*/
|
|
||||||
enum ReplyType {
|
|
||||||
Status,
|
|
||||||
Error,
|
|
||||||
Integer,
|
|
||||||
Bulk,
|
|
||||||
MultiBulk
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Creates a request
|
* @brief Creates a request
|
||||||
* @param parent
|
* @param parent
|
||||||
|
|
@ -52,11 +41,34 @@ namespace QRedis
|
||||||
Q_SIGNALS:
|
Q_SIGNALS:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Emitted when a reply is received
|
* @brief Emitted when a bulk reply is received
|
||||||
* @param type the type of value received
|
* @param value the value as a byte array
|
||||||
* @param value the value received
|
|
||||||
*/
|
*/
|
||||||
void reply(ReplyType type, const QVariant & value);
|
void bulk(const QByteArray & value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Emitted when an error reply is received
|
||||||
|
* @param message a descriptive error message
|
||||||
|
*/
|
||||||
|
void error(const QString & message);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Emitted when an integer reply is received
|
||||||
|
* @param value the integer value
|
||||||
|
*/
|
||||||
|
void integer(qlonglong value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Emitted when a multi-bulk reply is received
|
||||||
|
* @param a list of bulk values
|
||||||
|
*/
|
||||||
|
void multiBulk(const QVariantList & values);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Emitted when a status reply is received
|
||||||
|
* @param message a descriptive status message
|
||||||
|
*/
|
||||||
|
void status(const QString & message);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,14 +12,26 @@ ClientPrivate::ClientPrivate(Client * client)
|
||||||
connect(&socket, &QTcpSocket::readyRead, this, &ClientPrivate::readReply);
|
connect(&socket, &QTcpSocket::readyRead, this, &ClientPrivate::readReply);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Note: error replies actually contain a type and then the error description
|
||||||
|
* but we just combine them here for simplicity.
|
||||||
|
*/
|
||||||
|
|
||||||
bool ClientPrivate::readStatusOrError()
|
bool ClientPrivate::readStatusOrError()
|
||||||
{
|
{
|
||||||
/* Check if the reply contains \r\n. */
|
/* Check if the reply contains \r\n. */
|
||||||
int pos = buffer.indexOf("\r\n");
|
int pos = buffer.indexOf("\r\n");
|
||||||
if(pos != -1)
|
if(pos != -1)
|
||||||
{
|
{
|
||||||
emit queue.dequeue()->reply(Request::Status, buffer.mid(1, pos - 1));
|
Request * request = queue.dequeue();
|
||||||
|
|
||||||
|
if(buffer[0] == '+')
|
||||||
|
emit request->status(buffer.mid(1, pos - 1));
|
||||||
|
else
|
||||||
|
emit request->error(buffer.mid(1, pos - 1));
|
||||||
|
|
||||||
buffer.remove(0, pos + 2);
|
buffer.remove(0, pos + 2);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -27,6 +39,16 @@ bool ClientPrivate::readStatusOrError()
|
||||||
|
|
||||||
bool ClientPrivate::readInteger()
|
bool ClientPrivate::readInteger()
|
||||||
{
|
{
|
||||||
|
/* Check if the reply contains \r\n. */
|
||||||
|
int pos = buffer.indexOf("\r\n");
|
||||||
|
if(pos != -1)
|
||||||
|
{
|
||||||
|
emit queue.dequeue()->integer(buffer.mid(1, pos -1).toLongLong());
|
||||||
|
|
||||||
|
buffer.remove(0, pos + 2);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,11 @@ bool Request::waitForReply(int 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(ReplyType,QVariant)), d.data(), SLOT(quitEventLoop()));
|
connect(this, SIGNAL(bulk(QByteArray)), d.data(), SLOT(quitEventLoop()));
|
||||||
|
connect(this, SIGNAL(error(QString)), d.data(), SLOT(quitEventLoop()));
|
||||||
|
connect(this, SIGNAL(integer(qlonglong)), d.data(), SLOT(quitEventLoop()));
|
||||||
|
connect(this, SIGNAL(multiBulk(QVariantList)), d.data(), SLOT(quitEventLoop()));
|
||||||
|
connect(this, SIGNAL(status(QString)), d.data(), SLOT(quitEventLoop()));
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If the timer fires, the return value will be 0.
|
* If the timer fires, the return value will be 0.
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,6 @@
|
||||||
|
|
||||||
void TestClient::initTestCase()
|
void TestClient::initTestCase()
|
||||||
{
|
{
|
||||||
qRegisterMetaType<QRedis::Request::ReplyType>("ReplyType");
|
|
||||||
|
|
||||||
client.connectToHost("localhost");
|
client.connectToHost("localhost");
|
||||||
QVERIFY(client.waitForConnected());
|
QVERIFY(client.waitForConnected());
|
||||||
}
|
}
|
||||||
|
|
@ -23,9 +21,9 @@ void TestClient::cleanupTestCase()
|
||||||
void TestClient::testPing()
|
void TestClient::testPing()
|
||||||
{
|
{
|
||||||
QRedis::Request * request = client.sendCommand("PING");
|
QRedis::Request * request = client.sendCommand("PING");
|
||||||
QSignalSpy spy(request, SIGNAL(reply(ReplyType,QVariant)));
|
QSignalSpy spy(request, SIGNAL(status(QString)));
|
||||||
|
|
||||||
QVERIFY(request->waitForReply());
|
QVERIFY(request->waitForReply());
|
||||||
QCOMPARE(spy.count(), 1);
|
QCOMPARE(spy.count(), 1);
|
||||||
QCOMPARE(spy[0][1].toString(), QString("PONG"));
|
QCOMPARE(spy[0][0].toString(), QString("PONG"));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue