From 9dc460bab4ccf6aef120b5faa0cc50bb3e5c3bfc Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 24 Feb 2014 11:56:14 -0800 Subject: [PATCH] make multiple instances of scripted assignment use diff UUID --- domain-server/src/DomainServer.cpp | 58 ++++++++++++++--------------- libraries/shared/src/Assignment.cpp | 6 --- libraries/shared/src/Assignment.h | 7 +--- 3 files changed, 29 insertions(+), 42 deletions(-) diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index e9816abe4c..392bdb31e8 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -490,42 +490,47 @@ bool DomainServer::handleHTTPRequest(HTTPConnection* connection, const QString& // this is a script upload - ask the HTTPConnection to parse the form data QList formData = connection->parseFormData(); - // create an assignment for this saved script - Assignment* scriptAssignment = new Assignment(Assignment::CreateCommand, Assignment::AgentType); + // check how many instances of this assignment the user wants by checking the ASSIGNMENT-INSTANCES header const QString ASSIGNMENT_INSTANCES_HEADER = "ASSIGNMENT-INSTANCES"; QByteArray assignmentInstancesValue = connection->requestHeaders().value(ASSIGNMENT_INSTANCES_HEADER.toLocal8Bit()); + + int numInstances = 1; + if (!assignmentInstancesValue.isEmpty()) { // the user has requested a specific number of instances // so set that on the created assignment - int numInstances = assignmentInstancesValue.toInt(); - if (numInstances > 0) { - qDebug() << numInstances; - scriptAssignment->setNumberOfInstances(numInstances); - } + + numInstances = assignmentInstancesValue.toInt(); } - + const char ASSIGNMENT_SCRIPT_HOST_LOCATION[] = "resources/web/assignment"; - QString newPath(ASSIGNMENT_SCRIPT_HOST_LOCATION); - newPath += "/"; - // append the UUID for this script as the new filename, remove the curly braces - newPath += uuidStringWithoutCurlyBraces(scriptAssignment->getUUID()); - - // create a file with the GUID of the assignment in the script host locaiton - QFile scriptFile(newPath); - scriptFile.open(QIODevice::WriteOnly); - scriptFile.write(formData[0].second); - - qDebug("Saved a script for assignment at %s", qPrintable(newPath)); + for (int i = 0; i < numInstances; i++) { + + // create an assignment for this saved script + Assignment* scriptAssignment = new Assignment(Assignment::CreateCommand, Assignment::AgentType); + + QString newPath(ASSIGNMENT_SCRIPT_HOST_LOCATION); + newPath += "/"; + // append the UUID for this script as the new filename, remove the curly braces + newPath += uuidStringWithoutCurlyBraces(scriptAssignment->getUUID()); + + // create a file with the GUID of the assignment in the script host locaiton + QFile scriptFile(newPath); + scriptFile.open(QIODevice::WriteOnly); + scriptFile.write(formData[0].second); + + qDebug("Saved a script for assignment at %s", qPrintable(newPath)); + + // add the script assigment to the assignment queue + _assignmentQueue.enqueue(SharedAssignmentPointer(scriptAssignment)); + } // respond with a 200 code for successful upload connection->respond(HTTPConnection::StatusCode200); - - // add the script assigment to the assignment queue - _assignmentQueue.enqueue(SharedAssignmentPointer(scriptAssignment)); } } else if (connection->requestOperation() == QNetworkAccessManager::DeleteOperation) { if (path.startsWith(URI_NODE)) { @@ -639,14 +644,7 @@ SharedAssignmentPointer DomainServer::deployableAssignmentForRequest(const Assig if (assignment->getType() == Assignment::AgentType) { // if there is more than one instance to send out, simply decrease the number of instances - - if (assignment->getNumberOfInstances() == 1) { - return _assignmentQueue.takeAt(sharedAssignment - _assignmentQueue.begin()); - } else { - assignment->decrementNumberOfInstances(); - return *sharedAssignment; - } - + return _assignmentQueue.takeAt(sharedAssignment - _assignmentQueue.begin()); } else { // remove the assignment from the queue SharedAssignmentPointer deployableAssignment = _assignmentQueue.takeAt(sharedAssignment diff --git a/libraries/shared/src/Assignment.cpp b/libraries/shared/src/Assignment.cpp index 83296991f3..992ec96a67 100644 --- a/libraries/shared/src/Assignment.cpp +++ b/libraries/shared/src/Assignment.cpp @@ -45,7 +45,6 @@ Assignment::Assignment() : _type(Assignment::AllTypes), _pool(), _location(Assignment::LocalLocation), - _numberOfInstances(1), _payload() { @@ -57,7 +56,6 @@ Assignment::Assignment(Assignment::Command command, Assignment::Type type, const _type(type), _pool(pool), _location(location), - _numberOfInstances(1), _payload() { if (_command == Assignment::CreateCommand) { @@ -69,7 +67,6 @@ Assignment::Assignment(Assignment::Command command, Assignment::Type type, const Assignment::Assignment(const QByteArray& packet) : _pool(), _location(GlobalLocation), - _numberOfInstances(1), _payload() { PacketType packetType = packetTypeForPacket(packet); @@ -99,7 +96,6 @@ Assignment::Assignment(const Assignment& otherAssignment) { _type = otherAssignment._type; _location = otherAssignment._location; _pool = otherAssignment._pool; - _numberOfInstances = otherAssignment._numberOfInstances; _payload = otherAssignment._payload; } @@ -117,8 +113,6 @@ void Assignment::swap(Assignment& otherAssignment) { swap(_type, otherAssignment._type); swap(_location, otherAssignment._location); swap(_pool, otherAssignment._pool); - - swap(_numberOfInstances, otherAssignment._numberOfInstances); swap(_payload, otherAssignment._payload); } diff --git a/libraries/shared/src/Assignment.h b/libraries/shared/src/Assignment.h index dd34751b57..a8ab3be4a8 100644 --- a/libraries/shared/src/Assignment.h +++ b/libraries/shared/src/Assignment.h @@ -79,11 +79,7 @@ public: void setPool(const QString& pool) { _pool = pool; }; const QString& getPool() const { return _pool; } - - int getNumberOfInstances() const { return _numberOfInstances; } - void setNumberOfInstances(int numberOfInstances) { _numberOfInstances = numberOfInstances; } - void decrementNumberOfInstances() { --_numberOfInstances; } - + const char* getTypeName() const; // implement parseData to return 0 so we can be a subclass of NodeData @@ -99,7 +95,6 @@ protected: Assignment::Type _type; /// the type of the assignment, defines what the assignee will do QString _pool; /// the destination pool for this assignment Assignment::Location _location; /// the location of the assignment, allows a domain to preferentially use local ACs - int _numberOfInstances; /// the number of instances of this assignment QByteArray _payload; /// an optional payload attached to this assignment, a maximum for 1024 bytes will be packed };