summaryrefslogtreecommitdiff
path: root/server/src/session.cc
diff options
context:
space:
mode:
authordeva <deva>2011-02-10 10:15:25 +0000
committerdeva <deva>2011-02-10 10:15:25 +0000
commitc1f0cdeb919864e0f93fcfecb8c27d889b473acb (patch)
tree51d11a58f1a24d79af66be46e2332e498d9f53fd /server/src/session.cc
parent35472208a495541f2e5be866e6f55242d34506ba (diff)
Some cleaning up. Added activeSessions.
Diffstat (limited to 'server/src/session.cc')
-rw-r--r--server/src/session.cc59
1 files changed, 46 insertions, 13 deletions
diff --git a/server/src/session.cc b/server/src/session.cc
index 530efac..fe05ee5 100644
--- a/server/src/session.cc
+++ b/server/src/session.cc
@@ -178,6 +178,8 @@ static bool fexists(const std::string &f)
Session *Sessions::newSession(std::string patientid, std::string templ)
throw(SessionAlreadyActive)
{
+ MutexAutolock lock(mutex);
+
std::map<std::string, Session *>::iterator i = sessions.begin();
while(i != sessions.end()) {
if(i->second->patientid == patientid &&
@@ -207,6 +209,8 @@ Session *Sessions::newSession(std::string patientid, std::string templ)
Session *Sessions::session(std::string sessionid)
{
+ MutexAutolock lock(mutex);
+
if(sessions.find(sessionid) != sessions.end())
return sessions[sessionid];
@@ -223,6 +227,8 @@ Session *Sessions::session(std::string sessionid)
Session *Sessions::takeSession(std::string sessionid)
{
+ MutexAutolock lock(mutex);
+
DEBUG(session,"%s\n", sessionid.c_str());
Session *s = NULL;
@@ -251,6 +257,8 @@ size_t Sessions::size()
void Sessions::store()
{
+ MutexAutolock lock(mutex);
+
std::map<std::string, Session*>::iterator i = sessions.begin();
while(i != sessions.end()) {
SessionSerialiser ser(env, Conf::session_path);
@@ -262,6 +270,21 @@ void Sessions::store()
sessions.clear();
}
+std::vector<std::string> Sessions::activeSessions()
+{
+ MutexAutolock lock(mutex);
+
+ std::vector<std::string> act;
+
+ std::map<std::string, Session*>::iterator i = sessions.begin();
+ while(i != sessions.end()) {
+ act.push_back(i->first);
+ i++;
+ }
+
+ return act;
+}
+
SessionAutolock::SessionAutolock(Session &s)
: session(s)
{
@@ -274,28 +297,38 @@ SessionAutolock::~SessionAutolock()
}
#ifdef TEST_SESSION
-//deps: configuration.cc journal.cc journal_commit.cc mutex.cc debug.cc sessionserialiser.cc sessionparser.cc saxparser.cc
-//cflags: -I.. $(PTHREAD_CFLAGS) $(EXPAT_CFLAGS)
-//libs: $(PTHREAD_LIBS) $(EXPAT_LIBS)
+//deps: configuration.cc journal.cc journal_uploadserver.cc journal_commit.cc mutex.cc debug.cc sessionserialiser.cc sessionparser.cc saxparser.cc environment.cc semaphore.cc artefact.cc xml_encode_decode.cc database.cc pracrodaopgsql.cc pracrodaotest.cc pracrodao.cc entitylist.cc macrolist.cc templatelist.cc macroheaderparser.cc templateheaderparser.cc versionstr.cc exception.cc log.cc inotify.cc
+//cflags: -I.. $(PTHREAD_CFLAGS) $(EXPAT_CFLAGS) $(ATF_CFLAGS) $(PQXX_CXXFLAGS)
+//libs: $(PTHREAD_LIBS) $(EXPAT_LIBS) $(ATF_LIBS) $(PQXX_LIBS)
#include <test.h>
+#define PID "1505050505"
+#define TMPL "test"
+
TEST_BEGIN;
-Sessions sessions;
+Environment env;
Conf::session_path = "/tmp";
-srand(0); // force seed
-Session *s1 = sessions.newSession();
-srand(0); // force seed
-Session *s2 = sessions.newSession();
+Session *s1 = env.sessions.newSession(PID, TMPL);
+
+TEST_EXCEPTION(env.sessions.newSession(PID, TMPL),
+ Sessions::SessionAlreadyActive, "Session should be 'locked'");
-TEST_NOTEQUAL(s1->id(), s2->id(), "Testing if IDs are unique.");
+Session *s2 = env.sessions.session(s1->id());
+TEST_EQUAL(s1, s2, "They should be the same session.");
-TEST_EQUAL(sessions.size(), 2, "Testing if size match.");
+env.sessions.takeSession(s1->id());
+Session *s3 = env.sessions.newSession(PID, TMPL);
+TEST_NOTEQUAL_STR(s1->id(), s3->id(), "Testing if IDs are unique.");
+
+TEST_EQUAL_INT(env.sessions.size(), 1, "Testing if size is 1.");
+
+/*
std::string sessionid = s1->id();
-SessionSerialiser ser(Conf::session_path, s1);
-ser.save();
+SessionSerialiser ser(&env, Conf::session_path);
+ser.save(s1);
sessions.deleteSession(sessionid);
TEST_EQUAL(sessions.size(), 1, "Testing if size match.");
@@ -308,7 +341,7 @@ TEST_EQUAL(sessions.size(), 0, "Testing if size match.");
s1 = sessions.session(sessionid);
TEST_NOTEQUAL(s1, NULL, "Did we reload the session from disk?");
-
+*/
TEST_END;
#endif/*TEST_SESSION*/