Hello All.
I'm trying to write a Cocoa app that will take a list of URLs and download the page, then parse each page pulling out each http:// request found in the page and then download that page, and continue. This will permit me to pull lots of load through a Proxy Engine I'm testing. However, I'm having some trouble.
Here is my first function: - (IBAction)getURL:(id)sender { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSString *uri; NSDate *now=[NSDate date]; NSString *pageFile;
if([[urlTF stringValue] length] < 1) { [pool release]; return; } uri = [[NSString alloc] initWithFormat:@"http://%@",[urlTF stringValue]]; [uri retain];
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:uri] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
// create the connection with the request // and start loading the data NSURLDownload *theDownload=[[NSURLDownload alloc] initWithRequest:theRequest delegate:self]; if (theDownload) { currentPageCount++;
// set the destination file now pageFile = [[NSString alloc] initWithFormat:@"%@/%f",ironWebDir,[now timeIntervalSinceReferenceDate]]; [theDownload setDestination:pageFile allowOverwrite:YES]; NSMutableDictionary *record = [NSMutableDictionary dictionary]; [record setObject:theDownload forKey:@"nsurldownload"]; [record setObject:uri forKey:@"url"]; [record setObject:[NSDate date] forKey:@"startTime"]; [record setObject:pageFile forKey:@"filename"];
[urlArray addObject:record]; [downloadHistoryTV reloadData]; }
As you can see, the delegate is set to self. When I enter a single URL this function is called and it starts the download. When the download is completed, the delegate method downloadDidFinish is called:
- (void)downloadDidFinish:(NSURLDownload *)download { NSEnumerator *e; id object;
NSLog(@"download did finish: %@ ************************************************",download);
e=[urlArray objectEnumerator]; while ( object = [e nextObject] ) { if([object objectForKey:@"nsurldownload"] == download) { float difference = [[NSDate date] timeIntervalSinceReferenceDate] - [[object objectForKey:@"startTime"] timeIntervalSinceReferenceDate]; [object setObject:[NSNumber numberWithFloat:difference] forKey:@"latency"]; [downloadHistoryTV reloadData];
if([harvestSwitch state] == NSOnState) { if(currentPageCount < totalPagesPermitted) [NSThread detachNewThreadSelector:@selector(scanResults:) toTarget:self withObject:[object objectForKey:@"filename"]]; } } } // release the connection [download release];
// do something with the data // NSLog(@"%@",@"downloadDidFinish"); } However, within the thread call to scanResults, I parse each http:// address and create a new NSURLDownload. However, they never begin or finish. By this, I mean they never get a callback. I've done this within the same main thread to see if somehow threading was causing the problem, but it acts the same.
-(IBAction)scanResults:(id)sender { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *aRow; NSString *fileContents; NSArray *rowArray; NSArray *rowComponents; NSString *pageFile;
// Now load the results fileContents = [NSString stringWithContentsOfFile:sender]; rowArray = [fileContents componentsSeparatedByString:@"\n"]; int i,rowCount = [rowArray count]; for(i=0;i<rowCount;i++) { aRow = [rowArray objectAtIndex:i]; rowComponents = [aRow componentsSeparatedByString:@"http://"]; if([rowComponents count] < 2) continue;
NSString *tmpString = [NSString stringWithString:@"\""]; NSArray *parseArray = [[rowComponents objectAtIndex:1] componentsSeparatedByString:tmpString];
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:[parseArray objectAtIndex:0]] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
// create the connection with the request // and start loading the data NSURLDownload *theDownload=[[NSURLDownload alloc] initWithRequest:theRequest delegate:self]; if (theDownload) { // set the destination file now pageFile = [[NSString alloc] initWithFormat:@"%@/%f",ironWebDir,[[NSDate date] timeIntervalSinceReferenceDate]]; [theDownload setDestination:pageFile allowOverwrite:YES]; NSMutableDictionary *record = [NSMutableDictionary dictionary]; [record setObject:theDownload forKey:@"nsurldownload"]; [record setObject:[parseArray objectAtIndex:0] forKey:@"url"]; [record setObject:[NSDate date] forKey:@"startTime"]; [record setObject:pageFile forKey:@"filename"];
[urlArray addObject:record]; [downloadHistoryTV reloadData]; }
} [pool release]; }
Can someone please tell me why I don't receive a "Callback" on the URLs download requests I generate from scanResults????
Thanks Dalton Hamilton
|