From 8f00317567dab4c825a0eca76a9cae7951edd11f Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Tue, 6 Dec 2011 14:15:52 +0100 Subject: Clean up the way connections are handled. --- server/src/admin_connection.cc | 110 +++++++++++++++++++-------------------- server/src/admin_connection.h | 3 +- server/src/client_connection.cc | 50 +++++++++--------- server/src/client_connection.h | 3 +- server/src/connection.h | 3 +- server/src/saxparser.cc | 2 + server/src/server.cc | 4 +- server/src/transactionhandler.cc | 47 +++++++++-------- server/src/transactionhandler.h | 10 +--- 9 files changed, 116 insertions(+), 116 deletions(-) diff --git a/server/src/admin_connection.cc b/server/src/admin_connection.cc index fac52a1..5a15a34 100644 --- a/server/src/admin_connection.cc +++ b/server/src/admin_connection.cc @@ -90,70 +90,68 @@ AdminConnection::AdminConnection(Environment &e, headers_t a, std::string u) AdminConnection::~AdminConnection() {} -bool AdminConnection::handle(const char *data, size_t size) +bool AdminConnection::data(const char *, size_t) { return true; } + +bool AdminConnection::handle() { status = 200; // OK - if(data == NULL && size == 0) { - DEBUG(admin, "URI: %s\n", uri.c_str()); - - if(uri == "/") { - reply = admin_header(uri) + - "Command list:\n" - "/sessionunlock?id=[ID] unlock session with [ID] as its session id.\n" - "/listactivesessions lists all active sessions on the server.\n" - "/flushsessions flushes all active sessions to disc.\n" - "/export?template=[TEMPLATE] export template with name [TEMPLATE] to a\n csf file (comma seperated file, that can be opened in OOCalc or Excel).\n" - + admin_rc("footer"); - return true; - } - - if(uri == "/sessionunlock" && args.find("id") != args.end()) { - reply = admin_header(uri) + admin_sessionunlock(env, args["id"]) - + admin_rc("footer"); - return true; - } + DEBUG(admin, "URI: %s\n", uri.c_str()); - if(uri == "/listactivesessions") { - reply = admin_header(uri) + admin_listactivesessions(env) - + admin_rc("footer"); - return true; - } - - if(uri == "/flushsessions") { - reply = admin_header(uri) + admin_flush(env) + admin_rc("footer"); - return true; - } - - if(uri == "/export" && args.find("template") != args.end()) { - time_t from = 0; - if(args.find("from") != args.end()) from = atoi(args["from"].c_str()); - - time_t to = time(NULL); - if(args.find("to") != args.end()) to = atoi(args["to"].c_str()); - bool ok; - std::string res = admin_export(env, args["template"], &ok, from, to); - if(!ok) reply = admin_header(uri) + res + admin_rc("footer"); - else { - reply = res; - hdrs["Content-Type"] = "text/csv; charset=UTF-8"; - hdrs["Content-Disposition"] = "attachment; filename=\""+args["template"]+".csv\""; - } - return true; - } - - if(uri == "/favicon.ico") { - hdrs["Content-Type"] = "image/ico"; - reply = admin_rc("favicon"); - return true; - } - - reply = admin_header(uri) + - "'" + uri + "' not recognised as a valid command." + if(uri == "/") { + reply = admin_header(uri) + + "Command list:\n" + "/sessionunlock?id=[ID] unlock session with [ID] as its session id.\n" + "/listactivesessions lists all active sessions on the server.\n" + "/flushsessions flushes all active sessions to disc.\n" + "/export?template=[TEMPLATE] export template with name [TEMPLATE] to a\n csf file (comma seperated file, that can be opened in OOCalc or Excel).\n" + admin_rc("footer"); return true; } + + if(uri == "/sessionunlock" && args.find("id") != args.end()) { + reply = admin_header(uri) + admin_sessionunlock(env, args["id"]) + + admin_rc("footer"); + return true; + } + + if(uri == "/listactivesessions") { + reply = admin_header(uri) + admin_listactivesessions(env) + + admin_rc("footer"); + return true; + } + + if(uri == "/flushsessions") { + reply = admin_header(uri) + admin_flush(env) + admin_rc("footer"); + return true; + } + + if(uri == "/export" && args.find("template") != args.end()) { + time_t from = 0; + if(args.find("from") != args.end()) from = atoi(args["from"].c_str()); + + time_t to = time(NULL); + if(args.find("to") != args.end()) to = atoi(args["to"].c_str()); + bool ok; + std::string res = admin_export(env, args["template"], &ok, from, to); + if(!ok) reply = admin_header(uri) + res + admin_rc("footer"); + else { + reply = res; + hdrs["Content-Type"] = "text/csv; charset=UTF-8"; + hdrs["Content-Disposition"] = "attachment; filename=\""+args["template"]+".csv\""; + } + return true; + } + if(uri == "/favicon.ico") { + hdrs["Content-Type"] = "image/ico"; + reply = admin_rc("favicon"); + return true; + } + + reply = admin_header(uri) + + "'" + uri + "' not recognised as a valid command." + + admin_rc("footer"); return true; } diff --git a/server/src/admin_connection.h b/server/src/admin_connection.h index 4a413a6..591c128 100644 --- a/server/src/admin_connection.h +++ b/server/src/admin_connection.h @@ -38,7 +38,8 @@ public: AdminConnection(Environment &e, headers_t args, std::string uri); ~AdminConnection(); - bool handle(const char *data, size_t size); + bool data(const char *data, size_t size); + bool handle(); void getReply(Httpd::Reply &reply); diff --git a/server/src/client_connection.cc b/server/src/client_connection.cc index 075dc46..b9d17b9 100644 --- a/server/src/client_connection.cc +++ b/server/src/client_connection.cc @@ -85,7 +85,7 @@ ClientConnection::ClientConnection(Environment &e, headers_t headers, did_commit = false; #endif - parser_complete = false; + parser_complete = true; } ClientConnection::~ClientConnection() @@ -139,8 +139,19 @@ void ClientConnection::discard(Session *session) } } -bool ClientConnection::handle(const char *data, size_t size) +bool ClientConnection::data(const char *data, size_t size) { + DEBUG(connection, "data(%p %d)\n", data, size); + + parser_complete = parser.parse(data, size); + + return true; +} + +bool ClientConnection::handle() +{ + DEBUG(connection, "handle\n"); + if(patientid == "") { response = error_box(xml_encode("Missing patientid.")); parser_complete = true; @@ -189,33 +200,20 @@ bool ClientConnection::handle(const char *data, size_t size) sessionid = session->id(); - try { + // Force session discard on empty template name. + if(templ == "") dodiscard = true; - if((!data || !size) && (docommit || donocommit || dodiscard)) { - parser_complete = true; - commit(session); - nocommit(session); - discard(session); - return true; + try { + if(parser_complete) { + response = handleTransaction(request, transaction, env, *session); } - // Force session discard on empty template name. - if(templ == "") dodiscard = true; - - if(size == 0 || parser.parse(data, size)) { - parser_complete = true; - - { - //SessionAutolock lock(session); - response = handleTransaction(request, transaction, env, *session); - } - - commit(session); - nocommit(session); - discard(session); - - return true; - } + commit(session); + nocommit(session); + discard(session); + + return true; + } catch(...) { ERR(connection, "Failed to parse data!\n"); response = error_box(xml_encode("XML Parse error.")); diff --git a/server/src/client_connection.h b/server/src/client_connection.h index e64dcad..b8f7836 100644 --- a/server/src/client_connection.h +++ b/server/src/client_connection.h @@ -49,7 +49,8 @@ public: headers_t args, std::string uri); ~ClientConnection(); - bool handle(const char *data, size_t size); + bool data(const char *data, size_t size); + bool handle(); void getReply(Httpd::Reply &reply); diff --git a/server/src/connection.h b/server/src/connection.h index b7d17f4..098c735 100644 --- a/server/src/connection.h +++ b/server/src/connection.h @@ -36,7 +36,8 @@ class Connection { public: virtual ~Connection() {} - virtual bool handle(const char *data, size_t size) = 0; + virtual bool data(const char *data, size_t size) = 0; + virtual bool handle() = 0; virtual void getReply(Httpd::Reply &reply) = 0; }; diff --git a/server/src/saxparser.cc b/server/src/saxparser.cc index 95efffe..14f204c 100644 --- a/server/src/saxparser.cc +++ b/server/src/saxparser.cc @@ -130,6 +130,8 @@ bool SAXParser::parse(const char *data, size_t size) xml.append(data, size); DEBUG(sax, "parse %d bytes [%s]\n", size, xml.c_str()); + if(data == NULL || size == 0) return done; + bufferbytes = size; totalbytes += bufferbytes; diff --git a/server/src/server.cc b/server/src/server.cc index e4d6474..2d2d4c9 100644 --- a/server/src/server.cc +++ b/server/src/server.cc @@ -73,7 +73,7 @@ public: { if(ptr) { Connection *connection = (Connection *)ptr; - connection->handle(data, data_size); + connection->data(data, data_size); } return true; } @@ -84,7 +84,7 @@ public: Connection *connection = (Connection *)ptr; // Flush and do commit/discards - connection->handle(NULL, 0); + if(!connection->handle()) return false; connection->getReply(reply); } diff --git a/server/src/transactionhandler.cc b/server/src/transactionhandler.cc index ef112ee..7ec38f8 100644 --- a/server/src/transactionhandler.cc +++ b/server/src/transactionhandler.cc @@ -40,6 +40,14 @@ #include "widgetgenerator.h" #include "journal.h" +#include "exception.h" + +class NotFoundException : public Exception { +public: + NotFoundException(Request &r) + : Exception("Macro " + r.macro + " not found in template " + r.templ) {} +}; + static std::string error_box(std::string message) { std::string errorbox = @@ -55,31 +63,28 @@ static std::string handleCommits(Transaction &transaction, Environment &env, { std::string answer; - if(transaction.commits.size() > 0) { - - Commits::iterator i = transaction.commits.begin(); - while(i != transaction.commits.end()) { - Commit &commit = *i; - - MacroParser mp(env.macrolist.getLatestVersion(commit.macro)); - mp.parse(); - Macro *macro = mp.getMacro(); - - std::string resume = resume_parser(*macro, commit); - commit.fields["journal.resume"] = resume; - session.commitMacro(transaction, commit, *macro); + Commits::iterator i = transaction.commits.begin(); + while(i != transaction.commits.end()) { + Commit &commit = *i; + + MacroParser mp(env.macrolist.getLatestVersion(commit.macro)); + mp.parse(); + Macro *macro = mp.getMacro(); + + std::string resume = resume_parser(*macro, commit); + commit.fields["journal.resume"] = resume; + session.commitMacro(transaction, commit, *macro); - if(resume != "") { + if(resume != "") { - TemplateParser tp(env.templatelist.getLatestVersion(commit.templ)); - tp.parse(); - Template *templ = tp.getTemplate(); + TemplateParser tp(env.templatelist.getLatestVersion(commit.templ)); + tp.parse(); + Template *templ = tp.getTemplate(); - session.journal()->addEntry(transaction, commit, resume, templ); - } - - i++; + session.journal()->addEntry(transaction, commit, resume, templ); } + + i++; } return answer; diff --git a/server/src/transactionhandler.h b/server/src/transactionhandler.h index 43ecf0b..914c9ad 100644 --- a/server/src/transactionhandler.h +++ b/server/src/transactionhandler.h @@ -28,18 +28,12 @@ #ifndef __PRACRO_TRANSACTIONHANDLER_H__ #define __PRACRO_TRANSACTIONHANDLER_H__ +#include + #include "transaction.h" #include "session.h" #include "environment.h" -#include "exception.h" - -class NotFoundException : public Exception { -public: - NotFoundException(Request &r) - : Exception("Macro " + r.macro + " not found in template " + r.templ) {} -}; - std::string handleTransaction(Request &resuest, Transaction &transaction, Environment &env, -- cgit v1.2.3