diff --git a/animation-server/src/AnimationServer.cpp b/animation-server/src/AnimationServer.cpp index f684a6e672..ba07fac21a 100644 --- a/animation-server/src/AnimationServer.cpp +++ b/animation-server/src/AnimationServer.cpp @@ -807,7 +807,7 @@ AnimationServer::AnimationServer(int &argc, char **argv) : pthread_create(&::animateVoxelThread, NULL, animateVoxels, NULL); - NodeList::getInstance()->setNodeTypesOfInterest(&NODE_TYPE_VOXEL_SERVER, 1); + NodeList::getInstance()->addNodeTypeToInterestSet(NODE_TYPE_VOXEL_SERVER); QTimer* domainServerTimer = new QTimer(this); connect(domainServerTimer, SIGNAL(timeout()), nodeList, SLOT(sendDomainServerCheckIn())); diff --git a/assignment-client/src/Agent.cpp b/assignment-client/src/Agent.cpp index f4f9d6fb69..4f81c42046 100644 --- a/assignment-client/src/Agent.cpp +++ b/assignment-client/src/Agent.cpp @@ -52,13 +52,7 @@ void Agent::run() { NodeList* nodeList = NodeList::getInstance(); nodeList->setOwnerType(NODE_TYPE_AGENT); - // XXXBHG - this seems less than ideal. There might be classes (like jurisdiction listeners, that need access to - // other node types, but for them to get access to those node types, we have to add them here. It seems like - // NodeList should support adding types of interest - const NODE_TYPE AGENT_NODE_TYPES_OF_INTEREST[] = { NODE_TYPE_VOXEL_SERVER, NODE_TYPE_PARTICLE_SERVER, - NODE_TYPE_AUDIO_MIXER, NODE_TYPE_AVATAR_MIXER }; - - nodeList->setNodeTypesOfInterest(AGENT_NODE_TYPES_OF_INTEREST, sizeof(AGENT_NODE_TYPES_OF_INTEREST)); + nodeList->addSetOfNodeTypesToNodeInterestSet(QSet() << NODE_TYPE_AUDIO_MIXER << NODE_TYPE_AVATAR_MIXER); // figure out the URL for the script for this agent assignment QString scriptURLString("http://%1:8080/assignment/%2"); diff --git a/assignment-client/src/audio/AudioMixer.cpp b/assignment-client/src/audio/AudioMixer.cpp index aa54c874ef..705a877a00 100644 --- a/assignment-client/src/audio/AudioMixer.cpp +++ b/assignment-client/src/audio/AudioMixer.cpp @@ -241,8 +241,7 @@ void AudioMixer::run() { NodeList* nodeList = NodeList::getInstance(); - const char AUDIO_MIXER_NODE_TYPES_OF_INTEREST[2] = { NODE_TYPE_AGENT, NODE_TYPE_AUDIO_INJECTOR }; - nodeList->setNodeTypesOfInterest(AUDIO_MIXER_NODE_TYPES_OF_INTEREST, sizeof(AUDIO_MIXER_NODE_TYPES_OF_INTEREST)); + nodeList->addNodeTypeToInterestSet(NODE_TYPE_AGENT); nodeList->linkedDataCreateCallback = attachNewBufferToNode; diff --git a/assignment-client/src/avatars/AvatarMixer.cpp b/assignment-client/src/avatars/AvatarMixer.cpp index 56464d2415..2f7c4a83d4 100644 --- a/assignment-client/src/avatars/AvatarMixer.cpp +++ b/assignment-client/src/avatars/AvatarMixer.cpp @@ -149,7 +149,7 @@ void AvatarMixer::run() { commonInit(AVATAR_MIXER_LOGGING_NAME, NODE_TYPE_AVATAR_MIXER); NodeList* nodeList = NodeList::getInstance(); - nodeList->setNodeTypesOfInterest(&NODE_TYPE_AGENT, 1); + nodeList->addNodeTypeToInterestSet(NODE_TYPE_AGENT); nodeList->linkedDataCreateCallback = attachAvatarDataToNode; diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 0d4506f6be..a2c7106c8b 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -109,7 +109,7 @@ Application::Application(int& argc, char** argv, timeval &startup_time) : _window(new QMainWindow(desktop())), _glWidget(new GLCanvas()), _nodeThread(new QThread(this)), - _datagramProcessor(new DatagramProcessor()), + _datagramProcessor(), _frameCount(0), _fps(120.0f), _justStarted(true), @@ -174,20 +174,20 @@ Application::Application(int& argc, char** argv, timeval &startup_time) : listenPort = atoi(portStr); } - // put the NodeList and datagram processing on the node thread - NodeList* nodeList = NodeList::createInstance(NODE_TYPE_AGENT, listenPort); - - nodeList->moveToThread(_nodeThread); - _datagramProcessor->moveToThread(_nodeThread); - - // connect the DataProcessor processDatagrams slot to the QUDPSocket readyRead() signal - connect(&nodeList->getNodeSocket(), SIGNAL(readyRead()), _datagramProcessor, SLOT(processDatagrams())); + // start the nodeThread so its event loop is running + _nodeThread->start(); // make sure the node thread is given highest priority _nodeThread->setPriority(QThread::TimeCriticalPriority); - // start the nodeThread so its event loop is running - _nodeThread->start(); + // put the NodeList and datagram processing on the node thread + NodeList* nodeList = NodeList::createInstance(NODE_TYPE_AGENT, listenPort); + + nodeList->moveToThread(_nodeThread); + _datagramProcessor.moveToThread(_nodeThread); + + // connect the DataProcessor processDatagrams slot to the QUDPSocket readyRead() signal + connect(&nodeList->getNodeSocket(), SIGNAL(readyRead()), &_datagramProcessor, SLOT(processDatagrams())); // put the audio processing on a separate thread QThread* audioThread = new QThread(this); @@ -231,12 +231,12 @@ Application::Application(int& argc, char** argv, timeval &startup_time) : #endif // tell the NodeList instance who to tell the domain server we care about - const char nodeTypesOfInterest[] = {NODE_TYPE_AUDIO_MIXER, NODE_TYPE_AVATAR_MIXER, NODE_TYPE_VOXEL_SERVER, - NODE_TYPE_PARTICLE_SERVER, NODE_TYPE_METAVOXEL_SERVER}; - nodeList->setNodeTypesOfInterest(nodeTypesOfInterest, sizeof(nodeTypesOfInterest)); + nodeList->addSetOfNodeTypesToNodeInterestSet(QSet() << NODE_TYPE_AUDIO_MIXER << NODE_TYPE_AVATAR_MIXER + << NODE_TYPE_VOXEL_SERVER << NODE_TYPE_PARTICLE_SERVER + << NODE_TYPE_METAVOXEL_SERVER); // move the silentNodeTimer to the _nodeThread - QTimer* silentNodeTimer = new QTimer(this); + QTimer* silentNodeTimer = new QTimer(); connect(silentNodeTimer, SIGNAL(timeout()), nodeList, SLOT(removeSilentNodes())); silentNodeTimer->moveToThread(_nodeThread); silentNodeTimer->start(NODE_SILENCE_THRESHOLD_USECS / 1000); @@ -282,17 +282,35 @@ Application::Application(int& argc, char** argv, timeval &startup_time) : Application::~Application() { qInstallMessageHandler(NULL); - + // make sure we don't call the idle timer any more delete idleTimer; + Menu::getInstance()->saveSettings(); + + _rearMirrorTools->saveSettings(_settings); + _settings->sync(); + + // let the avatar mixer know we're out + NodeList::getInstance()->sendKillNode(&NODE_TYPE_AVATAR_MIXER, 1); + // ask the datagram processing thread to quit and wait until it is done - _nodeThread->thread()->quit(); - _nodeThread->thread()->wait(); - + _nodeThread->quit(); + _nodeThread->wait(); + // ask the audio thread to quit and wait until it is done _audio.thread()->quit(); _audio.thread()->wait(); + + _voxelProcessor.terminate(); + _voxelHideShowThread.terminate(); + _voxelEditSender.terminate(); + _particleEditSender.terminate(); + if (_persistThread) { + _persistThread->terminate(); + _persistThread->deleteLater(); + _persistThread = NULL; + } storeSizeAndPosition(); saveScripts(); @@ -377,9 +395,6 @@ void Application::initializeGL() { qDebug("Voxel parsing thread created."); } - // call terminate before exiting - connect(this, SIGNAL(aboutToQuit()), SLOT(terminate())); - // call our timer function every second QTimer* timer = new QTimer(this); connect(timer, SIGNAL(timeout()), SLOT(timer())); @@ -1329,11 +1344,11 @@ void Application::timer() { _fps = (float)_frameCount / ((float)diffclock(&_timerStart, &_timerEnd) / 1000.f); - _packetsPerSecond = (float) _datagramProcessor->getPacketCount() / ((float)diffclock(&_timerStart, &_timerEnd) / 1000.f); - _bytesPerSecond = (float) _datagramProcessor->getByteCount() / ((float)diffclock(&_timerStart, &_timerEnd) / 1000.f); + _packetsPerSecond = (float) _datagramProcessor.getPacketCount() / ((float)diffclock(&_timerStart, &_timerEnd) / 1000.f); + _bytesPerSecond = (float) _datagramProcessor.getByteCount() / ((float)diffclock(&_timerStart, &_timerEnd) / 1000.f); _frameCount = 0; - _datagramProcessor->resetCounters(); + _datagramProcessor.resetCounters(); gettimeofday(&_timerStart, NULL); @@ -1412,28 +1427,6 @@ void Application::idle() { } } -void Application::terminate() { - // Close serial port - // close(serial_fd); - - Menu::getInstance()->saveSettings(); - _rearMirrorTools->saveSettings(_settings); - _settings->sync(); - - // let the avatar mixer know we're out - NodeList::getInstance()->sendKillNode(&NODE_TYPE_AVATAR_MIXER, 1); - - _voxelProcessor.terminate(); - _voxelHideShowThread.terminate(); - _voxelEditSender.terminate(); - _particleEditSender.terminate(); - if (_persistThread) { - _persistThread->terminate(); - _persistThread->deleteLater(); - _persistThread = NULL; - } -} - void Application::checkBandwidthMeterClick() { // ... to be called upon button release diff --git a/interface/src/Application.h b/interface/src/Application.h index 514591e1df..803cb296a2 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -230,7 +230,6 @@ private slots: void timer(); void idle(); - void terminate(); void setFullscreen(bool fullscreen); void setEnable3DTVMode(bool enable3DTVMode); @@ -333,7 +332,7 @@ private: BandwidthMeter _bandwidthMeter; QThread* _nodeThread; - DatagramProcessor* _datagramProcessor; + DatagramProcessor _datagramProcessor; QNetworkAccessManager* _networkAccessManager; QSettings* _settings; diff --git a/interface/src/Audio.cpp b/interface/src/Audio.cpp index f31ab9056f..47ab8b0aba 100644 --- a/interface/src/Audio.cpp +++ b/interface/src/Audio.cpp @@ -443,6 +443,15 @@ void Audio::addReceivedAudioToBuffer(const QByteArray& audioByteArray) { QByteArray outputBuffer; outputBuffer.resize(numRequiredOutputSamples * sizeof(int16_t)); + + if (!_ringBuffer.isStarved() && _audioOutput->bytesFree() == _audioOutput->bufferSize()) { + // we don't have any audio data left in the output buffer + // we just starved + qDebug() << "Audio output just starved."; + _ringBuffer.setIsStarved(true); + _numFramesDisplayStarve = 10; + } + // if there is anything in the ring buffer, decide what to do if (_ringBuffer.samplesAvailable() > 0) { if (!_ringBuffer.isNotStarvedOrHasMinimumSamples(NETWORK_BUFFER_LENGTH_SAMPLES_STEREO @@ -515,12 +524,6 @@ void Audio::addReceivedAudioToBuffer(const QByteArray& audioByteArray) { } } - } else if (_audioOutput->bytesFree() == _audioOutput->bufferSize()) { - // we don't have any audio data left in the output buffer, and the ring buffer from - // the network has nothing in it either - we just starved - qDebug() << "Audio output just starved."; - _ringBuffer.setIsStarved(true); - _numFramesDisplayStarve = 10; } Application::getInstance()->getBandwidthMeter()->inputStream(BandwidthMeter::AUDIO).updateValue(audioByteArray.size()); diff --git a/libraries/octree-server/src/OctreeServer.cpp b/libraries/octree-server/src/OctreeServer.cpp index 4a0ad0d827..cc362f58a0 100644 --- a/libraries/octree-server/src/OctreeServer.cpp +++ b/libraries/octree-server/src/OctreeServer.cpp @@ -569,8 +569,7 @@ void OctreeServer::run() { nodeList->setOwnerType(getMyNodeType()); // we need to ask the DS about agents so we can ping/reply with them - const char nodeTypesOfInterest[] = { NODE_TYPE_AGENT, NODE_TYPE_ANIMATION_SERVER}; - nodeList->setNodeTypesOfInterest(nodeTypesOfInterest, sizeof(nodeTypesOfInterest)); + nodeList->addNodeTypeToInterestSet(NODE_TYPE_AGENT); setvbuf(stdout, NULL, _IOLBF, 0); diff --git a/libraries/octree/src/JurisdictionListener.cpp b/libraries/octree/src/JurisdictionListener.cpp index 6ed039f0bd..d8686debad 100644 --- a/libraries/octree/src/JurisdictionListener.cpp +++ b/libraries/octree/src/JurisdictionListener.cpp @@ -23,6 +23,9 @@ JurisdictionListener::JurisdictionListener(NODE_TYPE type, PacketSenderNotify* n connect(NodeList::getInstance(), &NodeList::nodeKilled, this, &JurisdictionListener::nodeKilled); //qDebug("JurisdictionListener::JurisdictionListener(NODE_TYPE type=%c)", type); + + // tell our NodeList we want to hear about nodes with our node type + NodeList::getInstance()->addNodeTypeToInterestSet(type); } void JurisdictionListener::nodeKilled(SharedNodePointer node) { diff --git a/libraries/shared/src/Node.cpp b/libraries/shared/src/Node.cpp index 271629ff10..fa8bf0ef8f 100644 --- a/libraries/shared/src/Node.cpp +++ b/libraries/shared/src/Node.cpp @@ -74,8 +74,6 @@ const char* Node::getTypeName() const { return NODE_TYPE_NAME_AUDIO_MIXER; case NODE_TYPE_AVATAR_MIXER: return NODE_TYPE_NAME_AVATAR_MIXER; - case NODE_TYPE_AUDIO_INJECTOR: - return NODE_TYPE_NAME_AUDIO_INJECTOR; case NODE_TYPE_ANIMATION_SERVER: return NODE_TYPE_NAME_ANIMATION_SERVER; case NODE_TYPE_UNASSIGNED: diff --git a/libraries/shared/src/NodeList.cpp b/libraries/shared/src/NodeList.cpp index 65993cb275..edffc127c0 100644 --- a/libraries/shared/src/NodeList.cpp +++ b/libraries/shared/src/NodeList.cpp @@ -61,7 +61,7 @@ NodeList::NodeList(char newOwnerType, unsigned short int newSocketListenPort) : _domainSockAddr(HifiSockAddr(QHostAddress::Null, DEFAULT_DOMAIN_SERVER_PORT)), _nodeSocket(this), _ownerType(newOwnerType), - _nodeTypesOfInterest(NULL), + _nodeTypesOfInterest(), _ownerUUID(QUuid::createUuid()), _numNoReplyDomainCheckIns(0), _assignmentServerSocket(), @@ -75,8 +75,6 @@ NodeList::NodeList(char newOwnerType, unsigned short int newSocketListenPort) : NodeList::~NodeList() { - delete _nodeTypesOfInterest; - clear(); } @@ -236,7 +234,11 @@ int NodeList::updateNodeWithData(Node *node, const HifiSockAddr& senderSockAddr, node->setLastHeardMicrostamp(usecTimestampNow()); if (!senderSockAddr.isNull()) { - activateSocketFromNodeCommunication(senderSockAddr); + if (senderSockAddr == node->getPublicSocket()) { + node->activatePublicSocket(); + } else if (senderSockAddr == node->getLocalSocket()) { + node->activateLocalSocket(); + } } if (node->getActiveSocket() || senderSockAddr.isNull()) { @@ -293,19 +295,18 @@ void NodeList::reset() { clear(); _numNoReplyDomainCheckIns = 0; - delete _nodeTypesOfInterest; - _nodeTypesOfInterest = NULL; + _nodeTypesOfInterest.clear(); // refresh the owner UUID _ownerUUID = QUuid::createUuid(); } -void NodeList::setNodeTypesOfInterest(const char* nodeTypesOfInterest, int numNodeTypesOfInterest) { - delete _nodeTypesOfInterest; +void NodeList::addNodeTypeToInterestSet(NODE_TYPE nodeTypeToAdd) { + _nodeTypesOfInterest << nodeTypeToAdd; +} - _nodeTypesOfInterest = new char[numNodeTypesOfInterest + sizeof(char)]; - memcpy(_nodeTypesOfInterest, nodeTypesOfInterest, numNodeTypesOfInterest); - _nodeTypesOfInterest[numNodeTypesOfInterest] = '\0'; +void NodeList::addSetOfNodeTypesToNodeInterestSet(const QSet& setOfNodeTypes) { + _nodeTypesOfInterest.unite(setOfNodeTypes); } const uint32_t RFC_5389_MAGIC_COOKIE = 0x2112A442; @@ -522,7 +523,7 @@ void NodeList::sendDomainServerCheckIn() { sendSTUNRequest(); } else { // construct the DS check in packet if we need to - int numBytesNodesOfInterest = _nodeTypesOfInterest ? strlen((char*) _nodeTypesOfInterest) : 0; + int numBytesNodesOfInterest = _nodeTypesOfInterest.size(); const int IP_ADDRESS_BYTES = 4; @@ -563,11 +564,8 @@ void NodeList::sendDomainServerCheckIn() { *(packetPosition++) = numBytesNodesOfInterest; // copy over the bytes for node types of interest, if required - if (numBytesNodesOfInterest > 0) { - memcpy(packetPosition, - _nodeTypesOfInterest, - numBytesNodesOfInterest); - packetPosition += numBytesNodesOfInterest; + foreach (NODE_TYPE nodeTypeOfInterest, _nodeTypesOfInterest) { + *(packetPosition++) = nodeTypeOfInterest; } _nodeSocket.writeDatagram((char*) checkInPacket, packetPosition - checkInPacket, @@ -754,7 +752,7 @@ unsigned NodeList::broadcastToNodes(unsigned char* broadcastData, size_t dataByt } void NodeList::pingInactiveNodes() { - foreach (const SharedNodePointer& node, _nodeHash) { + foreach (const SharedNodePointer& node, getNodeHash()) { if (!node->getActiveSocket()) { // we don't have an active link to this node, ping it to set that up pingPublicAndLocalSocketsForInactiveNode(node.data()); @@ -791,7 +789,7 @@ void NodeList::activateSocketFromNodeCommunication(const HifiSockAddr& nodeAddre SharedNodePointer NodeList::soloNodeOfType(char nodeType) { if (memchr(SOLO_NODE_TYPES, nodeType, sizeof(SOLO_NODE_TYPES)) != NULL) { - foreach (const SharedNodePointer& node, _nodeHash) { + foreach (const SharedNodePointer& node, getNodeHash()) { if (node->getType() == nodeType) { return node; } diff --git a/libraries/shared/src/NodeList.h b/libraries/shared/src/NodeList.h index 9ac6c5970c..2143ae87eb 100644 --- a/libraries/shared/src/NodeList.h +++ b/libraries/shared/src/NodeList.h @@ -22,6 +22,7 @@ #endif #include +#include #include #include #include @@ -87,10 +88,10 @@ public: int getNumNoReplyDomainCheckIns() const { return _numNoReplyDomainCheckIns; } - void clear(); void reset(); - - void setNodeTypesOfInterest(const char* nodeTypesOfInterest, int numNodeTypesOfInterest); + + void addNodeTypeToInterestSet(NODE_TYPE nodeTypeToAdd); + void addSetOfNodeTypesToNodeInterestSet(const QSet& setOfNodeTypes); int processDomainServerList(unsigned char *packetData, size_t dataBytes); @@ -151,7 +152,7 @@ private: HifiSockAddr _domainSockAddr; QUdpSocket _nodeSocket; char _ownerType; - char* _nodeTypesOfInterest; + QSet _nodeTypesOfInterest; QUuid _ownerUUID; int _numNoReplyDomainCheckIns; HifiSockAddr _assignmentServerSocket; @@ -163,6 +164,7 @@ private: void timePingReply(const HifiSockAddr& nodeAddress, unsigned char *packetData); void resetDomainData(char domainField[], const char* domainData); void domainLookup(); + void clear(); }; #endif /* defined(__hifi__NodeList__) */ diff --git a/libraries/shared/src/NodeTypes.h b/libraries/shared/src/NodeTypes.h index 37e0503bab..6e8523c7d7 100644 --- a/libraries/shared/src/NodeTypes.h +++ b/libraries/shared/src/NodeTypes.h @@ -25,7 +25,6 @@ const NODE_TYPE NODE_TYPE_ENVIRONMENT_SERVER = 'E'; const NODE_TYPE NODE_TYPE_AGENT = 'I'; const NODE_TYPE NODE_TYPE_AUDIO_MIXER = 'M'; const NODE_TYPE NODE_TYPE_AVATAR_MIXER = 'W'; -const NODE_TYPE NODE_TYPE_AUDIO_INJECTOR = 'A'; const NODE_TYPE NODE_TYPE_ANIMATION_SERVER = 'a'; const NODE_TYPE NODE_TYPE_UNASSIGNED = 1; diff --git a/libraries/voxel-server/src/VoxelServer.cpp b/libraries/voxel-server/src/VoxelServer.cpp index a1f5eaf55e..c8aee246e3 100644 --- a/libraries/voxel-server/src/VoxelServer.cpp +++ b/libraries/voxel-server/src/VoxelServer.cpp @@ -68,4 +68,6 @@ void VoxelServer::beforeRun() { qDebug("Using Minimal Environment=%s", debug::valueOf(_sendMinimalEnvironment)); } qDebug("Sending environments=%s", debug::valueOf(_sendEnvironments)); + + NodeList::getInstance()->addNodeTypeToInterestSet(NODE_TYPE_ANIMATION_SERVER); } diff --git a/libraries/voxels/src/VoxelsScriptingInterface.h b/libraries/voxels/src/VoxelsScriptingInterface.h index a1f52fc1c3..97bdfb2c59 100644 --- a/libraries/voxels/src/VoxelsScriptingInterface.h +++ b/libraries/voxels/src/VoxelsScriptingInterface.h @@ -19,7 +19,7 @@ /// handles scripting of voxel commands from JS passed to assigned clients class VoxelsScriptingInterface : public OctreeScriptingInterface { Q_OBJECT -public: +public: VoxelEditPacketSender* getVoxelPacketSender() { return (VoxelEditPacketSender*)getPacketSender(); } virtual NODE_TYPE getServerNodeType() const { return NODE_TYPE_VOXEL_SERVER; }