File: WOServerSessionStore.m

package info (click to toggle)
sope 5.12.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,588 kB
  • sloc: objc: 169,223; sh: 3,823; ansic: 3,409; javascript: 446; python: 318; makefile: 185
file content (302 lines) | stat: -rw-r--r-- 7,862 bytes parent folder | download | duplicates (9)
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
/*
  Copyright (C) 2000-2005 SKYRIX Software AG

  This file is part of SOPE.

  SOPE is free software; you can redistribute it and/or modify it under
  the terms of the GNU Lesser General Public License as published by the
  Free Software Foundation; either version 2, or (at your option) any
  later version.

  SOPE 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 Lesser General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with SOPE; see the file COPYING.  If not, write to the
  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  02111-1307, USA.
*/

#include <NGObjWeb/WOSessionStore.h>

/*
  The default session store. It stores all the sessions in memory
  (it's basically a simple hashmap of the session-id to the session object).

  If the application goes down, all sessions will go down - this store
  doesn't provide "session-failover".
*/

@interface WOServerSessionStore : WOSessionStore
{
  NSMapTable *idToSession;
  NSMapTable *activeSessions;
}
@end

#include <NGObjWeb/WOContext.h>
#include <NGObjWeb/WOApplication.h>
#include <NGObjWeb/WOSession.h>
#include "common.h"

/* hh: moved in here from application, check whether it's required ... */
@interface WOSessionInfo : NSObject
{
@private
  NSString *sessionID;
  NSDate   *timeoutDate;
}

+ (WOSessionInfo *)infoForSession:(WOSession *)_session;

- (NSString *)sessionID;
- (NSDate *)timeoutDate;

@end

@implementation WOServerSessionStore

static BOOL logExpiredSessions = NO;

- (id)init {
  if ((self = [super init])) {
    self->idToSession = NSCreateMapTable(NSObjectMapKeyCallBacks,
                                         NSObjectMapValueCallBacks,
                                         128);
    self->activeSessions = NSCreateMapTable(NSObjectMapKeyCallBacks,
                                            NSObjectMapValueCallBacks,
                                            128);
    self->checkedOutSessions = [[NSMutableSet allocWithZone:[self zone]]
                                              initWithCapacity:64];
    
    if ([[[NSUserDefaults standardUserDefaults]
                          objectForKey:@"WORunMultithreaded"]
                          boolValue]) {
      self->lock         = [[NSRecursiveLock allocWithZone:[self zone]] init];
      self->checkoutLock = [[NSConditionLock allocWithZone:[self zone]]
                                             initWithCondition:0];
    }
  }
  return self;
}

- (void)dealloc {
  if (self->activeSessions) {
    NSFreeMapTable(self->activeSessions);
    self->activeSessions = NULL;
  }
  if (self->idToSession) {
    NSFreeMapTable(self->idToSession);
    self->idToSession = NULL;
  }
  [self->checkedOutSessions release];
  [self->checkoutLock       release];
  [self->lock               release];
  [super dealloc];
}

/* accessors */

- (int)activeSessionsCount {
  int count;
  
  [self->lock lock];
  count = NSCountMapTable(self->idToSession);
  [self->lock unlock];

  return count;
}

/* store */

- (void)saveSessionForContext:(WOContext *)_context {
  if (![_context hasSession])
    return;
  
  [self->lock lock];
  {
    WOSession *sn = [_context session];
      
    if ([sn isTerminating]) {
      sn = [sn retain];
        
      NSMapRemove(self->idToSession,    [sn sessionID]);
      NSMapRemove(self->activeSessions, [sn sessionID]);
        
      NSLog(@"session %@ terminated at %@ ..",
	    [sn sessionID], [NSCalendarDate calendarDate]);
        
      [sn release]; sn = nil;
    }
    else {
      WOSessionInfo *info;
        
      NSMapInsert(self->idToSession, [sn sessionID], sn);
        
      info = [WOSessionInfo infoForSession:sn];
      NSMapInsert(self->activeSessions, [sn sessionID], info);
    }
  }
  [self->lock unlock];
}

- (id)restoreSessionWithID:(NSString *)_sid request:(WORequest *)_request {
  WOSession *session = nil;

  if ([_sid length] == 0)
    return nil;
  
  if (![_sid isKindOfClass:[NSString class]]) {
    [self warnWithFormat:@"%s: got invalid session id (expected string !): %@",
            __PRETTY_FUNCTION__, _sid];
    return nil;
  }
  
  if ([_sid isEqualToString:@"expired"])
    return nil;

  [self->lock lock];
  session = NSMapGet(self->idToSession, _sid);
  [self->lock unlock];

  if (logExpiredSessions) {
    if (session == nil)
      [self logWithFormat:@"session with id %@ expired.", _sid];
  }

  return session;
}

/* termination */

- (void)sessionExpired:(NSString *)_sessionID {
  [self->lock lock];
  NSMapRemove(self->idToSession, _sessionID);
  [self->lock unlock];
}

- (void)sessionTerminated:(WOSession *)_session {
  _session = [_session retain];
  [self->lock lock];
  NSMapRemove(self->idToSession, [_session sessionID]);
  [self->lock unlock];
  [_session release];
  
  [[WOApplication application]
                  logWithFormat:
                    @"WOServerSessionStore: session %@ terminated.",
                    [_session sessionID]];
}

/* expiration check */

- (void)performExpirationCheck:(NSTimer *)_timer {
  NSNotificationCenter *nc;
  NSMutableArray  *timedOut = nil;
  NSMapEnumerator e;
  NSString        *sid  = nil;
  WOSessionInfo   *info = nil;
  NSDate          *now;
  unsigned cnt, count;
    
  //NSLog(@"%s: perform expiration check ...", __PRETTY_FUNCTION__);
  
  if (self->activeSessions == NULL)
    count = 0;
  else
    count = NSCountMapTable(self->activeSessions);
  
  if (!(count > 0 && (self->activeSessions != NULL)))
    return;
  
  e   = NSEnumerateMapTable(self->activeSessions);
  now = [NSDate date];
      
  /* search for expired sessions */
  while (NSNextMapEnumeratorPair(&e, (void **)&sid, (void **)&info)) {
    NSDate *timeOutDate = [info timeoutDate];
    
    if (timeOutDate == nil) continue;

    if ([now compare:timeOutDate] != NSOrderedAscending) {
        [self logWithFormat:@"session %@ expired at %@.", sid, now];
          
        if (timedOut == nil)
          timedOut = [NSMutableArray arrayWithCapacity:32];
        [timedOut addObject:info];
    }
  }
      
  /* Expire sessions */
  if (!timedOut)
    return;

  nc = [NSNotificationCenter defaultCenter];
        
  for (cnt = 0, count = [timedOut count]; cnt < count; cnt++) {
    NSString *sid;
          
    info = [timedOut objectAtIndex:cnt];
    sid  = [[info sessionID] copy];
          
    NSMapRemove(self->activeSessions, sid);
    NSMapRemove(self->idToSession,    sid);
          
    [nc postNotificationName:WOSessionDidTimeOutNotification
	object:sid];
    
    [sid release];
  }
}

/* description */

- (NSString *)description {
  return [NSString stringWithFormat:@"<%@[0x%p]: active=%i>",
                     NSStringFromClass([self class]), self,
                     [self activeSessionsCount]
                   ];
}

@end /* WOServerSessionStore */

@implementation WOSessionInfo

- (id)initWithSession:(WOSession *)_session {
  self->sessionID = RETAIN([_session sessionID]);

  if ([_session respondsToSelector:@selector(timeoutDate)]) {
    self->timeoutDate = [(id)_session timeoutDate];
  }
  else {
    NSTimeInterval timeOut = [_session timeOut];
    
    self->timeoutDate = (timeOut > 0.0)
      ? [NSDate dateWithTimeIntervalSinceNow:(timeOut + 1.0)]
      : [NSDate distantFuture];
  }
  self->timeoutDate = RETAIN(self->timeoutDate);
  
  return self;
}

+ (WOSessionInfo *)infoForSession:(WOSession *)_session {
  return AUTORELEASE([[self alloc] initWithSession:_session]);
}

- (void)dealloc {
  [self->sessionID   release];
  [self->timeoutDate release];
  [super dealloc];
}

- (NSString *)sessionID {
  return self->sessionID;
}
- (NSDate *)timeoutDate {
  return self->timeoutDate;
}

@end /* WOSessionInfo */