From 74a28aa7125be6a603128ad600c98c4882f3b5c2 Mon Sep 17 00:00:00 2001 From: deva Date: Tue, 1 Jun 2010 12:58:32 +0000 Subject: From new_protocol branch. --- server/src/sessionserialiser.cc | 200 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 200 insertions(+) create mode 100644 server/src/sessionserialiser.cc (limited to 'server/src/sessionserialiser.cc') 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 +#include + +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::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 += "\n"; + xml += "id()+"\">\n"; + + JournalWriter *journal = session->journal(); + + xml += " currentcpr + + "\" userid=\"" + journal->currentuser + "\">\n"; + + std::map< int, JournalWriter::ResumeEntry >::iterator i = + journal->entrylist.begin(); + while(i != journal->entrylist.end()) { + + xml += " first) + "\"" + " macro=\"" + i->second.macro + "\">\n"; + xml += " " + i->second.resume + "\n"; + xml += " \n"; + + i++; + } + + xml += " \n"; + xml += "\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*/ -- cgit v1.2.3