diff options
author | deva <deva> | 2011-02-04 07:35:10 +0000 |
---|---|---|
committer | deva <deva> | 2011-02-04 07:35:10 +0000 |
commit | 9ec9a98e83076bb339d1d546fa445b2420e5a4fb (patch) | |
tree | d507ac2dff3825719c6557a672ad8eb64c303e18 /server/src/connection.cc | |
parent | e137415d0e0325d143b69a9917d8ebba411b3c12 (diff) |
A new connection handling mechanism.
Diffstat (limited to 'server/src/connection.cc')
-rw-r--r-- | server/src/connection.cc | 336 |
1 files changed, 0 insertions, 336 deletions
diff --git a/server/src/connection.cc b/server/src/connection.cc index e4ec7f4..4f0907d 100644 --- a/server/src/connection.cc +++ b/server/src/connection.cc @@ -27,339 +27,3 @@ */ #include "connection.h" -#include "transactionhandler.h" -#include "xml_encode_decode.h" - -static std::string error_box(std::string message) -{ - std::string errorbox = - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" - "<pracro version=\"1.0\">\n" - " <error>" + message + "</error>\n" - "</pracro>\n"; - return errorbox; -} - -#ifdef TEST_CONNECTION -static bool did_commit = false; -#endif - -Connection::Connection(Environment &e, std::string sid, - std::string pid, std::string t, - bool c, bool d, bool nc) - : env(e), parser(&transaction) -{ - DEBUG(connection, "[%p] CREATE\n", this); - - patientid = pid; - templ = t; - sessionid = sid; - docommit = c; - dodiscard = d; - donocommit = nc; - -#ifdef TEST_CONNECTION - did_commit = false; -#endif - - parser_complete = false; -} - -Connection::~Connection() -{ - DEBUG(connection, "[%p] DESTROY\n", this); -} - -void Connection::nocommit(Session *session) -{ - if(donocommit) { - donocommit = false; - session->nocommit(); - } -} - -void Connection::commit(Session *session) -{ - if(docommit) { - std::string sid = session->id(); - session->commit(); - env.sessions.deleteSession(sid); - sessionid = ""; - docommit = false; -#ifdef TEST_CONNECTION - did_commit = true; -#endif - } -} - -void Connection::discard(Session *session) -{ - if(dodiscard) { - std::string sid = session->id(); - session->discard(); - env.sessions.deleteSession(sid); - sessionid = ""; - dodiscard = false; - } -} - -bool Connection::handle(const char *data, size_t size) -{ - Session *session = NULL; - if(sessionid == "") { - // Create new session - session = env.sessions.newSession(patientid, templ); - } else { - // Attach to old session - session = env.sessions.session(sessionid); - - // Session didn't exist - create a new one anyway. - if(session == NULL) session = env.sessions.newSession(patientid, templ); - } - - if(session == NULL) { - ERR(connection, "New session could not be created."); - response = error_box(xml_encode("New session could not be created.")); - return true; - } - - sessionid = session->id(); - - try { - - if(!data || !size) { - parser_complete = true; - commit(session); - discard(session); - nocommit(session); - return true; - } - - if(parser.parse(data, size)) { - parser_complete = true; - - { - SessionAutolock lock(*session); - response = handleTransaction(transaction, env, *session); - } - - commit(session); - discard(session); - nocommit(session); - - return true; - } - } catch(...) { - ERR(server, "Failed to parse data!\n"); - response = error_box(xml_encode("XML Parse error.")); - return true; - } - - return false; -} - -std::string Connection::getResponse() -{ - if(parser_complete == false) - return error_box(xml_encode("XML Parser need more data.")); - return response; -} - -std::string Connection::getSessionID() -{ - return sessionid; -} - -#ifdef TEST_CONNECTION -//deps: debug.cc transactionparser.cc session.cc xml_encode_decode.cc saxparser.cc transactionhandler.cc journal.cc mutex.cc templateparser.cc exception.cc configuration.cc macroparser.cc semaphore.cc entitylist.cc luaquerymapper.cc inotify.cc log.cc queryhandlerpentominos.cc widgetgenerator.cc queryhandlerpracro.cc resumeparser.cc journal_commit.cc versionstr.cc luaresume.cc luautil.cc artefact.cc environment.cc database.cc macrolist.cc templatelist.cc pracrodao.cc templateheaderparser.cc macroheaderparser.cc pracrodaotest.cc pracrodaopgsql.cc -//cflags: -DWITHOUT_DATABASE -DWITHOUT_ARTEFACT -I.. $(LUA_CFLAGS) $(EXPAT_CFLAGS) $(PTHREAD_CFLAGS) $(PQXX_CXXFLAGS) -//libs: $(LUA_LIBS) $(EXPAT_LIBS) $(PTHREAD_LIBS) $(PQXX_LIBS) -#include "test.h" - -static char xml_request[] = -"<?xml version='1.0' encoding='UTF-8'?>\n" -"<pracro version=\"1.0\" user=\"testuser\" cpr=\"1505050505\">\n" -" <request macro=\"test\" template=\"test\"/>\n" -"</pracro>\n" - ; - -static char xml_commit[] = -"<?xml version='1.0' encoding='UTF-8'?>\n" -"<pracro version=\"1.0\" user=\"testuser\" cpr=\"1505050505\">\n" -" <commit version=\"\" macro=\"referral\" template=\"amd_forunders\" >\n" -" <field value=\"Some docs\" name=\"referral.doctor\"/>\n" -" <field value=\"DIMS\" name=\"referral.diagnosecode\"/>\n" -" <field value=\"Avs\" name=\"referral.diagnose\"/>\n" -" </commit>\n" -"</pracro>\n" - ; - -static char xml_commit_p1[] = -"<?xml version='1.0' encoding='UTF-8'?>\n" -"<pracro version=\"1.0\" user=\"testuser\" cpr=\"1505050505\">\n" -" <commit version=\"\" macro=\"referral\" template=\"amd_forunders\" >\n" -" <field value=\"Some docs\" name=\"referral.doctor\"/>\n" -" <field value=\"DIMS\" name=\"referral.diagn" - ; - -static char xml_commit_p2[] = -"ose\"/>\n" -" </commit>\n" -"</pracro>\n" - ; - -TEST_BEGIN; - -Environment env; -std::string sid; - -// Without data -{ - Connection con(env, "", false); - TEST_TRUE(con.handle("", 0), "Test handler return value."); - TEST_EQUAL_STR(con.getResponse(), "", "Test response value."); - sid = con.getSessionID(); - TEST_NOTEQUAL_STR(sid, "", "Test new session id."); - TEST_FALSE(did_commit, "No commit."); -} - -{ - Connection con(env, sid, false); - TEST_TRUE(con.handle("", 0), "Test handler return value."); - TEST_EQUAL_STR(con.getResponse(), "", "Test response value."); - TEST_NOTEQUAL_STR(con.getSessionID(), "", "Test existing session id."); - TEST_EQUAL_STR(con.getSessionID(), sid, "Test existing session id."); - TEST_FALSE(did_commit, "No commit."); -} - -{ - Connection con(env, sid, true); - TEST_TRUE(con.handle("", 0), "Test handler return value."); - TEST_EQUAL_STR(con.getResponse(), "", "Test response value."); - TEST_EQUAL_STR(con.getSessionID(), "", "Test existing session id."); - TEST_TRUE(did_commit, "Commit."); -} - -{ - Connection con(env, sid, false); - TEST_TRUE(con.handle("", 0), "Test handler return value."); - TEST_EQUAL_STR(con.getResponse(), "", "Test response value."); - TEST_NOTEQUAL_STR(con.getSessionID(), "", "Test existing session id."); - TEST_NOTEQUAL_STR(con.getSessionID(), sid, "Test new session id."); - TEST_FALSE(did_commit, "No commit."); -} - -// With commit partial data -{ - Connection con(env, "", false); - TEST_FALSE(con.handle(xml_commit_p1, sizeof(xml_commit_p1) - 1), - "Test handler return value."); - sid = con.getSessionID(); - TEST_NOTEQUAL_STR(sid, "", "Test new session id."); - TEST_FALSE(did_commit, "No commit."); - TEST_EQUAL_STR(con.getResponse(), "", "Test response value."); - TEST_TRUE(con.handle(xml_commit_p2, sizeof(xml_commit_p2) - 1), - "Test handler return value."); - TEST_EQUAL_STR(con.getSessionID(), sid, "Test session id."); - TEST_NOTEQUAL_STR(con.getResponse(), "", "Test response value."); - TEST_FALSE(did_commit, "No commit."); -} - -// With commit partial data and journal commit -{ - Connection con(env, "", true); - TEST_FALSE(con.handle(xml_commit_p1, sizeof(xml_commit_p1) - 1), - "Test handler return value."); - sid = con.getSessionID(); - TEST_NOTEQUAL_STR(sid, "", "Test new session id."); - TEST_EQUAL_STR(con.getResponse(), "", "Test response value."); - TEST_FALSE(did_commit, "No commit."); - TEST_TRUE(con.handle(xml_commit_p2, sizeof(xml_commit_p2) - 1), - "Test handler return value."); - TEST_EQUAL_STR(con.getSessionID(), "", "Test session id."); - TEST_TRUE(did_commit, "No commit."); - TEST_NOTEQUAL_STR(con.getResponse(), "", "Test response value."); -} - -// With commit data -{ - Connection con(env, "", false); - TEST_TRUE(con.handle(xml_commit, sizeof(xml_commit) - 1), - "Test handler return value."); - sid = con.getSessionID(); - TEST_NOTEQUAL_STR(sid, "", "Test new session id."); - TEST_NOTEQUAL_STR(con.getResponse(), "", "Test response value."); - TEST_FALSE(did_commit, "No commit."); -} - -{ - Connection con(env, sid, false); - TEST_TRUE(con.handle(xml_commit, sizeof(xml_commit) - 1), - "Test handler return value."); - TEST_NOTEQUAL_STR(con.getResponse(), "", "Test response value."); - TEST_NOTEQUAL_STR(con.getSessionID(), "", "Test existing session id."); - TEST_EQUAL_STR(con.getSessionID(), sid, "Test existing session id."); - TEST_FALSE(did_commit, "No commit."); -} - -{ - Connection con(env, sid, true); - TEST_TRUE(con.handle(xml_commit, sizeof(xml_commit) - 1), - "Test handler return value."); - TEST_NOTEQUAL_STR(con.getResponse(), "", "Test response value."); - TEST_EQUAL_STR(con.getSessionID(), "", "Test existing session id."); - TEST_TRUE(did_commit, "Commit."); -} - -{ - Connection con(env, sid, false); - TEST_TRUE(con.handle(xml_commit, sizeof(xml_commit) - 1), - "Test handler return value."); - TEST_NOTEQUAL_STR(con.getResponse(), "", "Test response value."); - TEST_NOTEQUAL_STR(con.getSessionID(), "", "Test existing session id."); - TEST_NOTEQUAL_STR(con.getSessionID(), sid, "Test new session id."); - TEST_FALSE(did_commit, "No commit."); -} - -// With request data -{ - Connection con(env, "", false); - TEST_TRUE(con.handle(xml_request, sizeof(xml_request) - 1), - "Test handler return value."); - TEST_NOTEQUAL_STR(con.getResponse(), "", "Test response value."); - sid = con.getSessionID(); - TEST_NOTEQUAL_STR(sid, "", "Test new session id."); - TEST_FALSE(did_commit, "No commit."); -} - -{ - Connection con(env, sid, false); - TEST_TRUE(con.handle(xml_request, sizeof(xml_request) - 1), - "Test handler return value."); - TEST_NOTEQUAL_STR(con.getResponse(), "", "Test response value."); - TEST_NOTEQUAL_STR(con.getSessionID(), "", "Test existing session id."); - TEST_EQUAL_STR(con.getSessionID(), sid, "Test existing session id."); - TEST_FALSE(did_commit, "No commit."); -} - -{ - Connection con(env, sid, true); - TEST_TRUE(con.handle(xml_request, sizeof(xml_request) - 1), - "Test handler return value."); - TEST_NOTEQUAL_STR(con.getResponse(), "", "Test response value."); - TEST_EQUAL_STR(con.getSessionID(), "", "Test existing session id."); - TEST_TRUE(did_commit, "Commit."); -} - -{ - Connection con(env, sid, false); - TEST_TRUE(con.handle(xml_request, sizeof(xml_request) - 1), - "Test handler return value."); - TEST_NOTEQUAL_STR(con.getSessionID(), "", "Test existing session id."); - TEST_NOTEQUAL_STR(con.getSessionID(), sid, "Test new session id."); - TEST_FALSE(did_commit, "No commit."); -} - -TEST_END; - -#endif/*TEST_CONNECTION*/ |