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
|
#include <core.h>
#include <session.h>
#include <fcntl.h>
#include <fileUtils.h>
#include <sys.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <factory.impl.h>
#include <boost/filesystem/operations.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <Ice/OutputStream.h>
#include <Ice/InputStream.h>
#include "Ice/Initialize.h"
namespace IceSpider {
class FileSessions : public Plugin, public SessionManager {
public:
FileSessions(Ice::CommunicatorPtr c, Ice::PropertiesPtr p) :
ic(c),
root(p->getProperty("IceSpider.FileSessions.Path")),
duration(p->getPropertyAsIntWithDefault("IceSpider.FileSessions.Duration", 3600))
{
if (!root.empty())
if (!boost::filesystem::exists(root))
boost::filesystem::create_directories(root);
}
~FileSessions()
{
removeExpired();
}
SessionPtr createSession(const ::Ice::Current &) override
{
auto s = std::make_shared<Session>();
s->id = boost::lexical_cast<std::string>(boost::uuids::random_generator()());
s->duration = duration;
save(s);
return s;
}
SessionPtr getSession(const ::std::string id, const ::Ice::Current & current) override
{
auto s = load(id);
if (s && isExpired(s)) {
destroySession(id, current);
return NULL;
}
return s;
}
void updateSession(const SessionPtr s, const ::Ice::Current &) override
{
save(s);
}
void destroySession(const ::std::string id, const ::Ice::Current &) override
{
try {
boost::filesystem::remove(root / id);
}
catch (const std::exception & e) {
throw SessionError(e.what());
}
}
private:
void save(SessionPtr s)
{
s->lastUsed = time(NULL);
Ice::OutputStream buf(ic);
buf.write(s);
auto range = buf.finished();
AdHoc::FileUtils::FileHandle f(root / s->id, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
sysassert(flock(f.fh, LOCK_EX), -1);
sysassert(pwrite(f.fh, range.first, range.second - range.first, 0), -1);
sysassert(ftruncate(f.fh, range.second - range.first), -1);
sysassert(flock(f.fh, LOCK_UN), -1);
}
SessionPtr load(const std::string & id)
{
auto path = root / id;
if (!boost::filesystem::exists(path)) return NULL;
try {
AdHoc::FileUtils::MemMap f(path);
sysassert(flock(f.fh, LOCK_SH), -1);
auto fbuf = (Ice::Byte *)f.data;
Ice::InputStream buf(ic, std::make_pair(fbuf, fbuf + f.getStat().st_size));
SessionPtr s;
buf.read(s);
sysassert(flock(f.fh, LOCK_UN), -1);
return s;
}
catch (const AdHoc::SystemException & e) {
if (e.errNo == ENOENT) {
return NULL;
}
throw;
}
}
void removeExpired()
{
if (root.empty() || !boost::filesystem::exists(root)) return;
boost::filesystem::directory_iterator di(root);
while (di != boost::filesystem::directory_iterator()) {
auto s = load(di->path().leaf().string());
if (s && isExpired(s)) {
destroySession(s->id, Ice::Current());
}
di++;
}
}
bool isExpired(SessionPtr s)
{
return (s->lastUsed + s->duration < time(NULL));
}
template<typename R, typename ER>
R sysassert(R rtn, ER ertn)
{
if (rtn == ertn) {
fprintf(stderr, "%s\n", strerror(errno));
throw SessionError(strerror(errno));
}
return rtn;
}
Ice::CommunicatorPtr ic;
const boost::filesystem::path root;
const Ice::Int duration;
};
}
NAMEDFACTORY("IceSpider-FileSessions", IceSpider::FileSessions, IceSpider::PluginFactory);
|