From d536b969ed2739c467b19ba73cc32e0c9e73645d Mon Sep 17 00:00:00 2001 From: deva Date: Fri, 13 Nov 2009 13:51:08 +0000 Subject: JournalWriter now works with sessions, i.e. just like in the old days before microhttpd. --- server/src/server.cc | 73 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 27 deletions(-) (limited to 'server') diff --git a/server/src/server.cc b/server/src/server.cc index d1104c4..a3c7667 100644 --- a/server/src/server.cc +++ b/server/src/server.cc @@ -59,13 +59,18 @@ #include "macrolist.h" #include "templatelist.h" #include "versionstr.h" +#include "mutex.h" typedef long long unsigned int sessionid_t; +typedef struct { + JournalWriter *journalwriter; +} session_t; + struct conn_t { Database *db; - sessionid_t sessionid; - bool commit; + Mutex mutex; + std::map sessions; }; static std::string error_box(std::string message) @@ -91,8 +96,7 @@ public: }; -static std::string handleCommits(sessionid_t sessiondid, - Transaction *transaction, Database &db, +static std::string handleCommits(Transaction *transaction, Database &db, JournalWriter &journalwriter, MacroList ¯olist, TemplateList &templatelist) { @@ -270,8 +274,7 @@ static std::string handleRequest(Transaction *transaction, return answer; } -static std::string handleTransaction(sessionid_t sessionid, - Transaction *transaction, +static std::string handleTransaction(Transaction *transaction, TCPSocket &pentominos_socket, Database &db, JournalWriter &journalwriter, @@ -283,7 +286,7 @@ static std::string handleTransaction(sessionid_t sessionid, answer += "\n"; try { - answer += handleCommits(sessionid, transaction, db, + answer += handleCommits(transaction, db, journalwriter, macrolist, templatelist); } catch( std::exception &e ) { PRACRO_ERR(server, "Commit error: %s\n", e.what()); @@ -304,18 +307,26 @@ static std::string handleTransaction(sessionid_t sessionid, return answer; } - -static std::string handleConnection(const char *buf, size_t size, struct conn_t *conn) +static std::string handleConnection(const char *buf, size_t size, struct conn_t *conn, + sessionid_t sid, bool commitsession) { + /* if(size == 0) return error_box(xml_encode("Empty document received.")); - + */ TCPSocket pentominos_socket; #ifndef WITHOUT_PENTOMINOS pentominos_socket.connect(Conf::pentominos_addr, Conf::pentominos_port); #endif/*WITHOUT_PENTOMINOS*/ - JournalWriter journalwriter(Conf::journal_commit_addr.c_str(), Conf::journal_commit_port); + JournalWriter *journalwriter = NULL; + conn->mutex.lock(); + if(conn->sessions.find(sid) == conn->sessions.end()) { + conn->sessions[sid].journalwriter = + new JournalWriter(Conf::journal_commit_addr.c_str(), Conf::journal_commit_port); + } + journalwriter = conn->sessions[sid].journalwriter; + conn->mutex.unlock(); MacroList macrolist(Conf::xml_basedir + "/macros"); TemplateList templatelist(Conf::xml_basedir + "/templates"); @@ -325,18 +336,26 @@ static std::string handleConnection(const char *buf, size_t size, struct conn_t PRACRO_DEBUG(server, "Read %d bytes from network\n", size); + std::string res; if(parser.parse(buf, size)) { PRACRO_DEBUG(server, "Got complete XML document, %d bytes in current buffer.\n", size); - std::string res = handleTransaction(conn->sessionid, &transaction, pentominos_socket, - *conn->db, journalwriter, macrolist, templatelist); - journalwriter.commit(); + res = handleTransaction(&transaction, pentominos_socket, + *conn->db, *journalwriter, macrolist, templatelist); + } else { + PRACRO_ERR(server, "Failed to parse data!\n"); + res = error_box(xml_encode("XML Parse error.")); + } - return res; + if(commitsession) { + journalwriter->commit(); + delete journalwriter; + conn->mutex.lock(); + conn->sessions.erase(sid); + conn->mutex.unlock(); } - PRACRO_ERR(server, "Failed to parse data!\n"); - return error_box(xml_encode("XML Parse error.")); + return res; } static sessionid_t newSessionID() @@ -360,28 +379,28 @@ static int handle_request(void *cls, "handle_request(url=\"%s\", method=\"%s\"," " version=\"%s\", data_size=\"%d\")\n", url, method, version, *data_size); - + sessionid_t sessionid; + bool commitsession = false; + 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); + if(sessionids == NULL) sessionid = newSessionID(); + else sessionid = atoll(sessionids); + printf("SessionID: %llu\n", 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; + printf("COMMIT: sessionid %llu\n", sessionid); + commitsession = true; } - std::string reply = handleConnection(data, *data_size, conn); + std::string reply = handleConnection(data, *data_size, conn, sessionid, commitsession); 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); + snprintf(idbuf, sizeof(idbuf), "%llu", sessionid); MHD_add_response_header(rsp, "SessionID", idbuf); int ret = MHD_queue_response(con, MHD_HTTP_OK, rsp); -- cgit v1.2.3