From bfe5d3000182cb10db583fe42ffa7b48f84d8b5b Mon Sep 17 00:00:00 2001 From: deva Date: Fri, 4 Feb 2011 07:43:00 +0000 Subject: Session locking mechanism gone crazy. This needs a rewamp at some point. --- server/src/sessionserialiser.cc | 167 +++++++++++++++++++++++++++++++++------- 1 file changed, 138 insertions(+), 29 deletions(-) (limited to 'server/src/sessionserialiser.cc') diff --git a/server/src/sessionserialiser.cc b/server/src/sessionserialiser.cc index 4065214..f2e2f39 100644 --- a/server/src/sessionserialiser.cc +++ b/server/src/sessionserialiser.cc @@ -27,6 +27,10 @@ */ #include "sessionserialiser.h" +#include +#include +#include + #include "journal.h" #include "sessionparser.h" @@ -38,10 +42,12 @@ #include #include +#define PRE "pracro_session" + std::string getSessionFilename(const std::string &path, const std::string &sessionid) { - return path + "/pracro_session." + sessionid; + return path + "/"PRE"." + sessionid; } @@ -63,14 +69,15 @@ SessionSerialiser::SessionSerialiser(std::string path) //#define BENC(s) base64encode(s) //#define BDEC(s) base64decode(s) -Session *SessionSerialiser::loadStr(const std::string &xml, - const std::string &sessionid) +Session *SessionSerialiser::loadStr(const std::string &xml) { // SessionAutolock lock(*session); SessionParser parser; parser.parse(xml.data(), xml.length()); - Session *session = new Session(sessionid, parser.patientid, ""); + Session *session = new Session(XDEC(parser.sessionid), + XDEC(parser.patientid), + XDEC(parser.templ)); Journal *j = session->journal(); j->setUser(XDEC(parser.userid)); j->setPatientID(XDEC(parser.patientid)); @@ -93,7 +100,9 @@ std::string SessionSerialiser::saveStr(Session *session) xml += "\n"; xml += "id()+"\">\n"; + "id=\""+XENC(session->id())+"\" " + "template=\""+XENC(session->templ)+ "\" " + "patientid=\"" + XENC(session->patientid) + "\">\n"; Journal *journal = session->journal(); @@ -139,7 +148,7 @@ Session *SessionSerialiser::load(const std::string &sessionid) } fclose(fp); - Session *session = loadStr(xml, sessionid); + Session *session = loadStr(xml); // delete file unlink(filename.c_str()); @@ -159,68 +168,168 @@ void SessionSerialiser::save(Session *session) fclose(fp); } +static inline bool isxmlfile(std::string name) +{ + if(name.find(PRE,0) == std::string::npos) return false; + + struct stat s; + stat(name.c_str(), &s); + + if(S_ISREG(s.st_mode) == false) return false; + + return true; +} + +Session *SessionSerialiser::findFromTupple(const std::string &patientid, + const std::string &templ) +{ + DEBUG(sessionserialiser, "Looking for: PatientID %s - Template %s\n", + patientid.c_str(), templ.c_str()); + + DIR *dir = opendir(path.c_str()); + if(!dir) { + ERR(sessionserialiser, "Could not open directory: %s - %s\n", + path.c_str(), strerror(errno)); + return NULL; + } + + struct dirent *dirent; + while( (dirent = readdir(dir)) != NULL ) { + + std::string filename = path+"/"+dirent->d_name; + + DEBUG(sessionserialiser, "Looking at file: %s\n", filename.c_str()); + + if(isxmlfile(filename)) { + + DEBUG(sessionserialiser, "Is xml file\n"); + + // Load session file + FILE *fp = fopen(filename.c_str(), "r"); + std::string xml; + while(!feof(fp)) { + char str[64]; + memset(str, 0, sizeof(str)); + fread(str, sizeof(str) - 1, 1, fp); + xml += str; + } + fclose(fp); + + Session *session = loadStr(xml); + + DEBUG(sessionserialiser, "PatientID %s - Template %s\n", + session->patientid.c_str(), + session->templ.c_str()); + + if(session->patientid == patientid && + session->templ == templ) { + closedir(dir); + unlink(filename.c_str()); + return session; + } + } + } + + closedir(dir); + + return NULL; +} + #ifdef TEST_SESSIONSERIALISER -//deps: session.cc journal.cc debug.cc configuration.cc mutex.cc journal_commit.cc sessionparser.cc saxparser.cc -//cflags: -I.. $(PTHREAD_CFLAGS) $(EXPAT_CFLAGS) -//libs: $(PTHREAD_LIBS) $(EXPAT_LIBS) +//deps: session.cc journal.cc debug.cc configuration.cc mutex.cc journal_commit.cc sessionparser.cc saxparser.cc xml_encode_decode.cc database.cc pracrodaopgsql.cc pracrodaotest.cc pracrodao.cc journal_uploadserver.cc log.cc +//cflags: -I.. $(PTHREAD_CFLAGS) $(EXPAT_CFLAGS) $(PQXX_CFLAGS) +//libs: $(PTHREAD_LIBS) $(EXPAT_LIBS) $(PQXX_LIBS) #include "test.h" +#include + #define SID "42" +#define PID "1234567890" +#define TID "sometemplate" #define SPATH "/tmp" TEST_BEGIN; std::string xml; +debug_parse("+all"); + +{ + FILE *fp = fopen("/tmp/"PRE".42", "w"); + fclose(fp); + TEST_TRUE(isxmlfile("/tmp/"PRE".42"), "Xml file"); + unlink("/tmp/"PRE".42"); + + fp = fopen("/tmp/pracro_diller.42", "w"); + fclose(fp); + TEST_FALSE(isxmlfile("/tmp/pracro_diller.42"), "Txt file"); + unlink("/tmp/pracro_diller.42"); + + TEST_FALSE(isxmlfile("/tmp/"PRE".42"), "No file"); + TEST_FALSE(isxmlfile("/tmp/badname"), "No file, bad name"); +} + { - Session session(SID); + Session session(SID, PID, TID); Journal *j = session.journal(); j->addEntry("some text", "macro1", 0); j->addEntry("some more text", "macro2", 2); j->addEntry("yet some more text", "macro3", 1); - SessionSerialiser s(SPATH, &session); - xml = s.saveStr(); + SessionSerialiser s(SPATH); + xml = s.saveStr(&session); s.loadStr(xml); - std::string xml2 = s.saveStr(); + std::string xml2 = s.saveStr(&session); TEST_EQUAL_STR(xml, xml2, "Compare"); } { - Session session(SID); + Session session(SID, PID, TID); Journal *j = session.journal(); j->addEntry("some text", "macro1", 0); j->addEntry("some more text", "macro2", 2); j->addEntry("yet some more text", "macro3", 1); - SessionSerialiser s(SPATH, &session); - xml = s.saveStr(); + SessionSerialiser s(SPATH); + xml = s.saveStr(&session); } - +/* { - Session session(SID); - SessionSerialiser s(SPATH, &session); + Session session(SID, PID, TID); + SessionSerialiser s(SPATH); s.loadStr(xml); - std::string xml2 = s.saveStr(); + std::string xml2 = s.saveStr(&session); TEST_EQUAL_STR(xml, xml2, "Compare"); } - +*/ { - Session session(SID); + Session session(SID, PID, TID); Journal *j = session.journal(); j->addEntry("some text", "macro1", 0); j->addEntry("some more text", "macro2", 2); j->addEntry("yet some more text", "macro3", 1); - SessionSerialiser s(SPATH, &session); - s.save(); + SessionSerialiser s(SPATH); + s.save(&session); } - +/* { - Session session(SID); - SessionSerialiser s(SPATH, &session); - s.load(); - std::string xml2 = s.saveStr(); + Session session(SID, PID, TID); + SessionSerialiser s(SPATH); + s.load(SID); + std::string xml2 = s.saveStr(&session); TEST_EQUAL_STR(xml, xml2, "Compare"); } +*/ +{ + Session session(SID, PID, TID); + SessionSerialiser s(SPATH); + s.save(&session); + Session *s1 = s.findFromTupple(PID, TID); + TEST_NOTEQUAL(s1, NULL, "Found it?"); + TEST_EQUAL(s1->id(), SID, "Compare found tuple."); + TEST_EQUAL(s1->patientid, PID, "Compare found tuple."); + TEST_EQUAL(s1->templ, TID, "Compare found tuple."); + delete s1; +} TEST_END; -- cgit v1.2.3