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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
|
/* -*- 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"
#include "environment.h"
Session::Session(Environment *e,
std::string sid, std::string pid, std::string t)
: env(e)
{
_journal = NULL;
sessionid = sid;
patientid = pid;
templ = t;
DEBUG(session, "[%p] new Session(sessionid: '%s', patientid: '%s',"
" template: '%s')\n", this, sid.c_str(), pid.c_str(), t.c_str());
isreadonly = true;
}
Session::~Session()
{
DEBUG(session, "[%p] delete Session(sessionid: '%s')\n", this,
sessionid.c_str());
if(_journal) delete _journal;
}
std::string Session::id()
{
if(sessionid == "") {
AutoBorrower<Database*> borrower(env->dbpool);
Database *db = borrower.get();
sessionid = db->newSessionId();
}
return sessionid;
}
void Session::lock()
{
mutex.lock();
}
void Session::unlock()
{
mutex.unlock();
}
void Session::commitMacro(Transaction &transaction, Commit &commit,
Macro ¯o)
{
DEBUG(session, "[%p] commitMacro(sessionid: '%s')\n", this,
sessionid.c_str());
AutoBorrower<Database*> borrower(env->dbpool);
Database *db = borrower.get();
db->commitTransaction(transaction, commit, macro, id());
isreadonly = false;
}
bool Session::idle()
{
if(isreadonly) return false;
{
AutoBorrower<Database*> borrower(env->dbpool);
Database *db = borrower.get();
return db->idle(id());
}
}
void Session::setIdle(bool idle)
{
if(isreadonly == false) {
AutoBorrower<Database*> borrower(env->dbpool);
Database *db = borrower.get();
return db->setIdle(id(), idle);
}
}
void Session::commit() throw(LUAScript::Exception, Journal::Exception)
{
DEBUG(session, "[%p] commit(sessionid: '%s')\n", this, sessionid.c_str());
if(_journal != NULL) {
try {
_journal->runOnCommitScripts();
} catch(LUAScript::Exception &e) {
throw e;
}
try {
_journal->commit();
} catch(Journal::Exception &e) {
throw e;
}
delete _journal;
_journal = NULL;
}
if(isreadonly == false) {
AutoBorrower<Database*> borrower(env->dbpool);
Database *db = borrower.get();
db->commit(id());
}
}
void Session::nocommit()
{
DEBUG(session, "[%p] nocommit(sessionid: '%s')\n", this, sessionid.c_str());
if(isreadonly == false) {
AutoBorrower<Database*> borrower(env->dbpool);
Database *db = borrower.get();
db->nocommit(id());
}
}
void Session::discard()
{
DEBUG(session, "[%p] discard(sessionid: '%s')\n", this, sessionid.c_str());
// Store session file in discarded folder before deleting.
if(isreadonly == false && Conf::session_discard_path != "") {
SessionSerialiser ser(env, Conf::session_discard_path);
ser.save(this);
}
if(_journal) {
delete _journal;
_journal = NULL;
}
if(isreadonly == false) {
AutoBorrower<Database*> borrower(env->dbpool);
Database *db = borrower.get();
db->discard(id());
}
}
Journal *Session::journal()
{
if(_journal == NULL) {
_journal =
new JournalUploadServer(Conf::journal_commit_addr,
Conf::journal_commit_port);
}
return _journal;
}
Sessions::Sessions(Environment *e) : env(e)
{
}
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::newLockedSession(std::string patientid, std::string templ)
throw(SessionAlreadyActive)
{
MutexAutolock lock(mutex);
std::map<std::string, Session *>::iterator i = sessions.begin();
while(i != sessions.end()) {
if(i->second->patientid == patientid &&
i->second->templ == templ) {
Session *session = i->second;
if(!session->idle()) {
DEBUG(session, "Patient/template matched session is already active.");
throw SessionAlreadyActive(session->id());
}
session->lock();
return session;
}
i++;
}
{ // Look up patientid / template tupple in session files.
SessionSerialiser ser(env, Conf::session_path);
Session *session = ser.findFromTupple(patientid, templ);
if(session != NULL) {
sessions[session->id()] = session;
if(!session->idle()) {
DEBUG(session, "Looked up session by id is already active.");
throw SessionAlreadyActive(session->id());
}
session->lock();
return session;
}
}
Session *session = new Session(env, "", patientid, templ);
sessions[session->id()] = session;
session->lock();
return session;
}
Session *Sessions::lockedSession(std::string sessionid)
{
MutexAutolock lock(mutex);
if(sessions.find(sessionid) != sessions.end()) {
Session *s = sessions[sessionid];
s->lock();
return s;
}
std::string filename = getSessionFilename(Conf::session_path, sessionid);
if(fexists(filename)) {
SessionSerialiser ser(env, Conf::session_path);
Session *s = ser.load(sessionid);
sessions[s->id()] = s;
s->lock();
return s;
}
return NULL;
}
Session *Sessions::takeSession(std::string sessionid)
{
MutexAutolock lock(mutex);
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()
{
MutexAutolock lock(mutex);
std::map<std::string, Session*>::iterator i = sessions.begin();
while(i != sessions.end()) {
SessionSerialiser ser(env, Conf::session_path);
ser.save(i->second);
delete i->second;
sessions.erase(i);
i++;
}
sessions.clear();
}
std::vector<std::string> Sessions::activeSessions()
{
MutexAutolock lock(mutex);
std::vector<std::string> act;
std::map<std::string, Session*>::iterator i = sessions.begin();
while(i != sessions.end()) {
act.push_back(i->first);
i++;
}
return act;
}
SessionAutounlock::SessionAutounlock(Session **s)
: session(s)
{
// session->lock();
}
SessionAutounlock::~SessionAutounlock()
{
if(*session) (*session)->unlock();
}
#ifdef TEST_SESSION
//deps: configuration.cc journal.cc journal_uploadserver.cc journal_commit.cc mutex.cc debug.cc sessionserialiser.cc sessionparser.cc saxparser.cc environment.cc semaphore.cc artefact.cc xml_encode_decode.cc database.cc pracrodaopgsql.cc pracrodaotest.cc pracrodao.cc entitylist.cc macrolist.cc templatelist.cc macroheaderparser.cc templateheaderparser.cc versionstr.cc exception.cc log.cc inotify.cc
//cflags: -I.. $(PTHREAD_CFLAGS) $(EXPAT_CFLAGS) -DWITHOUT_ARTEFACT -DWITHOUT_DB
//libs: $(PTHREAD_LIBS) $(EXPAT_LIBS)
#include <test.h>
#define PID "1505050505"
#define TMPL "test"
TEST_BEGIN;
debug_parse("+all");
Conf::database_backend = "testdb";
Conf::database_poolsize = 1;
Conf::xml_basedir = "../xml";
Environment env;
// Make sure we start out on an empty session directory.
Conf::session_path = "/tmp/test_session";
while(mkdir(Conf::session_path.c_str(), 0777) == -1 && errno == EEXIST) {
Conf::session_path += "X";
}
Session *s1 = env.sessions.newSession(PID, TMPL);
TEST_EXCEPTION(env.sessions.newSession(PID, TMPL),
Sessions::SessionAlreadyActive, "Session should be 'locked'");
Session *s2 = env.sessions.session(s1->id());
TEST_EQUAL(s1, s2, "They should be the same session.");
env.sessions.takeSession(s1->id());
Session *s3 = env.sessions.newSession(PID, TMPL);
TEST_NOTEQUAL_STR(s1->id(), s3->id(), "Testing if IDs are unique.");
TEST_EQUAL_INT(env.sessions.size(), 1, "Testing if size is 1.");
std::string id = s3->id();
env.sessions.deleteSession(id);
Session *s4 = env.sessions.session(id);
TEST_EQUAL(s4, NULL, "No session should be returned.");
TEST_EQUAL_INT(env.sessions.size(), 0, "Testing if size is 0.");
Session *s5 = env.sessions.newSession(PID, TMPL);
TEST_NOTEQUAL(s5, NULL, "A new session was created.");
{
Transaction transaction;
transaction.cpr = PID;
transaction.user = "me";
Commit commit;
commit.fields["field1"] = "hello";
commit.fields["field2"] = "world";
commit.templ = TMPL;
Macro macro;
macro.attributes["version"] = "1.0";
macro.attributes["name"] = "somemacro";
s5->commitMacro(transaction, commit, macro);
}
id = s5->id();
env.sessions.store();
// Resume session using session id.
Session *s6 = env.sessions.session(id);
TEST_NOTEQUAL(s6, NULL, "We did get one right?");
TEST_EQUAL_STR(s6->id(), id, "Did we get the stored session?");
{
Transaction transaction;
transaction.cpr = PID;
transaction.user = "me";
Commit commit;
commit.fields["field1"] = "hello";
commit.fields["field2"] = "world";
commit.templ = TMPL;
Macro macro;
macro.attributes["version"] = "1.0";
macro.attributes["name"] = "somemacro";
s6->commitMacro(transaction, commit, macro);
}
s6->nocommit();
env.sessions.store();
// Resume session using patientid/template tupple.
Session *s7 = env.sessions.newSession(PID, TMPL);
TEST_NOTEQUAL(s7, NULL, "We did get one right?");
TEST_EQUAL_STR(s7->id(), id, "Did we get the stored session?");
// Get an existing session and test locking.
Session *s8 = env.sessions.session(s7->id());
s7->lock();
TEST_FALSE(s8->mutex.trylock(), "Session should be locked.")
s7->unlock();
{
Transaction transaction;
transaction.cpr = PID;
transaction.user = "me";
Commit commit;
commit.fields["field1"] = "hello";
commit.fields["field2"] = "world";
commit.templ = TMPL;
Macro macro;
macro.attributes["version"] = "1.0";
macro.attributes["name"] = "somemacro";
s7->commitMacro(transaction, commit, macro);
}
TEST_FALSE(s7->idle(), "Session is active.");
TEST_FALSE(s7->isReadonly(), "Not read only session.");
s7->setIdle(true); // Force idle
TEST_TRUE(s7->idle(), "Session is idle.");
Session *s9 = env.sessions.newSession(PID, TMPL"empty");
TEST_TRUE(s9->isReadonly(), "Read only session.");
TEST_FALSE(s9->idle(), "Readonly session is not idle.");
s9->setIdle(true); // Force idle (no effect)
TEST_FALSE(s9->idle(), "Readonly session still not idle.");
TEST_END;
#endif/*TEST_SESSION*/
|