Showing the changes to assignment-client/src/AssignmentClientMonitor.cpp (1 of 107 files). Show all
Unified · Split
| @@ -53,7 +53,8 @@ AssignmentClientMonitor::AssignmentClientMonitor(const unsigned int numAssignmen · 52 unmodified lines | ||
| 53 | 53 | auto addressManager = DependencyManager::set<AddressManager>(); |
| 54 | 54 | auto nodeList = DependencyManager::set<LimitedNodeList>(); |
| 55 | 55 | |
| 56 | − connect(&nodeList->getNodeSocket(), &QUdpSocket::readyRead, this, &AssignmentClientMonitor::readPendingDatagrams); | |
| 56 | + auto& packetReceiver = DependencyManager::get<NodeList>()->getPacketReceiver(); | |
| 57 | + packetReceiver.registerListener(PacketType::AssignmentClientStatus, this, "handleChildStatusPacket"); | |
| 57 | 58 | |
| 58 | 59 | // use QProcess to fork off a process for each of the child assignment clients |
| 59 | 60 | for (unsigned int i = 0; i < _numAssignmentClientForks; i++) { |
| @@ -172,9 +173,9 @@ void AssignmentClientMonitor::checkSpares() { · 112 unmodified lines | ||
| 172 | 173 | nodeList->removeSilentNodes(); |
| 173 | 174 | |
| 174 | 175 | nodeList->eachNode([&](const SharedNodePointer& node) { |
| 175 | − AssignmentClientChildData *childData = static_cast<AssignmentClientChildData*>(node->getLinkedData()); | |
| 176 | + AssignmentClientChildData* childData = static_cast<AssignmentClientChildData*>(node->getLinkedData()); | |
| 176 | 177 | totalCount ++; |
| 177 | − if (childData->getChildType() == "none") { | |
| 178 | + if (childData->getChildType() == Assignment::Type::AllTypes) { | |
| 178 | 179 | spareCount ++; |
| 179 | 180 | aSpareId = node->getUUID(); |
| 180 | 181 | } |
| @@ -201,63 +202,53 @@ void AssignmentClientMonitor::checkSpares() { · 20 unmodified lines | ||
| 201 | 202 | } |
| 202 | 203 | } |
| 203 | 204 | |
| 205 | +void AssignmentClientMonitor::handleChildStatusPacket(QSharedPointer<NLPacket> packet) { | |
| 206 | + QUuid senderID = QUuid::fromRfc4122(QByteArray::fromRawData(packet->getData(), NUM_BYTES_RFC4122_UUID)); | |
| 204 | 207 | |
| 205 | −void AssignmentClientMonitor::readPendingDatagrams() { | |
| 206 | 208 | auto nodeList = DependencyManager::get<NodeList>(); |
| 207 | 209 | |
| 208 | − QByteArray receivedPacket; | |
| 209 | − HifiSockAddr senderSockAddr; | |
| 210 | − | |
| 211 | − while (nodeList->getNodeSocket().hasPendingDatagrams()) { | |
| 212 | − receivedPacket.resize(nodeList->getNodeSocket().pendingDatagramSize()); | |
| 213 | − nodeList->getNodeSocket().readDatagram(receivedPacket.data(), receivedPacket.size(), | |
| 214 | − senderSockAddr.getAddressPointer(), senderSockAddr.getPortPointer()); | |
| 215 | − | |
| 216 | − if (nodeList->packetVersionAndHashMatch(receivedPacket)) { | |
| 217 | − if (packetTypeForPacket(receivedPacket) == PacketType::NodeJsonStats) { | |
| 218 | − QUuid packetUUID = uuidFromPacketHeader(receivedPacket); | |
| 219 | − SharedNodePointer matchingNode = nodeList->sendingNodeForPacket(receivedPacket); | |
| 220 | − if (!matchingNode) { | |
| 221 | − // The parent only expects to be talking with prorams running on this same machine. | |
| 222 | − if (senderSockAddr.getAddress() == QHostAddress::LocalHost || | |
| 223 | − senderSockAddr.getAddress() == QHostAddress::LocalHostIPv6) { | |
| 224 | − if (!packetUUID.isNull()) { | |
| 225 | − matchingNode = DependencyManager::get<LimitedNodeList>()->addOrUpdateNode | |
| 226 | − (packetUUID, NodeType::Unassigned, senderSockAddr, senderSockAddr, false, false); | |
| 227 | − AssignmentClientChildData *childData = new AssignmentClientChildData("unknown"); | |
| 228 | − matchingNode->setLinkedData(childData); | |
| 229 | − } else { | |
| 230 | − // tell unknown assignment-client child to exit. | |
| 231 | − qDebug() << "asking unknown child to exit."; | |
| 232 | − | |
| 233 | − auto diePacket = NLPacket::create(PacketType::StopNode, 0); | |
| 234 | − nodeList->sendPacket(std::move(diePacket), senderSockAddr); | |
| 235 | − } | |
| 236 | − } | |
| 237 | − } | |
| 238 | − | |
| 239 | − if (matchingNode) { | |
| 240 | − // update our records about how to reach this child | |
| 241 | − matchingNode->setLocalSocket(senderSockAddr); | |
| 242 | − | |
| 243 | − QVariantMap packetVariantMap = | |
| 244 | − JSONBreakableMarshal::fromStringBuffer(receivedPacket.mid(numBytesForPacketHeader(receivedPacket))); | |
| 245 | − QJsonObject unpackedStatsJSON = QJsonObject::fromVariantMap(packetVariantMap); | |
| 246 | − | |
| 247 | − // get child's assignment type out of the decoded json | |
| 248 | − QString childType = unpackedStatsJSON["assignment_type"].toString(); | |
| 249 | − AssignmentClientChildData *childData = | |
| 250 | − static_cast<AssignmentClientChildData*>(matchingNode->getLinkedData()); | |
| 251 | − childData->setChildType(childType); | |
| 252 | − // note when this child talked | |
| 253 | − matchingNode->setLastHeardMicrostamp(usecTimestampNow()); | |
| 254 | − } | |
| 210 | + SharedNodePointer matchingNode = nodeList->nodeWithUUID(senderID); | |
| 211 | + const HifiSockAddr& senderSockAddr = packet->getSenderSockAddr(); | |
| 212 | + | |
| 213 | + AssignmentClientChildData* childData = nullptr; | |
| 214 | + | |
| 215 | + if (!matchingNode) { | |
| 216 | + // The parent only expects to be talking with prorams running on this same machine. | |
| 217 | + if (senderSockAddr.getAddress() == QHostAddress::LocalHost || | |
| 218 | + senderSockAddr.getAddress() == QHostAddress::LocalHostIPv6) { | |
| 219 | + | |
| 220 | + if (!senderID.isNull()) { | |
| 221 | + // We don't have this node yet - we should add it | |
| 222 | + matchingNode = DependencyManager::get<LimitedNodeList>()->addOrUpdateNode | |
| 223 | + (senderID, NodeType::Unassigned, senderSockAddr, senderSockAddr, false, false); | |
| 224 | + | |
| 225 | + childData = new AssignmentClientChildData(Assignment::Type::AllTypes); | |
| 226 | + matchingNode->setLinkedData(childData); | |
| 255 | 227 | } else { |
| 256 | − // have the NodeList attempt to handle it | |
| 257 | − nodeList->processNodeData(senderSockAddr, receivedPacket); | |
| 228 | + // tell unknown assignment-client child to exit. | |
| 229 | + qDebug() << "Asking unknown child at" << senderSockAddr << "to exit."; | |
| 230 | + | |
| 231 | + auto diePacket = NLPacket::create(PacketType::StopNode, 0); | |
| 232 | + nodeList->sendPacket(std::move(diePacket), senderSockAddr); | |
| 233 | + | |
| 234 | + return; | |
| 258 | 235 | } |
| 259 | 236 | } |
| 237 | + } else { | |
| 238 | + childData = dynamic_cast<AssignmentClientChildData*>(matchingNode->getLinkedData()); | |
| 260 | 239 | } |
| 261 | −} | |
| 262 | − | |
| 263 | 240 | |
| 241 | + if (childData) { | |
| 242 | + // update our records about how to reach this child | |
| 243 | + matchingNode->setLocalSocket(senderSockAddr); | |
| 244 | + | |
| 245 | + // get child's assignment type out of the packet | |
| 246 | + quint8 assignmentType; | |
| 247 | + packet->readPrimitive(&assignmentType); | |
| 248 | + | |
| 249 | + childData->setChildType((Assignment::Type) assignmentType); | |
| 250 | + | |
| 251 | + // note when this child talked | |
| 252 | + matchingNode->setLastHeardMicrostamp(usecTimestampNow()); | |
| 253 | + } | |
| 254 | +} | |