mirror of
https://github.com/HifiExperiments/overte.git
synced 2025-08-09 05:48:26 +02:00
Merge pull request #1001 from birarda/forked-js-ac
use libcurl to download JS in forked ACs
This commit is contained in:
commit
7772b91aa8
4 changed files with 134 additions and 84 deletions
|
@ -33,3 +33,7 @@ find_package(STK REQUIRED)
|
||||||
target_link_libraries(${TARGET_NAME} ${STK_LIBRARIES})
|
target_link_libraries(${TARGET_NAME} ${STK_LIBRARIES})
|
||||||
include_directories(${STK_INCLUDE_DIRS})
|
include_directories(${STK_INCLUDE_DIRS})
|
||||||
|
|
||||||
|
# link curl for synchronous script downloads
|
||||||
|
find_package(CURL REQUIRED)
|
||||||
|
include_directories(${CURL_INCLUDE_DIRS})
|
||||||
|
target_link_libraries(${TARGET_NAME} ${CURL_LIBRARY})
|
|
@ -6,8 +6,9 @@
|
||||||
// Copyright (c) 2013 HighFidelity, Inc. All rights reserved.
|
// Copyright (c) 2013 HighFidelity, Inc. All rights reserved.
|
||||||
//
|
//
|
||||||
|
|
||||||
|
#include <curl/curl.h>
|
||||||
|
|
||||||
#include <QtScript/QScriptEngine>
|
#include <QtScript/QScriptEngine>
|
||||||
#include <QtNetwork/QtNetwork>
|
|
||||||
|
|
||||||
#include <AvatarData.h>
|
#include <AvatarData.h>
|
||||||
#include <NodeList.h>
|
#include <NodeList.h>
|
||||||
|
@ -23,6 +24,22 @@ Agent::Agent(const unsigned char* dataBuffer, int numBytes) :
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Agent::stop() {
|
||||||
|
_shouldStop = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static size_t writeScriptDataToString(void *contents, size_t size, size_t nmemb, void *userdata) {
|
||||||
|
size_t realSize = size * nmemb;
|
||||||
|
|
||||||
|
QString* scriptContents = (QString*) userdata;
|
||||||
|
|
||||||
|
// append this chunk to the scriptContents
|
||||||
|
scriptContents->append(QByteArray((char*) contents, realSize));
|
||||||
|
|
||||||
|
// return the amount of data read
|
||||||
|
return realSize;
|
||||||
|
}
|
||||||
|
|
||||||
void Agent::run() {
|
void Agent::run() {
|
||||||
NodeList* nodeList = NodeList::getInstance();
|
NodeList* nodeList = NodeList::getInstance();
|
||||||
nodeList->setOwnerType(NODE_TYPE_AGENT);
|
nodeList->setOwnerType(NODE_TYPE_AGENT);
|
||||||
|
@ -30,101 +47,125 @@ void Agent::run() {
|
||||||
|
|
||||||
nodeList->getNodeSocket()->setBlocking(false);
|
nodeList->getNodeSocket()->setBlocking(false);
|
||||||
|
|
||||||
QNetworkAccessManager manager;
|
|
||||||
|
|
||||||
// figure out the URL for the script for this agent assignment
|
// figure out the URL for the script for this agent assignment
|
||||||
QString scriptURLString("http://%1:8080/assignment/%2");
|
QString scriptURLString("http://%1:8080/assignment/%2");
|
||||||
scriptURLString = scriptURLString.arg(NodeList::getInstance()->getDomainIP().toString(),
|
scriptURLString = scriptURLString.arg(NodeList::getInstance()->getDomainIP().toString(),
|
||||||
this->getUUIDStringWithoutCurlyBraces());
|
this->getUUIDStringWithoutCurlyBraces());
|
||||||
QUrl scriptURL(scriptURLString);
|
|
||||||
|
|
||||||
qDebug() << "Attemping download of " << scriptURL << "\n";
|
// setup curl for script download
|
||||||
|
CURLcode curlResult;
|
||||||
|
|
||||||
QNetworkReply* reply = manager.get(QNetworkRequest(scriptURL));
|
CURL* curlHandle = curl_easy_init();
|
||||||
|
|
||||||
QEventLoop loop;
|
// tell curl which file to grab
|
||||||
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
|
curl_easy_setopt(curlHandle, CURLOPT_URL, scriptURLString.toStdString().c_str());
|
||||||
loop.exec();
|
|
||||||
|
|
||||||
QString scriptString = QString(reply->readAll());
|
// send the data to the WriteMemoryCallback function
|
||||||
|
curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, writeScriptDataToString);
|
||||||
|
|
||||||
QScriptEngine engine;
|
QString scriptContents;
|
||||||
|
|
||||||
QScriptValue agentValue = engine.newQObject(this);
|
// pass the scriptContents QString to append data to
|
||||||
engine.globalObject().setProperty("Agent", agentValue);
|
curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, (void *)&scriptContents);
|
||||||
|
|
||||||
VoxelScriptingInterface voxelScripter;
|
// send a user agent since some servers will require it
|
||||||
QScriptValue voxelScripterValue = engine.newQObject(&voxelScripter);
|
curl_easy_setopt(curlHandle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
|
||||||
engine.globalObject().setProperty("Voxels", voxelScripterValue);
|
|
||||||
|
|
||||||
QScriptValue treeScaleValue = engine.newVariant(QVariant(TREE_SCALE));
|
qDebug() << "Downloading script at" << scriptURLString << "\n";
|
||||||
engine.globalObject().setProperty("TREE_SCALE", treeScaleValue);
|
|
||||||
|
|
||||||
const long long VISUAL_DATA_SEND_INTERVAL_USECS = (1 / 60.0f) * 1000 * 1000;
|
// blocking get for JS file
|
||||||
|
curlResult = curl_easy_perform(curlHandle);
|
||||||
QScriptValue visualSendIntervalValue = engine.newVariant((QVariant(VISUAL_DATA_SEND_INTERVAL_USECS / 1000)));
|
|
||||||
engine.globalObject().setProperty("VISUAL_DATA_SEND_INTERVAL_MS", visualSendIntervalValue);
|
if (curlResult == CURLE_OK) {
|
||||||
|
// cleanup curl
|
||||||
qDebug() << "Downloaded script:" << scriptString << "\n";
|
curl_easy_cleanup(curlHandle);
|
||||||
QScriptValue result = engine.evaluate(scriptString);
|
curl_global_cleanup();
|
||||||
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;
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
// if we're not hearing from the domain-server we should stop running
|
QScriptEngine engine;
|
||||||
if (NodeList::getInstance()->getNumNoReplyDomainCheckIns() == MAX_SILENT_DOMAIN_SERVER_CHECK_INS) {
|
|
||||||
break;
|
QScriptValue agentValue = engine.newQObject(this);
|
||||||
|
engine.globalObject().setProperty("Agent", agentValue);
|
||||||
|
|
||||||
|
VoxelScriptingInterface voxelScripter;
|
||||||
|
QScriptValue voxelScripterValue = engine.newQObject(&voxelScripter);
|
||||||
|
engine.globalObject().setProperty("Voxels", voxelScripterValue);
|
||||||
|
|
||||||
|
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:" << scriptContents << "\n";
|
||||||
|
QScriptValue result = engine.evaluate(scriptContents);
|
||||||
|
qDebug() << "Evaluated script.\n";
|
||||||
|
|
||||||
|
if (engine.hasUncaughtException()) {
|
||||||
|
int line = engine.uncaughtExceptionLineNumber();
|
||||||
|
qDebug() << "Uncaught exception at line" << line << ":" << result.toString() << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
// send a check in packet to the domain server if DOMAIN_SERVER_CHECK_IN_USECS has elapsed
|
timeval thisSend;
|
||||||
if (usecTimestampNow() - usecTimestamp(&lastDomainServerCheckIn) >= DOMAIN_SERVER_CHECK_IN_USECS) {
|
timeval lastDomainServerCheckIn = {};
|
||||||
gettimeofday(&lastDomainServerCheckIn, NULL);
|
int numMicrosecondsSleep = 0;
|
||||||
NodeList::getInstance()->sendDomainServerCheckIn();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!hasVoxelServer) {
|
sockaddr_in senderAddress;
|
||||||
for (NodeList::iterator node = nodeList->begin(); node != nodeList->end(); node++) {
|
unsigned char receivedData[MAX_PACKET_SIZE];
|
||||||
if (node->getType() == NODE_TYPE_VOXEL_SERVER) {
|
ssize_t receivedBytes;
|
||||||
hasVoxelServer = true;
|
|
||||||
|
bool hasVoxelServer = false;
|
||||||
|
|
||||||
|
while (!_shouldStop) {
|
||||||
|
// update the thisSend timeval to the current time
|
||||||
|
gettimeofday(&thisSend, NULL);
|
||||||
|
|
||||||
|
// if we're not hearing from the domain-server we should stop running
|
||||||
|
if (NodeList::getInstance()->getNumNoReplyDomainCheckIns() == MAX_SILENT_DOMAIN_SERVER_CHECK_INS) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// send a check in packet to the domain server if DOMAIN_SERVER_CHECK_IN_USECS has elapsed
|
||||||
|
if (usecTimestampNow() - usecTimestamp(&lastDomainServerCheckIn) >= DOMAIN_SERVER_CHECK_IN_USECS) {
|
||||||
|
gettimeofday(&lastDomainServerCheckIn, NULL);
|
||||||
|
NodeList::getInstance()->sendDomainServerCheckIn();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hasVoxelServer) {
|
||||||
|
for (NodeList::iterator node = nodeList->begin(); node != nodeList->end(); node++) {
|
||||||
|
if (node->getType() == NODE_TYPE_VOXEL_SERVER) {
|
||||||
|
hasVoxelServer = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
} else {
|
// allow the scripter's call back to setup visual data
|
||||||
// allow the scripter's call back to setup visual data
|
emit willSendVisualDataCallback();
|
||||||
emit willSendVisualDataCallback();
|
|
||||||
|
if (engine.hasUncaughtException()) {
|
||||||
if (engine.hasUncaughtException()) {
|
int line = engine.uncaughtExceptionLineNumber();
|
||||||
int line = engine.uncaughtExceptionLineNumber();
|
qDebug() << "Uncaught exception at line" << line << ":" << engine.uncaughtException().toString() << "\n";
|
||||||
qDebug() << "Uncaught exception at line" << line << ":" << engine.uncaughtException().toString() << "\n";
|
}
|
||||||
|
|
||||||
|
voxelScripter.getVoxelPacketSender()->processWithoutSleep();
|
||||||
}
|
}
|
||||||
|
|
||||||
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 = VISUAL_DATA_SEND_INTERVAL_USECS - (usecTimestampNow() - usecTimestamp(&thisSend))) > 0) {
|
||||||
|
usleep(numMicrosecondsSleep);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while (NodeList::getInstance()->getNodeSocket()->receive((sockaddr*) &senderAddress, receivedData, &receivedBytes)) {
|
} else {
|
||||||
NodeList::getInstance()->processNodeData((sockaddr*) &senderAddress, receivedData, receivedBytes);
|
// error in curl_easy_perform
|
||||||
}
|
qDebug() << "curl_easy_perform for JS failed:" << curl_easy_strerror(curlResult) << "\n";
|
||||||
|
|
||||||
// sleep for the correct amount of time to have data send be consistently timed
|
// cleanup curl
|
||||||
if ((numMicrosecondsSleep = VISUAL_DATA_SEND_INTERVAL_USECS - (usecTimestampNow() - usecTimestamp(&thisSend))) > 0) {
|
curl_easy_cleanup(curlHandle);
|
||||||
usleep(numMicrosecondsSleep);
|
curl_global_cleanup();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,11 +19,13 @@ class Agent : public Assignment {
|
||||||
public:
|
public:
|
||||||
Agent(const unsigned char* dataBuffer, int numBytes);
|
Agent(const unsigned char* dataBuffer, int numBytes);
|
||||||
|
|
||||||
bool volatile _shouldStop;
|
|
||||||
|
|
||||||
void run();
|
void run();
|
||||||
|
public slots:
|
||||||
|
void stop();
|
||||||
signals:
|
signals:
|
||||||
void willSendVisualDataCallback();
|
void willSendVisualDataCallback();
|
||||||
|
private:
|
||||||
|
bool volatile _shouldStop;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* defined(__hifi__Operative__) */
|
#endif /* defined(__hifi__Operative__) */
|
||||||
|
|
|
@ -36,8 +36,11 @@ sockaddr_in customAssignmentSocket = {};
|
||||||
int numForks = 0;
|
int numForks = 0;
|
||||||
Assignment::Type overiddenAssignmentType = Assignment::AllTypes;
|
Assignment::Type overiddenAssignmentType = Assignment::AllTypes;
|
||||||
|
|
||||||
|
int argc = 0;
|
||||||
|
char** argv = NULL;
|
||||||
|
|
||||||
void childClient() {
|
void childClient() {
|
||||||
// this is one of the child forks or there is a single assignment client, continue assignment-client execution
|
QCoreApplication application(::argc, ::argv);
|
||||||
|
|
||||||
// set the logging target to the the CHILD_TARGET_NAME
|
// set the logging target to the the CHILD_TARGET_NAME
|
||||||
Logging::setTargetName(CHILD_TARGET_NAME);
|
Logging::setTargetName(CHILD_TARGET_NAME);
|
||||||
|
@ -171,9 +174,9 @@ void parentMonitor() {
|
||||||
delete[] ::childForks;
|
delete[] ::childForks;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, const char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
|
::argc = argc;
|
||||||
QCoreApplication app(argc, (char**) argv);
|
::argv = argv;
|
||||||
|
|
||||||
setvbuf(stdout, NULL, _IOLBF, 0);
|
setvbuf(stdout, NULL, _IOLBF, 0);
|
||||||
|
|
||||||
|
@ -187,8 +190,8 @@ int main(int argc, const char* argv[]) {
|
||||||
const char CUSTOM_ASSIGNMENT_SERVER_PORT_OPTION[] = "-p";
|
const char CUSTOM_ASSIGNMENT_SERVER_PORT_OPTION[] = "-p";
|
||||||
|
|
||||||
// grab the overriden assignment-server hostname from argv, if it exists
|
// grab the overriden assignment-server hostname from argv, if it exists
|
||||||
const char* customAssignmentServerHostname = getCmdOption(argc, argv, CUSTOM_ASSIGNMENT_SERVER_HOSTNAME_OPTION);
|
const char* customAssignmentServerHostname = getCmdOption(argc, (const char**)argv, CUSTOM_ASSIGNMENT_SERVER_HOSTNAME_OPTION);
|
||||||
const char* customAssignmentServerPortString = getCmdOption(argc, argv, CUSTOM_ASSIGNMENT_SERVER_PORT_OPTION);
|
const char* customAssignmentServerPortString = getCmdOption(argc,(const char**)argv, CUSTOM_ASSIGNMENT_SERVER_PORT_OPTION);
|
||||||
|
|
||||||
if (customAssignmentServerHostname || customAssignmentServerPortString) {
|
if (customAssignmentServerHostname || customAssignmentServerPortString) {
|
||||||
|
|
||||||
|
@ -205,7 +208,7 @@ int main(int argc, const char* argv[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const char ASSIGNMENT_TYPE_OVVERIDE_OPTION[] = "-t";
|
const char ASSIGNMENT_TYPE_OVVERIDE_OPTION[] = "-t";
|
||||||
const char* assignmentTypeString = getCmdOption(argc, argv, ASSIGNMENT_TYPE_OVVERIDE_OPTION);
|
const char* assignmentTypeString = getCmdOption(argc, (const char**)argv, ASSIGNMENT_TYPE_OVVERIDE_OPTION);
|
||||||
|
|
||||||
if (assignmentTypeString) {
|
if (assignmentTypeString) {
|
||||||
// the user is asking to only be assigned to a particular type of assignment
|
// the user is asking to only be assigned to a particular type of assignment
|
||||||
|
@ -214,7 +217,7 @@ int main(int argc, const char* argv[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* NUM_FORKS_PARAMETER = "-n";
|
const char* NUM_FORKS_PARAMETER = "-n";
|
||||||
const char* numForksString = getCmdOption(argc, argv, NUM_FORKS_PARAMETER);
|
const char* numForksString = getCmdOption(argc, (const char**)argv, NUM_FORKS_PARAMETER);
|
||||||
|
|
||||||
int processID = 0;
|
int processID = 0;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue