summaryrefslogtreecommitdiff
path: root/server/src/session.cc
diff options
context:
space:
mode:
authordeva <deva>2010-01-06 07:47:58 +0000
committerdeva <deva>2010-01-06 07:47:58 +0000
commit0e819eb42b4d680a99ae7b04702bfc9510495aee (patch)
treea425aeb98c679a0891a974b6f93090343d51a4e8 /server/src/session.cc
parent582913fc18937c87afe7d351e66095dccb435a9e (diff)
New artefact connection class (to later wrap libartefact). New environment class to hold all global resources. Made ConnectionPool a template class. Split journal code up into two files (class from commit code).
Diffstat (limited to 'server/src/session.cc')
-rw-r--r--server/src/session.cc64
1 files changed, 48 insertions, 16 deletions
diff --git a/server/src/session.cc b/server/src/session.cc
index 0e8679c..b53df1a 100644
--- a/server/src/session.cc
+++ b/server/src/session.cc
@@ -29,22 +29,14 @@
#include <stdlib.h>
-#include "journal_commit.h"
-
-static std::string newSessionID(std::map<std::string, Session *> &sessions)
-{
- char sid[32];
- do {
- snprintf(sid, sizeof(sid)-1, "%d", rand());
- } while(sessions.find(sid) != sessions.end());
- return sid;
-}
-
+#include "journalwriter.h"
+#include "configuration.h"
+#include "connectionpool.h"
Session::Session(std::string sessionid)
{
_id = sessionid;
- journal = NULL;
+ _journal = NULL;
}
std::string Session::id()
@@ -52,13 +44,46 @@ std::string Session::id()
return _id;
}
+void Session::lock()
+{
+ mutex.lock();
+}
+
+void Session::unlock()
+{
+ mutex.unlock();
+}
+
+void Session::commit()
+{
+ if(_journal != NULL) {
+ _journal->commit();
+ delete _journal;
+ _journal = NULL;
+ }
+}
+
+JournalWriter *Session::journal()
+{
+ if(_journal == NULL) {
+ _journal =
+ new JournalWriter(Conf::journal_commit_addr, Conf::journal_commit_port);
+ }
+ return _journal;
+}
+
Sessions::Sessions()
{
}
Session *Sessions::newSession()
{
- Session *session = new Session(newSessionID(sessions));
+ char sessionid[32];
+ do {
+ snprintf(sessionid, sizeof(sessionid)-1, "%d", rand());
+ } while(sessions.find(sessionid) != sessions.end());
+
+ Session *session = new Session(sessionid);
sessions[session->id()] = session;
return session;
}
@@ -84,10 +109,15 @@ void Sessions::deleteSession(std::string sessionid)
if(s) delete s;
}
+size_t Sessions::size()
+{
+ return sessions.size();
+}
+
#ifdef TEST_SESSION
-//deps:
-//cflags:
-//libs:
+//deps: configuration.cc journalwriter.cc journal_commit.cc mutex.cc debug.cc
+//cflags: -I..
+//libs: -lpthread
#include <test.h>
TEST_BEGIN;
@@ -100,6 +130,8 @@ Session *s2 = sessions.newSession();
TEST_NOTEQUAL(s1->id(), s2->id(), "Testing if IDs are unique.");
+TEST_EQUAL(sessions.size(), 2, "Testing if size match.");
+
TEST_END;
#endif/*TEST_SESSION*/