/[glsr]/trunk/harmonious/_sessionutils.py
Gentoo

Diff of /trunk/harmonious/_sessionutils.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 108 Revision 109
1__id__ = '$Id$' 1__id__ = '$Id$'
2__modulename__ = '_sessionutils' 2__modulename__ = '_sessionutils'
3 3
4import os
5import sys
4import sha 6import sha
5import hmac 7import hmac
8import cPickle
6import threading 9import threading
7from time import time 10from time import time
8from random import Random 11from random import Random
9from types import BooleanType 12from types import BooleanType
10 13
11from harmonious import server 14from harmonious import server
12 15
13class Session: 16class Session(object):
14 17
15 def __init__(self): 18 def __init__(self):
16 19
17 self.__id = '' 20 self.__id = ''
18 self.attrs = {} 21 self.attrs = {}
19 self.__persistent = False 22 self.__persistent = False
20 self.expires = '' 23 self.expires = ''
21 self.__removed = False 24 self.__removed = False
25 self.__modified = False
26 self.__end_init = False
27
28 def __setattr__(self, name, value):
29
30 if hasattr(self, '_Session__end_init'):
31 if self.__end_init:
32 if not self.__modified:
33 self.__dict__['_Session__modified'] = True
34
35 object.__setattr__(self, name, value)
36
37 def _end_init(self):
38
39 self.__end_init = True
22 40
23 def _set_id(self, id): 41 def _set_id(self, id):
24 42
25 self.__id = id 43 self.__id = id
26 44
27 def get_id(self): 45 def get_id(self):
28 46
29 return self.__id 47 return self.__id
30 48
49 def is_modified(self):
50
51 return self.__modified
52
31 def set_persistent(self, flag): 53 def set_persistent(self, flag):
32 54
33 if type(flag) == BooleanType: 55 if type(flag) == BooleanType:
34 56
35 self.__persistent = flag 57 self.__persistent = flag
36 58
37 def get_persistent(self): 59 def is_persistent(self):
38 60
39 return self.__persistent 61 return self.__persistent
40 62
41 def set_removed(self): 63 def set_removed(self):
42 64
43 self.__removed = True 65 self.__removed = True
44 66
45 server._sessionStore.remove_session(self.__id) 67 server._sessionStore.remove_session(self.__id)
46 68
47 def get_removed(self): 69 def is_removed(self):
48 70
49 return self.__removed 71 return self.__removed
50 72
51class SessionStorage: 73class SessionStorage:
52 74
53 def __init__(self, type): 75 def __init__(self, type):
54 76
55 # type is ['ram', 'file', 'cookie'] 77 # type is ['ram', 'flush2disk']
56 # TODO: Implement locking. An instance of this class with be shared across all threads
57 # TODO: We need a cleanup method called every n houres 78 # TODO: We need a cleanup method called every n houres
58 79
59 self.__type = str(type).lower() 80 self.__type = str(type).lower()
60 self.__lock = threading.Lock() 81 self.__lock = threading.Lock()
61
62 if self.__type == 'ram':
63
64 self.__store = {} 82 self.__store = {}
83
84 if self.__type == 'flush2disk':
85
86 if not os.path.exists(server.config.session['StoreDir']):
87
88 print >> sys.stderr, "Harmonious: StoreDir '%s' does not exist" % server.config.session['StoreDir']
89 sys.exit(1)
90
91 if not os.path.isdir(server.config.session['StoreDir']):
92
93 print >> sys.stderr, "Harmonious: StoreDir '%s' is not a directory" % server.config.session['StoreDir']
94 sys.exit(1)
95
96 def load_store(self):
97
98 if self.__type == 'flush2disk':
99
100 for file in os.listdir(server.config.session['StoreDir']):
101
102 if file.endswith('.session'):
103
104 try:
105 session_dict = cPickle.load(open(os.path.join(server.config.session['StoreDir'], file), 'r'))
106 except IOError, e:
107 print >> sys.stderr, "Harmonious: Failed to load session file '%s': %s" % (file, e)
108 sys.exit(1)
109
110 self.__store[session_dict.keys()[0]] = session_dict[session_dict.keys()[0]]
111
112 def save_store(self):
113
114 if self.__type == 'flush2disk':
115
116 for session in self.__store.iterkeys():
117
118 try:
119 cPickle.dump({session: self.__store[session]}, \
120 open(os.path.join(server.config.session['StoreDir'], "%s.session" % session), 'w'))
121 except IOError, e:
122 print >> sys.stderr, "Harmonious: Failed to save session file '%s.session': %s" % (session, e)
123 sys.exit(1)
65 124
66 def has_expired(self, sessionId): 125 def has_expired(self, sessionId):
67 126
68 if not self.has_session(sessionId): 127 if not self.has_session(sessionId):
69 128
101 160
102 self.__lock.acquire() 161 self.__lock.acquire()
103 self.__store[sessionId] = {'expires': 0, 'attrs': {}, 'persistent': False} 162 self.__store[sessionId] = {'expires': 0, 'attrs': {}, 'persistent': False}
104 self.__store[sessionId]['expires'] = int(time() + (expires * 60)) 163 self.__store[sessionId]['expires'] = int(time() + (expires * 60))
105 self.__lock.release() 164 self.__lock.release()
106
107 if self.__type == 'file':
108
109 pass
110 # self.__flush_file()
111 165
112 def set_persistent(self, sessionId, flag): 166 def set_persistent(self, sessionId, flag):
113 167
114 if self.has_session(sessionId): 168 if self.has_session(sessionId):
115 self.__lock.acquire() 169 self.__lock.acquire()
116 self.__store[sessionId]['persistent'] = flag 170 self.__store[sessionId]['persistent'] = flag
117 self.__lock.release() 171 self.__lock.release()
118 172
119 def load_session(self, sessionId): 173 def load_session(self, sessionId):
120 174
121 if self.__type == 'ram':
122
123 self.__lock.acquire() 175 self.__lock.acquire()
124 if self.__store.has_key(sessionId): 176 if self.__store.has_key(sessionId):
125 177
126 self.__lock.release() 178 self.__lock.release()
127 if self.has_expired(sessionId): 179 if self.has_expired(sessionId):
128
129 self.__lock.acquire()
130 self.__store.pop(sessionId)
131 self.__lock.release()
132 return None
133 180
134 self.__lock.acquire() 181 self.__lock.acquire()
135 x = self.__store[sessionId] 182 self.__store.pop(sessionId)
136 self.__lock.release()
137 return x
138
139 else:
140
141 self.__lock.release() 183 self.__lock.release()
142 return None 184 return None
143 185
186 self.__lock.acquire()
187 x = self.__store[sessionId]
188 self.__lock.release()
189 return x
190
191 else:
192
193 self.__lock.release()
194 return None
195
144 def update_session(self, sessionId, **args): 196 def update_session(self, sessionId, **args):
145 197
146 self.__lock.acquire() 198 self.__lock.acquire()
147 199
148 if sessionId in self.__store.keys(): 200 if sessionId in self.__store.keys():

Legend:
Removed from v.108  
changed lines
  Added in v.109

  ViewVC Help
Powered by ViewVC 1.1.20