first cut at domain server config support

This commit is contained in:
ZappoMan 2013-09-17 12:18:46 -07:00
parent 00506b1a84
commit 48a579ae49

View file

@ -27,7 +27,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <QtCore/QCoreApplication> #include <QtCore/QCoreApplication>
#include <QtCore/QMap>
#include <QtCore/QMutex> #include <QtCore/QMutex>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <civetweb.h> #include <civetweb.h>
@ -45,6 +48,7 @@ const int NODE_COUNT_STAT_INTERVAL_MSECS = 5000;
QMutex assignmentQueueMutex; QMutex assignmentQueueMutex;
std::deque<Assignment*> assignmentQueue; std::deque<Assignment*> assignmentQueue;
QMap<QString, QString> configMap;
unsigned char* addNodeToBroadcastPacket(unsigned char* currentPosition, Node* nodeToAdd) { unsigned char* addNodeToBroadcastPacket(unsigned char* currentPosition, Node* nodeToAdd) {
*currentPosition++ = nodeToAdd->getType(); *currentPosition++ = nodeToAdd->getType();
@ -58,7 +62,7 @@ unsigned char* addNodeToBroadcastPacket(unsigned char* currentPosition, Node* no
} }
static int mongooseRequestHandler(struct mg_connection *conn) { static int mongooseRequestHandler(struct mg_connection *conn) {
const struct mg_request_info *ri = mg_get_request_info(conn); const struct mg_request_info* ri = mg_get_request_info(conn);
if (strcmp(ri->uri, "/assignment") == 0 && strcmp(ri->request_method, "POST") == 0) { if (strcmp(ri->uri, "/assignment") == 0 && strcmp(ri->request_method, "POST") == 0) {
// return a 200 // return a 200
@ -66,6 +70,37 @@ static int mongooseRequestHandler(struct mg_connection *conn) {
// upload the file // upload the file
mg_upload(conn, "/tmp"); mg_upload(conn, "/tmp");
return 1;
} else if (strncmp(ri->uri, "/config", strlen("/config")) == 0 && strcmp(ri->request_method, "GET") == 0) {
// Let's split up the URL into it's pieces so we can service it
QString uri(ri->uri);
QString delimiterPattern("/");
QStringList uriParts = uri.split(delimiterPattern);
QString configGuid = uriParts[2];
// first part should be empty
// second part should be "config"
// third part should be a guid, and therefore should not be empty
if (!uriParts[0].isEmpty() || uriParts[1] != "config" || configGuid.isEmpty()) {
mg_printf(conn, "%s", "HTTP/1.0 400 Bad Request\r\nContent-Type: text/plain\r\n\r\nInvalid parameters.");
return 1;
}
// assuming the request format was valid, look up the guid in our configs
if (!::configMap.contains(configGuid)) {
mg_printf(conn, "%s", "HTTP/1.0 404 Not Found\r\nContent-Type: text/plain\r\n\r\nInvalid config GUID.");
return 1;
}
const char * configPayload = configMap[configGuid].toLocal8Bit().data();
// return a 200
mg_printf(conn, "%s", "HTTP/1.0 200 OK\r\n\r\n");
// return the configuration "payload"
mg_printf(conn, "%s", configPayload);
return 1; return 1;
} else { } else {
// have mongoose process this request from the document_root // have mongoose process this request from the document_root
@ -157,13 +192,13 @@ int main(int argc, const char* argv[]) {
localSocket.sin_addr.s_addr = serverLocalAddress; localSocket.sin_addr.s_addr = serverLocalAddress;
// setup the mongoose web server // setup the mongoose web server
struct mg_context *ctx; struct mg_context* ctx;
struct mg_callbacks callbacks = {}; struct mg_callbacks callbacks = {};
QString documentRoot = QString("%1/resources/web").arg(QCoreApplication::applicationDirPath()); QString documentRoot = QString("%1/resources/web").arg(QCoreApplication::applicationDirPath());
// list of options. Last element must be NULL. // list of options. Last element must be NULL.
const char *options[] = {"listening_ports", "8080", const char* options[] = {"listening_ports", "8080",
"document_root", documentRoot.toStdString().c_str(), NULL}; "document_root", documentRoot.toStdString().c_str(), NULL};
callbacks.begin_request = mongooseRequestHandler; callbacks.begin_request = mongooseRequestHandler;