Skip to content

Commit

Permalink
merge in cancel support
Browse files Browse the repository at this point in the history
  • Loading branch information
vickeryj committed Jan 14, 2009
2 parents 28288ba + 83eeccb commit 267c9d4
Show file tree
Hide file tree
Showing 5 changed files with 163 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
48 changes: 42 additions & 6 deletions Classes/lib/Connection.m
Expand Up @@ -9,7 +9,7 @@
#import "Connection.h"
#import "Response.h"
#import "NSData+Additions.h"

#import "ConnectionDelegate.h"

//#define debugLog(...) NSLog(__VA_ARGS__)
#ifndef debugLog(...)
Expand All @@ -20,6 +20,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 @@ -51,14 +61,34 @@ + (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];

[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 date]];
}
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];

return resp;
}

Expand Down Expand Up @@ -100,4 +130,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 cancel];
}
}

@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
80 changes: 80 additions & 0 deletions Classes/lib/ConnectionDelegate.m
@@ -0,0 +1,80 @@
//
// 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];
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 @@ -83,6 +83,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 @@ -170,6 +172,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 @@ -431,6 +435,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 @@ -552,6 +558,7 @@
3520BE790F1644EC00BEF309 /* NSString+SBJSON.m in Sources */,
3520BE7A0F1644EC00BEF309 /* SBJSON.m in Sources */,
3520BE850F16482800BEF309 /* NSObject+JSONSerializableSupport.m in Sources */,
35CA32650F1E67A1001513AA /* ConnectionDelegate.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -595,6 +602,7 @@
23B4A6420F092B620021AB9D /* DogErrorTest.m in Sources */,
23F71FFC0F13FDFC009C7C2E /* NSString+XMLSerializableSupport.m in Sources */,
3520BE860F16482800BEF309 /* NSObject+JSONSerializableSupport.m in Sources */,
35CA32660F1E67A1001513AA /* ConnectionDelegate.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down

0 comments on commit 267c9d4

Please sign in to comment.