From 0e819eb42b4d680a99ae7b04702bfc9510495aee Mon Sep 17 00:00:00 2001 From: deva Date: Wed, 6 Jan 2010 07:47:58 +0000 Subject: 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). --- server/src/session.cc | 64 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 16 deletions(-) (limited to 'server/src/session.cc') 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 -#include "journal_commit.h" - -static std::string newSessionID(std::map &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_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*/ -- cgit v1.2.3