Store a short ID with the Node on the domain-server side

This commit is contained in:
Simon Walton 2018-03-23 16:55:19 -07:00
parent 113670e33b
commit b5f165d481
3 changed files with 48 additions and 1 deletions

View file

@ -14,6 +14,7 @@
#include <openssl/err.h>
#include <openssl/rsa.h>
#include <openssl/x509.h>
#include <random>
#include <AccountManager.h>
#include <Assignment.h>
@ -26,7 +27,7 @@ using SharedAssignmentPointer = QSharedPointer<Assignment>;
DomainGatekeeper::DomainGatekeeper(DomainServer* server) :
_server(server)
{
initLocalIDManagement();
}
void DomainGatekeeper::addPendingAssignedNode(const QUuid& nodeUUID, const QUuid& assignmentUUID,
@ -525,6 +526,7 @@ SharedNodePointer DomainGatekeeper::addVerifiedNodeFromConnectRequest(const Node
SharedNodePointer newNode = limitedNodeList->addOrUpdateNode(nodeID, nodeConnection.nodeType,
nodeConnection.publicSockAddr, nodeConnection.localSockAddr);
newNode->setLocalID(findOrCreateLocalID(nodeID));
// So that we can send messages to this node at will - we need to activate the correct socket on this node now
newNode->activateMatchingOrNewSymmetricSocket(discoveredSocket);
@ -1014,3 +1016,29 @@ void DomainGatekeeper::refreshGroupsCache() {
_server->_settingsManager.debugDumpGroupsState();
#endif
}
void DomainGatekeeper::initLocalIDManagement() {
std::uniform_int_distribution<quint16> sixteenBitRand;
std::random_device randomDevice;
std::default_random_engine engine {randomDevice()};
_currentLocalID = sixteenBitRand(engine);
// Ensure increment is odd.
_idIncrement = sixteenBitRand(engine) | 1;
}
Node::LocalID DomainGatekeeper::findOrCreateLocalID(const QUuid& uuid) {
auto existingLocalIDIt = _uuidToLocalID.find(uuid);
if (existingLocalIDIt != _uuidToLocalID.end()) {
return existingLocalIDIt->second;
}
Node::LocalID newLocalID;
do {
newLocalID = _currentLocalID;
_currentLocalID += _idIncrement;
} while (_localIDToUUID.find(newLocalID) != _localIDToUUID.end());
_uuidToLocalID.emplace(uuid, newLocalID);
_localIDToUUID.emplace(newLocalID, uuid);
return newLocalID;
}

View file

@ -120,6 +120,20 @@ private:
void getGroupMemberships(const QString& username);
// void getIsGroupMember(const QString& username, const QUuid groupID);
void getDomainOwnerFriendsList();
// Local ID management.
void initLocalIDManagement();
Node::LocalID findOrCreateLocalID(const QUuid& uuid);
struct UuidHash {
size_t operator()(const QUuid& uuid) const { return qHash(uuid); }
};
using UUIDToLocalID = std::unordered_map<QUuid, Node::LocalID, UuidHash> ;
using LocalIDToUUID = std::unordered_map<Node::LocalID, QUuid>;
UUIDToLocalID _uuidToLocalID;
LocalIDToUUID _localIDToUUID;
Node::LocalID _currentLocalID;
quint16 _idIncrement;
};

View file

@ -39,6 +39,10 @@ public:
const QUuid& getUUID() const { return _uuid; }
void setUUID(const QUuid& uuid) { _uuid = uuid; }
using LocalID = quint16;
LocalID getLocalID() const { return _localID; }
void setLocalID(LocalID localID) { _localID = localID; }
void softReset();
void reset();
@ -99,6 +103,7 @@ protected:
void setActiveSocket(HifiSockAddr* discoveredSocket);
QUuid _uuid;
LocalID _localID { 0 };
HifiSockAddr _publicSocket;
HifiSockAddr _localSocket;