use libcurl to download JS in forked ACs

This commit is contained in:
Stephen Birarda 2013-10-01 11:41:01 -07:00
parent 617b905385
commit bb0d28141a
3 changed files with 126 additions and 82 deletions

View file

@ -33,3 +33,7 @@ find_package(STK REQUIRED)
target_link_libraries(${TARGET_NAME} ${STK_LIBRARIES})
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})

View file

@ -6,8 +6,9 @@
// Copyright (c) 2013 HighFidelity, Inc. All rights reserved.
//
#include <curl/curl.h>
#include <QtScript/QScriptEngine>
#include <QtNetwork/QtNetwork>
#include <AvatarData.h>
#include <NodeList.h>
@ -23,6 +24,18 @@ Agent::Agent(const unsigned char* dataBuffer, int numBytes) :
}
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() {
NodeList* nodeList = NodeList::getInstance();
nodeList->setOwnerType(NODE_TYPE_AGENT);
@ -30,23 +43,39 @@ void Agent::run() {
nodeList->getNodeSocket()->setBlocking(false);
QNetworkAccessManager manager;
// figure out the URL for the script for this agent assignment
QString scriptURLString("http://%1:8080/assignment/%2");
scriptURLString = scriptURLString.arg(NodeList::getInstance()->getDomainIP().toString(),
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;
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
// tell curl which file to grab
curl_easy_setopt(curlHandle, CURLOPT_URL, scriptURLString.toStdString().c_str());
QString scriptString = QString(reply->readAll());
// send the data to the WriteMemoryCallback function
curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, writeScriptDataToString);
QString scriptContents;
// pass the scriptContents QString to append data to
curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, (void *)&scriptContents);
// send a user agent since some servers will require it
curl_easy_setopt(curlHandle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
qDebug() << "Downloading script at" << scriptURLString << "\n";
// blocking get for JS file
curlResult = curl_easy_perform(curlHandle);
if (curlResult == CURLE_OK) {
// cleanup curl
curl_easy_cleanup(curlHandle);
curl_global_cleanup();
QScriptEngine engine;
@ -65,8 +94,8 @@ void Agent::run() {
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";
QScriptValue result = engine.evaluate(scriptString);
qDebug() << "Downloaded script:" << scriptContents << "\n";
QScriptValue result = engine.evaluate(scriptContents);
qDebug() << "Evaluated script.\n";
if (engine.hasUncaughtException()) {
@ -127,4 +156,12 @@ void Agent::run() {
}
}
} else {
// error in curl_easy_perform
qDebug() << "curl_easy_perform for JS failed:" << curl_easy_strerror(curlResult) << "\n";
// cleanup curl
curl_easy_cleanup(curlHandle);
curl_global_cleanup();
}
}

View file

@ -36,8 +36,11 @@ sockaddr_in customAssignmentSocket = {};
int numForks = 0;
Assignment::Type overiddenAssignmentType = Assignment::AllTypes;
int argc = 0;
char** argv = NULL;
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
Logging::setTargetName(CHILD_TARGET_NAME);
@ -171,9 +174,9 @@ void parentMonitor() {
delete[] ::childForks;
}
int main(int argc, const char* argv[]) {
QCoreApplication app(argc, (char**) argv);
int main(int argc, char* argv[]) {
::argc = argc;
::argv = argv;
setvbuf(stdout, NULL, _IOLBF, 0);
@ -187,8 +190,8 @@ int main(int argc, const char* argv[]) {
const char CUSTOM_ASSIGNMENT_SERVER_PORT_OPTION[] = "-p";
// grab the overriden assignment-server hostname from argv, if it exists
const char* customAssignmentServerHostname = getCmdOption(argc, argv, CUSTOM_ASSIGNMENT_SERVER_HOSTNAME_OPTION);
const char* customAssignmentServerPortString = getCmdOption(argc, argv, CUSTOM_ASSIGNMENT_SERVER_PORT_OPTION);
const char* customAssignmentServerHostname = getCmdOption(argc, (const char**)argv, CUSTOM_ASSIGNMENT_SERVER_HOSTNAME_OPTION);
const char* customAssignmentServerPortString = getCmdOption(argc,(const char**)argv, CUSTOM_ASSIGNMENT_SERVER_PORT_OPTION);
if (customAssignmentServerHostname || customAssignmentServerPortString) {
@ -205,7 +208,7 @@ int main(int argc, const char* argv[]) {
}
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) {
// 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* numForksString = getCmdOption(argc, argv, NUM_FORKS_PARAMETER);
const char* numForksString = getCmdOption(argc, (const char**)argv, NUM_FORKS_PARAMETER);
int processID = 0;