summaryrefslogtreecommitdiff
path: root/server/src/session.cc
blob: c919e545d47312139d5c7b70d3d262535e464b76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* -*- 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 <stdio.h>

// for stat
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>

#include "journal.h"
#include "journal_uploadserver.h"

#include "database.h"
#include "configuration.h"
#include "connectionpool.h"
#include "sessionserialiser.h"

Session::Session(std::string sessionid, std::string pid, std::string t)
{
  _journal = NULL;
  _database = NULL;

  patientid = pid;
  templ = t;
  
  isreadonly = true;

  database()->setSessionId(sessionid);
}

Session::~Session()
{
  if(_journal) delete _journal;
  if(_database) delete _database;
}

std::string Session::id()
{
  return database()->sessionId();
}

void Session::lock()
{
  mutex.lock();
}

void Session::unlock()
{
  mutex.unlock();
}

bool Session::active()
{
  return isreadonly || database()->active();
}

void Session::commit()
{
  if(_journal != NULL) {
    _journal->commit();
    delete _journal;
    _journal = NULL;
  }
  if(_database != NULL) {
    _database->commit();
    delete _database;
    _database = NULL;
  }
}

void Session::nocommit()
{
  if(_database != NULL) {
    _database->nocommit();
  }
}

void Session::discard()
{
  if(_journal) {
    delete _journal;
    _journal = NULL;
  }
  if(_database != NULL) {
    _database->discard();
    delete _database;
    _database = NULL;
  }
}

Journal *Session::journal()
{
  if(_journal == NULL) {
   _journal =
      new JournalUploadServer(Conf::journal_commit_addr,
                              Conf::journal_commit_port);
  }
  return _journal;
}

Database *Session::database()
{
  if(_database == NULL) {
   _database =
      new Database(Conf::database_backend, Conf::database_addr, "",
                   Conf::database_user, Conf::database_passwd, "");
  }
  return _database;
}

Sessions::Sessions()
{
}

static bool fexists(const std::string &f)
{
  bool ret;

  FILE *fp = fopen(f.c_str(), "r");
  ret = fp != NULL;
  if(fp) fclose(fp);

  return ret;
}

Session *Sessions::newSession(std::string patientid, std::string templ)
  throw(SessionAlreadyActive)
{
  std::map<std::string, Session *>::iterator i = sessions.begin();
  while(i != sessions.end()) {
    if(i->second->patientid == patientid &&
       i->second->templ == templ) {
      Session *s = i->second;
      if(s->active()) throw SessionAlreadyActive(s->id());
      return s;
    }

    i++;
  }

  { // Look up patientid / template tupple in session files.
    SessionSerialiser ser(Conf::session_path);
    Session *session = ser.findFromTupple(patientid, templ);
    if(session) {
      sessions[session->id()] = session;
      return session;
    }
  }

  Session *session = new Session("", patientid, templ);
  sessions[session->id()] = session;
  return session;
}

Session *Sessions::session(std::string sessionid)
{
  if(sessions.find(sessionid) != sessions.end())
    return sessions[sessionid];

  std::string filename = getSessionFilename(Conf::session_path, sessionid);
  if(fexists(filename)) {
    SessionSerialiser ser(Conf::session_path);
    Session *s = ser.load(sessionid);
    sessions[s->id()] = s;
    return s;
  }

  return NULL;
}

Session *Sessions::takeSession(std::string sessionid)
{
  DEBUG(session,"%s\n", sessionid.c_str());

  Session *s = NULL;
  if(sessions.find(sessionid) != sessions.end()) {
    s = sessions[sessionid];
  }

  if(s) {
    sessions.erase(sessionid);
  }
  else DEBUG(session, "No such session!\n");

  return s;
}

void Sessions::deleteSession(std::string sessionid)
{
  Session *s = takeSession(sessionid);
  if(s) delete s;
}

size_t Sessions::size()
{
  return sessions.size();
}

void Sessions::store()
{
  std::map<std::string, Session*>::iterator i = sessions.begin();
  while(i != sessions.end()) {
    SessionSerialiser ser(Conf::session_path);
    ser.save(i->second);
    delete i->second;
    sessions.erase(i);
    i++;
  }
  sessions.clear();
}

SessionAutolock::SessionAutolock(Session &s)
  : session(s)
{
  session.lock();
}

SessionAutolock::~SessionAutolock()
{
  session.unlock();
}

#ifdef TEST_SESSION
//deps: configuration.cc journal.cc journal_commit.cc mutex.cc debug.cc sessionserialiser.cc sessionparser.cc saxparser.cc
//cflags: -I.. $(PTHREAD_CFLAGS) $(EXPAT_CFLAGS)
//libs: $(PTHREAD_LIBS) $(EXPAT_LIBS)
#include <test.h>

TEST_BEGIN;
Sessions sessions;

Conf::session_path = "/tmp";

srand(0); // force seed
Session *s1 = sessions.newSession();
srand(0); // force seed
Session *s2 = sessions.newSession();

TEST_NOTEQUAL(s1->id(), s2->id(), "Testing if IDs are unique.");

TEST_EQUAL(sessions.size(), 2, "Testing if size match.");

std::string sessionid = s1->id();
SessionSerialiser ser(Conf::session_path, s1);
ser.save();

sessions.deleteSession(sessionid);
TEST_EQUAL(sessions.size(), 1, "Testing if size match.");

s1 = sessions.session(sessionid);
TEST_NOTEQUAL(s1, NULL, "Did we reload the session from disk?");

sessions.store();
TEST_EQUAL(sessions.size(), 0, "Testing if size match.");

s1 = sessions.session(sessionid);
TEST_NOTEQUAL(s1, NULL, "Did we reload the session from disk?");

TEST_END;

#endif/*TEST_SESSION*/