From 0e819eb42b4d680a99ae7b04702bfc9510495aee Mon Sep 17 00:00:00 2001 From: deva Date: Wed, 6 Jan 2010 07:47:58 +0000 Subject: New artefact connection class (to later wrap libartefact). New environment class to hold all global resources. Made ConnectionPool a template class. Split journal code up into two files (class from commit code). --- server/src/server.cc | 180 +++++++++++++++++++++------------------------------ 1 file changed, 72 insertions(+), 108 deletions(-) (limited to 'server/src/server.cc') diff --git a/server/src/server.cc b/server/src/server.cc index 9b8f7f1..84c3463 100644 --- a/server/src/server.cc +++ b/server/src/server.cc @@ -43,40 +43,13 @@ #include "configuration.h" #include "transaction.h" #include "transactionparser.h" -#include "templateparser.h" -#include "macroparser.h" - -#include "queryhandler.h" -#include "queryhandlerpracro.h" -#include "queryhandlerpentominos.h" - -#include "queryparser.h" -#include "luaquerymapper.h" #include "database.h" -#include "widgetgenerator.h" -#include "resumeparser.h" -#include "journal_commit.h" -#include "xml_encode_decode.h" - -#include "macrolist.h" -#include "templatelist.h" -#include "versionstr.h" -#include "mutex.h" #include "log.h" - +#include "environment.h" #include "transactionhandler.h" - -typedef long long unsigned int sessionid_t; - -typedef struct { - JournalWriter *journalwriter; -} session_t; - -struct conn_t { - Database *db; - Mutex mutex; - std::map sessions; -}; +#include "connectionpool.h" +#include "session.h" +#include "xml_encode_decode.h" static std::string error_box(std::string message) { @@ -88,32 +61,52 @@ static std::string error_box(std::string message) return errorbox; } -static std::string handleConnection(const char *buf, size_t size, struct conn_t *conn, - sessionid_t sid, bool commitsession) +static std::string handleConnection(const char *buf, size_t size, + Environment &env, + char **sessionid, + const char *sessioncommit) { - /* - 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 = 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); + std::string res; + + Session *session = NULL; + if(*sessionid == NULL) { + session = env.sessions.newSession(); + } else { + session = env.sessions.session(*sessionid); + if(session == NULL) session = env.sessions.newSession(); + } + + if(session == NULL) { + PRACRO_ERR(server, "New session could not be created."); + return error_box(xml_encode("New session could not be created.")); } - journalwriter = conn->sessions[sid].journalwriter; - conn->mutex.unlock(); - MacroList macrolist(Conf::xml_basedir + "/macros"); - TemplateList templatelist(Conf::xml_basedir + "/templates"); + session->lock(); + + if(asprintf(sessionid, "%s", session->id().c_str()) == -1) *sessionid = NULL; Transaction transaction; TransactionParser parser(&transaction); + + if(!parser.parse(buf, size)) { + PRACRO_ERR(server, "Failed to parse data!\n"); + res = error_box(xml_encode("XML Parse error.")); + } else { + res = handleTransaction(transaction, env, *session); + } + + session->unlock(); + + if(sessioncommit != NULL) { + session->commit(); + env.sessions.deleteSession(session->id()); + } + + return res; + + /* + Transaction transaction; + TransactionParser parser(&transaction); PRACRO_DEBUG(server, "Read %d bytes from network\n", size); @@ -123,75 +116,49 @@ static std::string handleConnection(const char *buf, size_t size, struct conn_t PRACRO_DEBUG(server, "Got complete XML document, %d bytes in current buffer.\n", size); res = handleTransaction(&transaction, pentominos_socket, - *conn->db, *journalwriter, macrolist, templatelist); + *conn->db, session, macrolist, templatelist); } else { PRACRO_ERR(server, "Failed to parse data!\n"); res = error_box(xml_encode("XML Parse error.")); } } - - if(commitsession) { - journalwriter->commit(); - delete journalwriter; - conn->mutex.lock(); - conn->sessions.erase(sid); - conn->mutex.unlock(); - } - return res; + + */ } -static int handle_request(void *cls, - struct MHD_Connection *con, - const char *url, - const char *method, - const char *version, - const char *data, - unsigned int *data_size, - void **ptr) +static int handle_request_callback(void *cls, + struct MHD_Connection *con, + const char *url, + const char *method, + const char *version, + const char *data, + unsigned int *data_size, + void **ptr) { - struct conn_t *conn = (struct conn_t*)cls; + Environment *env = (Environment *)cls; PRACRO_DEBUG(httpd, "handle_request(url=\"%s\", method=\"%s\"," " version=\"%s\", data_size=\"%d\")\n", url, method, version, *data_size); - sessionid_t sessionid; - bool commitsession = false; - bool sid_ok = true; - - const char *sessionids = MHD_lookup_connection_value(con, MHD_HEADER_KIND, "SessionID"); - if(sessionids == NULL) { - sessionid = 42;//newSessionID(conn); - } else { - sessionid = atoll(sessionids); - conn->mutex.lock(); - sid_ok = conn->sessions.find(sessionid) != conn->sessions.end(); - conn->mutex.unlock(); - } - PRACRO_DEBUG(httpd, "SessionID: %llu\n", sessionid); - const char *session_commit = MHD_lookup_connection_value(con, MHD_HEADER_KIND, "SessionCommit"); - if(session_commit) { - PRACRO_DEBUG(httpd, "COMMIT: sessionid %llu\n", sessionid); - commitsession = true; - } + const char *sessionid = + MHD_lookup_connection_value(con, MHD_HEADER_KIND, "SessionID"); + const char *sessioncommit = + MHD_lookup_connection_value(con, MHD_HEADER_KIND, "SessionCommit"); - std::string reply; - if(sid_ok) { - reply = handleConnection(data, *data_size, conn, sessionid, commitsession); - } else { - PRACRO_ERR(httpd, "No such sessionid %llu\n", sessionid); - reply = error_box("No such session ID!"); - } + std::string reply = + handleConnection(data, *data_size, *env, (char**)&sessionid, sessioncommit); - struct MHD_Response *rsp; - rsp = MHD_create_response_from_data(reply.length(), (char*)reply.c_str(), MHD_NO, MHD_YES); + struct MHD_Response *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", sessionid); - MHD_add_response_header(rsp, "SessionID", idbuf); + if(*sessionid) { + MHD_add_response_header(rsp, "SessionID", sessionid); + free((char*)sessionid); + } int ret = MHD_queue_response(con, MHD_HTTP_OK, rsp); MHD_destroy_response(rsp); @@ -221,13 +188,11 @@ void server() PRACRO_DEBUG(server, "Server running on port %d.\n", port); - struct conn_t conn; - conn.db = new Database(Conf::database_backend, Conf::database_addr, - "", Conf::database_user, Conf::database_passwd, ""); + Environment env; struct MHD_Daemon *d; d = MHD_start_daemon(flags, port, NULL, NULL, - handle_request, &conn, + handle_request_callback, &env, MHD_OPTION_NOTIFY_COMPLETED, NULL, NULL, MHD_OPTION_CONNECTION_LIMIT, Conf::connection_limit, #ifndef WITHOUT_SSL @@ -245,11 +210,11 @@ void server() again: while(pracro_is_running) sleep(1); - if(!forceshutdown && conn.sessions.size() != 0) { + if(!forceshutdown && env.sessions.size() != 0) { char *errbuf; if(asprintf(&errbuf, "There are %d live sessions." " Kill again to force shutdown.\n", - conn.sessions.size()) != -1) { + env.sessions.size()) != -1) { PRACRO_ERR_LOG(server, "%s", errbuf); log(errbuf); free(errbuf); @@ -258,7 +223,6 @@ void server() forceshutdown = true; goto again; } - delete conn.db; MHD_stop_daemon(d); -- cgit v1.2.3