summaryrefslogtreecommitdiff
path: root/server/src
diff options
context:
space:
mode:
authordeva <deva>2009-12-15 18:28:57 +0000
committerdeva <deva>2009-12-15 18:28:57 +0000
commit2261e83f6d961e4c9f88dc2687881d57295e5778 (patch)
tree684049ff65a51a57a2e115df5b4ba1d6d0c3cd7e /server/src
parent708f52b2905701457188732d4b6881d5284674b4 (diff)
New Sessions and Session classes.
Diffstat (limited to 'server/src')
-rw-r--r--server/src/Makefile.am14
-rw-r--r--server/src/server.cc16
-rw-r--r--server/src/session.cc107
-rw-r--r--server/src/session.h80
4 files changed, 202 insertions, 15 deletions
diff --git a/server/src/Makefile.am b/server/src/Makefile.am
index 9af183f..2daa37e 100644
--- a/server/src/Makefile.am
+++ b/server/src/Makefile.am
@@ -30,6 +30,7 @@ pracrod_SOURCES = \
resumeparser.cc \
saxparser.cc \
server.cc \
+ session.cc \
templatelist.cc \
templateheaderparser.cc \
templateparser.cc \
@@ -97,6 +98,7 @@ EXTRA_DIST = \
resumeparser.h \
saxparser.h \
server.h \
+ session.h \
templatelist.h \
templateheaderparser.h \
templateparser.h \
@@ -133,7 +135,8 @@ TESTFILES = \
test_pracrodaopgsql \
test_macroparser \
test_xml_encode_decode \
- test_journal_commit
+ test_journal_commit \
+ test_session
TESTLOGS = `for F in ${TESTFILES}; do echo $$F.log; done`
@@ -152,6 +155,15 @@ test: $(TESTFILES)
test_clean:
rm -f $(TESTFILES) $(TESTLOGS)
+TEST_SESSION_FILES = \
+ session.cc \
+ journal_commit.cc \
+ templateparser.cc \
+ $(PARSERFILES) \
+ $(BASICFILES)
+test_session: $(TEST_SESSION_FILES)
+ @../../tools/test $(TEST_SESSION_FILES) $(PARSERFLAGS) $(BASICFLAGS)
+
TEST_TCPSOCKET_FILES = \
tcpsocket.cc \
$(BASICFILES)
diff --git a/server/src/server.cc b/server/src/server.cc
index c59abbf..6a2a61b 100644
--- a/server/src/server.cc
+++ b/server/src/server.cc
@@ -65,7 +65,7 @@
typedef long long unsigned int sessionid_t;
typedef struct {
- JournalWriter *journalwriter;
+ JournalWriter *journalwriter;
} session_t;
struct conn_t {
@@ -355,18 +355,6 @@ static std::string handleConnection(const char *buf, size_t size, struct conn_t
return res;
}
-static sessionid_t newSessionID(struct conn_t *conn)
-{
- sessionid_t sid;
- conn->mutex.lock();
- // Find a random session id that is not in use.
- do {
- sid = rand();
- } while(conn->sessions.find(sid) != conn->sessions.end());
- conn->mutex.unlock();
- return sid;
-}
-
static int handle_request(void *cls,
struct MHD_Connection *con,
const char *url,
@@ -388,7 +376,7 @@ static int handle_request(void *cls,
const char *sessionids = MHD_lookup_connection_value(con, MHD_HEADER_KIND, "SessionID");
if(sessionids == NULL) {
- sessionid = newSessionID(conn);
+ sessionid = 42;//newSessionID(conn);
} else {
sessionid = atoll(sessionids);
conn->mutex.lock();
diff --git a/server/src/session.cc b/server/src/session.cc
new file mode 100644
index 0000000..02a78c5
--- /dev/null
+++ b/server/src/session.cc
@@ -0,0 +1,107 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set et sw=2 ts=2: */
+/***************************************************************************
+ * session.cc
+ *
+ * Tue Dec 15 13:36:49 CET 2009
+ * Copyright 2009 Bent Bisballe Nyeng
+ * deva@aasimon.org
+ ****************************************************************************/
+
+/*
+ * This file is part of Pracro.
+ *
+ * Pracro is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Pracro is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Pracro; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+#include "session.h"
+
+#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;
+}
+
+
+Session::Session(std::string sessionid)
+{
+ _id = sessionid;
+ journal = NULL;
+}
+
+std::string Session::id()
+{
+ return _id;
+}
+
+Sessions::Sessions()
+{
+}
+
+Session *Sessions::newSession()
+{
+ Session *session = new Session(newSessionID(sessions));
+ sessions[session->id()] = session;
+ return session;
+}
+
+Session *Sessions::session(std::string sessionid)
+{
+ if(sessions.find(sessionid) == sessions.end()) return NULL;
+ return sessions[sessionid];
+}
+
+Session *Sessions::takeSession(std::string sessionid)
+{
+ Session *s = session(sessionid);
+ if(s) {
+ sessions.erase(sessionid);
+ }
+ return s;
+}
+
+void Sessions::deleteSession(std::string sessionid)
+{
+ Session *s = takeSession(sessionid);
+ if(s) delete s;
+}
+
+#ifdef TEST_SESSION
+
+int main()
+{
+ Sessions sessions;
+
+ srand(0);
+ Session *s1 = sessions.newSession();
+ srand(0);
+ Session *s2 = sessions.newSession();
+
+ if(s1->id() == s2->id()) {
+ printf("IDs not unique.\n");
+ return 1;
+ }
+
+ return 0;
+
+}
+
+#endif/*TEST_SESSION*/
diff --git a/server/src/session.h b/server/src/session.h
new file mode 100644
index 0000000..a4d93f4
--- /dev/null
+++ b/server/src/session.h
@@ -0,0 +1,80 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set et sw=2 ts=2: */
+/***************************************************************************
+ * session.h
+ *
+ * Tue Dec 15 13:36:49 CET 2009
+ * Copyright 2009 Bent Bisballe Nyeng
+ * deva@aasimon.org
+ ****************************************************************************/
+
+/*
+ * This file is part of Pracro.
+ *
+ * Pracro is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Pracro is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Pracro; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+#ifndef __PRACRO_SESSION_H__
+#define __PRACRO_SESSION_H__
+
+#include <string>
+#include <map>
+
+class JournalWriter;
+
+class Session {
+public:
+ Session(std::string sessionid);
+
+ std::string id();
+
+private:
+ JournalWriter *journal;
+ std::string _id;
+};
+
+class Sessions {
+public:
+ Sessions();
+
+ /**
+ * Create a new session, with a unique id. Insert it into the session list,
+ * and return its pointer.
+ */
+ Session *newSession();
+
+ /**
+ * Lookup session in session list. Returns the session or NULL if no session
+ * exists with that sessionid.
+ */
+ Session *session(std::string sessionid);
+
+ /**
+ * Remove session from the session list and return its pointer. It is up to
+ * the caller to delete it.
+ */
+ Session *takeSession(std::string sessionid);
+
+ /**
+ * Remove session from the session list and delete it.
+ */
+ void deleteSession(std::string sessionid);
+
+private:
+ std::map<std::string, Session *> sessions;
+
+};
+
+
+#endif/*__PRACRO_SESSION_H__*/