From bb1c42800a2f48129100222317520b690f13c40e Mon Sep 17 00:00:00 2001 From: deva Date: Fri, 18 Feb 2011 14:16:09 +0000 Subject: Made Session isreadonly property private and created new commitMacro method. --- server/src/admin_connection.cc | 2 +- server/src/client_connection.cc | 4 +- server/src/session.cc | 105 +++++++++++++++++++++++++-------------- server/src/session.h | 8 ++- server/src/sessionserialiser.cc | 19 ++++--- server/src/transactionhandler.cc | 6 +-- 6 files changed, 91 insertions(+), 53 deletions(-) (limited to 'server/src') diff --git a/server/src/admin_connection.cc b/server/src/admin_connection.cc index ecc7c65..0fe5380 100644 --- a/server/src/admin_connection.cc +++ b/server/src/admin_connection.cc @@ -38,7 +38,7 @@ static std::string admin_sessionunlock(Environment &env, std::string id) { Session *session = env.sessions.session(id); if(session) { - if(session->isreadonly) { + if(session->isReadonly()) { env.sessions.deleteSession(id); return "Session " + id + " was 'readonly' and has been discarded."; } else { diff --git a/server/src/client_connection.cc b/server/src/client_connection.cc index 5aee707..9a1ac0f 100644 --- a/server/src/client_connection.cc +++ b/server/src/client_connection.cc @@ -75,7 +75,7 @@ ClientConnection::~ClientConnection() void ClientConnection::nocommit(Session *session) { if(donocommit) { - if(session->isreadonly) { // NoCommit of an empty session discards it. + if(session->isReadonly()) { // NoCommit of an empty session discards it. dodiscard = true; return; } @@ -89,7 +89,7 @@ void ClientConnection::nocommit(Session *session) void ClientConnection::commit(Session *session) { if(docommit) { - if(session->isreadonly) { // Commit of an empty session discards it. + if(session->isReadonly()) { // Commit of an empty session discards it. dodiscard = true; return; } diff --git a/server/src/session.cc b/server/src/session.cc index 24a9c12..6850e9b 100644 --- a/server/src/session.cc +++ b/server/src/session.cc @@ -84,6 +84,16 @@ void Session::unlock() mutex.unlock(); } +void Session::commitMacro(Transaction &transaction, Commit &commit, + Macro ¯o) +{ + AutoBorrower borrower(env->dbpool); + Database *db = borrower.get(); + db->commitTransaction(transaction, commit, macro, id()); + isreadonly = false; +} + + bool Session::idle() { if(isreadonly) return false; @@ -149,17 +159,7 @@ Journal *Session::journal() } return _journal; } -/* -Database *Session::database() -{ - if(_database == NULL) { - _database = - new Database(Conf::database_backend, Conf::database_addr, "", - Conf::database_user, Conf::database_passwd, ""); - } - return _database; -} -*/ + Sessions::Sessions(Environment *e) : env(e) { } @@ -185,7 +185,10 @@ Session *Sessions::newSession(std::string patientid, std::string templ) if(i->second->patientid == patientid && i->second->templ == templ) { Session *session = i->second; - if(!session->idle()) throw SessionAlreadyActive(session->id()); + if(!session->idle()) { + DEBUG(session, "Patient/template matched session is already active."); + throw SessionAlreadyActive(session->id()); + } return session; } @@ -195,9 +198,12 @@ Session *Sessions::newSession(std::string patientid, std::string templ) { // Look up patientid / template tupple in session files. SessionSerialiser ser(env, Conf::session_path); Session *session = ser.findFromTupple(patientid, templ); - if(session) { + if(session != NULL) { sessions[session->id()] = session; - if(!session->idle()) throw SessionAlreadyActive(session->id()); + if(!session->idle()) { + DEBUG(session, "Looked up session by id is already active."); + throw SessionAlreadyActive(session->id()); + } return session; } } @@ -307,13 +313,19 @@ SessionAutolock::~SessionAutolock() TEST_BEGIN; +debug_parse("+all"); + Conf::database_backend = "testdb"; Conf::database_poolsize = 1; -Conf::xml_basedir = "/tmp"; +Conf::xml_basedir = "../xml"; Environment env; -Conf::session_path = "/tmp"; +// Make sure we start out on an empty session directory. +Conf::session_path = "/tmp/test_session"; +while(mkdir(Conf::session_path.c_str(), 0777) == -1 && errno == EEXIST) { + Conf::session_path += "X"; +} Session *s1 = env.sessions.newSession(PID, TMPL); @@ -340,37 +352,56 @@ TEST_EQUAL_INT(env.sessions.size(), 0, "Testing if size is 0."); Session *s5 = env.sessions.newSession(PID, TMPL); TEST_NOTEQUAL(s5, NULL, "A new session was created."); -s5->isreadonly = false; +{ + Transaction transaction; + transaction.cpr = PID; + transaction.user = "me"; + + Commit commit; + commit.fields["field1"] = "hello"; + commit.fields["field2"] = "world"; + commit.templ = TMPL; + + Macro macro; + macro.attributes["version"] = "1.0"; + macro.attributes["name"] = "somemacro"; + + s5->commitMacro(transaction, commit, macro); +} +id = s5->id(); env.sessions.store(); +// Resume session using session id. Session *s6 = env.sessions.session(id); -TEST_EQUAL_STR(s6->id(), s5->id(), "Did we get the stored session?"); +TEST_NOTEQUAL(s6, NULL, "We did get one right?"); +TEST_EQUAL_STR(s6->id(), id, "Did we get the stored session?"); -s6->isreadonly = false; +{ + Transaction transaction; + transaction.cpr = PID; + transaction.user = "me"; + + Commit commit; + commit.fields["field1"] = "hello"; + commit.fields["field2"] = "world"; + commit.templ = TMPL; + + Macro macro; + macro.attributes["version"] = "1.0"; + macro.attributes["name"] = "somemacro"; + + s6->commitMacro(transaction, commit, macro); +} +s6->nocommit(); env.sessions.store(); +// Resume session using patientid/template tupple. Session *s7 = env.sessions.newSession(PID, TMPL); -TEST_EQUAL_STR(s7->id(), s5->id(), "Did we get the stored session?"); - -/* -std::string sessionid = s1->id(); -SessionSerialiser ser(&env, Conf::session_path); -ser.save(s1); - -sessions.deleteSession(sessionid); -TEST_EQUAL(sessions.size(), 1, "Testing if size match."); - -s1 = sessions.session(sessionid); -TEST_NOTEQUAL(s1, NULL, "Did we reload the session from disk?"); - -sessions.store(); -TEST_EQUAL(sessions.size(), 0, "Testing if size match."); +TEST_NOTEQUAL(s7, NULL, "We did get one right?"); +TEST_EQUAL_STR(s7->id(), id, "Did we get the stored session?"); -s1 = sessions.session(sessionid); -TEST_NOTEQUAL(s1, NULL, "Did we reload the session from disk?"); -*/ TEST_END; #endif/*TEST_SESSION*/ diff --git a/server/src/session.h b/server/src/session.h index 533f7a0..e2751bd 100644 --- a/server/src/session.h +++ b/server/src/session.h @@ -34,11 +34,14 @@ #include #include "mutex.h" +#include "transaction.h" +#include "template.h" class Environment; class Journal; class Session { + friend class SessionSerialiser; public: Session(Environment *env, std::string sessionid, std::string patientid, std::string templ); @@ -53,6 +56,8 @@ public: void nocommit(); void discard(); + void commitMacro(Transaction &transaction, Commit &commit, Macro ¯o); + Journal *journal(); std::string patientid; @@ -61,13 +66,14 @@ public: bool idle(); void setIdle(bool idle); - bool isreadonly; + bool isReadonly() { return isreadonly; } private: Environment *env; Journal *_journal; Mutex mutex; std::string sessionid; + bool isreadonly; }; class Sessions { diff --git a/server/src/sessionserialiser.cc b/server/src/sessionserialiser.cc index 99f880b..b9e825a 100644 --- a/server/src/sessionserialiser.cc +++ b/server/src/sessionserialiser.cc @@ -252,10 +252,15 @@ Session *SessionSerialiser::findFromTupple(const std::string &patientid, #define SID "42" #define PID "1234567890" #define TID "sometemplate" -#define SPATH "/tmp" TEST_BEGIN; +// Make sure we start out on an empty session directory. +std::string spath = "/tmp/test_sessionserialiser"; +while(mkdir(spath.c_str(), 0777) == -1 && errno == EEXIST) { + spath += "X"; +} + std::string xml; debug_parse("+all"); @@ -283,7 +288,7 @@ Environment env; j->addEntry("some text", "macro1", 0); j->addEntry("some more text", "macro2", 2); j->addEntry("yet some more text", "macro3", 1); - SessionSerialiser s(&env, SPATH); + SessionSerialiser s(&env, spath); xml = s.saveStr(&session); s.loadStr(xml); std::string xml2 = s.saveStr(&session); @@ -296,13 +301,13 @@ Environment env; j->addEntry("some text", "macro1", 0); j->addEntry("some more text", "macro2", 2); j->addEntry("yet some more text", "macro3", 1); - SessionSerialiser s(&env, SPATH); + SessionSerialiser s(&env, spath); xml = s.saveStr(&session); } /* { Session session(SID, PID, TID); - SessionSerialiser s(SPATH); + SessionSerialiser s(spath); s.loadStr(xml); std::string xml2 = s.saveStr(&session); TEST_EQUAL_STR(xml, xml2, "Compare"); @@ -314,13 +319,13 @@ Environment env; j->addEntry("some text", "macro1", 0); j->addEntry("some more text", "macro2", 2); j->addEntry("yet some more text", "macro3", 1); - SessionSerialiser s(&env, SPATH); + SessionSerialiser s(&env, spath); s.save(&session); } /* { Session session(SID, PID, TID); - SessionSerialiser s(SPATH); + SessionSerialiser s(spath); s.load(SID); std::string xml2 = s.saveStr(&session); TEST_EQUAL_STR(xml, xml2, "Compare"); @@ -329,7 +334,7 @@ Environment env; { Session session(&env, SID, PID, TID); - SessionSerialiser s(&env, SPATH); + SessionSerialiser s(&env, spath); s.save(&session); Session *s1 = s.findFromTupple(PID, TID); TEST_NOTEQUAL(s1, NULL, "Found it?"); diff --git a/server/src/transactionhandler.cc b/server/src/transactionhandler.cc index 0ebf782..bcdd638 100644 --- a/server/src/transactionhandler.cc +++ b/server/src/transactionhandler.cc @@ -54,9 +54,6 @@ static std::string handleCommits(Transaction &transaction, Environment &env, std::string answer; if(transaction.commits.size() > 0) { - AutoBorrower borrower(env.dbpool); - Database *db = borrower.get(); - //Database *db = session.database(); Commits::iterator i = transaction.commits.begin(); while(i != transaction.commits.end()) { @@ -68,8 +65,7 @@ static std::string handleCommits(Transaction &transaction, Environment &env, std::string resume = resume_parser(*macro, commit); commit.fields["journal.resume"] = resume; - session.isreadonly = false; - db->commitTransaction(transaction, commit, *macro, session.id()); + session.commitMacro(transaction, commit, *macro); if(resume != "") { -- cgit v1.2.3