-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMPConnection.m
More file actions
199 lines (173 loc) · 5.96 KB
/
MPConnection.m
File metadata and controls
199 lines (173 loc) · 5.96 KB
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
//
// MPConnection.m
// MPConnectionAgent
//
// Created by Matt Patenaude on 1/3/09.
// Copyright (C) 2008 - 10 Matt Patenaude.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import "MPConnection.h"
static NSMutableArray *__MPConnectionActiveList = nil;
@interface MPConnection()
// Private methods
- (void)_beginRequestWithHandlerInvocation:(NSInvocation *)theInvocation userInfo:(NSDictionary *)userInfo;
- (NSDictionary *)_runForConnection:(NSURLConnection *)theConnection;
@end
@implementation MPConnection
#pragma mark Initializers
+ (void)initialize
{
__MPConnectionActiveList = [[NSMutableArray alloc] init];
}
- (id)init
{
return [self initWithRequest:nil];
}
- (id)initWithRequest:(NSURLRequest *)aRequest
{
if (self = [super init])
{
if (aRequest == nil)
{
NSLog(@"MPConnection: warning, cannot init with nil request, nil was returned");
[self release];
return nil;
}
theRequest = [aRequest retain];
requestRuns = [[NSMutableArray alloc] init];
}
return self;
}
+ (id)connectionWithRequest:(NSURLRequest *)aRequest
{
return [[[self alloc] initWithRequest:aRequest] autorelease];
}
#pragma mark Deallocator
- (void)dealloc
{
[theRequest release];
NSEnumerator *rns = [requestRuns objectEnumerator];
NSDictionary *run;
while (run = [rns nextObject])
[[run objectForKey:@"NSURLConnection"] cancel];
[requestRuns release];
[super dealloc];
}
#pragma mark Properties
- (NSURLRequest *)request
{
return theRequest;
}
- (id)delegate
{
return delegate;
}
- (void)setDelegate:(id)theDelegate
{
delegate = theDelegate;
}
#pragma mark Methods
- (void)beginRequestWithHandlerInvocation:(NSInvocation *)theInvocation
{
[self _beginRequestWithHandlerInvocation:theInvocation userInfo:nil];
}
- (void)beginRequestWithTarget:(id)theTarget selector:(SEL)theSelector userInfo:(NSDictionary *)theUserInfo
{
if (!theSelector)
{
NSLog(@"MPConnection: warning, nil selector specified, cannot begin request, cancelling");
return;
}
NSInvocation *theInvocation = [NSInvocation invocationWithMethodSignature:[theTarget methodSignatureForSelector:theSelector]];
[theInvocation setTarget:theTarget];
[theInvocation setSelector:theSelector];
[theInvocation setArgument:&theUserInfo atIndex:3];
[theInvocation retainArguments];
[self _beginRequestWithHandlerInvocation:theInvocation userInfo:theUserInfo];
}
#pragma mark Private methods
- (void)_beginRequestWithHandlerInvocation:(NSInvocation *)theInvocation userInfo:(NSDictionary *)userInfo
{
if (!theInvocation)
{
NSLog(@"MPConnection: warning, nil invocation provided, cannot begin request, cancelling");
return;
}
if (![__MPConnectionActiveList containsObject:self])
[__MPConnectionActiveList addObject:self];
NSMutableDictionary *info = [[NSMutableDictionary alloc] init];
[info setObject:theInvocation forKey:@"NSInvocation"];
[info setObject:[NSMutableData data] forKey:@"NSMutableData"];
if (userInfo)
[info setObject:userInfo forKey:@"userInfo"];
[requestRuns addObject:info];
NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:theRequest delegate:self];
[info setObject:theConnection forKey:@"NSURLConnection"];
[info release];
}
- (NSDictionary *)_runForConnection:(NSURLConnection *)theConnection
{
NSEnumerator *rns = [requestRuns objectEnumerator];
NSDictionary *run;
while (run = [rns nextObject])
{
if ([run objectForKey:@"NSURLConnection"] == theConnection)
return run;
}
return nil;
}
#pragma mark NSURLConnection delegate methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSDictionary *run = [self _runForConnection:connection];
[[run objectForKey:@"NSMutableData"] setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSDictionary *run = [self _runForConnection:connection];
[[run objectForKey:@"NSMutableData"] appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSDictionary *run = [self _runForConnection:connection];
if ([delegate respondsToSelector:@selector(connection:didFailWithError:userInfo:)])
{
NSDictionary *userInfo = ([[run allKeys] containsObject:@"userInfo"]) ? [run objectForKey:@"userInfo"] : nil;
[delegate connection:connection didFailWithError:error userInfo:userInfo];
}
// clean-up
[requestRuns removeObject:run];
if ([requestRuns count] < 1)
[__MPConnectionActiveList removeObject:self];
NSLog(@"Uh oh, an error: %@ %@", [error localizedDescription], [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSDictionary *run = [self _runForConnection:connection];
NSData *data = [run objectForKey:@"NSMutableData"];
NSInvocation *theInvocation = [run objectForKey:@"NSInvocation"];
[theInvocation setArgument:&data atIndex:2];
[theInvocation invoke];
// clean-up
[requestRuns removeObject:run];
if ([requestRuns count] < 1)
[__MPConnectionActiveList removeObject:self];
}
@end