From 313410ae0dd08517a523fca6ef7a0d8c26da5e1e Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 23 Sep 2013 09:57:05 -0700 Subject: [PATCH] updates to Agent class needed for game of life --- assignment-client/src/Agent.cpp | 49 +++++++++++++++++++++++++-------- assignment-client/src/Agent.h | 2 +- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/assignment-client/src/Agent.cpp b/assignment-client/src/Agent.cpp index cfdb34208c..aabf9fba66 100644 --- a/assignment-client/src/Agent.cpp +++ b/assignment-client/src/Agent.cpp @@ -21,10 +21,11 @@ Agent::Agent(const unsigned char* dataBuffer, int numBytes) : Assignment(dataBuf } void Agent::run() { - NodeList::getInstance()->setOwnerType(NODE_TYPE_AGENT); - NodeList::getInstance()->setNodeTypesOfInterest(&NODE_TYPE_VOXEL_SERVER, 1); - - NodeList::getInstance()->getNodeSocket()->setBlocking(false); + NodeList* nodeList = NodeList::getInstance(); + nodeList->setOwnerType(NODE_TYPE_AGENT); + nodeList->setNodeTypesOfInterest(&NODE_TYPE_VOXEL_SERVER, 1); + + nodeList->getNodeSocket()->setBlocking(false); QNetworkAccessManager manager; @@ -56,19 +57,30 @@ void Agent::run() { QScriptValue treeScaleValue = engine.newVariant(QVariant(TREE_SCALE)); engine.globalObject().setProperty("TREE_SCALE", treeScaleValue); + const long long VISUAL_DATA_SEND_INTERVAL_USECS = (1 / 60.0f) * 1000 * 1000; + + QScriptValue visualSendIntervalValue = engine.newVariant((QVariant(VISUAL_DATA_SEND_INTERVAL_USECS / 1000))); + engine.globalObject().setProperty("VISUAL_DATA_SEND_INTERVAL_MS", visualSendIntervalValue); + qDebug() << "Downloaded script:" << scriptString << "\n"; - qDebug() << "Evaluated script:" << engine.evaluate(scriptString).toString() << "\n"; + QScriptValue result = engine.evaluate(scriptString); + qDebug() << "Evaluated script.\n"; + + if (engine.hasUncaughtException()) { + int line = engine.uncaughtExceptionLineNumber(); + qDebug() << "Uncaught exception at line" << line << ":" << result.toString() << "\n"; + } timeval thisSend; timeval lastDomainServerCheckIn = {}; int numMicrosecondsSleep = 0; - const long long DATA_SEND_INTERVAL_USECS = (1 / 60.0f) * 1000 * 1000; - sockaddr_in senderAddress; unsigned char receivedData[MAX_PACKET_SIZE]; ssize_t receivedBytes; + bool hasVoxelServer = false; + while (!_shouldStop) { // update the thisSend timeval to the current time gettimeofday(&thisSend, NULL); @@ -84,17 +96,30 @@ void Agent::run() { NodeList::getInstance()->sendDomainServerCheckIn(); } - // allow the scripter's call back to setup visual data - emit preSendCallback(); - // flush the voxel packet queue, don't allow it to sleep us - voxelScripter.getVoxelPacketSender()->processWithoutSleep(); + if (!hasVoxelServer) { + for (NodeList::iterator node = nodeList->begin(); node != nodeList->end(); node++) { + if (node->getType() == NODE_TYPE_VOXEL_SERVER) { + hasVoxelServer = true; + } + } + } else { + // allow the scripter's call back to setup visual data + emit willSendVisualDataCallback(); + + if (engine.hasUncaughtException()) { + int line = engine.uncaughtExceptionLineNumber(); + qDebug() << "Uncaught exception at line" << line << ":" << engine.uncaughtException().toString() << "\n"; + } + + voxelScripter.getVoxelPacketSender()->processWithoutSleep(); + } while (NodeList::getInstance()->getNodeSocket()->receive((sockaddr*) &senderAddress, receivedData, &receivedBytes)) { NodeList::getInstance()->processNodeData((sockaddr*) &senderAddress, receivedData, receivedBytes); } // sleep for the correct amount of time to have data send be consistently timed - if ((numMicrosecondsSleep = DATA_SEND_INTERVAL_USECS - (usecTimestampNow() - usecTimestamp(&thisSend))) > 0) { + if ((numMicrosecondsSleep = VISUAL_DATA_SEND_INTERVAL_USECS - (usecTimestampNow() - usecTimestamp(&thisSend))) > 0) { usleep(numMicrosecondsSleep); } } diff --git a/assignment-client/src/Agent.h b/assignment-client/src/Agent.h index 5e0765fd33..6b5362c3ab 100644 --- a/assignment-client/src/Agent.h +++ b/assignment-client/src/Agent.h @@ -23,7 +23,7 @@ public: void run(); signals: - void preSendCallback(); + void willSendVisualDataCallback(); }; #endif /* defined(__hifi__Operative__) */