Skip to content

Commit

Permalink
More progress tweaks
Browse files Browse the repository at this point in the history
Cast zlib stuff to unsigned int to fix problems with uInt / UInt being a different size on different architectures
Have ASINetworkQueue reset progress in when setting progress delegates
Stop calling [super initialize] in ASIHTTPRequest
iPhone sample app now uses the same images as Mac sample in the queue example
  • Loading branch information
pokeb committed Apr 14, 2010
1 parent ffe54e9 commit b18cbc9
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 58 deletions.
3 changes: 3 additions & 0 deletions Classes/ASIHTTPRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount;
// HTTP status code, eg: 200 = OK, 404 = Not found etc
int responseStatusCode;

// Description of the HTTP status code
NSString *responseStatusMessage;

// Size of the response
Expand Down Expand Up @@ -471,6 +472,8 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount;
// Helper method for interacting with progress indicators to abstract the details of different APIS (NSProgressIndicator and UIProgressView)
+ (void)updateProgressIndicator:(id)indicator withProgress:(unsigned long long)progress ofTotal:(unsigned long long)total;

// Helper method used for performing invocations on the main thread (used for progress)
+ (void)performSelector:(SEL)selector onTarget:(id)target withObject:(id)object amount:(void *)amount;

#pragma mark handling request complete / failure

Expand Down
30 changes: 14 additions & 16 deletions Classes/ASIHTTPRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@


// Automatically set on build
NSString *ASIHTTPRequestVersion = @"v1.6.1-13 2010-04-14";
NSString *ASIHTTPRequestVersion = @"v1.6.1-14 2010-04-14";

NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain";

Expand Down Expand Up @@ -141,9 +141,6 @@ - (BOOL)shouldTimeOut;

- (void)updateStatus:(NSTimer*)timer;

// Helper method used for performing invocations on the main thread
+ (void)performSelector:(SEL)selector onTarget:(id)target withObject:(id)object amount:(void *)amount;

#if TARGET_OS_IPHONE
+ (void)registerForNetworkReachabilityNotifications;
+ (void)unsubscribeFromNetworkReachabilityNotifications;
Expand Down Expand Up @@ -220,7 +217,6 @@ + (void)initialize
isiPhoneOS2 = NO;
#endif
}
[super initialize];
}


Expand Down Expand Up @@ -1034,12 +1030,16 @@ - (void)startRequest

[[self cancelledLock] unlock];

if ([self shouldResetProgressIndicators]) {
if (![self mainRequest] && [self shouldResetProgressIndicators]) {
if ([self showAccurateProgress]) {
[self incrementUploadSizeBy:[self postLength]];
} else {
[self incrementUploadSizeBy:1];
}
[ASIHTTPRequest updateProgressIndicator:[self uploadProgressDelegate] withProgress:0 ofTotal:1];
if (![self partialDownloadSize]) {
[ASIHTTPRequest updateProgressIndicator:[self downloadProgressDelegate] withProgress:0 ofTotal:1];
}
}


Expand All @@ -1057,8 +1057,6 @@ - (void)startRequest
}
}



- (void)setStatusTimer:(NSTimer *)timer
{
[self retain];
Expand Down Expand Up @@ -1361,7 +1359,7 @@ - (void)updateUploadProgress

[ASIHTTPRequest performSelector:@selector(request:didSendBytes:) onTarget:[self queue] withObject:self amount:&value];
[ASIHTTPRequest performSelector:@selector(request:didSendBytes:) onTarget:[self uploadProgressDelegate] withObject:self amount:&value];
[ASIHTTPRequest updateProgressIndicator:[self uploadProgressDelegate] withProgress:[self totalBytesSent] ofTotal:[self postLength]];
[ASIHTTPRequest updateProgressIndicator:[self uploadProgressDelegate] withProgress:[self totalBytesSent]-[self uploadBufferSize] ofTotal:[self postLength]];
}


Expand Down Expand Up @@ -1633,7 +1631,7 @@ - (void)readResponseHeaders
} else {
[theRequest setContentLength:length];
if ([self showAccurateProgress] && [self shouldResetProgressIndicators]) {
[theRequest incrementDownloadSizeBy:[self contentLength]+[self partialDownloadSize]];
[theRequest incrementDownloadSizeBy:[theRequest contentLength]+[theRequest partialDownloadSize]];
}
}

Expand Down Expand Up @@ -3010,7 +3008,7 @@ + (NSData *)uncompressZippedData:(NSData*)compressedData

z_stream strm;
strm.next_in = (Bytef *)[compressedData bytes];
strm.avail_in = (UInt)[compressedData length];
strm.avail_in = (unsigned int)[compressedData length];
strm.total_out = 0;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
Expand All @@ -3023,7 +3021,7 @@ + (NSData *)uncompressZippedData:(NSData*)compressedData
[decompressed increaseLengthBy: half_length];
}
strm.next_out = [decompressed mutableBytes] + strm.total_out;
strm.avail_out = (UInt)([decompressed length] - strm.total_out);
strm.avail_out = (unsigned int)([decompressed length] - strm.total_out);

// Inflate another chunk.
status = inflate (&strm, Z_SYNC_FLUSH);
Expand Down Expand Up @@ -3098,7 +3096,7 @@ + (int)uncompressZippedDataFromSource:(FILE *)source toDestination:(FILE *)dest

/* decompress until deflate stream ends or end of file */
do {
strm.avail_in = (UInt)fread(in, 1, CHUNK, source);
strm.avail_in = (unsigned int)fread(in, 1, CHUNK, source);
if (ferror(source)) {
(void)inflateEnd(&strm);
return Z_ERRNO;
Expand Down Expand Up @@ -3152,7 +3150,7 @@ + (NSData *)compressData:(NSData*)uncompressedData
strm.opaque = Z_NULL;
strm.total_out = 0;
strm.next_in=(Bytef *)[uncompressedData bytes];
strm.avail_in = (UInt)[uncompressedData length];
strm.avail_in = (unsigned int)[uncompressedData length];

// Compresssion Levels:
// Z_NO_COMPRESSION
Expand All @@ -3170,7 +3168,7 @@ + (NSData *)compressData:(NSData*)uncompressedData
[compressed increaseLengthBy: 16384];

strm.next_out = [compressed mutableBytes] + strm.total_out;
strm.avail_out = (UInt)([compressed length] - strm.total_out);
strm.avail_out = (unsigned int)([compressed length] - strm.total_out);

deflate(&strm, Z_FINISH);

Expand Down Expand Up @@ -3231,7 +3229,7 @@ + (int)compressDataFromSource:(FILE *)source toDestination:(FILE *)dest

/* compress until end of file */
do {
strm.avail_in = (UInt)fread(in, 1, CHUNK, source);
strm.avail_in = (unsigned int)fread(in, 1, CHUNK, source);
if (ferror(source)) {
(void)deflateEnd(&strm);
return Z_ERRNO;
Expand Down
50 changes: 30 additions & 20 deletions Classes/ASINetworkQueue.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

// Private stuff
@interface ASINetworkQueue ()
- (void)resetProgressDelegate:(id)progressDelegate;
@property (assign) int requestsCount;
@end

Expand Down Expand Up @@ -104,36 +105,35 @@ - (void)cancelAllOperations
- (void)setUploadProgressDelegate:(id)newDelegate
{
uploadProgressDelegate = newDelegate;
[self resetProgressDelegate:newDelegate];

#if !TARGET_OS_IPHONE
// If the uploadProgressDelegate is an NSProgressIndicator, we set it's MaxValue to 1.0 so we can treat it similarly to UIProgressViews
SEL selector = @selector(setMaxValue:);
if ([[self uploadProgressDelegate] respondsToSelector:selector]) {
double max = 1.0;
NSMethodSignature *signature = [[[self uploadProgressDelegate] class] instanceMethodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:selector];
[invocation setArgument:&max atIndex:2];
[invocation invokeWithTarget:[self uploadProgressDelegate]];
}
#endif
}


- (void)setDownloadProgressDelegate:(id)newDelegate
{
downloadProgressDelegate = newDelegate;
[self resetProgressDelegate:newDelegate];
}

- (void)resetProgressDelegate:(id)progressDelegate
{
#if !TARGET_OS_IPHONE
// If the downloadProgressDelegate is an NSProgressIndicator, we set it's MaxValue to 1.0 so we can treat it similarly to UIProgressViews
// If the uploadProgressDelegate is an NSProgressIndicator, we set its MaxValue to 1.0 so we can treat it similarly to UIProgressViews
SEL selector = @selector(setMaxValue:);
if ([[self downloadProgressDelegate] respondsToSelector:selector]) {
if ([progressDelegate respondsToSelector:selector]) {
double max = 1.0;
NSMethodSignature *signature = [[[self downloadProgressDelegate] class] instanceMethodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:@selector(setMaxValue:)];
[invocation setArgument:&max atIndex:2];
[invocation invokeWithTarget:[self downloadProgressDelegate]];
[ASIHTTPRequest performSelector:selector onTarget:progressDelegate withObject:nil amount:&max];
}
selector = @selector(setDoubleValue:);
if ([progressDelegate respondsToSelector:selector]) {
double value = 0.0;
[ASIHTTPRequest performSelector:selector onTarget:progressDelegate withObject:nil amount:&value];
}
#else
SEL selector = @selector(setProgress:);
if ([progressDelegate respondsToSelector:selector]) {
float value = 0.0f;
[ASIHTTPRequest performSelector:selector onTarget:progressDelegate withObject:nil amount:&value];
}
#endif
}
Expand Down Expand Up @@ -177,6 +177,10 @@ - (void)addOperation:(NSOperation *)operation
ASIHTTPRequest *HEADRequest = [request HEADRequest];
[self addHEADOperation:HEADRequest];

if ([request shouldResetProgressIndicators]) {
[self resetProgressDelegate:[request downloadProgressDelegate]];
}

//Tell the request not to reset the progress indicator when it gets a content-length, as we will get the length from the HEAD request
[request setShouldResetProgressIndicators:NO];

Expand All @@ -186,6 +190,12 @@ - (void)addOperation:(NSOperation *)operation
} else if (uploadProgressDelegate) {
[request buildPostBody];
[self setTotalBytesToUpload:[self totalBytesToUpload]+[request postLength]];


if ([request shouldResetProgressIndicators]) {
[self resetProgressDelegate:[request uploadProgressDelegate]];
}

[request setShouldResetProgressIndicators:NO];
}
}
Expand Down
12 changes: 1 addition & 11 deletions Mac Sample/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,14 @@

- (IBAction)fetchThreeImages:(id)sender;

- (void)authenticationNeededForRequest:(ASIHTTPRequest *)request;

- (IBAction)dismissAuthSheet:(id)sender;
- (IBAction)fetchTopSecretInformation:(id)sender;

- (IBAction)postWithProgress:(id)sender;

- (IBAction)throttleBandwidth:(id)sender;

- (void)updateBandwidthUsageIndicator;
- (void)URLFetchWithProgressComplete:(ASIHTTPRequest *)request;
- (void)URLFetchWithProgressFailed:(ASIHTTPRequest *)request;
- (void)imageFetch1Complete:(ASIHTTPRequest *)request;
- (void)imageFetch2Complete:(ASIHTTPRequest *)request;
- (void)imageFetch3Complete:(ASIHTTPRequest *)request;
- (void)topSecretFetchComplete:(ASIHTTPRequest *)request;
- (void)authSheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo;
- (void)postFinished:(ASIHTTPRequest *)request;
- (void)postFailed:(ASIHTTPRequest *)request;

@property (retain, nonatomic) ASIHTTPRequest *bigFetchRequest;
@end
22 changes: 17 additions & 5 deletions Mac Sample/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@
#import "ASIFormDataRequest.h"
#import "ASINetworkQueue.h"

@interface AppDelegate ()
- (void)updateBandwidthUsageIndicator;
- (void)URLFetchWithProgressComplete:(ASIHTTPRequest *)request;
- (void)URLFetchWithProgressFailed:(ASIHTTPRequest *)request;
- (void)imageFetch1Complete:(ASIHTTPRequest *)request;
- (void)imageFetch2Complete:(ASIHTTPRequest *)request;
- (void)imageFetch3Complete:(ASIHTTPRequest *)request;
- (void)topSecretFetchComplete:(ASIHTTPRequest *)request;
- (void)authSheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo;
- (void)postFinished:(ASIHTTPRequest *)request;
- (void)postFailed:(ASIHTTPRequest *)request;
@end

@implementation AppDelegate

- (id)init
Expand Down Expand Up @@ -79,7 +92,7 @@ - (IBAction)resumeURLFetchWithProgress:(id)sender
[startButton setAction:@selector(stopURLFetchWithProgress:)];

// Stop any other requests
[networkQueue cancelAllOperations];
[networkQueue reset];

[self setBigFetchRequest:[[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/redirect_resume"]] autorelease]];
[[self bigFetchRequest] setDownloadDestinationPath:[[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"The Great American Novel.txt"]];
Expand Down Expand Up @@ -116,8 +129,7 @@ - (IBAction)fetchThreeImages:(id)sender
[imageView2 setImage:nil];
[imageView3 setImage:nil];

[networkQueue cancelAllOperations];
[networkQueue setRequestDidFinishSelector:NULL];
[networkQueue reset];
[networkQueue setDownloadProgressDelegate:progressIndicator];
[networkQueue setDelegate:self];
[networkQueue setShowAccurateProgress:([showAccurateProgress state] == NSOnState)];
Expand Down Expand Up @@ -192,7 +204,7 @@ - (void)imageFetch3Complete:(ASIHTTPRequest *)request

- (IBAction)fetchTopSecretInformation:(id)sender
{
[networkQueue cancelAllOperations];
[networkQueue reset];

[progressIndicator setDoubleValue:0];

Expand Down Expand Up @@ -268,7 +280,7 @@ - (IBAction)postWithProgress:(id)sender
[data writeToFile:path atomically:NO];


[networkQueue cancelAllOperations];
[networkQueue reset];
[networkQueue setShowAccurateProgress:YES];
[networkQueue setUploadProgressDelegate:progressIndicator];
[networkQueue setRequestDidFailSelector:@selector(postFailed:)];
Expand Down
11 changes: 7 additions & 4 deletions iPhone Sample/QueueViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ - (IBAction)fetchThreeImages:(id)sender
[networkQueue setDelegate:self];

ASIHTTPRequest *request;
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/i/logo.png"]];
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/images/small-image.jpg"]];
[request setDownloadDestinationPath:[[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"1.png"]];
[networkQueue addOperation:request];

request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/i/trailsnetwork.png"]] autorelease];
request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/images/medium-image.jpg"]] autorelease];
[request setDownloadDestinationPath:[[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"2.png"]];
[networkQueue addOperation:request];

request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/i/sharedspace20.png"]] autorelease];
request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/images/large-image.jpg"]] autorelease];
[request setDownloadDestinationPath:[[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"3.png"]];
[networkQueue addOperation:request];

[networkQueue go];
Expand All @@ -46,7 +49,7 @@ - (IBAction)fetchThreeImages:(id)sender

- (void)imageFetchComplete:(ASIHTTPRequest *)request
{
UIImage *img = [UIImage imageWithData:[request responseData]];
UIImage *img = [UIImage imageWithContentsOfFile:[request downloadDestinationPath]];
if (img) {
if ([imageView1 image]) {
if ([imageView2 image]) {
Expand Down
4 changes: 2 additions & 2 deletions strict.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ GCC_WARN_INITIALIZER_NOT_FULLY_BRACKETED = YES
GCC_WARN_ABOUT_RETURN_TYPE = YES
//GCC_WARN_MISSING_PARENTHESES = YES
GCC_WARN_ABOUT_MISSING_FIELD_INITIALIZERS = YES
GCC_WARN_ABOUT_MISSING_NEWLINE = YES
//GCC_WARN_ABOUT_MISSING_NEWLINE = YES
GCC_WARN_SIGN_COMPARE = YES
GCC_WARN_STRICT_SELECTOR_MATCH = missing value
GCC_WARN_TYPECHECK_CALLS_TO_PRINTF = YES
GCC_WARN_UNDECLARED_SELECTOR = YES
//GCC_WARN_UNDECLARED_SELECTOR = YES
GCC_WARN_UNUSED_FUNCTION = YES
GCC_WARN_UNUSED_LABEL = YES
GCC_WARN_UNUSED_VALUE = YES
Expand Down

0 comments on commit b18cbc9

Please sign in to comment.