// // 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" @implementation WRTStatusClient @synthesize wrtReachable; @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]; wrtReachable = false; wanPort = nil; return self; } #pragma mark - - (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 { 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 } - (void) getConnectionStatus { [self getStatusUpdate:@"Status_Bandwidth.asp" delegate:self callback:@selector(cbConnectionStatus:)]; } - (void) cbConnectionStatus:(NSData *)data { if (data == nil) { wrtReachable = false; } else { NSString *stringData = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; //Bandwidth Monitoring NSRange pageValid = [stringData rangeOfString:@"Bandwidth Monitoring"]; if (pageValid.location != NSNotFound) { wrtReachable = true; // grab the wan port name NSRange startRange = [stringData rangeOfString:@"WAN"]; stringData = [stringData substringFromIndex:startRange.location]; NSString *key = @"