// // WRTStatusClient.m // DDWRT-Monitor // // Created by Matthieu Lalonde & Spike Grobstein on 11-05-27. // Copyleft 2011 Spurf CC BY-SH-NC. Some rights reserved. // #import "WRTStatusClient.h" #import @implementation WRTStatusClient @synthesize receivedData; @synthesize delegate; @synthesize callback; @synthesize errorCallback; @synthesize statusDelegate; @synthesize statusCallback; - (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]; wrtReachable = false; wanPort = nil; return self; } #pragma mark - - (void) registerStatusCallback:(id)status_delegate callback:(SEL)status_callback { statusDelegate = status_delegate; statusCallback = status_callback; } - (NSMutableURLRequest*) buildRequest:(NSString*)uri { NSString *urlAddress = [NSString stringWithFormat:@"%@://%@:%d/%@", protocol, 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 *urlAddress = [NSString stringWithFormat:@"%@://%@:%@@%@:%d/graph_if.svg?%@", protocol, username, password, hostname, port, wanPort]; NSLog(@"Building request form %@", urlAddress); NSURL *url = [NSURL URLWithString:urlAddress]; NSURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: url]; return [request autorelease]; } - (void) getStatusUpdate:(NSString*)uri delegate:(id)requestDelegate callback:(SEL)requestSelector { delegate = requestDelegate; 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 } - (void) getConnectionStatus { [self getStatusUpdate:@"Status_Bandwidth.asp" delegate:self callback:@selector(cbConnectionStatus:)]; } - (void) cbConnectionStatus:(NSData *)data { if (data == nil) { [self setWrtReachable:false]; } else { NSString *stringData = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; NSRange pageValid = [stringData rangeOfString:@"Bandwidth Monitoring"]; NSRange startRange = [stringData rangeOfString:@"WAN"]; if (pageValid.location != NSNotFound && startRange.location != NSNotFound) { // grab the wan port name stringData = [stringData substringFromIndex:startRange.location]; NSString *key = @"