1
0
Fork 0

Begining of a status query, ability to auto detect wan port although not implemented yet

master
Matthieu Lalonde 14 years ago
parent cc68591a5c
commit f44983df4c

@ -110,11 +110,10 @@
- (IBAction) refreshClientsMenu:(id)sender;
- (void) saveConfig:(id)sender;
- (void) updateThroughput;
- (void) updateThroughput:(NSTimer*)timer;
- (void) throughputCallback:(NSData *)data;
- (void) doUpdateStatus:(NSData *)data;
- (void) configSaveRequestCallback:(NSData *)data;
- (NSString *) getKey:(NSString * )data key:(NSString *)key;
- (NSString *) stringFromFileSize:(unsigned long long)theSize;
//@property (assign) IBOutlet NSWindow *window;

@ -35,10 +35,12 @@
// initialize the client
wrt_client = [[WRTStatusClient alloc] initWithHostname:hostname port:port protocol:protocol username:username password:password];
//_readTimer = [NSTimer scheduledTimerWithTimeInterval:[[NSUserDefaults standardUserDefaults] integerForKey:@"refreshTime"] target:self selector:@selector(updateThroughput:) userInfo:nil repeats:YES];
//[_readTimer fire];
[wrt_client getConnectionStatus];
NSLog(@"Status %@ wan port: %@", ([wrt_client wrtReachable] ? @"Up" : @"Down"), [wrt_client wanPort]);
_readTimer = [NSTimer scheduledTimerWithTimeInterval:[[NSUserDefaults standardUserDefaults] integerForKey:@"refreshTime"] target:self selector:@selector(updateThroughput:) userInfo:nil repeats:NO];
[_readTimer fire];
[self updateThroughput];
//[self updateThroughput];
//WRTRequest *wrtr = [[WRTRequest alloc] init];
@ -112,6 +114,7 @@
}
- (void) terminate:(id)sender {
[_readTimer invalidate];
[[NSUserDefaults standardUserDefaults] synchronize];
[self dealloc];
[[NSApplication sharedApplication] terminate:self];
@ -280,31 +283,6 @@
[self configSaveRequestCallback:nil];
}
-(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;
}
-(void)configSaveRequestCallback:(NSData *)data
{
// TODO check that the request is actually successful
@ -344,8 +322,9 @@
[self hideConfigPanel:nil];
}
- (void) updateThroughput
- (void) updateThroughput:(NSTimer*)timer
{
NSLog(@"Status %@ wan port: %@", ([wrt_client wrtReachable] ? @"Up" : @"Down"), [wrt_client wanPort]);
//WRTRequest *wrtr = [[WRTRequest alloc] init];
//[wrtr doRequest:self requestSelector:@selector(throughputCallback:) uri:@"fetchif.cgi?ppp1"];
@ -372,7 +351,7 @@
-(void)doUpdateStatus:(NSData *)data
{
NSString *stringData = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
/*
NSString *ipconn = [self getKey:stringData key:@"ip_conntrack"];
NSString *wanip = [self getKey:stringData key:@"ipinfo"];
@ -389,7 +368,7 @@
[menuUptime setTitle:uptime];
[menuLoad setTitle:load];
[menuLoad setHidden:false];
[menuConnections setTitle:[NSString stringWithFormat:@"Connections: %@", ipconn]];
[menuConnections setTitle:[NSString stringWithFormat:@"Connections: %@", ipconn]];*/
}
- (void) refreshMenu:(id)sender
@ -489,7 +468,7 @@
- (void) refreshClientsMenu:(id)sender
{
[self updateThroughput];
[self updateThroughput:nil];
}
- (NSString *) stringFromFileSize:(unsigned long long)theSize

@ -10,13 +10,14 @@
@interface WRTStatusClient : NSObject {
BOOL wrtReachable;
BOOL wrtReachable;
NSString *wanPort;
@private
NSString *hostname;
NSString *protocol;
int port;
NSString *username;
NSString *password;
NSString *hostname;
NSString *protocol;
int port;
NSString *username;
NSString *password;
NSMutableData *receivedData;
NSURLRequest *theRequest;
@ -27,6 +28,9 @@
SEL errorCallback;
}
@property BOOL wrtReachable;
@property(nonatomic, retain) NSString *wanPort;
@property(nonatomic, retain) NSMutableData *receivedData;
@property(nonatomic, retain) id delegate;
@property(nonatomic) SEL callback;
@ -37,6 +41,9 @@
- (NSMutableURLRequest*) buildRequest:(NSString*)uri;
- (NSMutableURLRequest*) requestForBandwidthViewerForInterface:(NSString*)interface;
- (void) getStatusUpdate:(NSString*)uri delegate:(id)requestDelegate callback:(SEL)requestSelector;
- (void) getConnectionStatus;
-(NSString *)getKey:(NSString *)stringData key:(NSString *)key;
- (NSString*) getHostname;
- (void) setHostname:(NSString*)new_hostname;

@ -11,6 +11,9 @@
@implementation WRTStatusClient
@synthesize wrtReachable;
@synthesize wanPort;
@synthesize receivedData;
@synthesize delegate;
@synthesize callback;
@ -26,6 +29,8 @@
[self setUsername: new_username];
[self setPassword: new_password];
wrtReachable = false;
return self;
}
@ -77,6 +82,62 @@
// - (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:
@ -181,10 +242,14 @@
//[theRequest release];
//TODO Return alert
NSLog(@"Connection failed! Error - %@ %@", [error localizedDescription], [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
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];
}
}
}

Loading…
Cancel
Save