From a362fdc2ed37da917b759297b2181f9dff83af8a Mon Sep 17 00:00:00 2001 From: stojce Date: Tue, 10 Sep 2013 21:51:50 +0200 Subject: [PATCH 1/2] name the current domain server --- interface/src/Application.cpp | 8 ++- interface/src/Application.h | 4 +- libraries/shared/src/NodeList.cpp | 92 ++++++++++++++++++++++--------- libraries/shared/src/NodeList.h | 12 ++++ 4 files changed, 87 insertions(+), 29 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 7989d8b715..089b1cd9fb 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -147,6 +147,7 @@ Application::Application(int& argc, char** argv, timeval &startup_time) : NodeList::getInstance()->addHook(&_voxels); NodeList::getInstance()->addHook(this); + NodeList::getInstance()->addDomainListener(this); // network receive thread and voxel parsing thread are both controlled by the --nonblocking command line @@ -236,6 +237,7 @@ Application::Application(int& argc, char** argv, timeval &startup_time) : Application::~Application() { NodeList::getInstance()->removeHook(&_voxels); NodeList::getInstance()->removeHook(this); + NodeList::getInstance()->removeDomainListener(this); _sharedVoxelSystem.changeTree(new VoxelTree); @@ -314,8 +316,6 @@ void Application::initializeGL() { char title[50]; sprintf(title, "Interface: %4.2f seconds\n", startupTime); qDebug("%s", title); - _window->setWindowTitle(title); - const char LOGSTASH_INTERFACE_START_TIME_KEY[] = "interface-start-time"; // ask the Logstash class to record the startup time @@ -3235,6 +3235,10 @@ void Application::attachNewHeadToNode(Node* newNode) { } } +void Application::domainChanged(QString domain) { + _window->setWindowTitle(domain); +} + void Application::nodeAdded(Node* node) { } diff --git a/interface/src/Application.h b/interface/src/Application.h index 6c9f2cf66d..7a21c3ca51 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -76,7 +76,7 @@ static const float NODE_KILLED_RED = 1.0f; static const float NODE_KILLED_GREEN = 0.0f; static const float NODE_KILLED_BLUE = 0.0f; -class Application : public QApplication, public NodeListHook, public PacketSenderNotify { +class Application : public QApplication, public NodeListHook, public PacketSenderNotify, public DomainChangeListener { Q_OBJECT friend class VoxelPacketProcessor; @@ -142,6 +142,8 @@ public: virtual void nodeAdded(Node* node); virtual void nodeKilled(Node* node); virtual void packetSentNotification(ssize_t length); + + virtual void domainChanged(QString domain); public slots: void sendAvatarFaceVideoMessage(int frameCount, const QByteArray& data); diff --git a/libraries/shared/src/NodeList.cpp b/libraries/shared/src/NodeList.cpp index a982e22242..290b8ffb18 100644 --- a/libraries/shared/src/NodeList.cpp +++ b/libraries/shared/src/NodeList.cpp @@ -82,22 +82,20 @@ NodeList::~NodeList() { stopSilentNodeRemovalThread(); } -void NodeList::setDomainHostname(const char* domainHostname) { - memset(_domainHostname, 0, sizeof(_domainHostname)); - memcpy(_domainHostname, domainHostname, strlen(domainHostname)); - +void NodeList::setDomainHostname(const char* domainHostname) { // reset the domain IP so the hostname is checked again - setDomainIP(""); + resetDomainData(_domainHostname, domainHostname); } void NodeList::setDomainIP(const char* domainIP) { - memset(_domainIP, 0, sizeof(_domainIP)); - memcpy(_domainIP, domainIP, strlen(domainIP)); + resetDomainData(_domainIP, domainIP); } void NodeList::setDomainIPToLocalhost() { int ip = getLocalAddress(); - sprintf(_domainIP, "%d.%d.%d.%d", (ip & 0xFF), ((ip >> 8) & 0xFF),((ip >> 16) & 0xFF), ((ip >> 24) & 0xFF)); + char _localIP[INET_ADDRSTRLEN]; + sprintf(_localIP, "%d.%d.%d.%d", (ip & 0xFF), ((ip >> 8) & 0xFF),((ip >> 16) & 0xFF), ((ip >> 24) & 0xFF)); + setDomainIP(_localIP); } void NodeList::timePingReply(sockaddr *nodeAddress, unsigned char *packetData) { @@ -269,25 +267,8 @@ void NodeList::setNodeTypesOfInterest(const char* nodeTypesOfInterest, int numNo } void NodeList::sendDomainServerCheckIn() { - static bool printedDomainServerIP = false; - - // Lookup the IP address of the domain server if we need to - if (atoi(_domainIP) == 0) { - printf("Looking up %s\n", _domainHostname); - struct hostent* pHostInfo; - if ((pHostInfo = gethostbyname(_domainHostname)) != NULL) { - sockaddr_in tempAddress; - memcpy(&tempAddress.sin_addr, pHostInfo->h_addr_list[0], pHostInfo->h_length); - strcpy(_domainIP, inet_ntoa(tempAddress.sin_addr)); - qDebug("Domain Server: %s\n", _domainHostname); - } else { - qDebug("Failed domain server lookup\n"); - } - } else if (!printedDomainServerIP) { - qDebug("Domain Server IP: %s\n", _domainIP); - printedDomainServerIP = true; - } + domainLookup(); static unsigned char* checkInPacket = NULL; static int checkInPacketSize = 0; @@ -650,6 +631,21 @@ void NodeListIterator::skipDeadAndStopIncrement() { } } +void NodeList::addDomainListener(DomainChangeListener* listener) { + _domainListeners.push_back(listener); + QString domain = (strlen(_domainHostname) > 0) ? _domainHostname : _domainIP; + listener->domainChanged(domain); +} + +void NodeList::removeDomainListener(DomainChangeListener* listener) { + for (int i = 0; i < _domainListeners.size(); i++) { + if (_domainListeners[i] == listener) { + _domainListeners.erase(_domainListeners.begin() + i); + return; + } + } +} + void NodeList::addHook(NodeListHook* hook) { _hooks.push_back(hook); } @@ -676,3 +672,47 @@ void NodeList::notifyHooksOfKilledNode(Node* node) { _hooks[i]->nodeKilled(node); } } + +void NodeList::notifyDomainChanged() { + QString domain = (strlen(_domainHostname) > 0) ? _domainHostname : _domainIP; + for (int i = 0; i < _domainListeners.size(); i++) { + _domainListeners[i]->domainChanged(domain); + } +} + +void NodeList::resetDomainData(char domainField[], const char* domainValue) { + memset(_domainHostname, 0, sizeof(_domainHostname)); + memcpy(_domainHostname, "", strlen("")); + + memset(_domainIP, 0, sizeof(_domainIP)); + memcpy(_domainIP, "", strlen("")); + + memset(domainField, 0, sizeof(&domainField)); + memcpy(domainField, domainValue, strlen(domainValue)); + + domainLookup(); + notifyDomainChanged(); +} + +void NodeList::domainLookup() { + static bool printedDomainServerIP = false; + + // Lookup the IP address of the domain server if we need to + if (atoi(_domainIP) == 0) { + printf("Looking up %s\n", _domainHostname); + struct hostent* pHostInfo; + if ((pHostInfo = gethostbyname(_domainHostname)) != NULL) { + sockaddr_in tempAddress; + memcpy(&tempAddress.sin_addr, pHostInfo->h_addr_list[0], pHostInfo->h_length); + strcpy(_domainIP, inet_ntoa(tempAddress.sin_addr)); + + qDebug("Domain Server: %s\n", _domainHostname); + } else { + qDebug("Failed domain server lookup\n"); + } + } else if (!printedDomainServerIP) { + notifyDomainChanged(); + qDebug("Domain Server IP: %s\n", _domainIP); + printedDomainServerIP = true; + } +} diff --git a/libraries/shared/src/NodeList.h b/libraries/shared/src/NodeList.h index ea94e8c080..e7d08ddda5 100644 --- a/libraries/shared/src/NodeList.h +++ b/libraries/shared/src/NodeList.h @@ -54,6 +54,10 @@ public: virtual void nodeKilled(Node* node) = 0; }; +class DomainChangeListener { +public: + virtual void domainChanged(QString domain) = 0; +}; class NodeList { public: @@ -130,6 +134,9 @@ public: void notifyHooksOfAddedNode(Node* node); void notifyHooksOfKilledNode(Node* node); + void addDomainListener(DomainChangeListener* listener); + void removeDomainListener(DomainChangeListener* listener); + private: static NodeList* _sharedInstance; @@ -158,6 +165,11 @@ private: void timePingReply(sockaddr *nodeAddress, unsigned char *packetData); std::vector _hooks; + std::vector _domainListeners; + + void resetDomainData(char domainField[], const char* domainData); + void notifyDomainChanged(); + void domainLookup(); }; class NodeListIterator : public std::iterator { From a0f23d409ed711587ecc078c322eef93b3bc8433 Mon Sep 17 00:00:00 2001 From: stojce Date: Sat, 28 Sep 2013 13:16:50 +0200 Subject: [PATCH 2/2] Merging changes --- interface/src/Application.cpp | 1 + interface/src/Application.h | 4 +++- libraries/shared/src/NodeList.cpp | 23 +++++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 533a4b4246..f10f98c3b3 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -3423,6 +3423,7 @@ void Application::attachNewHeadToNode(Node* newNode) { } void Application::domainChanged(QString domain) { + qDebug("Application title set to: %s.\n", domain.toStdString().c_str()); _window->setWindowTitle(domain); } diff --git a/interface/src/Application.h b/interface/src/Application.h index a8e84e5c2a..620d6d57f5 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -77,7 +77,7 @@ static const float NODE_KILLED_RED = 1.0f; static const float NODE_KILLED_GREEN = 0.0f; static const float NODE_KILLED_BLUE = 0.0f; -class Application : public QApplication, public NodeListHook, public PacketSenderNotify { +class Application : public QApplication, public NodeListHook, public PacketSenderNotify, public DomainChangeListener { Q_OBJECT friend class VoxelPacketProcessor; @@ -146,6 +146,8 @@ public: virtual void nodeKilled(Node* node); virtual void packetSentNotification(ssize_t length); + virtual void domainChanged(QString domain); + VoxelShader& getVoxelShader() { return _voxelShader; } public slots: diff --git a/libraries/shared/src/NodeList.cpp b/libraries/shared/src/NodeList.cpp index 8901ca3271..7b31a2de15 100644 --- a/libraries/shared/src/NodeList.cpp +++ b/libraries/shared/src/NodeList.cpp @@ -109,6 +109,7 @@ void NodeList::setDomainHostname(const QString& domainHostname) { // reset our _domainIP to the null address so that a lookup happens on next check in _domainIP.clear(); + notifyDomainChanged(); } void NodeList::timePingReply(sockaddr *nodeAddress, unsigned char *packetData) { @@ -585,6 +586,7 @@ void NodeList::loadData(QSettings *settings) { if (domainServerHostname.size() > 0) { _domainHostname = domainServerHostname; + notifyDomainChanged(); } settings->endGroup(); @@ -678,6 +680,21 @@ void NodeListIterator::skipDeadAndStopIncrement() { } } +void NodeList::addDomainListener(DomainChangeListener* listener) { + _domainListeners.push_back(listener); + QString domain = _domainHostname.isEmpty() ? _domainIP.toString() : _domainHostname; + listener->domainChanged(domain); +} + +void NodeList::removeDomainListener(DomainChangeListener* listener) { + for (int i = 0; i < _domainListeners.size(); i++) { + if (_domainListeners[i] == listener) { + _domainListeners.erase(_domainListeners.begin() + i); + return; + } + } +} + void NodeList::addHook(NodeListHook* hook) { _hooks.push_back(hook); } @@ -704,3 +721,9 @@ void NodeList::notifyHooksOfKilledNode(Node* node) { _hooks[i]->nodeKilled(node); } } + +void NodeList::notifyDomainChanged() { + for (int i = 0; i < _domainListeners.size(); i++) { + _domainListeners[i]->domainChanged(_domainHostname); + } +}