History

add API for Profile userString and multiple value requests

Stephen Birarda committed 13 years ago

Commit 32317761 parent d66137e4 files · +46 −31Browse files

Showing the changes to interface/src/DataServerClient.cpp (1 of 4 files). Show all

Unified · Split

interface/src/DataServerClient.cpp+31−30
@@ -26,11 +26,7 @@ const unsigned short DATA_SERVER_PORT = 3282; · 25 unmodified lines
26const sockaddr_in DATA_SERVER_SOCKET = socketForHostnameAndHostOrderPort(DATA_SERVER_HOSTNAME, DATA_SERVER_PORT);26const sockaddr_in DATA_SERVER_SOCKET = socketForHostnameAndHostOrderPort(DATA_SERVER_HOSTNAME, DATA_SERVER_PORT);
27 27
28void DataServerClient::putValueForKey(const char* key, const char* value) {28void DataServerClient::putValueForKey(const char* key, const char* value) {
29 Profile* userProfile = Application::getInstance()->getProfile();29 QString clientString = Application::getInstance()->getProfile()->getUserString();
30 QString clientString = userProfile->getUUID().isNull()
31 ? userProfile->getUsername()
32 : uuidStringWithoutCurlyBraces(userProfile->getUUID().toString());
33
34 if (!clientString.isEmpty()) {30 if (!clientString.isEmpty()) {
35 31
36 unsigned char* putPacket = new unsigned char[MAX_PACKET_SIZE];32 unsigned char* putPacket = new unsigned char[MAX_PACKET_SIZE];
@@ -62,40 +58,45 @@ void DataServerClient::putValueForKey(const char* key, const char* value) { · 25 unmodified lines
62}58}
63 59
64void DataServerClient::getValueForKeyAndUUID(const char* key, const QUuid &uuid) {60void DataServerClient::getValueForKeyAndUUID(const char* key, const QUuid &uuid) {
61 getValuesForKeysAndUUID(QStringList(QString(key)), uuid);
62}
63
64void DataServerClient::getValuesForKeysAndUUID(const QStringList& keys, const QUuid& uuid) {
65 if (!uuid.isNull()) {65 if (!uuid.isNull()) {
66 getValueForKeyAndUserString(key, uuidStringWithoutCurlyBraces(uuid));66 getValuesForKeysAndUserString(keys, uuidStringWithoutCurlyBraces(uuid));
67 }67 }
68}68}
69 69
70void DataServerClient::getValueForKeyAndUserString(const char* key, const QString& userString) {70void DataServerClient::getValuesForKeysAndUserString(const QStringList& keys, const QString& userString) {
71 unsigned char* getPacket = new unsigned char[MAX_PACKET_SIZE];71 if (!userString.isEmpty()) {
72 72 unsigned char* getPacket = new unsigned char[MAX_PACKET_SIZE];
73 // setup the header for this packet73
74 int numPacketBytes = populateTypeAndVersion(getPacket, PACKET_TYPE_DATA_SERVER_GET);74 // setup the header for this packet
75 75 int numPacketBytes = populateTypeAndVersion(getPacket, PACKET_TYPE_DATA_SERVER_GET);
76 // pack the user string (could be username or UUID string), null-terminate76
77 memcpy(getPacket + numPacketBytes, userString.toLocal8Bit().constData(), userString.toLocal8Bit().size());77 // pack the user string (could be username or UUID string), null-terminate
78 numPacketBytes += userString.toLocal8Bit().size();78 memcpy(getPacket + numPacketBytes, userString.toLocal8Bit().constData(), userString.toLocal8Bit().size());
79 getPacket[numPacketBytes++] = '\0';79 numPacketBytes += userString.toLocal8Bit().size();
80
81 // pack the key, null terminated
82 strcpy((char*) getPacket + numPacketBytes, key);
83 int numKeyBytes = strlen(key);
84
85 if (numKeyBytes > 0) {
86 numPacketBytes += numKeyBytes;
87 getPacket[numPacketBytes++] = '\0';80 getPacket[numPacketBytes++] = '\0';
81
82 for (int i = 0; i < keys.size(); ++i) {
83 // pack the keys, null terminated
84 strcpy((char*) getPacket + numPacketBytes, keys[i].toLocal8Bit().constData());
85
86 numPacketBytes += keys[i].size();
87 getPacket[numPacketBytes++] = '\0';
88 }
89
90 // add the getPacket to our vector of uncofirmed packets, will be deleted once we get a response from the nameserver
91 _unmatchedPackets.insert(std::pair<unsigned char*, int>(getPacket, numPacketBytes));
92
93 // send the get to the data server
94 NodeList::getInstance()->getNodeSocket()->send((sockaddr*) &DATA_SERVER_SOCKET, getPacket, numPacketBytes);
88 }95 }
89
90 // add the getPacket to our vector of uncofirmed packets, will be deleted once we get a response from the nameserver
91 _unmatchedPackets.insert(std::pair<unsigned char*, int>(getPacket, numPacketBytes));
92
93 // send the get to the data server
94 NodeList::getInstance()->getNodeSocket()->send((sockaddr*) &DATA_SERVER_SOCKET, getPacket, numPacketBytes);
95}96}
96 97
97void DataServerClient::getClientValueForKey(const char* key) {98void DataServerClient::getClientValueForKey(const char* key) {
98 getValueForKeyAndUserString(key, Application::getInstance()->getProfile()->getUsername());99 getValuesForKeysAndUserString(QStringList(QString(key)), Application::getInstance()->getProfile()->getUserString());
99}100}
100 101
101void DataServerClient::processConfirmFromDataServer(unsigned char* packetData, int numPacketBytes) {102void DataServerClient::processConfirmFromDataServer(unsigned char* packetData, int numPacketBytes) {