From 357bd8f7b87082951cb8cedf50b9a26689a792dd Mon Sep 17 00:00:00 2001 From: mguesdon Date: Wed, 25 Jun 2003 15:38:01 +0000 Subject: [PATCH] * GSWeb.framework/GSWSessionStore.m/.h: o partial rewriting of checkin/checkout code git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gsweb/trunk@17027 72102866-910b-0410-8b05-ffd578937521 --- GSWeb.framework/GSWSessionStore.h | 13 +- GSWeb.framework/GSWSessionStore.m | 195 +++++++++++++----------------- 2 files changed, 88 insertions(+), 120 deletions(-) diff --git a/GSWeb.framework/GSWSessionStore.h b/GSWeb.framework/GSWSessionStore.h index 8aac37a..6165ef4 100644 --- a/GSWeb.framework/GSWSessionStore.h +++ b/GSWeb.framework/GSWSessionStore.h @@ -35,18 +35,13 @@ @interface GSWSessionStore : NSObject { - NSMutableSet* _usedIDs; NSRecursiveLock* _lock; GSWSessionTimeOutManager* _timeOutManager; #ifndef NDEBUG int _lockn; #endif -//TODO void* sessionCheckedInCondition; }; --(void)dealloc; --(id)init; - -(GSWSession*)removeSessionWithID:(NSString*)aSessionID; -(GSWSession*)restoreSessionWithID:(NSString*)aSessionID request:(GSWRequest*)aRequest; @@ -56,10 +51,6 @@ -(void)checkInSessionForContext:(GSWContext*)aContext; -(void)_checkInSessionForContext:(GSWContext*)aContext; --(GSWSession*)_checkOutSessionWithID:(NSString*)aSessionID - request:(GSWRequest*)aRequest; --(void)_checkinSessionID:(NSString*)aSessionID; --(void)_checkoutSessionID:(NSString*)aSessionID; -(void)unlock; -(BOOL)tryLock; -(void)lock; @@ -82,13 +73,13 @@ secure:(BOOL)flag; -(void)saveSession:(GSWSession*)session; @end */ - +/* //==================================================================== @interface GSWSessionStore (GSWSessionStoreA) -(BOOL)_isSessionIDCheckedOut:(NSString*)aSessionID; @end - +*/ //==================================================================== @interface GSWSessionStore (GSWSessionStoreB) -(void)_validateAPI; diff --git a/GSWeb.framework/GSWSessionStore.m b/GSWeb.framework/GSWSessionStore.m index 7e75598..1b623c2 100644 --- a/GSWeb.framework/GSWSessionStore.m +++ b/GSWeb.framework/GSWSessionStore.m @@ -33,6 +33,8 @@ RCS_ID("$Id$") #include "GSWeb.h" +#include +#include //==================================================================== @implementation GSWSessionStore @@ -43,7 +45,6 @@ RCS_ID("$Id$") LOGObjectFnStart(); if ((self=[super init])) { - _usedIDs=[NSMutableSet new]; _lock=[NSRecursiveLock new]; _timeOutManager=[GSWSessionTimeOutManager new]; NSDebugMLLog(@"sessions",@"GSWSessionStore self=%p class=%@",self,[self class]); @@ -60,8 +61,6 @@ RCS_ID("$Id$") -(void)dealloc { GSWLogC("Dealloc GSWSessionStore"); - GSWLogC("Dealloc GSWSessionStore: usedIDs"); - DESTROY(_usedIDs); GSWLogC("Dealloc GSWSessionStore: lock"); DESTROY(_lock); GSWLogC("Dealloc GSWSessionStore: timeOutManager"); @@ -96,51 +95,87 @@ RCS_ID("$Id$") [self subclassResponsibility: _cmd]; }; + //-------------------------------------------------------------------- -(GSWSession*)checkOutSessionWithID:(NSString*)aSessionID request:(GSWRequest*)aRequest { GSWSession* session=nil; - BOOL sessionUsed=YES; - NSDate* limit=[NSDate dateWithTimeIntervalSinceNow:60]; + BOOL isCheckedOut=YES; + int tryCount=0; + int startTime=(int)time(NULL); + int currentTime=startTime; + int expirationTime=60; // default exp time is 60s //OK LOGObjectFnStart(); NSDebugMLLog(@"sessions",@"aSessionID=%@",aSessionID); - NSDebugMLLog(@"sessions",@"usedIDs=%@",_usedIDs); NSDebugMLLog(@"sessions",@"self=%@",self); - NSDebugMLLog(@"sessions",@"[NSDate date]=%@",[NSDate date]); - NSDebugMLLog(@"sessions",@"limit=%@",limit); - NSDebugMLLog(@"sessions",@"[[NSDate date]compare:limit]==NSOrderedAscending=%d", - (int)([[NSDate date]compare:limit]==NSOrderedAscending)); - - while(!session && sessionUsed && [[NSDate date]compare:limit]==NSOrderedAscending) + do { - BOOL tmpUsed=NO; + tryCount++; if ([self tryLock]) { - tmpUsed=[_usedIDs containsObject:aSessionID]; - if (tmpUsed) - [self unlock]; - else + GSWSessionTimeOut* entry=[_timeOutManager sessionTimeOutForSessionID:aSessionID]; + NS_DURING { - NS_DURING + expirationTime=(int)[entry sessionTimeOut];//seconds + //NSLog(@"expirationTime=%d",(int)expirationTime); + isCheckedOut=[entry isCheckedOut]; // See if session is used + //NSLog(@"aSessionID=%@ isCheckedOut=%d",aSessionID,(int)isCheckedOut); + //NSLog(@"entry=%@",(int)entry); + + // if it is not used, restore it and so on.... + if (!isCheckedOut) { - session=[self _checkOutSessionWithID:aSessionID + session=[self restoreSessionWithID:aSessionID request:aRequest]; - } - NS_HANDLER - { - NSDebugMLLog(@"sessions",@"Can't checkOutSessionID=%@",aSessionID); - if ([[localException name]isEqualToString:@"GSWSessionStoreException"]) - sessionUsed=YES; - } - NS_ENDHANDLER; + if (session) + { + //NSLog(@"CheckOut: %@. SessionID:%@",aSessionID,[session sessionID]); + + // If sessionID has Changed, re-find entry + if (![[session sessionID] isEqualToString:aSessionID]) + { + aSessionID=[session sessionID]; + entry=[_timeOutManager sessionTimeOutForSessionID:aSessionID]; + }; + isCheckedOut=[entry isCheckedOut]; + if (!isCheckedOut) + { + [session _createAutoreleasePool]; + [entry setIsCheckedOut:YES]; + }; + }; + }; + } + NS_HANDLER + { + NSDebugMLLog(@"sessions",@"Can't checkOutSessionID=%@",aSessionID); + localException=ExceptionByAddingUserInfoObjectFrameInfo0(localException, + @"In checkOutSessionWithID:request:"); + LOGException(@"%@ (%@)",localException,[localException reason]); + // Put the previous check in/out state + [entry setIsCheckedOut:isCheckedOut]; [self unlock]; - sessionUsed=NO; - NSDebugMLLog(@"sessions",@"session=%@",session); - }; + [localException raise]; + } + NS_ENDHANDLER; + [self unlock]; + currentTime=(int)time(NULL); + if (isCheckedOut) // Session already check out. Wait... + { + usleep(10000); // sleep for 10 ms + if (tryCount%100==0) + NSLog(@"Try check out %@ for %d seconds (tryCount=%d)",aSessionID,(currentTime-startTime),tryCount); + }; + } + else + { + NSLog(@"Try lock failed"); + currentTime=(int)time(NULL); }; - }; + } + while(isCheckedOut && (currentTime-startTime)