complete sharing of local domain-server port to local AC

This commit is contained in:
Stephen Birarda 2014-11-30 17:54:10 -08:00
parent fed7d789f6
commit 59432e8d29
3 changed files with 55 additions and 13 deletions

View file

@ -10,6 +10,7 @@
// //
#include <QtCore/QProcess> #include <QtCore/QProcess>
#include <QtCore/qsharedmemory.h>
#include <QtCore/QThread> #include <QtCore/QThread>
#include <QtCore/QTimer> #include <QtCore/QTimer>
@ -38,7 +39,8 @@ int hifiSockAddrMeta = qRegisterMetaType<HifiSockAddr>("HifiSockAddr");
AssignmentClient::AssignmentClient(int &argc, char **argv) : AssignmentClient::AssignmentClient(int &argc, char **argv) :
QCoreApplication(argc, argv), QCoreApplication(argc, argv),
_shutdownEventListener(this), _shutdownEventListener(this),
_assignmentServerHostname(DEFAULT_ASSIGNMENT_SERVER_HOSTNAME) _assignmentServerHostname(DEFAULT_ASSIGNMENT_SERVER_HOSTNAME),
_localASPortSharedMem(NULL)
{ {
LogUtils::init(); LogUtils::init();
@ -89,13 +91,7 @@ AssignmentClient::AssignmentClient(int &argc, char **argv) :
// create a NodeList as an unassigned client // create a NodeList as an unassigned client
NodeList* nodeList = NodeList::createInstance(NodeType::Unassigned); NodeList* nodeList = NodeList::createInstance(NodeType::Unassigned);
unsigned short assignmentServerPort = DEFAULT_DOMAIN_SERVER_PORT; quint16 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();
}
// check for an overriden assignment server hostname // check for an overriden assignment server hostname
if (argumentVariantMap.contains(CUSTOM_ASSIGNMENT_SERVER_HOSTNAME_OPTION)) { 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(); _assignmentServerHostname = argumentVariantMap.value(CUSTOM_ASSIGNMENT_SERVER_HOSTNAME_OPTION).toString();
} }
HifiSockAddr assignmentServerSocket(_assignmentServerHostname, assignmentServerPort, true); // check for an overriden assignment server port
nodeList->setAssignmentServerSocket(assignmentServerSocket); 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 // call a timer function every ASSIGNMENT_REQUEST_INTERVAL_MSECS to ask for assignment, if required
qDebug() << "Waiting for assignment -" << _requestAssignment; qDebug() << "Waiting for assignment -" << _requestAssignment;
@ -129,7 +131,40 @@ AssignmentClient::AssignmentClient(int &argc, char **argv) :
void AssignmentClient::sendAssignmentRequest() { void AssignmentClient::sendAssignmentRequest() {
if (!_currentAssignment) { 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);
} }
} }

View file

@ -17,6 +17,8 @@
#include "ShutdownEventListener.h" #include "ShutdownEventListener.h"
#include "ThreadedAssignment.h" #include "ThreadedAssignment.h"
class QSharedMemory;
class AssignmentClient : public QCoreApplication { class AssignmentClient : public QCoreApplication {
Q_OBJECT Q_OBJECT
public: public:
@ -34,6 +36,8 @@ private:
static SharedAssignmentPointer _currentAssignment; static SharedAssignmentPointer _currentAssignment;
ShutdownEventListener _shutdownEventListener; ShutdownEventListener _shutdownEventListener;
QString _assignmentServerHostname; QString _assignmentServerHostname;
HifiSockAddr _assignmentServerSocket;
QSharedMemory* _localASPortSharedMem;
}; };
#endif // hifi_AssignmentClient_h #endif // hifi_AssignmentClient_h

View file

@ -235,8 +235,11 @@ void DomainServer::setupNodeListAndAssignments(const QUuid& sessionUUID) {
// attempt to create the shared memory segment // attempt to create the shared memory segment
if (sharedPortMem->create(sizeof(localPort)) || sharedPortMem->attach()) { if (sharedPortMem->create(sizeof(localPort)) || sharedPortMem->attach()) {
sharedPortMem->lock();
memcpy(sharedPortMem->data(), &localPort, sizeof(localPort)); 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 { } else {
qWarning() << "Failed to create and attach to shared memory to share local port with assignment-client children."; qWarning() << "Failed to create and attach to shared memory to share local port with assignment-client children.";
} }