00001
00002
00003
00004
00005
00006
00007
00008
00009 #import "WIRemoting.h"
00010
00011 @implementation RMSession
00012
00013 @synthesize call, authenticated;
00014 @dynamic semaphore;
00015
00016 #pragma mark session management
00017
00018 - (id) initWithAuthenticator: (id<RMAuthenticator>) newAuthenticator
00019 call:(RMCall*) newCall
00020 {
00021 self = [super init];
00022 if (nil != self) {
00023
00024 authenticator = [newAuthenticator retain];
00025 call = [newCall retain];
00026 delegateChain = [[NSMutableArray alloc] initWithCapacity:1];
00027
00028
00029 authenticated = NO;
00030
00031 }
00032 return self;
00033 }
00034
00035 - (void) dealloc
00036 {
00037 [authenticator release];
00038 [call release];
00039 [delegateChain release];
00040 [super dealloc];
00041 }
00042
00043 - (void) close {
00044 }
00045
00046 - (void) addDelegate: (id<RMResultDelegate>) delegate
00047 {
00048 @synchronized(self.semaphore) {
00049 [delegateChain addObject:delegate];
00050 }
00051 }
00052
00053 - (void) removeDelegate: (id<RMResultDelegate>) delegate
00054 {
00055 @synchronized(self.semaphore) {
00056 [delegateChain removeObject:delegate];
00057 }
00058 }
00059
00060 - (id) semaphore
00061 {
00062 return self;
00063 }
00064
00065 #pragma mark authentication
00066
00067 - (void) authenticate
00068 {
00069 @synchronized(self.call.semaphore) {
00070 [self.call addDelegate:self];
00071 [authenticator authenticateWithCall:self.call];
00072 [self.call removeDelegate:self];
00073 }
00074 }
00075
00076 - (void)finished:(RMResponse *)response
00077 {
00078 BOOL auth = NO;
00079
00080 if ([authenticator
00081 respondsToSelector:@selector(authenticateResponseString:)]) {
00082 if ([authenticator authenticateResponseString:[response responseString]]) {
00083 auth = YES;
00084 }
00085 }
00086
00087 if ([authenticator
00088 respondsToSelector:@selector(authenticateResponseData:)]) {
00089 if ([authenticator authenticateResponseData:[response responseData]]) {
00090 auth = YES;
00091 }
00092 }
00093
00094 @synchronized(self.semaphore) {
00095 if (auth) {
00096 authenticated = YES;
00097
00098 for (id<RMResultDelegate> delegate in delegateChain) {
00099 [delegate finished:response];
00100 }
00101 }
00102 else {
00103 authenticated = NO;
00104
00105 NSError *error =
00106 [NSError errorWithDomain:NSURLErrorDomain
00107 code:EINVAL
00108 userInfo:
00109 [NSDictionary
00110 dictionaryWithObjectsAndKeys:
00111 @"Authentication error.", NSLocalizedDescriptionKey,
00112 @"Unable to authenticate the session.", NSLocalizedFailureReasonErrorKey,
00113 nil]];
00114 for (id<RMResultDelegate> delegate in delegateChain) {
00115 [delegate failed:response error:error];
00116 }
00117 }
00118 }
00119 }
00120
00121 - (void)failed:(RMResponse *)response error:(NSError *)error
00122 {
00123 @synchronized(self.semaphore) {
00124 for (id<RMResultDelegate> delegate in delegateChain) {
00125 [delegate failed:response error:error];
00126 }
00127 }
00128 }
00129
00130 #pragma mark call
00131
00132 - (BOOL)call:(NSString*) method
00133 arguments:(NSDictionary*) arguments
00134 {
00135 return [call call:method
00136 arguments:arguments
00137 protocol:[authenticator callProtocol]];
00138 }
00139
00140 - (BOOL)call:(NSString*) method
00141 arguments:(NSDictionary*) arguments
00142 delegate:(id) delegate
00143 {
00144 return [call call:method
00145 arguments:arguments
00146 delegate:delegate
00147 protocol:[authenticator callProtocol]];
00148 }
00149
00150 @end