Skip to content

Commit

Permalink
merge of cancel support and json
Browse files Browse the repository at this point in the history
  • Loading branch information
jjburka committed Jan 21, 2009
2 parents 395ab49 + aebd8d9 commit 84ab1bb
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Classes/lib/Connection.h
Expand Up @@ -17,4 +17,7 @@
+ (Response *)get:(NSString *)url;
+ (Response *)put:(NSString *)body to:(NSString *)url withUser:(NSString *)user andPassword:(NSString *)password;
+ (Response *)delete:(NSString *)url withUser:(NSString *)user andPassword:(NSString *)password;

+ (void) cancelAllActiveConnections;

@end
54 changes: 48 additions & 6 deletions Classes/lib/Connection.m
Expand Up @@ -10,7 +10,7 @@
#import "Response.h"
#import "NSData+Additions.h"
#import "NSMutableURLRequest+ResponseType.h"

#import "ConnectionDelegate.h"

//#define debugLog(...) NSLog(__VA_ARGS__)
#ifndef debugLog(...)
Expand All @@ -21,6 +21,16 @@ @implementation Connection

static float timeoutInterval = 5.0;

static NSMutableArray *activeDelegates;

+ (NSMutableArray *)activeDelegates {
if (nil == activeDelegates) {
activeDelegates = [NSMutableArray array];
[activeDelegates retain];
}
return activeDelegates;
}

+ (void)setTimeout:(float)timeOut {
timeoutInterval = timeOut;
}
Expand Down Expand Up @@ -49,14 +59,40 @@ + (Response *)sendRequest:(NSMutableURLRequest *)request withUser:(NSString *)us
NSURL *authURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@://%@:%@@%@:%@%@?%@", [url scheme],escapedUser, escapedPassword,
[url host], [url port], [url path], [url query]]];
[request setURL:authURL];
NSHTTPURLResponse *response;
NSError *error;

[self logRequest:request to:[authURL absoluteString]];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

ConnectionDelegate *connectionDelegate = [[[ConnectionDelegate alloc] init] autorelease];

[[self activeDelegates] addObject:connectionDelegate];
NSURLConnection *connection = [[[NSURLConnection alloc] initWithRequest:request delegate:connectionDelegate startImmediately:NO] autorelease];
connectionDelegate.connection = connection;

NSRunLoop* runLoop = [NSRunLoop mainRunLoop];

//This needs to run in the main run loop in the default mode
[connection unscheduleFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[connection scheduleInRunLoop:runLoop forMode:NSDefaultRunLoopMode];
[connection start];
while (![connectionDelegate isDone]) {
[runLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:.3]];
}
Response *resp = [Response responseFrom:(NSHTTPURLResponse *)connectionDelegate.response
withBody:connectionDelegate.data
andError:connectionDelegate.error];
[resp log];

[escapedUser release];
[escapedPassword release];
Response *resp = [Response responseFrom:response withBody:data andError:error];
[resp log];
[activeDelegates removeObject:connectionDelegate];

//if there are no more active delegates release the array
if (0 == [activeDelegates count]) {
NSMutableArray *tempDelegates = activeDelegates;
activeDelegates = nil;
[tempDelegates release];
}

return resp;
}

Expand Down Expand Up @@ -92,4 +128,10 @@ + (Response *)delete:(NSString *)url withUser:(NSString *)user andPassword:(NSSt
return [self sendRequest:request withUser:user andPassword:password];
}

+ (void) cancelAllActiveConnections {
for (ConnectionDelegate *delegate in activeDelegates) {
[delegate performSelectorOnMainThread:@selector(cancel) withObject:nil waitUntilDone:NO];
}
}

@end
30 changes: 30 additions & 0 deletions Classes/lib/ConnectionDelegate.h
@@ -0,0 +1,30 @@
//
// ConnectionDelegate.h
// iphone-harvest
//
// Created by vickeryj on 1/14/09.
// Copyright 2009 Joshua Vickery. All rights reserved.
//

#import <Foundation/Foundation.h>


@interface ConnectionDelegate : NSObject {

NSMutableData *data;
NSURLResponse *response;
BOOL done;
NSError *error;
NSURLConnection *connection;

}

- (BOOL) isDone;
- (void) cancel;

@property(nonatomic, retain) NSURLResponse *response;
@property(nonatomic, retain) NSMutableData *data;
@property(nonatomic, retain) NSError *error;
@property(nonatomic, retain) NSURLConnection *connection;

@end
83 changes: 83 additions & 0 deletions Classes/lib/ConnectionDelegate.m
@@ -0,0 +1,83 @@
//
// ConnectionDelegate.m
// iphone-harvest
//
// Created by vickeryj on 1/14/09.
// Copyright 2009 Joshua Vickery. All rights reserved.
//

#import "ConnectionDelegate.h"


@implementation ConnectionDelegate

@synthesize response, data, error, connection;

- (id) init
{
self = [super init];
if (self != nil) {
self.data = [NSMutableData data];
done = NO;
}
return self;
}

- (BOOL) isDone {
return done;
}

- (void) cancel {
[connection cancel];
self.response = nil;
self.data = nil;
self.error = nil;
done = YES;
}

#pragma mark NSURLConnectionDelegate methods
- (NSURLRequest *)connection:(NSURLConnection *)aConnection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response {
return request;
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
[[challenge sender] useCredential:[challenge proposedCredential] forAuthenticationChallenge:challenge];
}
- (void)connection:(NSURLConnection *)connection didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
done = YES;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)aResponse {
self.response = aResponse;
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)someData {
[data appendData:someData];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)aConnection {
done = YES;
}

- (void)connection:(NSURLConnection *)aConnection didFailWithError:(NSError *)error {
done = YES;
}

//don't cache resources for now
- (NSCachedURLResponse *)connection:(NSURLConnection *)aConnection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
return nil;
}


#pragma mark cleanup
- (void) dealloc
{
[data release];
[response release];
[error release];
[connection release];
[super dealloc];
}


@end
8 changes: 8 additions & 0 deletions active_resource.xcodeproj/project.pbxproj
Expand Up @@ -97,6 +97,8 @@
357A91BE0E9A55EF0025D9AF /* NSString+GSub.m in Sources */ = {isa = PBXBuildFile; fileRef = 357A91AC0E9A55EF0025D9AF /* NSString+GSub.m */; };
357A91BF0E9A55EF0025D9AF /* NSString+InflectionSupport.m in Sources */ = {isa = PBXBuildFile; fileRef = 357A91AE0E9A55EF0025D9AF /* NSString+InflectionSupport.m */; };
357A91C00E9A55EF0025D9AF /* Response.m in Sources */ = {isa = PBXBuildFile; fileRef = 357A91B00E9A55EF0025D9AF /* Response.m */; };
35CA32650F1E67A1001513AA /* ConnectionDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 35CA32640F1E67A1001513AA /* ConnectionDelegate.m */; };
35CA32660F1E67A1001513AA /* ConnectionDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 35CA32640F1E67A1001513AA /* ConnectionDelegate.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -201,6 +203,8 @@
357A91B00E9A55EF0025D9AF /* Response.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = Response.m; path = lib/Response.m; sourceTree = "<group>"; };
357A91B10E9A55EF0025D9AF /* XMLSerializable.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = XMLSerializable.h; path = lib/XMLSerializable.h; sourceTree = "<group>"; };
357A91B20E9A55EF0025D9AF /* XMLSerializableSupport.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = XMLSerializableSupport.h; path = lib/XMLSerializableSupport.h; sourceTree = "<group>"; };
35CA32630F1E67A1001513AA /* ConnectionDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ConnectionDelegate.h; path = lib/ConnectionDelegate.h; sourceTree = "<group>"; };
35CA32640F1E67A1001513AA /* ConnectionDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ConnectionDelegate.m; path = lib/ConnectionDelegate.m; sourceTree = "<group>"; };
8D1107310486CEB800E47090 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -477,6 +481,8 @@
357A91630E9A436E0025D9AF /* Connection */ = {
isa = PBXGroup;
children = (
35CA32630F1E67A1001513AA /* ConnectionDelegate.h */,
35CA32640F1E67A1001513AA /* ConnectionDelegate.m */,
2377C4F30F019E67006E155F /* NSHTTPURLResponse+Error.h */,
2377C4F40F019E67006E155F /* NSHTTPURLResponse+Error.m */,
357A919A0E9A55EF0025D9AF /* Connection.h */,
Expand Down Expand Up @@ -607,6 +613,7 @@
239AC9D30F2574EA00F5B672 /* NSObject+Serialize.m in Sources */,
239ACA160F25789700F5B672 /* NSDate+Serialize.m in Sources */,
239ACA3F0F2579D000F5B672 /* NSString+Serialize.m in Sources */,
35CA32650F1E67A1001513AA /* ConnectionDelegate.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -657,6 +664,7 @@
239AC9D20F2574EA00F5B672 /* NSObject+Serialize.m in Sources */,
239ACA150F25789700F5B672 /* NSDate+Serialize.m in Sources */,
239ACA3E0F2579D000F5B672 /* NSString+Serialize.m in Sources */,
35CA32660F1E67A1001513AA /* ConnectionDelegate.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down

0 comments on commit 84ab1bb

Please sign in to comment.