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.

363 lines
8.8 KiB

14 years ago
//
// WRTStatusClient.m
// DDWRT-Monitor
//
// Created by Matthieu Lalonde & Spike Grobstein on 11-05-27.
// Copyleft 2011 Spurf CC BY-SH-NC. Some rights reserved.
14 years ago
//
#import "WRTStatusClient.h"
#import <Security/Security.h>
14 years ago
@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
14 years ago
{
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;
14 years ago
}
#pragma mark -
- (void) registerStatusCallback:(id)status_delegate callback:(SEL)status_callback
{
statusDelegate = status_delegate;
statusCallback = status_callback;
}
14 years ago
- (NSMutableURLRequest*) buildRequest:(NSString*)uri
{
NSString *urlAddress = [NSString stringWithFormat:@"%@://%@:%d/%@",
protocol,
hostname,
port,
uri];
//NSLog(@"Building request form %@", urlAddress);
14 years ago
NSURL *url = [NSURL URLWithString:urlAddress];
//[self request:url]; // ---V
theRequest = [[NSMutableURLRequest alloc] initWithURL: url];
14 years ago
return [theRequest autorelease];
14 years ago
}
- (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
14 years ago
{
14 years ago
delegate = requestDelegate;
callback = requestSelector;
theConnection = [[NSURLConnection alloc] initWithRequest:[self buildRequest: uri] delegate:self];
if (theConnection) {
receivedData = [[NSMutableData data] retain];
}
14 years ago
[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
14 years ago
}
- (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</h2>"];
if (pageValid.location != NSNotFound && startRange.location != NSNotFound) {
// grab the wan port name
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:@"\""];
14 years ago
[self setWanPort:[stringData substringToIndex:endRange.location]];
[self setWrtReachable:true];
} else {
[self setWrtReachable:false];
wanPort = nil;
}
}
}
#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];
}
if (returnData) {
returnData = [returnData stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
}
//[keyString release];
return returnData;
}
14 years ago
#pragma mark -
#pragma mark Accessors:
- (BOOL) getWrtReachable
{
return wrtReachable;
}
- (void) setWrtReachable:(BOOL)new_wrtReachable
{
BOOL old_wrtReachable = wrtReachable;
wrtReachable = new_wrtReachable;
if (old_wrtReachable != new_wrtReachable && statusDelegate && statusCallback) {
if ([statusDelegate respondsToSelector:statusCallback]) {
[statusDelegate performSelector:statusCallback withObject:nil];
}
}
}
- (NSString*) getWanPort
{
return wanPort;
}
- (void) setWanPort:(NSString*)new_wanport
{
NSString *old_wanport = hostname;
wanPort = [new_wanport retain];
[old_wanport autorelease];
}
14 years ago
- (NSString*) getHostname
{
14 years ago
return hostname;
14 years ago
}
- (void) setHostname:(NSString*)new_hostname
{
14 years ago
NSString *old_hostname = hostname;
hostname = [new_hostname retain];
[old_hostname autorelease];
14 years ago
}
- (NSString*) getProtocol
{
return protocol;
}
- (void) setProtocol:(NSString *)new_protocol
{
NSString *old_protocol = protocol;
protocol = [new_protocol retain];
[old_protocol autorelease];
}
- (int) getPort
14 years ago
{
14 years ago
return port;
14 years ago
}
- (void) setPort:(int)new_port
14 years ago
{
14 years ago
port = new_port;
14 years ago
}
- (NSString*) getUsername
{
14 years ago
return username;
14 years ago
}
- (void) setUsername:(NSString*)new_username
{
14 years ago
NSString *old_username = username;
username = [new_username retain];
[old_username autorelease];
14 years ago
}
- (NSString*) getPassword
{
14 years ago
return password;
14 years ago
}
- (void) setPassword:(NSString*)new_password
{
14 years ago
NSString *old_password = password;
password = [new_password retain];
[old_password autorelease];
14 years ago
}
#pragma mark -
#pragma mark Credential Window Methods:
/*
- (void) showCredentialsWindow
{
[credentialsWindow makeKeyAndOrderFront:nil];
}
- (IBAction) hideCredentialsWindow
{
[self setUsername:[fieldUsername stringValue]];
[self setPassword:[fieldPassword stringValue]];
}
*/
#pragma mark -
#pragma mark NSURLConnection Delegate Methods:
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
//NSLog(@"1.0.1");
/* NSAlert *alert = [[NSAlert alloc] init];
[alert setAlertStyle:NSCriticalAlertStyle];
[alert setMessageText:@"LOGIN MOFO"];
[alert setAccessoryView:credentialsView];
[alert layout];
*/
/*NSInteger button = *///[alert runModal];
return NO; // FIXME this need to return YES if an entry doesn't already exist in the keychain and the user will need to be prompted for credentials
}
- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection
{
//NSLog(@"1.0.2");
return YES;
}
14 years ago
-(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:NSURLCredentialPersistencePermanent];
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");
}
}
14 years ago
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//NSLog(@"2");
[receivedData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
14 years ago
//NSLog(@"4");
//[connection release];
//[receivedData release];
//[theRequest release];
//TODO Return alert
NSLog(@"Connection failed! Error - %@ %@", [error localizedDescription], [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
[self setWrtReachable:false];
if (errorCallback) {
[delegate performSelector:errorCallback withObject:error];
} else if (delegate && callback) {
14 years ago
if ([delegate respondsToSelector:callback]) {
[delegate performSelector:callback withObject:nil];
}
}
}
14 years ago
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//NSLog(@"5");
// TODO: Return the data
if (delegate && callback) {
14 years ago
if ([delegate respondsToSelector:callback]) {
[delegate performSelector: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];
}
14 years ago
@end