repairs to new Qt'ed DataServer and DataServerClient

This commit is contained in:
Stephen Birarda 2014-01-20 16:55:14 -08:00
parent 657589fa64
commit cf40fb0ef8
6 changed files with 27 additions and 26 deletions

View file

@ -66,7 +66,8 @@ void DataServer::readPendingDatagrams() {
int readBytes = numBytesForPacketHeader(packetData); int readBytes = numBytesForPacketHeader(packetData);
// pull the sequence number used for this packet // pull the sequence number used for this packet
quint16 sequenceNumber = *reinterpret_cast<quint16>(packetData + readBytes); quint8 sequenceNumber = 0;
memcpy(&sequenceNumber, packetData + readBytes, sizeof(sequenceNumber));
readBytes += sizeof(sequenceNumber); readBytes += sizeof(sequenceNumber);
// pull the UUID that we will need as part of the key // pull the UUID that we will need as part of the key
@ -77,8 +78,7 @@ void DataServer::readPendingDatagrams() {
// we failed to parse a UUID, this means the user has sent us a username // we failed to parse a UUID, this means the user has sent us a username
QString username(reinterpret_cast<char*>(packetData + readBytes)); QString username(reinterpret_cast<char*>(packetData + readBytes));
readBytes += sizeof(username) + sizeof('\0'); readBytes += username.size() + sizeof('\0');
// ask redis for the UUID for this user // ask redis for the UUID for this user
redisReply* reply = (redisReply*) redisCommand(_redis, "GET user:%s", qPrintable(username)); redisReply* reply = (redisReply*) redisCommand(_redis, "GET user:%s", qPrintable(username));
@ -113,7 +113,7 @@ void DataServer::readPendingDatagrams() {
QString dataValue(reinterpret_cast<char*>(packetData + readBytes)); QString dataValue(reinterpret_cast<char*>(packetData + readBytes));
readBytes += dataValue.size() + sizeof('\0'); readBytes += dataValue.size() + sizeof('\0');
qDebug("Sending command to redis: SET uuid:%s:%s %s\n", qDebug("Sending command to redis: SET uuid:%s:%s %s",
qPrintable(uuidStringWithoutCurlyBraces(parsedUUID)), qPrintable(uuidStringWithoutCurlyBraces(parsedUUID)),
qPrintable(dataKey), qPrintable(dataValue)); qPrintable(dataKey), qPrintable(dataValue));
@ -133,19 +133,15 @@ void DataServer::readPendingDatagrams() {
} else { } else {
// setup a send packet with the returned data // setup a send packet with the returned data
// leverage the packetData sent by overwriting and appending // leverage the packetData sent by overwriting and appending
int numSendPacketBytes = 0; int numSendPacketBytes = receivedBytes;
numSendPacketBytes += populateTypeAndVersion(packetData, PACKET_TYPE_DATA_SERVER_SEND); packetData[0] = PACKET_TYPE_DATA_SERVER_SEND;
memcpy(packetData + numSendPacketBytes, &sequenceNumber, sizeof(sequenceNumber));
numSendPacketBytes += sizeof(sequenceNumber);
const char MULTI_KEY_VALUE_SEPARATOR = '|'; const char MULTI_KEY_VALUE_SEPARATOR = '|';
if (strcmp((char*) packetData + readBytes, "uuid") != 0) { if (strcmp((char*) packetData + readBytes, "uuid") != 0) {
// the user has sent one or more keys - make the associated requests // the user has sent one or more keys - make the associated requests
for (int j = 0; j < numKeys; j++) { for (int j = 0; j < numKeys; j++) {
// pull the key that specifies the data the user is putting/getting, null terminate it // pull the key that specifies the data the user is putting/getting, null terminate it
@ -160,7 +156,7 @@ void DataServer::readPendingDatagrams() {
QString dataKey(QByteArray(reinterpret_cast<char*>(packetData + readBytes), numDataKeyBytes)); QString dataKey(QByteArray(reinterpret_cast<char*>(packetData + readBytes), numDataKeyBytes));
readBytes += dataKey.size() + sizeof('\0'); readBytes += dataKey.size() + sizeof('\0');
qDebug("Sending command to redis: GET uuid:%s:%s\n", qDebug("Sending command to redis: GET uuid:%s:%s",
qPrintable(uuidStringWithoutCurlyBraces(parsedUUID)), qPrintable(uuidStringWithoutCurlyBraces(parsedUUID)),
qPrintable(dataKey)); qPrintable(dataKey));
redisReply* reply = (redisReply*) redisCommand(_redis, "GET uuid:%s:%s", redisReply* reply = (redisReply*) redisCommand(_redis, "GET uuid:%s:%s",
@ -182,16 +178,21 @@ void DataServer::readPendingDatagrams() {
freeReplyObject(reply); freeReplyObject(reply);
} }
// null terminate the packet we're sending back (erases the trailing separator)
packetData[(numSendPacketBytes - 1)] = '\0';
} else { } else {
// user is asking for a UUID matching username, copy the UUID we found // user is asking for a UUID matching username, copy the UUID we found
QString uuidString = uuidStringWithoutCurlyBraces(parsedUUID); QString uuidString = uuidStringWithoutCurlyBraces(parsedUUID);
memcpy(packetData + numSendPacketBytes, uuidString.constData(), uuidString.size() + sizeof('\0')); memcpy(packetData + numSendPacketBytes, uuidString.constData(), uuidString.size() + sizeof('\0'));
numSendPacketBytes = uuidString.size() + sizeof('\0'); numSendPacketBytes += uuidString.size() + sizeof('\0');
} }
// reply back with the send packet // reply back with the send packet
_socket.writeDatagram(reinterpret_cast<char*>(packetData), numSendPacketBytes, _socket.writeDatagram(reinterpret_cast<char*>(packetData), numSendPacketBytes,
senderAddress, senderPort); senderAddress, senderPort);
} }
} }
} }

View file

@ -12,12 +12,16 @@
#include "DataServer.h" #include "DataServer.h"
const char DATA_SERVER_LOGGING_TARGET_NAME[] = "data-server";
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
setvbuf(stdout, NULL, _IOLBF, 0); setvbuf(stdout, NULL, _IOLBF, 0);
qInstallMessageHandler(Logging::verboseMessageHandler); qInstallMessageHandler(Logging::verboseMessageHandler);
Logging::setTargetName(DATA_SERVER_LOGGING_TARGET_NAME);
DataServer dataServer(argc, argv); DataServer dataServer(argc, argv);
return dataServer.exec(); return dataServer.exec();

View file

@ -24,7 +24,7 @@ quint8 DataServerClient::_sequenceNumber = 0;
const char MULTI_KEY_VALUE_SEPARATOR = '|'; const char MULTI_KEY_VALUE_SEPARATOR = '|';
const char DATA_SERVER_HOSTNAME[] = "data.highfidelity.io"; const char DATA_SERVER_HOSTNAME[] = "localhost";
const unsigned short DATA_SERVER_PORT = 3282; const unsigned short DATA_SERVER_PORT = 3282;
const HifiSockAddr& DataServerClient::dataServerSockAddr() { const HifiSockAddr& DataServerClient::dataServerSockAddr() {
@ -35,7 +35,7 @@ const HifiSockAddr& DataServerClient::dataServerSockAddr() {
void DataServerClient::putValueForKey(const QString& key, const char* value) { void DataServerClient::putValueForKey(const QString& key, const char* value) {
if (!_clientIdentifier.isEmpty()) { if (!_clientIdentifier.isEmpty()) {
static unsigned char putPacket[MAX_PACKET_SIZE]; unsigned char putPacket[MAX_PACKET_SIZE];
// setup the header for this packet // setup the header for this packet
int numPacketBytes = populateTypeAndVersion(putPacket, PACKET_TYPE_DATA_SERVER_PUT); int numPacketBytes = populateTypeAndVersion(putPacket, PACKET_TYPE_DATA_SERVER_PUT);
@ -88,7 +88,7 @@ void DataServerClient::getValuesForKeysAndUUID(const QStringList& keys, const QU
void DataServerClient::getValuesForKeysAndUserString(const QStringList& keys, const QString& userString, void DataServerClient::getValuesForKeysAndUserString(const QStringList& keys, const QString& userString,
DataServerCallbackObject* callbackObject) { DataServerCallbackObject* callbackObject) {
if (!userString.isEmpty() && keys.size() <= UCHAR_MAX) { if (!userString.isEmpty() && keys.size() <= UCHAR_MAX) {
static unsigned char getPacket[MAX_PACKET_SIZE]; unsigned char getPacket[MAX_PACKET_SIZE];
// setup the header for this packet // setup the header for this packet
int numPacketBytes = populateTypeAndVersion(getPacket, PACKET_TYPE_DATA_SERVER_GET); int numPacketBytes = populateTypeAndVersion(getPacket, PACKET_TYPE_DATA_SERVER_GET);
@ -146,19 +146,15 @@ void DataServerClient::processSendFromDataServer(unsigned char* packetData, int
DataServerCallbackObject* callbackObject = _callbackObjects.take(_sequenceNumber); DataServerCallbackObject* callbackObject = _callbackObjects.take(_sequenceNumber);
char* userStringPosition = (char*) packetData + numHeaderBytes + sizeof(_sequenceNumber); char* userStringPosition = (char*) packetData + numHeaderBytes + sizeof(_sequenceNumber);
QString userString(userStringPosition);
QString userString(QByteArray(userStringPosition, strlen(userStringPosition))); char* keysPosition = userStringPosition + userString.size() + sizeof('\0') + sizeof(unsigned char);
QUuid userUUID(userString);
char* keysPosition = (char*) packetData + numHeaderBytes + strlen(userStringPosition)
+ sizeof('\0') + sizeof(unsigned char);
char* valuesPosition = keysPosition + strlen(keysPosition) + sizeof('\0'); char* valuesPosition = keysPosition + strlen(keysPosition) + sizeof('\0');
QStringList keyList = QString(keysPosition).split(MULTI_KEY_VALUE_SEPARATOR); QStringList keyList = QString(keysPosition).split(MULTI_KEY_VALUE_SEPARATOR);
QStringList valueList = QString(valuesPosition).split(MULTI_KEY_VALUE_SEPARATOR); QStringList valueList = QString(valuesPosition).split(MULTI_KEY_VALUE_SEPARATOR);
callbackObject->processDataServerResponse(userUUID, keyList, valueList); callbackObject->processDataServerResponse(userString, keyList, valueList);
} }

View file

@ -19,7 +19,7 @@
class DataServerCallbackObject { class DataServerCallbackObject {
public: public:
virtual void processDataServerResponse(const QUuid& userUUID, const QStringList& keyList, const QStringList& valueList) = 0; virtual void processDataServerResponse(const QString& userString, const QStringList& keyList, const QStringList& valueList) = 0;
}; };
class DataServerClient { class DataServerClient {

View file

@ -146,7 +146,7 @@ void Profile::loadData(QSettings* settings) {
settings->endGroup(); settings->endGroup();
} }
void Profile::processDataServerResponse(const QUuid& userUUID, const QStringList& keyList, const QStringList& valueList) { void Profile::processDataServerResponse(const QString& userString, const QStringList& keyList, const QStringList& valueList) {
qDebug() << "Called with" << keyList << valueList; qDebug() << "Called with" << keyList << valueList;
} }

View file

@ -47,7 +47,7 @@ public:
void saveData(QSettings* settings); void saveData(QSettings* settings);
void loadData(QSettings* settings); void loadData(QSettings* settings);
void processDataServerResponse(const QUuid& userUUID, const QStringList& keyList, const QStringList& valueList); void processDataServerResponse(const QString& userString, const QStringList& keyList, const QStringList& valueList);
private: private:
void setUsername(const QString& username); void setUsername(const QString& username);