diff options
Diffstat (limited to 'server/src/server.cc')
-rw-r--r-- | server/src/server.cc | 48 |
1 files changed, 38 insertions, 10 deletions
diff --git a/server/src/server.cc b/server/src/server.cc index 67ae74d..d1104c4 100644 --- a/server/src/server.cc +++ b/server/src/server.cc @@ -60,9 +60,12 @@ #include "templatelist.h" #include "versionstr.h" +typedef long long unsigned int sessionid_t; + struct conn_t { Database *db; - + sessionid_t sessionid; + bool commit; }; static std::string error_box(std::string message) @@ -88,7 +91,8 @@ public: }; -static std::string handleCommits(Transaction *transaction, Database &db, +static std::string handleCommits(sessionid_t sessiondid, + Transaction *transaction, Database &db, JournalWriter &journalwriter, MacroList ¯olist, TemplateList &templatelist) { @@ -125,7 +129,6 @@ static std::string handleCommits(Transaction *transaction, Database &db, static std::string handleRequest(Transaction *transaction, TCPSocket &pentominos_socket, Database &db, - JournalWriter &journalwriter, MacroList ¯olist, TemplateList &templatelist) { @@ -267,7 +270,8 @@ static std::string handleRequest(Transaction *transaction, return answer; } -static std::string handleTransaction(Transaction *transaction, +static std::string handleTransaction(sessionid_t sessionid, + Transaction *transaction, TCPSocket &pentominos_socket, Database &db, JournalWriter &journalwriter, @@ -279,15 +283,15 @@ static std::string handleTransaction(Transaction *transaction, answer += "<pracro version=\"1.0\">\n"; try { - answer += handleCommits(transaction, db, journalwriter, macrolist, templatelist); + answer += handleCommits(sessionid, transaction, db, + journalwriter, macrolist, templatelist); } catch( std::exception &e ) { PRACRO_ERR(server, "Commit error: %s\n", e.what()); return error_box(xml_encode(e.what())); } try { - answer += handleRequest(transaction, pentominos_socket, db, journalwriter, - macrolist, templatelist); + answer += handleRequest(transaction, pentominos_socket, db, macrolist, templatelist); } catch( std::exception &e ) { PRACRO_ERR(server, "Request error: %s\n", e.what()); return error_box(xml_encode(e.what())); @@ -301,7 +305,7 @@ static std::string handleTransaction(Transaction *transaction, } -static std::string handleConnection(char *buf, size_t size, struct conn_t *conn) +static std::string handleConnection(const char *buf, size_t size, struct conn_t *conn) { if(size == 0) return error_box(xml_encode("Empty document received.")); @@ -324,7 +328,7 @@ static std::string handleConnection(char *buf, size_t size, struct conn_t *conn) if(parser.parse(buf, size)) { PRACRO_DEBUG(server, "Got complete XML document, %d bytes in current buffer.\n", size); - std::string res = handleTransaction(&transaction, pentominos_socket, + std::string res = handleTransaction(conn->sessionid, &transaction, pentominos_socket, *conn->db, journalwriter, macrolist, templatelist); journalwriter.commit(); @@ -335,6 +339,12 @@ static std::string handleConnection(char *buf, size_t size, struct conn_t *conn) return error_box(xml_encode("XML Parse error.")); } +static sessionid_t newSessionID() +{ + static volatile sessionid_t sessionid = 0; + return sessionid++; +} + static int handle_request(void *cls, struct MHD_Connection *con, const char *url, @@ -351,11 +361,29 @@ static int handle_request(void *cls, " version=\"%s\", data_size=\"%d\")\n", url, method, version, *data_size); - std::string reply = handleConnection((char*)data, *data_size, conn); + const char *sessionids = MHD_lookup_connection_value(con, MHD_HEADER_KIND, "SessionID"); + if(sessionids == NULL) conn->sessionid = newSessionID(); + else conn->sessionid = atoll(sessionids); + printf("SessionID: %llu\n", conn->sessionid); + + const char *session_commit = MHD_lookup_connection_value(con, MHD_HEADER_KIND, "SessionCommit"); + if(session_commit) { + printf("COMMIT: sessionid %llu\n", conn->sessionid); + conn->commit = true; + } else { + conn->commit = false; + } + + std::string reply = handleConnection(data, *data_size, conn); struct MHD_Response *rsp; rsp = MHD_create_response_from_data(reply.length(), (char*)reply.c_str(), MHD_NO, MHD_YES); MHD_add_response_header(rsp, MHD_HTTP_HEADER_CONTENT_TYPE, "text/plain; charset=UTF-8"); + + char idbuf[32]; + snprintf(idbuf, sizeof(idbuf), "%llu", conn->sessionid); + MHD_add_response_header(rsp, "SessionID", idbuf); + int ret = MHD_queue_response(con, MHD_HTTP_OK, rsp); MHD_destroy_response(rsp); |