1
0
Fork 0

FIXED - useBytes

FIXED - Time intervals
FIXED - No menu icon option
master
Matthieu Lalonde 14 years ago
parent bc547d0c36
commit 09c3127e61

@ -79,7 +79,7 @@
WRTStatusClient *wrt_client; WRTStatusClient *wrt_client;
NSTimeInterval _lastDateThroughput; NSDate *_lastDateThroughput;
unsigned long long _lastInThroughput; unsigned long long _lastInThroughput;
unsigned long long _lastOutThroughput; unsigned long long _lastOutThroughput;
@ -122,6 +122,7 @@
- (void) doUpdateStatus:(NSData *)data; - (void) doUpdateStatus:(NSData *)data;
- (IBAction) copyParentMenuTitle:(id)sender; - (IBAction) copyParentMenuTitle:(id)sender;
- (NSString *) stringFromFileSize:(unsigned long long)theSize; - (NSString *) stringFromSize:(unsigned long long)theBytes;
- (NSString *) stringFromSpeed:(unsigned long long)theBytes;
@end @end

@ -40,12 +40,6 @@
_readTimer = [NSTimer scheduledTimerWithTimeInterval:refreshTime+1 target:self selector:@selector(updateThroughput:) userInfo:nil repeats:YES]; _readTimer = [NSTimer scheduledTimerWithTimeInterval:refreshTime+1 target:self selector:@selector(updateThroughput:) userInfo:nil repeats:YES];
[_readTimer fire]; [_readTimer fire];
//[self updateThroughput];
//WRTRequest *wrtr = [[WRTRequest alloc] init];
//[wrtr doRequest:self requestSelector:@selector(doUpdateStatus:) uri:@"Status_Router.live.asp"];
} }
- (void) dealloc { - (void) dealloc {
@ -335,7 +329,11 @@
NSLog(@"Status %@ wan port: %@", ([wrt_client wrtReachable] ? @"Up" : @"Down"), [wrt_client getWanPort]); NSLog(@"Status %@ wan port: %@", ([wrt_client wrtReachable] ? @"Up" : @"Down"), [wrt_client getWanPort]);
if ([wrt_client wrtReachable] == true && _wrtReachable == false) { if ([wrt_client wrtReachable] == true && _wrtReachable == false) {
_wrtReachable = true; _wrtReachable = true;
if (showMenuIcon == true) {
[self showMenubarIcon:true]; [self showMenubarIcon:true];
} else {
[self hideMenubarIcon];
}
} else if ([wrt_client wrtReachable] == false && _wrtReachable == true) { } else if ([wrt_client wrtReachable] == false && _wrtReachable == true) {
_wrtReachable = false; _wrtReachable = false;
[self showMenubarIcon:false]; [self showMenubarIcon:false];
@ -343,13 +341,7 @@
if (_wrtReachable == true) { if (_wrtReachable == true) {
[wrt_client getStatusUpdate:[NSString stringWithFormat:@"fetchif.cgi?%@", [wrt_client getWanPort]] delegate:self callback:@selector(throughputCallback:)]; [wrt_client getStatusUpdate:[NSString stringWithFormat:@"fetchif.cgi?%@", [wrt_client getWanPort]] delegate:self callback:@selector(throughputCallback:)];
//[wrt_client getStatusUpdate:@"Status_Router.live.asp" delegate:self callback:@selector(doUpdateStatus:)];
//[wrt_client getStatusUpdate:@"Status_Internet.live.asp" delegate:self callback:@selector(wanRequestCallback:)];
} }
//WRTRequest *wrtr = [[WRTRequest alloc] init];
//[wrtr doRequest:self requestSelector:@selector(throughputCallback:) uri:@"fetchif.cgi?ppp1"];
} }
- (void) throughputCallback:(NSData *)data - (void) throughputCallback:(NSData *)data
@ -372,16 +364,21 @@
unsigned long long diffIn = ifIn - _lastInThroughput; unsigned long long diffIn = ifIn - _lastInThroughput;
unsigned long long diffOut = ifOut - _lastOutThroughput; unsigned long long diffOut = ifOut - _lastOutThroughput;
double speedIn = diffIn / ([NSDate timeIntervalSinceReferenceDate] - _lastDateThroughput); double timeDiff = fabs([_lastDateThroughput timeIntervalSinceNow]);
double speedOut = diffOut / ([NSDate timeIntervalSinceReferenceDate] - _lastDateThroughput); _lastDateThroughput = [[NSDate date] retain];
if (timeDiff <= 0) timeDiff = 1; // avoid division by zero
double speedIn = diffIn / timeDiff;
double speedOut = diffOut / timeDiff;
speedIn = round(2.2f * speedIn) / 2.2f; speedIn = round(2.2f * speedIn) / 2.2f;
speedOut = round(2.2f * speedOut) / 2.2f; speedOut = round(2.2f * speedOut) / 2.2f;
[ self setMenubarText:[NSString stringWithFormat:@"%@s\n%@s", [self stringFromSpeed:speedOut], [self stringFromSpeed:speedIn]] ];
[ self setMenubarText:[NSString stringWithFormat:@"%@s\n%@s", [self stringFromFileSize:speedOut], [self stringFromFileSize:speedIn]] ]; NSLog(@"In %llu Out %llu Last In %llu Last Out %llu diffIn %llu diffOut %llu In %f Out %f Interval %f", ifIn, ifOut, _lastInThroughput,
_lastOutThroughput, diffIn, diffOut, speedIn, speedOut, timeDiff);
} }
_lastDateThroughput = [NSDate timeIntervalSinceReferenceDate];
_lastInThroughput = ifIn; _lastInThroughput = ifIn;
_lastOutThroughput = ifOut; _lastOutThroughput = ifOut;
} }
@ -391,7 +388,7 @@
[wrt_client getStatusUpdate:@"Status_Internet.live.asp" delegate:self callback:@selector(wanRequestCallback:)]; [wrt_client getStatusUpdate:@"Status_Internet.live.asp" delegate:self callback:@selector(wanRequestCallback:)];
NSString *stringData = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; NSString *stringData = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"%@", stringData);
NSString *ipconn = [wrt_client getKey:stringData key:@"ip_conntrack"]; NSString *ipconn = [wrt_client getKey:stringData key:@"ip_conntrack"];
NSString *wanip = [wrt_client getKey:stringData key:@"ipinfo"]; NSString *wanip = [wrt_client getKey:stringData key:@"ipinfo"];
@ -415,10 +412,14 @@
- (void) refreshMenu:(id)sender - (void) refreshMenu:(id)sender
{ {
NSLog(@"Refresh Menu"); NSLog(@"Refresh Menu");
if ([wrt_client wrtReachable] == true) {
[_readTimer invalidate]; [_readTimer invalidate];
_readTimer = nil; _readTimer = nil;
[wrt_client getStatusUpdate:@"Status_Router.live.asp" delegate:self callback:@selector(doUpdateStatus:)]; [wrt_client getStatusUpdate:@"Status_Router.live.asp" delegate:self callback:@selector(doUpdateStatus:)];
} else {
[_appMenu popUpStatusItemMenu:AppMenu];
}
} }
@ -476,8 +477,8 @@
NSString *trafficIn = [wrt_client getKey:stringData key:@"ttraff_in"]; NSString *trafficIn = [wrt_client getKey:stringData key:@"ttraff_in"];
NSString *trafficOut = [wrt_client getKey:stringData key:@"ttraff_out"]; NSString *trafficOut = [wrt_client getKey:stringData key:@"ttraff_out"];
trafficIn = [self stringFromFileSize:([trafficIn longLongValue] * 1024 * 1024)]; trafficIn = [self stringFromSize:([trafficIn longLongValue] * 1024 * 1024)];
trafficOut = [self stringFromFileSize:([trafficOut longLongValue] * 1024 * 1024)]; trafficOut = [self stringFromSize:([trafficOut longLongValue] * 1024 * 1024)];
[menuWanTrafficIn setTitle:[NSString stringWithFormat:@"In: %@", trafficIn]]; [menuWanTrafficIn setTitle:[NSString stringWithFormat:@"In: %@", trafficIn]];
[menuWanTrafficOut setTitle:[NSString stringWithFormat:@"Out: %@", trafficOut]]; [menuWanTrafficOut setTitle:[NSString stringWithFormat:@"Out: %@", trafficOut]];
@ -619,26 +620,58 @@
[pasteBoard setString:[[sender parentItem] title] forType:NSStringPboardType]; [pasteBoard setString:[[sender parentItem] title] forType:NSStringPboardType];
} }
- (NSString *) stringFromFileSize:(unsigned long long)theSize - (NSString *) stringFromSize:(unsigned long long)theBytes
{ {
double floatSize = theBytes;
double floatSize = theSize; if (theBytes<1023)
if (theSize<1023) return([NSString stringWithFormat:@"%lluB", theBytes]);
return([NSString stringWithFormat:@"%qxB",theSize]);
floatSize = floatSize / 1024; floatSize = floatSize / 1024;
if (floatSize<1023) if (floatSize<1023)
return([NSString stringWithFormat:@"%1.1fKiB",floatSize]); return([NSString stringWithFormat:@"%1.1fKiB", floatSize]);
floatSize = floatSize / 1024; floatSize = floatSize / 1024;
if (floatSize<1023) if (floatSize<1023)
return([NSString stringWithFormat:@"%1.1fMiB",floatSize]); return([NSString stringWithFormat:@"%1.2fMiB", floatSize]);
floatSize = floatSize / 1024; floatSize = floatSize / 1024;
if (floatSize<1023) if (floatSize<1023)
return([NSString stringWithFormat:@"%1.1fGiB",floatSize]); return([NSString stringWithFormat:@"%1.2fGiB", floatSize]);
floatSize = floatSize / 1024; floatSize = floatSize / 1024;
return([NSString stringWithFormat:@"%1.1fTiB",floatSize]); return([NSString stringWithFormat:@"%1.2fTiB", floatSize]);
} }
- (NSString *) stringFromSpeed:(unsigned long long)theBytes
{
unsigned int minSize = /*(useBytes ? */1023/* : 999)*/;
unsigned int divSize = /*(useBytes ? */1024/* : 1000)*/;
NSString *suffix;
if (useBytes == false) {
theBytes *= 8;
}
double floatSize = theBytes;
suffix = (useBytes ? @"B" : @"b");
if (theBytes<minSize)
return([NSString stringWithFormat:@"%llu%@", theBytes, suffix]);
floatSize = floatSize / divSize;
suffix = (useBytes ? @"KiB" : @"Kb");
if (floatSize<minSize)
return([NSString stringWithFormat:@"%1.1f%@", floatSize, suffix]);
floatSize = floatSize / divSize;
suffix = (useBytes ? @"MiB" : @"Mb");
if (floatSize<minSize)
return([NSString stringWithFormat:@"%1.2f%@", floatSize, suffix]);
floatSize = floatSize / divSize;
suffix = (useBytes ? @"GiB" : @"Gb");
if (floatSize<minSize)
return([NSString stringWithFormat:@"%1.2f%@", floatSize, suffix]);
floatSize = floatSize / divSize;
suffix = (useBytes ? @"TiB" : @"Tb");
return([NSString stringWithFormat:@"%1.2f%@", floatSize, suffix]);
}
- (void) connection:(NSURLConnection*)connection didReceiveData:(NSData*)data - (void) connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{ {

@ -231,6 +231,16 @@
#pragma mark - #pragma mark -
#pragma mark NSURLConnection Delegate Methods: #pragma mark NSURLConnection Delegate Methods:
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
return YES;
}
- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection
{
return YES;
}
-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge -(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{ {
NSLog(@"1"); NSLog(@"1");

Loading…
Cancel
Save