diff options
author | deva <deva> | 2010-06-01 12:58:32 +0000 |
---|---|---|
committer | deva <deva> | 2010-06-01 12:58:32 +0000 |
commit | 74a28aa7125be6a603128ad600c98c4882f3b5c2 (patch) | |
tree | 1a9e4ab74f29d5ff10f2701e4e112f4525c0dcec /server/src/sessionserialiser.cc | |
parent | 9b9c1e2dd3e5807ff7714b378b03b9ba31f42df7 (diff) |
From new_protocol branch.
Diffstat (limited to 'server/src/sessionserialiser.cc')
-rw-r--r-- | server/src/sessionserialiser.cc | 200 |
1 files changed, 200 insertions, 0 deletions
diff --git a/server/src/sessionserialiser.cc b/server/src/sessionserialiser.cc new file mode 100644 index 0000000..10d449d --- /dev/null +++ b/server/src/sessionserialiser.cc @@ -0,0 +1,200 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * sessionserialiser.cc + * + * Thu May 20 11:26:18 CEST 2010 + * Copyright 2010 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 "sessionserialiser.h" + +#include "journalwriter.h" + +#include "sessionparser.h" + +#include <stdio.h> +#include <string.h> + +std::string getSessionFilename(const std::string &path, + const std::string &sessionid) +{ + return path + "/pracro_session." + sessionid; +} + + +static std::string itostr(int i) +{ + char sid[32]; + snprintf(sid, sizeof(sid), "%d", i); + return sid; +} + +SessionSerialiser::SessionSerialiser(std::string path, Session *session) +{ + this->session = session; + this->path = path; +} + +void SessionSerialiser::loadStr(const std::string &xml) +{ + // SessionAutolock lock(*session); + SessionParser parser; + parser.parse(xml.data(), xml.length()); + JournalWriter *j = session->journal(); + j->currentuser = parser.userid; + j->currentcpr = parser.patientid; + std::vector<SessionParser::Entry>::iterator i = parser.entries.begin(); + while(i != parser.entries.end()) { + j->addEntry(i->resume, i->macro, i->index); + i++; + } +} + +std::string SessionSerialiser::saveStr() +{ + // SessionAutolock lock(*session); + + std::string xml; + + xml += "<?xml version='1.0' encoding='UTF-8'?>\n"; + xml += "<session timestamp=\""+itostr(time(NULL))+"\" " + "id=\""+session->id()+"\">\n"; + + JournalWriter *journal = session->journal(); + + xml += " <journal patientid=\"" + journal->currentcpr + + "\" userid=\"" + journal->currentuser + "\">\n"; + + std::map< int, JournalWriter::ResumeEntry >::iterator i = + journal->entrylist.begin(); + while(i != journal->entrylist.end()) { + + xml += " <entry index=\""+itostr(i->first) + "\"" + " macro=\"" + i->second.macro + "\">\n"; + xml += " <resume>" + i->second.resume + "</resume>\n"; + xml += " </entry>\n"; + + i++; + } + + xml += " </journal>\n"; + xml += "</session>\n"; + + return xml; +} + +void SessionSerialiser::load() +{ + // read xml from file + std::string filename = getSessionFilename(path, session->id()); + + FILE *fp = fopen(filename.c_str(), "r"); + char xml[2048]; + memset(xml, 0, sizeof(xml)); + fread(xml, sizeof(xml), 1, fp); + fclose(fp); + + loadStr(xml); + + // delete file + unlink(filename.c_str()); + +} + +void SessionSerialiser::save() +{ + std::string filename = getSessionFilename(path, session->id()); + + std::string xml = saveStr(); + + // write xml to file + FILE *fp = fopen(filename.c_str(), "w"); + fwrite(xml.data(), xml.size(), 1, fp); + fclose(fp); +} + +#ifdef TEST_SESSIONSERIALISER +//deps: session.cc journalwriter.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) +#include "test.h" + +#define SID "42" +#define SPATH "/tmp" + +TEST_BEGIN; + +std::string xml; + +{ + Session session(SID); + JournalWriter *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(); + s.loadStr(xml); + std::string xml2 = s.saveStr(); + TEST_EQUAL_STR(xml, xml2, "Compare"); +} + +{ + Session session(SID); + JournalWriter *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(); +} + +{ + Session session(SID); + SessionSerialiser s(SPATH, &session); + s.loadStr(xml); + std::string xml2 = s.saveStr(); + TEST_EQUAL_STR(xml, xml2, "Compare"); +} + +{ + Session session(SID); + JournalWriter *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(); +} + +{ + Session session(SID); + SessionSerialiser s(SPATH, &session); + s.load(); + std::string xml2 = s.saveStr(); + TEST_EQUAL_STR(xml, xml2, "Compare"); +} + + +TEST_END; + +#endif/*TEST_SESSIONSERIALISER*/ |