void ThreadedAssignment::setFinished(bool isFinished) {
    _isFinished = isFinished;

    if (_isFinished) {
        aboutToFinish();
        
        NodeList* nodeList = NodeList::getInstance();
        
        // if we have a datagram processing thread, quit it and wait on it to make sure that
        // the node socket is back on the same thread as the NodeList
        
        if (_datagramProcessingThread) {
            // tell the datagram processing thread to quit and wait until it is done, then return the node socket to the NodeList
            _datagramProcessingThread->quit();
            _datagramProcessingThread->wait();
            
            // set node socket parent back to NodeList
            nodeList->getNodeSocket().setParent(nodeList);
        }
        
        // move the NodeList back to the QCoreApplication instance's thread
        nodeList->moveToThread(QCoreApplication::instance()->thread());
        
        emit finished();
    }
}
Exemple #2
0
void AssignmentClient::readPendingDatagrams() {
    NodeList* nodeList = NodeList::getInstance();
    
    QByteArray receivedPacket;
    HifiSockAddr senderSockAddr;
    
    while (nodeList->getNodeSocket().hasPendingDatagrams()) {
        receivedPacket.resize(nodeList->getNodeSocket().pendingDatagramSize());
        nodeList->getNodeSocket().readDatagram(receivedPacket.data(), receivedPacket.size(),
                                               senderSockAddr.getAddressPointer(), senderSockAddr.getPortPointer());
        
        if (nodeList->packetVersionAndHashMatch(receivedPacket)) {
            if (packetTypeForPacket(receivedPacket) == PacketTypeCreateAssignment) {
                // construct the deployed assignment from the packet data
                _currentAssignment = AssignmentFactory::unpackAssignment(receivedPacket);
                
                if (_currentAssignment) {
                    qDebug() << "Received an assignment -" << *_currentAssignment;
                    
                    // switch our nodelist domain IP and port to whoever sent us the assignment
                    
                    nodeList->getDomainInfo().setSockAddr(senderSockAddr);
                    nodeList->getDomainInfo().setAssignmentUUID(_currentAssignment->getUUID());
                    
                    qDebug() << "Destination IP for assignment is" << nodeList->getDomainInfo().getIP().toString();
                    
                    // start the deployed assignment
                    QThread* workerThread = new QThread(this);
                    
                    connect(workerThread, SIGNAL(started()), _currentAssignment, SLOT(run()));
                    
                    connect(_currentAssignment, SIGNAL(finished()), this, SLOT(assignmentCompleted()));
                    connect(_currentAssignment, SIGNAL(finished()), workerThread, SLOT(quit()));
                    connect(_currentAssignment, SIGNAL(finished()), _currentAssignment, SLOT(deleteLater()));
                    connect(workerThread, SIGNAL(finished()), workerThread, SLOT(deleteLater()));
                    
                    _currentAssignment->moveToThread(workerThread);
                    
                    // move the NodeList to the thread used for the _current assignment
                    nodeList->moveToThread(workerThread);
                    
                    // let the assignment handle the incoming datagrams for its duration
                    disconnect(&nodeList->getNodeSocket(), 0, this, 0);
                    connect(&nodeList->getNodeSocket(), &QUdpSocket::readyRead, _currentAssignment,
                            &ThreadedAssignment::readPendingDatagrams);
                    
                    // Starts an event loop, and emits workerThread->started()
                    workerThread->start();
                } else {
                    qDebug() << "Received an assignment that could not be unpacked. Re-requesting.";
                }
            } else {
                // have the NodeList attempt to handle it
                nodeList->processNodeData(senderSockAddr, receivedPacket);
            }
        }
    }
}
Exemple #3
0
void AssignmentClient::readPendingDatagrams() {
    NodeList* nodeList = NodeList::getInstance();
    
    QByteArray receivedPacket;
    HifiSockAddr senderSockAddr;
    
    while (nodeList->getNodeSocket().hasPendingDatagrams()) {
        receivedPacket.resize(nodeList->getNodeSocket().pendingDatagramSize());
        nodeList->getNodeSocket().readDatagram(receivedPacket.data(), receivedPacket.size(),
                                               senderSockAddr.getAddressPointer(), senderSockAddr.getPortPointer());
        
        if (packetVersionMatch(receivedPacket)) {
            if (_currentAssignment) {
                // have the threaded current assignment handle this datagram
                QMetaObject::invokeMethod(_currentAssignment, "processDatagram", Qt::QueuedConnection,
                                          Q_ARG(QByteArray, receivedPacket),
                                          Q_ARG(HifiSockAddr, senderSockAddr));
            } else if (packetTypeForPacket(receivedPacket) == PacketTypeCreateAssignment) {
                
                if (_currentAssignment) {
                    qDebug() << "Dropping received assignment since we are currently running one.";
                } else {
                    // construct the deployed assignment from the packet data
                    _currentAssignment = AssignmentFactory::unpackAssignment(receivedPacket);
                    
                    if (_currentAssignment) {
                        qDebug() << "Received an assignment -" << *_currentAssignment;
                        
                        // switch our nodelist domain IP and port to whoever sent us the assignment
                        
                        nodeList->setDomainSockAddr(senderSockAddr);
                        nodeList->setOwnerUUID(_currentAssignment->getUUID());
                        
                        qDebug() << "Destination IP for assignment is" << nodeList->getDomainIP().toString();
                        
                        // start the deployed assignment
                        QThread* workerThread = new QThread(this);
                        
                        connect(workerThread, SIGNAL(started()), _currentAssignment, SLOT(run()));
                        
                        connect(_currentAssignment, SIGNAL(finished()), this, SLOT(assignmentCompleted()));
                        connect(_currentAssignment, SIGNAL(finished()), workerThread, SLOT(quit()));
                        connect(_currentAssignment, SIGNAL(finished()), _currentAssignment, SLOT(deleteLater()));
                        connect(workerThread, SIGNAL(finished()), workerThread, SLOT(deleteLater()));
                        
                        _currentAssignment->moveToThread(workerThread);
                        
                        // move the NodeList to the thread used for the _current assignment
                        nodeList->moveToThread(workerThread);
                        
                        // Starts an event loop, and emits workerThread->started()
                        workerThread->start();
                    } else {
                        qDebug() << "Received an assignment that could not be unpacked. Re-requesting.";
                    }
                }
            } else {
                // have the NodeList attempt to handle it
                nodeList->processNodeData(senderSockAddr, receivedPacket);
            }
        }
    }
}