Skip to content
Browse files

Switched everything over to my new connection class, instead of IKCon…

…nectionDelegate.
  • Loading branch information...
1 parent c78788b commit 9ea072d84a437bbc9598c26416e0310e1a48e886 @CarterA committed Feb 25, 2011
Showing with 139 additions and 30 deletions.
  1. +32 −5 Campus/CZURLConnection.h
  2. +94 −15 Campus/CZURLConnection.m
  3. +13 −10 Campus/ICConnection.m
View
37 Campus/CZURLConnection.h
@@ -2,15 +2,42 @@
// CZURLConnection.h
// Campus
//
-// Created by Carter Allen on 1/18/11.
+// Created by Carter Allen on 2/25/11.
// Copyright 2011 Opt-6 Products, LLC. All rights reserved.
//
+// The basic architecture of this class was created by Mike Ash,
+// and was re-implemented by Carter Allen. The original code can
+// be found here: https://gist.github.com/837409 Thanks Mike!
+//
+
+typedef void(^CZURLConnectionTimeoutHandler)(void);
+typedef void(^CZURLConnectionResponseHandler)(NSURLResponse *response);
+typedef void(^CZURLConnectionProgressHandler)(long long currentLength, long long totalLength);
+typedef void(^CZURLConnectionCompletionHandler)(NSData *data, NSURLResponse *response);
+typedef void(^CZURLConnectionErrorHandler)(NSError *error);
+
+@interface CZURLConnection : NSObject {}
+
+#pragma mark -
+#pragma mark Creating a Connection
++ (CZURLConnection *)connectionWithRequest:(NSURLRequest *)request;
+- (id)initWithRequest:(NSURLRequest *)request;
-typedef void (^CZURLConnectionCompletionHandler)(NSData *data);
-typedef void (^CZURLConnectionErrorHandler)(NSError *error);
+#pragma mark -
+#pragma mark Configuring a Connection
+@property (nonatomic, retain) NSURLRequest *request;
+@property (nonatomic, assign) NSTimeInterval timeout;
-@interface CZURLConnection : NSURLConnection {}
+#pragma mark Connection Event Handlers
+@property (nonatomic, copy) CZURLConnectionTimeoutHandler timeoutHandler;
+@property (nonatomic, copy) CZURLConnectionResponseHandler responseHandler;
+@property (nonatomic, copy) CZURLConnectionProgressHandler progressHandler;
@property (nonatomic, copy) CZURLConnectionCompletionHandler completionHandler;
@property (nonatomic, copy) CZURLConnectionErrorHandler errorHandler;
-- (id)initWithRequest:(NSURLRequest *)request;
+
+#pragma mark -
+#pragma mark Starting and Stopping a Connection
+- (void)start;
+- (void)cancel;
+
@end
View
109 Campus/CZURLConnection.m
@@ -2,35 +2,114 @@
// CZURLConnection.m
// Campus
//
-// Created by Carter Allen on 1/18/11.
+// Created by Carter Allen on 2/25/11.
// Copyright 2011 Opt-6 Products, LLC. All rights reserved.
//
#import "CZURLConnection.h"
-@interface CZURLConnection()
+@interface CZURLConnection ()
+@property (nonatomic, retain) NSURLConnection *connection;
+@property (nonatomic, retain) NSURLResponse *response;
+@property (nonatomic, retain) NSTimer *timeoutTimer;
@property (nonatomic, retain) NSMutableData *receivedData;
+@property (nonatomic, assign) long long estimatedResponseLength;
+- (void)connectionDidTimeout;
@end
+#pragma mark -
@implementation CZURLConnection
-@synthesize completionHandler, errorHandler, receivedData;
+
+#pragma mark Public Properties
+@synthesize request=_request;
+@synthesize timeout=_timeout;
+@synthesize timeoutHandler=_timeoutHandler;
+@synthesize responseHandler=_responseHandler;
+@synthesize progressHandler=_progressHandler;
+@synthesize completionHandler=_completionHandler;
+@synthesize errorHandler=_errorHandler;
+
+#pragma mark -
+#pragma mark Private Properties
+@synthesize connection=_connection;
+@synthesize response=_response;
+@synthesize timeoutTimer=_timeoutTimer;
+@synthesize receivedData=_receivedData;
+@synthesize estimatedResponseLength=_estimatedResponseLength;
+
+#pragma mark -
+#pragma mark Initializers
++ (CZURLConnection *)connectionWithRequest:(NSURLRequest *)request {
+ return [[[self alloc] initWithRequest:request] autorelease];
+}
- (id)initWithRequest:(NSURLRequest *)request {
- self = [super initWithRequest:request delegate:self startImmediately:NO];
+ if ((self = [super init])) {
+ _request = [request retain];
+ }
return self;
}
-- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate startImmediately:(BOOL)startImmediately {
- if ((self = [super initWithRequest:request delegate:self startImmediately:NO])) {
- self.receivedData = [NSMutableData data];
- }
+- (id)init {
+ self = [self initWithRequest:nil];
return self;
}
-- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [self.receivedData appendData:data]; }
-- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
- self.receivedData = nil;
- self.errorHandler(error);
+
+#pragma mark -
+#pragma mark Memory Management
+- (void)dealloc {
+ [_request release];
+ [_response release];
+ [_timeoutHandler release];
+ [_responseHandler release];
+ [_progressHandler release];
+ [_completionHandler release];
+ [_errorHandler release];
+ [_connection release];
+ [_timeoutTimer release];
+ [_receivedData release];
+ [super dealloc];
+}
+
+#pragma mark -
+#pragma mark Starting and Stopping a Connection
+- (void)start {
+ if (self.timeout)
+ self.timeoutTimer = [NSTimer scheduledTimerWithTimeInterval:self.timeout target:self selector:@selector(connectionDidTimeout) userInfo:nil repeats:NO];
+ if (!self.connection)
+ self.connection = [[NSURLConnection alloc] initWithRequest:self.request delegate:self startImmediately:NO];
+ [self.connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
+ [self.connection start];
+}
+- (void)cancel {
+ [self.timeoutTimer invalidate];
+ [self.connection cancel];
}
+
+#pragma mark -
+#pragma mark Connection Delegate Methods
+- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
+ self.response = response;
+ self.estimatedResponseLength = [response expectedContentLength];
+ if (self.responseHandler) self.responseHandler(response);
+}
+
+- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
+ if (!self.receivedData) self.receivedData = [NSMutableData data];
+ [self.receivedData appendData:data];
+ if (self.progressHandler) self.progressHandler([self.receivedData length], self.estimatedResponseLength);
+}
+
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
- self.completionHandler(self.receivedData);
- self.receivedData = nil;
+ [self.timeoutTimer invalidate];
+ if (self.completionHandler) self.completionHandler(self.receivedData, self.response);
}
-@end
+
+- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
+ [self.timeoutTimer invalidate];
+ if(self.errorHandler) self.errorHandler(error);
+}
+- (void)connectionDidTimeout {
+ [self cancel];
+ if (self.timeoutHandler) self.timeoutHandler();
+}
+
+@end
View
23 Campus/ICConnection.m
@@ -10,7 +10,7 @@
#import "ICTerm.h"
#import "ICCourse.h"
#import "CampusLogin.h"
-#import "IKConnectionDelegate.h"
+#import "CZURLConnection.h"
#import "XPathQuery/XPathQuery.h"
@implementation ICConnection
@@ -35,7 +35,8 @@ - (void)scrapeDataWithUsername:(NSString *)username password:(NSString *)passwor
// Load up the portal.
NSURLRequest *portalRequest = [NSURLRequest requestWithURL:portalURL];
- IKConnectionDelegate *portalRequestDelegate = [IKConnectionDelegate connectionDelegateWithDownloadProgress:nil uploadProgress:nil completion:^(NSData *portalResponseData, NSURLResponse *response, NSError *error) {
+ CZURLConnection *portalConnection = [CZURLConnection connectionWithRequest:portalRequest];
+ [portalConnection setCompletionHandler:^(NSData *portalResponseData, NSURLResponse *response) {
// Search for the portal schedule link in this mess...
//NSString *query = @"/html/body/table/tr[3]/td[1]/table[5]/tr/td[4]/a";
@@ -46,7 +47,8 @@ - (void)scrapeDataWithUsername:(NSString *)username password:(NSString *)passwor
// Load the schedule page.
NSURLRequest *scheduleRequest = [NSURLRequest requestWithURL:scheduleURL];
- IKConnectionDelegate *scheduleRequestDelegate = [IKConnectionDelegate connectionDelegateWithDownloadProgress:nil uploadProgress:nil completion:^(NSData *scheduleResponseData, NSURLResponse *response, NSError *error) {
+ CZURLConnection *scheduleConnection = [CZURLConnection connectionWithRequest:scheduleRequest];
+ [scheduleConnection setCompletionHandler:^(NSData *scheduleResponseData, NSURLResponse *response) {
// Create each term.
NSMutableArray *terms = [NSMutableArray array];
@@ -118,12 +120,13 @@ - (void)scrapeDataWithUsername:(NSString *)username password:(NSString *)passwor
course.instructor = instructor;
course.url = courseURL;
- if ([courseName isEqualToString:@"DSA Chorale Intermediate 2 S1"]) { // For testing only. My brain can only handle one assload of assignment data at once.
+ //if ([courseName isEqualToString:@"DSA Chorale Intermediate 2 S1"]) { // For testing only. My brain can only handle one assload of assignment data at once.
// Scrape and add assignment data to the course, if it contains a gradebook.
if (course.url) {
// Load up the gradebook.
NSURLRequest *gradebookRequest = [NSURLRequest requestWithURL:course.url];
- IKConnectionDelegate *gradebookRequestDelegate = [IKConnectionDelegate connectionDelegateWithDownloadProgress:nil uploadProgress:nil completion:^(NSData *gradebookResponseData, NSURLResponse *response, NSError *error) {
+ CZURLConnection *gradebookConnection = [CZURLConnection connectionWithRequest:gradebookRequest];
+ [gradebookConnection setCompletionHandler:^(NSData *gradebookResponseData, NSURLResponse *response) {
NSString *tableQuery = @"/html/body/table/tr[3]/td[2]/table/tr[3]/td/table";
NSArray *tableQueryResults = PerformHTMLXPathQuery(gradebookResponseData, tableQuery);
@@ -206,13 +209,13 @@ - (void)scrapeDataWithUsername:(NSString *)username password:(NSString *)passwor
}
}
}
- NSLog(@"%@", [assignmentCategoriesWithRawAssignmentData allKeys]); // Here's yer dang category names!
+ //NSLog(@"%@", [assignmentCategoriesWithRawAssignmentData allKeys]); // Here's yer dang category names!
// Add the completed course to the term in which we are currently working.
[[terms objectAtIndex:columnIndex-1] addCourse:course];
}];
- [NSURLConnection connectionWithRequest:gradebookRequest delegate:gradebookRequestDelegate];
+ [gradebookConnection start];
}
else { // If the course doesn't have a gradebook, just add it to the term without parsing assignments.
[[terms objectAtIndex:columnIndex-1] addCourse:course];
@@ -222,7 +225,7 @@ - (void)scrapeDataWithUsername:(NSString *)username password:(NSString *)passwor
// Add the course we just finished parsing to the list of completed courses so we don't parse it again.
[parsedCourses addObject:courseIdentifier];
- }
+ //}
}
}
@@ -240,10 +243,10 @@ - (void)scrapeDataWithUsername:(NSString *)username password:(NSString *)passwor
[campusResponse release];
}];
- [NSURLConnection connectionWithRequest:scheduleRequest delegate:scheduleRequestDelegate];
+ [scheduleConnection start];
}];
- [NSURLConnection connectionWithRequest:portalRequest delegate:portalRequestDelegate];
+ [portalConnection start];
}];
}

0 comments on commit 9ea072d

Please sign in to comment.
Something went wrong with that request. Please try again.