1
0
Fork 0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
6.7 KiB

//
// WRTStatusClient.m
// DDWRT-Monitor
//
// Created by Spike Grobstein on 5/30/11.
// Copyright 2011 Sadistech. All rights reserved.
//
#import "WRTStatusClient.h"
@implementation WRTStatusClient
@synthesize wrtReachable;
@synthesize wanPort;
@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;
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
}
- (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</h2>"];
stringData = [stringData substringFromIndex:startRange.location];
NSString *key = @"<iframe src=\"/graph_if.svg?";
startRange = [stringData rangeOfString:key];
stringData = [stringData substringFromIndex:(startRange.location + [key length])];
NSRange endRange = [stringData rangeOfString:@"\""];
wanPort = [stringData substringToIndex:endRange.location];
}
}
}
#pragma mark -
#pragma mark Utilities:
-(NSString *)getKey:(NSString *)stringData key:(NSString *)key
{
NSString *keyString = [NSString stringWithFormat:@"%@::", key];
NSString *returnData;
NSRange formatValid = [stringData rangeOfString:@"}"];
if (formatValid.location != NSNotFound) {
NSRange startRange = [stringData rangeOfString:keyString];
if (startRange.location != NSNotFound) {
returnData = [NSString stringWithString:stringData];
returnData = [returnData substringFromIndex:(startRange.location + [keyString length])];
returnData = [returnData substringToIndex:[returnData rangeOfString:@"}"].location];
}
returnData = [returnData stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
//[keyString release];
return returnData;
}
#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:NSURLErrorFailingURLStringErrorKey]);
if (errorCallback) {
[delegate performSelector:errorCallback withObject:error];
} else if (delegate && callback) {
if ([delegate respondsToSelector:self.callback]) {
[delegate performSelector:self.callback withObject:nil];
}
}
}
-(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