History

add a Profile class to hold user data from data-server

Stephen Birarda committed 13 years ago

Commit 85edb931 parent 56b71eb9 files · +150 −56Browse files

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

Unified · Split

interface/src/DataServerClient.cpp+46−35
@@ -14,9 +14,11 @@ · 13 unmodified lines
14#include <UUID.h>14#include <UUID.h>
15 15
16#include "Application.h"16#include "Application.h"
17#include "Profile.h"
18
17#include "DataServerClient.h"19#include "DataServerClient.h"
18 20
19QString DataServerClient::_clientUsername;21
20std::map<unsigned char*, int> DataServerClient::_unmatchedPackets;22std::map<unsigned char*, int> DataServerClient::_unmatchedPackets;
21 23
22const char DATA_SERVER_HOSTNAME[] = "127.0.0.1";24const char DATA_SERVER_HOSTNAME[] = "127.0.0.1";
@@ -24,33 +26,36 @@ const unsigned short DATA_SERVER_PORT = 3282; · 1 unmodified line
24const 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);
25 27
26void DataServerClient::putValueForKey(const char* key, const char* value) {28void DataServerClient::putValueForKey(const char* key, const char* value) {
27 if (!_clientUsername.isEmpty()) {29 Profile* userProfile = Application::getInstance()->getProfile();
28 unsigned char* putPacket = new unsigned char[MAX_PACKET_SIZE];30 QString clientString = userProfile->getUUID().isNull() ? userProfile->getUsername() : userProfile->getUUID().toString();
29 31
30 // setup the header for this packet32 Application::getInstance();
31 int numPacketBytes = populateTypeAndVersion(putPacket, PACKET_TYPE_DATA_SERVER_PUT);33
32 34 unsigned char* putPacket = new unsigned char[MAX_PACKET_SIZE];
33 // pack the client UUID, null terminated35
34 memcpy(putPacket + numPacketBytes, _clientUsername.toLocal8Bit().constData(), _clientUsername.toLocal8Bit().size());36 // setup the header for this packet
35 numPacketBytes += _clientUsername.toLocal8Bit().size();37 int numPacketBytes = populateTypeAndVersion(putPacket, PACKET_TYPE_DATA_SERVER_PUT);
36 putPacket[numPacketBytes++] = '\0';38
37 39 // pack the client UUID, null terminated
38 // pack the key, null terminated40 memcpy(putPacket + numPacketBytes, clientString.toLocal8Bit().constData(), clientString.toLocal8Bit().size());
39 strcpy((char*) putPacket + numPacketBytes, key);41 numPacketBytes += clientString.toLocal8Bit().size();
40 numPacketBytes += strlen(key);42 putPacket[numPacketBytes++] = '\0';
41 putPacket[numPacketBytes++] = '\0';43
42 44 // pack the key, null terminated
43 // pack the value, null terminated45 strcpy((char*) putPacket + numPacketBytes, key);
44 strcpy((char*) putPacket + numPacketBytes, value);46 numPacketBytes += strlen(key);
45 numPacketBytes += strlen(value);47 putPacket[numPacketBytes++] = '\0';
46 putPacket[numPacketBytes++] = '\0';48
47 49 // pack the value, null terminated
48 // add the putPacket to our vector of unconfirmed packets, will be deleted once put is confirmed50 strcpy((char*) putPacket + numPacketBytes, value);
49 _unmatchedPackets.insert(std::pair<unsigned char*, int>(putPacket, numPacketBytes));51 numPacketBytes += strlen(value);
50 52 putPacket[numPacketBytes++] = '\0';
51 // send this put request to the data server53
52 NodeList::getInstance()->getNodeSocket()->send((sockaddr*) &DATA_SERVER_SOCKET, putPacket, numPacketBytes);54 // add the putPacket to our vector of unconfirmed packets, will be deleted once put is confirmed
53 }55 _unmatchedPackets.insert(std::pair<unsigned char*, int>(putPacket, numPacketBytes));
56
57 // send this put request to the data server
58 NodeList::getInstance()->getNodeSocket()->send((sockaddr*) &DATA_SERVER_SOCKET, putPacket, numPacketBytes);
54}59}
55 60
56void DataServerClient::getValueForKeyAndUUID(const char* key, QUuid &uuid) {61void DataServerClient::getValueForKeyAndUUID(const char* key, QUuid &uuid) {
@@ -83,7 +88,9 @@ void DataServerClient::getValueForKeyAndUserString(const char* key, QString& use · 26 unmodified lines
83 NodeList::getInstance()->getNodeSocket()->send((sockaddr*) &DATA_SERVER_SOCKET, getPacket, numPacketBytes);88 NodeList::getInstance()->getNodeSocket()->send((sockaddr*) &DATA_SERVER_SOCKET, getPacket, numPacketBytes);
84}89}
85 90
86 91void DataServerClient::getClientValueForKey(const char* key) {
92 getValueForKeyAndUserString(key, Application::getInstance()->getProfile()->getUsername());
93}
87 94
88void DataServerClient::processConfirmFromDataServer(unsigned char* packetData, int numPacketBytes) {95void DataServerClient::processConfirmFromDataServer(unsigned char* packetData, int numPacketBytes) {
89 removeMatchedPacketFromMap(packetData, numPacketBytes);96 removeMatchedPacketFromMap(packetData, numPacketBytes);
@@ -102,18 +109,22 @@ void DataServerClient::processSendFromDataServer(unsigned char* packetData, int · 12 unmodified lines
102 // for now assume this means that it is for our avatar109 // for now assume this means that it is for our avatar
103 110
104 char* dataKeyPosition = (char*) packetData + numHeaderBytes + sizeof(userString);111 char* dataKeyPosition = (char*) packetData + numHeaderBytes + sizeof(userString);
112 char* dataValuePosition = dataKeyPosition + strlen(dataKeyPosition) + sizeof(char);
113
114 QString dataValueString(QByteArray(dataValuePosition,
115 numPacketBytes - ((unsigned char*) dataValuePosition - packetData)));
105 116
106 if (strcmp(dataKeyPosition, DataServerKey::FaceMeshURL) == 0) {117 if (strcmp(dataKeyPosition, DataServerKey::FaceMeshURL) == 0) {
107 // pull the user's face mesh and set it on the Avatar instance118 // pull the user's face mesh and set it on the Avatar instance
108 char* faceMeshPosition = dataKeyPosition + strlen(dataKeyPosition) + sizeof(char);
109
110 QUrl faceMeshURL(QByteArray(faceMeshPosition,
111 numPacketBytes - ((unsigned char*) faceMeshPosition - packetData)));
112 119
113 qDebug("Changing user's face model URL to %s\n", faceMeshURL.toString().toLocal8Bit().constData());120
121 qDebug("Changing user's face model URL to %s\n", dataValueString.toLocal8Bit().constData());
114 QMetaObject::invokeMethod(&Application::getInstance()->getAvatar()->getHead().getBlendFace(),122 QMetaObject::invokeMethod(&Application::getInstance()->getAvatar()->getHead().getBlendFace(),
115 "setModelURL",123 "setModelURL",
116 Q_ARG(QUrl, faceMeshURL));124 Q_ARG(QUrl, QUrl(dataValueString)));
125 } else if (strcmp(dataKeyPosition, DataServerKey::UUID) == 0) {
126 // this is the user's UUID - set it on the profile
127 Application::getInstance()->getProfile()->setUUID(dataValueString);
117 }128 }
118 } else {129 } else {
119 // user string was UUID, find matching avatar and associate data130 // user string was UUID, find matching avatar and associate data