cleanup checking of request method, clarify that node is being deleted

This commit is contained in:
Stephen Birarda 2013-10-18 10:33:22 -07:00
parent d85c0bb88a
commit f6c9c57585

View file

@ -49,87 +49,97 @@ int DomainServer::civetwebRequestHandler(struct mg_connection *connection) {
const char RESPONSE_400[] = "HTTP/1.0 400 Bad Request\r\n\r\n"; const char RESPONSE_400[] = "HTTP/1.0 400 Bad Request\r\n\r\n";
const char ASSIGNMENT_URI[] = "/assignment"; const char ASSIGNMENT_URI[] = "/assignment";
const char NODE_URI[] = "/node";
if (strcmp(ri->uri, "/assignment") == 0 && strcmp(ri->request_method, "POST") == 0) { if (strcmp(ri->request_method, "GET") == 0) {
// return a 200 if (strcmp(ri->uri, "/assignments.json") == 0) {
mg_printf(connection, "%s", RESPONSE_200); // user is asking for json list of assignments
// upload the file
mg_upload(connection, "/tmp"); // start with a 200 response
mg_printf(connection, "%s", RESPONSE_200);
return 1;
} else if (strcmp(ri->uri, "/assignments.json") == 0) { // setup the JSON
// user is asking for json list of assignments QJsonObject assignmentJSON;
// start with a 200 response QJsonObject assignedNodesJSON;
mg_printf(connection, "%s", RESPONSE_200);
// enumerate the NodeList to find the assigned nodes
// setup the JSON NodeList* nodeList = NodeList::getInstance();
QJsonObject assignmentJSON;
const char ASSIGNMENT_JSON_UUID_KEY[] = "UUID";
QJsonObject assignedNodesJSON;
for (NodeList::iterator node = nodeList->begin(); node != nodeList->end(); node++) {
// enumerate the NodeList to find the assigned nodes if (node->getLinkedData()) {
NodeList* nodeList = NodeList::getInstance(); // this is a node with assignment
QJsonObject assignedNodeJSON;
const char ASSIGNMENT_JSON_UUID_KEY[] = "UUID";
// add the assignment UUID
for (NodeList::iterator node = nodeList->begin(); node != nodeList->end(); node++) { QString assignmentUUID = uuidStringWithoutCurlyBraces(((Assignment*) node->getLinkedData())->getUUID());
if (node->getLinkedData()) { assignedNodeJSON[ASSIGNMENT_JSON_UUID_KEY] = assignmentUUID;
// this is a node with assignment
QJsonObject assignedNodeJSON; // add the node socket information
assignedNodeJSON["public"] = jsonForSocket(node->getPublicSocket());
// add the assignment UUID assignedNodeJSON["local"] = jsonForSocket(node->getLocalSocket());
QString assignmentUUID = uuidStringWithoutCurlyBraces(((Assignment*) node->getLinkedData())->getUUID());
assignedNodeJSON[ASSIGNMENT_JSON_UUID_KEY] = assignmentUUID; // re-format the type name so it matches the target name
QString nodeTypeName(node->getTypeName());
// add the node socket information nodeTypeName = nodeTypeName.toLower();
assignedNodeJSON["public"] = jsonForSocket(node->getPublicSocket()); nodeTypeName.replace(' ', '-');
assignedNodeJSON["local"] = jsonForSocket(node->getLocalSocket());
assignedNodesJSON[nodeTypeName] = assignedNodeJSON;
// re-format the type name so it matches the target name }
QString nodeTypeName(node->getTypeName());
nodeTypeName = nodeTypeName.toLower();
nodeTypeName.replace(' ', '-');
assignedNodesJSON[nodeTypeName] = assignedNodeJSON;
} }
assignmentJSON["fulfilled"] = assignedNodesJSON;
QJsonObject queuedAssignmentsJSON;
// add the queued but unfilled assignments to the json
std::deque<Assignment*>::iterator assignment = domainServerInstance->_assignmentQueue.begin();
while (assignment != domainServerInstance->_assignmentQueue.end()) {
QJsonObject queuedAssignmentJSON;
QString uuidString = uuidStringWithoutCurlyBraces((*assignment)->getUUID());
queuedAssignmentJSON[ASSIGNMENT_JSON_UUID_KEY] = uuidString;
// add this queued assignment to the JSON
queuedAssignmentsJSON[(*assignment)->getTypeName()] = queuedAssignmentJSON;
// push forward the iterator to check the next assignment
assignment++;
}
assignmentJSON["queued"] = queuedAssignmentsJSON;
// print out the created JSON
QJsonDocument assignmentDocument(assignmentJSON);
mg_printf(connection, "%s", assignmentDocument.toJson().constData());
// we've processed this request
return 1;
} }
assignmentJSON["fulfilled"] = assignedNodesJSON; // not processed, pass to document root
return 0;
QJsonObject queuedAssignmentsJSON; } else if (strcmp(ri->request_method, "POST") == 0) {
if (strcmp(ri->uri, ASSIGNMENT_URI) == 0) {
// add the queued but unfilled assignments to the json // return a 200
std::deque<Assignment*>::iterator assignment = domainServerInstance->_assignmentQueue.begin(); mg_printf(connection, "%s", RESPONSE_200);
// upload the file
while (assignment != domainServerInstance->_assignmentQueue.end()) { mg_upload(connection, "/tmp");
QJsonObject queuedAssignmentJSON;
QString uuidString = uuidStringWithoutCurlyBraces((*assignment)->getUUID()); return 1;
queuedAssignmentJSON[ASSIGNMENT_JSON_UUID_KEY] = uuidString;
// add this queued assignment to the JSON
queuedAssignmentsJSON[(*assignment)->getTypeName()] = queuedAssignmentJSON;
// push forward the iterator to check the next assignment
assignment++;
} }
assignmentJSON["queued"] = queuedAssignmentsJSON; return 0;
// print out the created JSON
QJsonDocument assignmentDocument(assignmentJSON);
mg_printf(connection, "%s", assignmentDocument.toJson().constData());
// we've processed this request
return 1;
} else if (strcmp(ri->request_method, "DELETE") == 0) { } else if (strcmp(ri->request_method, "DELETE") == 0) {
// this is a DELETE request // this is a DELETE request
// check if it is for an assignment // check if it is for an assignment
if (memcmp(ri->uri, ASSIGNMENT_URI, sizeof(ASSIGNMENT_URI) - sizeof('\0')) == 0) { if (memcmp(ri->uri, NODE_URI, strlen(NODE_URI)) == 0) {
// pull the UUID from the url // pull the UUID from the url
QUuid deleteUUID = QUuid(QString(ri->uri + strlen(ASSIGNMENT_URI) + sizeof('/'))); QUuid deleteUUID = QUuid(QString(ri->uri + strlen(NODE_URI) + sizeof('/')));
if (!deleteUUID.isNull()) { if (!deleteUUID.isNull()) {
Node *nodeToKill = NodeList::getInstance()->nodeWithUUID(deleteUUID); Node *nodeToKill = NodeList::getInstance()->nodeWithUUID(deleteUUID);