// // WRTStatusClient.m // DDWRT-Monitor // // Created by Spike Grobstein on 5/30/11. // Copyright 2011 Sadistech. All rights reserved. // #import "WRTStatusClient.h" @implementation WRTStatusClient @synthesize receivedData; @synthesize delegate; @synthesize callback; @synthesize errorCallback; - (id) initWithHostname:(NSString*)new_hostname port:(int)new_port protocol:(NSString*)new_protocol username:(NSString*)new_username password:(NSString*)new_password { self = [super init]; [self setHostname: new_hostname]; [self setProtocol: new_protocol]; [self setPort: new_port]; [self setUsername: new_username]; [self setPassword: new_password]; return self; } #pragma mark - - (NSMutableURLRequest*) buildRequest:(NSString*)uri { NSString *urlAddress = [NSString stringWithFormat:@"%@://%@:%@@%@:%d/%@", protocol, username, password, hostname, port, uri]; NSLog(@"Building request form %@", urlAddress); NSURL *url = [NSURL URLWithString:urlAddress]; //[self request:url]; // ---V theRequest = [[NSMutableURLRequest alloc] initWithURL: url]; return [theRequest autorelease]; } - (NSMutableURLRequest*) requestForBandwidthViewerForInterface:(NSString*)interface { NSString *endpoint = [NSString stringWithFormat:@"graph_if.svg?%@", interface]; return [self buildRequest:endpoint]; } - (void) getStatusUpdate:(NSString*)uri delegate:(id)requestDelegate callback:(SEL)requestSelector { self.delegate = requestDelegate; self.callback = requestSelector; theConnection = [[NSURLConnection alloc] initWithRequest:[self buildRequest: uri] delegate:self]; if (theConnection) { receivedData = [[NSMutableData data] retain]; } [theConnection autorelease]; // this function returns immediately // the delegate will receive a function call with the following signature upon completion: // - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data } #pragma mark - #pragma mark Accessors: - (NSString*) getHostname { return hostname; } - (void) setHostname:(NSString*)new_hostname { NSString *old_hostname = hostname; hostname = [new_hostname retain]; [old_hostname autorelease]; } - (NSString*) getProtocol { return protocol; } - (void) setProtocol:(NSString *)new_protocol { NSString *old_protocol = protocol; protocol = [new_protocol retain]; [old_protocol autorelease]; } - (int) getPort { return port; } - (void) setPort:(int)new_port { port = new_port; } - (NSString*) getUsername { return username; } - (void) setUsername:(NSString*)new_username { NSString *old_username = username; username = [new_username retain]; [old_username autorelease]; } - (NSString*) getPassword { return password; } - (void) setPassword:(NSString*)new_password { NSString *old_password = password; password = [new_password retain]; [old_password autorelease]; } #pragma mark - #pragma mark NSURLConnection Delegate Methods: -(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { NSLog(@"1"); if ([challenge previousFailureCount] == 0) { NSLog(@"1.1.1"); NSURLCredential *newCredential; newCredential = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]; NSLog(@"1.1.2"); [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge]; } else { NSLog(@"1.2"); [[challenge sender] cancelAuthenticationChallenge:challenge]; // TODO Pop up authentication error NSLog(@"Invalid Username or Password"); } } -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSLog(@"2"); [receivedData setLength:0]; } -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { NSLog(@"3 %lu", [data length]); [receivedData appendData:data]; NSLog(@"3 %lu", [receivedData length]); } -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"4"); //[connection release]; //[receivedData release]; //[theRequest release]; //TODO Return alert NSLog(@"Connection failed! Error - %@ %@", [error localizedDescription], [[error userInfo] objectForKey:NSErrorFailingURLStringKey]); if (errorCallback) { [delegate performSelector:errorCallback withObject:error]; } } -(void)connectionDidFinishLoading:(NSURLConnection *)connection { NSLog(@"5"); // TODO: Return the data if (delegate && callback) { if ([delegate respondsToSelector:self.callback]) { [delegate performSelector:self.callback withObject:receivedData]; } else { // TODO: Return no data NSLog(@"No response data from delegate"); } } else { NSLog(@"Invalid delegate callback"); } //[theConnection release]; //[receivedData release]; //[theRequest release]; } @end