History

Merge branch 'receive_packets' of https://github.com/huffman/hifi into protocol

Atlante45 committed 11 years ago

Commit fbb6a942 parents aca759a c6ffda1107 files · +1921 −2050Browse files

Showing the changes to assignment-client/src/AssignmentClientMonitor.cpp (1 of 107 files). Show all

Unified · Split

assignment-client/src/AssignmentClientMonitor.cpp+46−55
@@ -53,7 +53,8 @@ AssignmentClientMonitor::AssignmentClientMonitor(const unsigned int numAssignmen · 52 unmodified lines
53 auto addressManager = DependencyManager::set<AddressManager>();53 auto addressManager = DependencyManager::set<AddressManager>();
54 auto nodeList = DependencyManager::set<LimitedNodeList>();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 // use QProcess to fork off a process for each of the child assignment clients59 // use QProcess to fork off a process for each of the child assignment clients
59 for (unsigned int i = 0; i < _numAssignmentClientForks; i++) {60 for (unsigned int i = 0; i < _numAssignmentClientForks; i++) {
@@ -172,9 +173,9 @@ void AssignmentClientMonitor::checkSpares() { · 112 unmodified lines
172 nodeList->removeSilentNodes();173 nodeList->removeSilentNodes();
173 174
174 nodeList->eachNode([&](const SharedNodePointer& node) {175 nodeList->eachNode([&](const SharedNodePointer& node) {
175 AssignmentClientChildData *childData = static_cast<AssignmentClientChildData*>(node->getLinkedData());176 AssignmentClientChildData* childData = static_cast<AssignmentClientChildData*>(node->getLinkedData());
176 totalCount ++;177 totalCount ++;
177 if (childData->getChildType() == "none") {178 if (childData->getChildType() == Assignment::Type::AllTypes) {
178 spareCount ++;179 spareCount ++;
179 aSpareId = node->getUUID();180 aSpareId = node->getUUID();
180 }181 }
@@ -201,63 +202,53 @@ void AssignmentClientMonitor::checkSpares() { · 20 unmodified lines
201 }202 }
202}203}
203 204
205void AssignmentClientMonitor::handleChildStatusPacket(QSharedPointer<NLPacket> packet) {
206 QUuid senderID = QUuid::fromRfc4122(QByteArray::fromRawData(packet->getData(), NUM_BYTES_RFC4122_UUID));
204 207
205void AssignmentClientMonitor::readPendingDatagrams() {
206 auto nodeList = DependencyManager::get<NodeList>();208 auto nodeList = DependencyManager::get<NodeList>();
207 209
208 QByteArray receivedPacket;210 SharedNodePointer matchingNode = nodeList->nodeWithUUID(senderID);
209 HifiSockAddr senderSockAddr;211 const HifiSockAddr& senderSockAddr = packet->getSenderSockAddr();
210 212
211 while (nodeList->getNodeSocket().hasPendingDatagrams()) {213 AssignmentClientChildData* childData = nullptr;
212 receivedPacket.resize(nodeList->getNodeSocket().pendingDatagramSize());214
213 nodeList->getNodeSocket().readDatagram(receivedPacket.data(), receivedPacket.size(),215 if (!matchingNode) {
214 senderSockAddr.getAddressPointer(), senderSockAddr.getPortPointer());216 // The parent only expects to be talking with prorams running on this same machine.
215 217 if (senderSockAddr.getAddress() == QHostAddress::LocalHost ||
216 if (nodeList->packetVersionAndHashMatch(receivedPacket)) {218 senderSockAddr.getAddress() == QHostAddress::LocalHostIPv6) {
217 if (packetTypeForPacket(receivedPacket) == PacketType::NodeJsonStats) {219
218 QUuid packetUUID = uuidFromPacketHeader(receivedPacket);220 if (!senderID.isNull()) {
219 SharedNodePointer matchingNode = nodeList->sendingNodeForPacket(receivedPacket);221 // We don't have this node yet - we should add it
220 if (!matchingNode) {222 matchingNode = DependencyManager::get<LimitedNodeList>()->addOrUpdateNode
221 // The parent only expects to be talking with prorams running on this same machine.223 (senderID, NodeType::Unassigned, senderSockAddr, senderSockAddr, false, false);
222 if (senderSockAddr.getAddress() == QHostAddress::LocalHost ||224
223 senderSockAddr.getAddress() == QHostAddress::LocalHostIPv6) {225 childData = new AssignmentClientChildData(Assignment::Type::AllTypes);
224 if (!packetUUID.isNull()) {226 matchingNode->setLinkedData(childData);
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 }
255 } else {227 } else {
256 // have the NodeList attempt to handle it228 // tell unknown assignment-client child to exit.
257 nodeList->processNodeData(senderSockAddr, receivedPacket);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}