From fed7d789f6023eef1670974eb75b558691b16a8d Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Sun, 30 Nov 2014 17:29:18 -0800 Subject: [PATCH 1/2] have the domain-server write its local port into shared memory --- domain-server/src/DomainServer.cpp | 13 +++++++++++++ libraries/networking/src/LimitedNodeList.h | 2 ++ 2 files changed, 15 insertions(+) diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index 02f17bc502..84948e2406 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -228,6 +229,18 @@ void DomainServer::setupNodeListAndAssignments(const QUuid& sessionUUID) { LimitedNodeList* nodeList = LimitedNodeList::createInstance(domainServerPort, domainServerDTLSPort); + // no matter the local port, save it to shared mem so that local assignment clients can ask what it is + QSharedMemory* sharedPortMem = new QSharedMemory(DOMAIN_SERVER_LOCAL_PORT_SMEM_KEY, this); + quint16 localPort = nodeList->getNodeSocket().localPort(); + + // attempt to create the shared memory segment + if (sharedPortMem->create(sizeof(localPort)) || sharedPortMem->attach()) { + memcpy(sharedPortMem->data(), &localPort, sizeof(localPort)); + qDebug() << "Wrote local listening port to shared memory at key" << DOMAIN_SERVER_LOCAL_PORT_SMEM_KEY; + } else { + qWarning() << "Failed to create and attach to shared memory to share local port with assignment-client children."; + } + // set our LimitedNodeList UUID to match the UUID from our config // nodes will currently use this to add resources to data-web that relate to our domain const QString METAVERSE_DOMAIN_ID_KEY_PATH = "metaverse.id"; diff --git a/libraries/networking/src/LimitedNodeList.h b/libraries/networking/src/LimitedNodeList.h index 64495fbd34..ad8c1688bb 100644 --- a/libraries/networking/src/LimitedNodeList.h +++ b/libraries/networking/src/LimitedNodeList.h @@ -44,6 +44,8 @@ const char DEFAULT_ASSIGNMENT_SERVER_HOSTNAME[] = "localhost"; const char STUN_SERVER_HOSTNAME[] = "stun.highfidelity.io"; const unsigned short STUN_SERVER_PORT = 3478; +const QString DOMAIN_SERVER_LOCAL_PORT_SMEM_KEY = "domain-server.local-port"; + class HifiSockAddr; typedef QSet NodeSet; From 59432e8d2935f2b1937a26b784fc64fdc29640e4 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Sun, 30 Nov 2014 17:54:10 -0800 Subject: [PATCH 2/2] complete sharing of local domain-server port to local AC --- assignment-client/src/AssignmentClient.cpp | 59 +++++++++++++++++----- assignment-client/src/AssignmentClient.h | 4 ++ domain-server/src/DomainServer.cpp | 5 +- 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/assignment-client/src/AssignmentClient.cpp b/assignment-client/src/AssignmentClient.cpp index ae235eb1ff..9972adfda2 100644 --- a/assignment-client/src/AssignmentClient.cpp +++ b/assignment-client/src/AssignmentClient.cpp @@ -10,6 +10,7 @@ // #include +#include #include #include @@ -38,7 +39,8 @@ int hifiSockAddrMeta = qRegisterMetaType("HifiSockAddr"); AssignmentClient::AssignmentClient(int &argc, char **argv) : QCoreApplication(argc, argv), _shutdownEventListener(this), - _assignmentServerHostname(DEFAULT_ASSIGNMENT_SERVER_HOSTNAME) + _assignmentServerHostname(DEFAULT_ASSIGNMENT_SERVER_HOSTNAME), + _localASPortSharedMem(NULL) { LogUtils::init(); @@ -89,13 +91,7 @@ AssignmentClient::AssignmentClient(int &argc, char **argv) : // create a NodeList as an unassigned client NodeList* nodeList = NodeList::createInstance(NodeType::Unassigned); - unsigned short assignmentServerPort = DEFAULT_DOMAIN_SERVER_PORT; - - // check for an overriden assignment server port - if (argumentVariantMap.contains(CUSTOM_ASSIGNMENT_SERVER_PORT_OPTION)) { - assignmentServerPort = - argumentVariantMap.value(CUSTOM_ASSIGNMENT_SERVER_PORT_OPTION).toString().toUInt(); - } + quint16 assignmentServerPort = DEFAULT_DOMAIN_SERVER_PORT; // check for an overriden assignment server hostname if (argumentVariantMap.contains(CUSTOM_ASSIGNMENT_SERVER_HOSTNAME_OPTION)) { @@ -103,10 +99,16 @@ AssignmentClient::AssignmentClient(int &argc, char **argv) : _assignmentServerHostname = argumentVariantMap.value(CUSTOM_ASSIGNMENT_SERVER_HOSTNAME_OPTION).toString(); } - HifiSockAddr assignmentServerSocket(_assignmentServerHostname, assignmentServerPort, true); - nodeList->setAssignmentServerSocket(assignmentServerSocket); + // check for an overriden assignment server port + if (argumentVariantMap.contains(CUSTOM_ASSIGNMENT_SERVER_PORT_OPTION)) { + assignmentServerPort = + argumentVariantMap.value(CUSTOM_ASSIGNMENT_SERVER_PORT_OPTION).toString().toUInt(); + } + + _assignmentServerSocket = HifiSockAddr(_assignmentServerHostname, assignmentServerPort, true); + nodeList->setAssignmentServerSocket(_assignmentServerSocket); - qDebug() << "Assignment server socket is" << assignmentServerSocket; + qDebug() << "Assignment server socket is" << _assignmentServerSocket; // call a timer function every ASSIGNMENT_REQUEST_INTERVAL_MSECS to ask for assignment, if required qDebug() << "Waiting for assignment -" << _requestAssignment; @@ -129,7 +131,40 @@ AssignmentClient::AssignmentClient(int &argc, char **argv) : void AssignmentClient::sendAssignmentRequest() { if (!_currentAssignment) { - NodeList::getInstance()->sendAssignment(_requestAssignment); + + NodeList* nodeList = NodeList::getInstance(); + + if (_assignmentServerHostname == "localhost") { + // we want to check again for the local domain-server port in case the DS has restarted + if (!_localASPortSharedMem) { + _localASPortSharedMem = new QSharedMemory(DOMAIN_SERVER_LOCAL_PORT_SMEM_KEY, this); + + if (!_localASPortSharedMem->attach(QSharedMemory::ReadOnly)) { + qWarning() << "Could not attach to shared memory at key" << DOMAIN_SERVER_LOCAL_PORT_SMEM_KEY + << "- will attempt to connect to domain-server on" << _assignmentServerSocket.getPort(); + } + } + + if (_localASPortSharedMem->isAttached()) { + _localASPortSharedMem->lock(); + + quint16 localAssignmentServerPort; + memcpy(&localAssignmentServerPort, _localASPortSharedMem->data(), sizeof(localAssignmentServerPort)); + + _localASPortSharedMem->unlock(); + + if (localAssignmentServerPort != _assignmentServerSocket.getPort()) { + qDebug() << "Port for local assignment server read from shared memory is" + << localAssignmentServerPort; + + _assignmentServerSocket.setPort(localAssignmentServerPort); + nodeList->setAssignmentServerSocket(_assignmentServerSocket); + } + } + + } + + nodeList->sendAssignment(_requestAssignment); } } diff --git a/assignment-client/src/AssignmentClient.h b/assignment-client/src/AssignmentClient.h index 7628aa0a3b..566805d67f 100644 --- a/assignment-client/src/AssignmentClient.h +++ b/assignment-client/src/AssignmentClient.h @@ -17,6 +17,8 @@ #include "ShutdownEventListener.h" #include "ThreadedAssignment.h" +class QSharedMemory; + class AssignmentClient : public QCoreApplication { Q_OBJECT public: @@ -34,6 +36,8 @@ private: static SharedAssignmentPointer _currentAssignment; ShutdownEventListener _shutdownEventListener; QString _assignmentServerHostname; + HifiSockAddr _assignmentServerSocket; + QSharedMemory* _localASPortSharedMem; }; #endif // hifi_AssignmentClient_h diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index 84948e2406..ddc06c8015 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -235,8 +235,11 @@ void DomainServer::setupNodeListAndAssignments(const QUuid& sessionUUID) { // attempt to create the shared memory segment if (sharedPortMem->create(sizeof(localPort)) || sharedPortMem->attach()) { + sharedPortMem->lock(); memcpy(sharedPortMem->data(), &localPort, sizeof(localPort)); - qDebug() << "Wrote local listening port to shared memory at key" << DOMAIN_SERVER_LOCAL_PORT_SMEM_KEY; + sharedPortMem->unlock(); + + qDebug() << "Wrote local listening port" << localPort << "to shared memory at key" << DOMAIN_SERVER_LOCAL_PORT_SMEM_KEY; } else { qWarning() << "Failed to create and attach to shared memory to share local port with assignment-client children."; }