summaryrefslogtreecommitdiff
path: root/server/src/session.cc
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/session.cc')
-rw-r--r--server/src/session.cc86
1 files changed, 78 insertions, 8 deletions
diff --git a/server/src/session.cc b/server/src/session.cc
index fcd138a..ea4d11c 100644
--- a/server/src/session.cc
+++ b/server/src/session.cc
@@ -55,6 +55,8 @@ Session::Session(Environment *e,
patientid = pid;
templ = t;
+ mutex.name = "session-" + sid;
+
DEBUG(session, "[%p] new Session(sessionid: '%s', patientid: '%s',"
" template: '%s')\n", this, sid.c_str(), pid.c_str(), t.c_str());
@@ -82,10 +84,20 @@ std::string Session::id()
void Session::lock()
{
mutex.lock();
+ DEBUG(session, "lock() %p (%s)\n", this, sessionid.c_str());
+}
+
+bool Session::trylock()
+{
+ bool r = mutex.trylock();
+ DEBUG(session, "trylock() %p (%s) == %s\n",
+ this, sessionid.c_str(), r?"true":"false");
+ return r;
}
void Session::unlock()
{
+ DEBUG(session, "unlock() %p (%s)\n", this, sessionid.c_str());
mutex.unlock();
}
@@ -121,12 +133,21 @@ void Session::setIdle(bool idle)
}
}
-void Session::commit()
+void Session::commit() throw(LUAScript::Exception, Journal::Exception)
{
DEBUG(session, "[%p] commit(sessionid: '%s')\n", this, sessionid.c_str());
if(_journal != NULL) {
- _journal->commit();
+ try {
+ _journal->runOnCommitScripts();
+ } catch(LUAScript::Exception &e) {
+ throw e;
+ }
+ try {
+ _journal->commit();
+ } catch(Journal::Exception &e) {
+ throw e;
+ }
delete _journal;
_journal = NULL;
}
@@ -290,28 +311,76 @@ size_t Sessions::size()
void Sessions::store()
{
+ Session *session = NULL;
+ std::string sessionid;
+ do {
+ bool waitcont = false;
+ session = NULL;
+ {
+ MutexAutolock lock(mutex);
+ std::map<std::string, Session*>::iterator head = sessions.begin();
+ if(head != sessions.end()) {
+ session = head->second;
+ sessionid = head->first;
+ if(session->trylock()) {
+ sessions.erase(sessionid);
+ } else {
+ waitcont = true;
+ }
+ }
+ }
+
+ if(waitcont) {
+ usleep(200000); // sleep 200ms
+ continue;
+ }
+
+ if(session != NULL) {
+ SessionSerialiser ser(env, Conf::session_path);
+ ser.save(session);
+ delete session;
+ }
+ } while(session != NULL);
+
+ /*
MutexAutolock lock(mutex);
std::map<std::string, Session*>::iterator i = sessions.begin();
while(i != sessions.end()) {
SessionSerialiser ser(env, Conf::session_path);
- ser.save(i->second);
- delete i->second;
- sessions.erase(i);
+ Session *s = i->second;
+ s->lock();
+ ser.save(s);
+ delete s;
i++;
}
sessions.clear();
+ */
}
-std::vector<std::string> Sessions::activeSessions()
+std::vector<Sessions::SessionInfo> Sessions::activeSessions()
{
MutexAutolock lock(mutex);
- std::vector<std::string> act;
+ std::vector<SessionInfo> act;
std::map<std::string, Session*>::iterator i = sessions.begin();
while(i != sessions.end()) {
- act.push_back(i->first);
+ Session *s = i->second;
+ SessionInfo si;
+ si.id = i->first;
+ si.templ = "LOCKED";
+
+ if(s->trylock()) {
+ // si.user = "simpson";
+ // si.course = s->course;
+ si.patientid = s->patientid;
+ si.templ = s->templ;
+ si.idle = s->idle();
+ // si.ondisc = false;
+ s->unlock();
+ }
+ act.push_back(si);
i++;
}
@@ -326,6 +395,7 @@ SessionAutounlock::SessionAutounlock(Session **s)
SessionAutounlock::~SessionAutounlock()
{
+ DEBUG(session, "SessionAutounlock(%p)\n", *session);
if(*session) (*session)->unlock();
}