123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- #import "ASINetworkQueue.h"
- #import "ASIHTTPRequest.h"
- @interface ASINetworkQueue ()
- - (void)resetProgressDelegate:(id *)progressDelegate;
- @property (assign) int requestsCount;
- @end
- @implementation ASINetworkQueue
- - (id)init
- {
- self = [super init];
- [self setShouldCancelAllRequestsOnFailure:YES];
- [self setMaxConcurrentOperationCount:4];
- [self setSuspended:YES];
-
- return self;
- }
- + (id)queue
- {
- return [[[self alloc] init] autorelease];
- }
- - (void)dealloc
- {
-
- for (ASIHTTPRequest *request in [self operations]) {
- [request setQueue:nil];
- }
- [userInfo release];
- [super dealloc];
- }
- - (void)setSuspended:(BOOL)suspend
- {
- [super setSuspended:suspend];
- }
- - (void)reset
- {
- [self cancelAllOperations];
- [self setDelegate:nil];
- [self setDownloadProgressDelegate:nil];
- [self setUploadProgressDelegate:nil];
- [self setRequestDidStartSelector:NULL];
- [self setRequestDidReceiveResponseHeadersSelector:NULL];
- [self setRequestDidFailSelector:NULL];
- [self setRequestDidFinishSelector:NULL];
- [self setQueueDidFinishSelector:NULL];
- [self setSuspended:YES];
- }
- - (void)go
- {
- [self setSuspended:NO];
- }
- - (void)cancelAllOperations
- {
- [self setBytesUploadedSoFar:0];
- [self setTotalBytesToUpload:0];
- [self setBytesDownloadedSoFar:0];
- [self setTotalBytesToDownload:0];
- [super cancelAllOperations];
- }
- - (void)setUploadProgressDelegate:(id)newDelegate
- {
- uploadProgressDelegate = newDelegate;
- [self resetProgressDelegate:&uploadProgressDelegate];
- }
- - (void)setDownloadProgressDelegate:(id)newDelegate
- {
- downloadProgressDelegate = newDelegate;
- [self resetProgressDelegate:&downloadProgressDelegate];
- }
- - (void)resetProgressDelegate:(id *)progressDelegate
- {
- #if !TARGET_OS_IPHONE
-
- SEL selector = @selector(setMaxValue:);
- if ([*progressDelegate respondsToSelector:selector]) {
- double max = 1.0;
- [ASIHTTPRequest performSelector:selector onTarget:progressDelegate withObject:nil amount:&max callerToRetain:nil];
- }
- selector = @selector(setDoubleValue:);
- if ([*progressDelegate respondsToSelector:selector]) {
- double value = 0.0;
- [ASIHTTPRequest performSelector:selector onTarget:progressDelegate withObject:nil amount:&value callerToRetain:nil];
- }
- #else
- SEL selector = @selector(setProgress:);
- if ([*progressDelegate respondsToSelector:selector]) {
- float value = 0.0f;
- [ASIHTTPRequest performSelector:selector onTarget:progressDelegate withObject:nil amount:&value callerToRetain:nil];
- }
- #endif
- }
- - (void)addHEADOperation:(NSOperation *)operation
- {
- if ([operation isKindOfClass:[ASIHTTPRequest class]]) {
-
- ASIHTTPRequest *request = (ASIHTTPRequest *)operation;
- [request setRequestMethod:@"HEAD"];
- [request setQueuePriority:10];
- [request setShowAccurateProgress:YES];
- [request setQueue:self];
-
-
- [super addOperation:request];
- }
- }
- - (void)addOperation:(NSOperation *)operation
- {
- if (![operation isKindOfClass:[ASIHTTPRequest class]]) {
- [NSException raise:@"AttemptToAddInvalidRequest" format:@"Attempted to add an object that was not an ASIHTTPRequest to an ASINetworkQueue"];
- }
-
- [self setRequestsCount:[self requestsCount]+1];
-
- ASIHTTPRequest *request = (ASIHTTPRequest *)operation;
-
- if ([self showAccurateProgress]) {
-
-
- [request buildPostBody];
-
-
-
-
-
- if ([[request requestMethod] isEqualToString:@"GET"]) {
- if ([self isSuspended]) {
- ASIHTTPRequest *HEADRequest = [request HEADRequest];
- [self addHEADOperation:HEADRequest];
- [request addDependency:HEADRequest];
- if ([request shouldResetDownloadProgress]) {
- [self resetProgressDelegate:&downloadProgressDelegate];
- [request setShouldResetDownloadProgress:NO];
- }
- }
- }
- [request buildPostBody];
- [self request:nil incrementUploadSizeBy:(long long)[request postLength]];
- } else {
- [self request:nil incrementDownloadSizeBy:1];
- [self request:nil incrementUploadSizeBy:1];
- }
-
- if ([request shouldResetUploadProgress]) {
- [self resetProgressDelegate:&uploadProgressDelegate];
- [request setShouldResetUploadProgress:NO];
- }
-
- [request setShowAccurateProgress:[self showAccurateProgress]];
-
- [request setQueue:self];
- [super addOperation:request];
- }
- - (void)requestStarted:(ASIHTTPRequest *)request
- {
- if ([self requestDidStartSelector]) {
- [[self delegate] performSelector:[self requestDidStartSelector] withObject:request];
- }
- }
- - (void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders
- {
- if ([self requestDidReceiveResponseHeadersSelector]) {
- [[self delegate] performSelector:[self requestDidReceiveResponseHeadersSelector] withObject:request withObject:responseHeaders];
- }
- }
- - (void)request:(ASIHTTPRequest *)request willRedirectToURL:(NSURL *)newURL
- {
- if ([self requestWillRedirectSelector]) {
- [[self delegate] performSelector:[self requestWillRedirectSelector] withObject:request withObject:newURL];
- }
- }
- - (void)requestFinished:(ASIHTTPRequest *)request
- {
- [self setRequestsCount:[self requestsCount]-1];
- if ([self requestDidFinishSelector]) {
- [[self delegate] performSelector:[self requestDidFinishSelector] withObject:request];
- }
- if ([self requestsCount] == 0) {
- if ([self queueDidFinishSelector]) {
- [[self delegate] performSelector:[self queueDidFinishSelector] withObject:self];
- }
- }
- }
- - (void)requestFailed:(ASIHTTPRequest *)request
- {
- [self setRequestsCount:[self requestsCount]-1];
- if ([self requestDidFailSelector]) {
- [[self delegate] performSelector:[self requestDidFailSelector] withObject:request];
- }
- if ([self requestsCount] == 0) {
- if ([self queueDidFinishSelector]) {
- [[self delegate] performSelector:[self queueDidFinishSelector] withObject:self];
- }
- }
- if ([self shouldCancelAllRequestsOnFailure] && [self requestsCount] > 0) {
- [self cancelAllOperations];
- }
-
- }
- - (void)request:(ASIHTTPRequest *)request didReceiveBytes:(long long)bytes
- {
- [self setBytesDownloadedSoFar:[self bytesDownloadedSoFar]+(unsigned long long)bytes];
- if ([self downloadProgressDelegate]) {
- [ASIHTTPRequest updateProgressIndicator:&downloadProgressDelegate withProgress:[self bytesDownloadedSoFar] ofTotal:[self totalBytesToDownload]];
- }
- }
- - (void)request:(ASIHTTPRequest *)request didSendBytes:(long long)bytes
- {
- [self setBytesUploadedSoFar:[self bytesUploadedSoFar]+(unsigned long long)bytes];
- if ([self uploadProgressDelegate]) {
- [ASIHTTPRequest updateProgressIndicator:&uploadProgressDelegate withProgress:[self bytesUploadedSoFar] ofTotal:[self totalBytesToUpload]];
- }
- }
- - (void)request:(ASIHTTPRequest *)request incrementDownloadSizeBy:(long long)newLength
- {
- [self setTotalBytesToDownload:[self totalBytesToDownload]+(unsigned long long)newLength];
- }
- - (void)request:(ASIHTTPRequest *)request incrementUploadSizeBy:(long long)newLength
- {
- [self setTotalBytesToUpload:[self totalBytesToUpload]+(unsigned long long)newLength];
- }
- - (void)authenticationNeededForRequest:(ASIHTTPRequest *)request
- {
- if ([[self delegate] respondsToSelector:@selector(authenticationNeededForRequest:)]) {
- [[self delegate] performSelector:@selector(authenticationNeededForRequest:) withObject:request];
- }
- }
- - (void)proxyAuthenticationNeededForRequest:(ASIHTTPRequest *)request
- {
- if ([[self delegate] respondsToSelector:@selector(proxyAuthenticationNeededForRequest:)]) {
- [[self delegate] performSelector:@selector(proxyAuthenticationNeededForRequest:) withObject:request];
- }
- }
- - (BOOL)respondsToSelector:(SEL)selector
- {
-
-
- if (selector == @selector(authenticationNeededForRequest:)) {
- if ([[self delegate] respondsToSelector:@selector(authenticationNeededForRequest:)]) {
- return YES;
- }
- return NO;
-
- } else if (selector == @selector(proxyAuthenticationNeededForRequest:)) {
- if ([[self delegate] respondsToSelector:@selector(proxyAuthenticationNeededForRequest:)]) {
- return YES;
- }
- return NO;
-
- } else if (selector == @selector(request:willRedirectToURL:)) {
- if ([self requestWillRedirectSelector] && [[self delegate] respondsToSelector:[self requestWillRedirectSelector]]) {
- return YES;
- }
- return NO;
- }
- return [super respondsToSelector:selector];
- }
- #pragma mark NSCopying
- - (id)copyWithZone:(NSZone *)zone
- {
- ASINetworkQueue *newQueue = [[[self class] alloc] init];
- [newQueue setDelegate:[self delegate]];
- [newQueue setRequestDidStartSelector:[self requestDidStartSelector]];
- [newQueue setRequestWillRedirectSelector:[self requestWillRedirectSelector]];
- [newQueue setRequestDidReceiveResponseHeadersSelector:[self requestDidReceiveResponseHeadersSelector]];
- [newQueue setRequestDidFinishSelector:[self requestDidFinishSelector]];
- [newQueue setRequestDidFailSelector:[self requestDidFailSelector]];
- [newQueue setQueueDidFinishSelector:[self queueDidFinishSelector]];
- [newQueue setUploadProgressDelegate:[self uploadProgressDelegate]];
- [newQueue setDownloadProgressDelegate:[self downloadProgressDelegate]];
- [newQueue setShouldCancelAllRequestsOnFailure:[self shouldCancelAllRequestsOnFailure]];
- [newQueue setShowAccurateProgress:[self showAccurateProgress]];
- [newQueue setUserInfo:[[[self userInfo] copyWithZone:zone] autorelease]];
- return newQueue;
- }
- @synthesize requestsCount;
- @synthesize bytesUploadedSoFar;
- @synthesize totalBytesToUpload;
- @synthesize bytesDownloadedSoFar;
- @synthesize totalBytesToDownload;
- @synthesize shouldCancelAllRequestsOnFailure;
- @synthesize uploadProgressDelegate;
- @synthesize downloadProgressDelegate;
- @synthesize requestDidStartSelector;
- @synthesize requestDidReceiveResponseHeadersSelector;
- @synthesize requestWillRedirectSelector;
- @synthesize requestDidFinishSelector;
- @synthesize requestDidFailSelector;
- @synthesize queueDidFinishSelector;
- @synthesize delegate;
- @synthesize showAccurateProgress;
- @synthesize userInfo;
- @end
|