Changeset 935
- Timestamp:
- 01/14/05 16:25:52 (4 years ago)
- Files:
-
- WiredServer/trunk/WCStatusController.h (modified) (5 diffs)
- WiredServer/trunk/WCStatusController.m (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
WiredServer/trunk/WCStatusController.h
r923 r935 1 /* $Id: WCStatusController.h,v 1. 1 2005/01/07 23:14:20morris Exp $ */1 /* $Id: WCStatusController.h,v 1.2 2005/01/14 15:25:52 morris Exp $ */ 2 2 3 3 /* … … 31 31 IBOutlet NSTextField *_versionTextField; 32 32 33 IBOutlet NSButton *_startButton;34 IBOutlet NSButton *_restartButton;35 IBOutlet NSButton *_reloadButton;36 IBOutlet NSButton *_registerButton;37 IBOutlet NSButton *_indexButton;38 39 33 IBOutlet NSTextField *_currentUsersTextField; 40 34 IBOutlet NSTextField *_totalUsersTextField; … … 54 48 55 49 NSTimer *_timer; 56 57 50 NSDate *_uptime; 58 51 52 NSMutableArray *_status; 59 53 NSMutableArray *_usersData; 60 54 NSMutableArray *_downloadsData, *_uploadsData; … … 63 57 unsigned long long _inBytes, _outBytes; 64 58 65 BOOL _requiresAuthorization;66 BOOL _launchesAtBoot;67 59 BOOL _running; 68 60 BOOL _available; 69 BOOL _authorized;70 61 } 71 62 … … 79 70 80 71 - (void) awakeFromController; 81 82 - (void) setRequiresAuthorization:(BOOL)value; 83 - (BOOL) requiresAuthorization; 84 - (void) setLaunchesAtBoot:(BOOL)value; 85 - (BOOL) launchesAtBoot; 72 - (void) updateFromController; 86 73 87 74 - (BOOL) isRunning; 88 75 - (BOOL) isAvailable; 89 - (BOOL) isAuthorized; 90 91 - (IBAction) start:(id)sender; 92 - (IBAction) restart:(id)sender; 93 - (IBAction) reload:(id)sender; 94 - (IBAction) registerWithTracker:(id)sender; 95 - (IBAction) indexFiles:(id)sender; 76 - (NSArray *) status; 96 77 97 78 @end WiredServer/trunk/WCStatusController.m
r923 r935 1 /* $Id: WCStatusController.m,v 1. 1 2005/01/07 23:14:20morris Exp $ */1 /* $Id: WCStatusController.m,v 1.2 2005/01/14 15:25:52 morris Exp $ */ 2 2 3 3 /* … … 35 35 - (void) updatePid; 36 36 - (void) updateStatus; 37 38 - (NSArray *) arguments; 37 - (void) updateInterface; 39 38 40 39 @end … … 64 63 65 64 // --- create arrays of graph data 65 _status = [[NSMutableArray alloc] init]; 66 66 _usersData = [[NSMutableArray alloc] initWithCapacity:WCStatusDataPoints]; 67 67 _downloadsData = [[NSMutableArray alloc] initWithCapacity:WCStatusDataPoints]; … … 77 77 [_outData addObject:value]; 78 78 } 79 80 // --- subscribe to these81 [[NSNotificationCenter defaultCenter]82 addObserver:self83 selector:@selector(applicationWillTerminate:)84 name:NSApplicationWillTerminateNotification85 object:NULL];86 87 [[NSNotificationCenter defaultCenter]88 addObserver:self89 selector:@selector(authorizationStatusDidChange:)90 name:WCAuthorizationStatusDidChange91 object:NULL];92 93 [[NSNotificationCenter defaultCenter]94 addObserver:self95 selector:@selector(configDidChange:)96 name:WCConfigDidChange97 object:NULL];98 79 } 99 80 … … 114 95 115 96 97 - (void)updateFromController { 98 [_timer fire]; 99 } 100 101 102 116 103 - (void)dealloc { 117 104 [_timer release]; 118 105 106 [_status release]; 119 107 [_usersData release]; 120 108 [_downloadsData release]; … … 130 118 #pragma mark - 131 119 132 - (void)applicationWillTerminate:(NSNotification *)notification {133 if([self isRunning] && [WCSettings boolForKey:WCTerminateServerOnQuit])134 [self start:self];135 }136 137 138 139 - (void)authorizationStatusDidChange:(NSNotification *)notification {140 _authorized = [[WCAuthorization authorization] isAuthorized];141 142 [self update];143 }144 145 146 147 - (void)configDidChange:(NSNotification *)notification {148 if([self isAvailable] && [self isRunning])149 [self reload:self];150 }151 152 153 154 #pragma mark -155 156 120 - (void)statusTimer:(NSTimer *)timer { 157 121 [self updatePid]; 158 [self updateStatus]; 122 123 if(_running) 124 [self updateStatus]; 125 126 if(_running && [_status count] > 0) 127 [self updateInterface]; 159 128 160 129 [_timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:[WCSettings doubleForKey:WCUpdateInterval]]]; … … 166 135 167 136 - (void)update { 168 BOOL enabled;169 BOOL authorized;170 171 137 // --- set running status 172 138 if([self isRunning]) { … … 180 146 [_statusTextField setStringValue:NSLS(@"Wired is not available", @"Status")]; 181 147 } 182 183 // --- update start/stop button titles184 if([self isRunning])185 [_startButton setTitle:NSLS(@"Stop", "Stop button title")];186 else187 [_startButton setTitle:NSLS(@"Start", "Start button title")];188 189 // --- update buttons190 authorized = [self requiresAuthorization] ? [self isAuthorized] : YES;191 enabled = authorized && [self isAvailable];192 [_startButton setEnabled:enabled];193 enabled = authorized && [self isAvailable] && [self isRunning];194 [_restartButton setEnabled:enabled];195 [_reloadButton setEnabled:enabled];196 [_registerButton setEnabled:enabled];197 [_indexButton setEnabled:enabled];198 148 } 199 149 … … 287 237 288 238 // --- running status changed? 289 if(running != [self isRunning]) {239 if(running != _running) { 290 240 _running = running; 291 241 … … 302 252 303 253 - (void)updateStatus { 304 static BOOL loaded; 305 NSString *status, *inSpeedString, *outSpeedString; 306 NSArray *items; 307 NSDate *date; 308 NSTimeInterval interval; 309 unsigned long long inBytes, outBytes; 310 unsigned int users, downloads, uploads, inSpeed, outSpeed; 254 NSString *status; 255 NSDate *date; 311 256 312 257 // --- read wired status file 313 258 status = [NSString stringWithContentsOfFile:WCExpandWiredPath(@"wired.status")]; 314 259 315 if(status) { 316 interval = [WCSettings doubleForKey:WCUpdateInterval]; 317 items = [status componentsSeparatedByString:@" "]; 318 date = [NSDate dateWithTimeIntervalSince1970:[[items objectAtIndex:0] intValue]]; 260 if(!status) { 261 [_status removeAllObjects]; 262 } else { 263 [_status setArray:[status componentsSeparatedByString:@" "]]; 264 265 date = [NSDate dateWithTimeIntervalSince1970:[[_status objectAtIndex:0] intValue]]; 319 266 320 267 if(![date isEqualToDate:_uptime]) { 321 268 [_uptime release]; 322 269 _uptime = [date retain]; 323 324 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(update) object:NULL]; 325 [self performSelector:@selector(update) withObject:NULL afterDelay:0.1]; 270 271 [self performSelectorOnce:@selector(update) withObject:NULL afterDelay:0.1]; 326 272 } 327 328 users = [[items objectAtIndex:1] unsignedIntValue]; 273 } 274 } 275 276 277 278 - (void)updateInterface { 279 NSString *inSpeedString, *outSpeedString; 280 NSTimeInterval interval; 281 unsigned long long inBytes, outBytes; 282 unsigned int inSpeed, outSpeed; 283 int users, downloads, uploads; 284 285 // --- update users graph 286 if(_usersGraphView) { 287 users = [[_status objectAtIndex:1] intValue]; 329 288 [_usersData removeObjectAtIndex:0]; 330 [_usersData addObject:[NSNumber numberWithUnsignedInt:users]]; 331 332 // --- update users graph 289 [_usersData addObject:[NSNumber numberWithInt:users]]; 333 290 [_usersGraphView setInData:_usersData]; 334 291 [_usersGraphView setNeedsDisplay:YES]; … … 338 295 ? NSLS(@"user", @"User singular") 339 296 : NSLS(@"users", @"User plural")]]; 340 341 // --- get transfers 342 downloads = [[items objectAtIndex:3] unsignedIntValue]; 343 uploads = [[items objectAtIndex:5] unsignedIntValue]; 297 } 298 299 // --- update transfers graph 300 if(_transfersGraphView) { 301 downloads = [[_status objectAtIndex:3] unsignedIntValue]; 302 uploads = [[_status objectAtIndex:5] unsignedIntValue]; 344 303 [_downloadsData removeObjectAtIndex:0]; 345 304 [_downloadsData addObject:[NSNumber numberWithUnsignedInt:downloads]]; 346 305 [_uploadsData removeObjectAtIndex:0]; 347 306 [_uploadsData addObject:[NSNumber numberWithUnsignedInt:uploads]]; 348 349 // --- update transfers graph350 307 [_transfersGraphView setInData:_uploadsData]; 351 308 [_transfersGraphView setOutData:_downloadsData]; … … 361 318 ? NSLS(@"dl", @"Download singular") 362 319 : NSLS(@"dls", @"Download plural")]]; 363 364 // --- get bandwidth 365 inBytes = [[items objectAtIndex:8] unsignedLongLongValue]; 366 outBytes = [[items objectAtIndex:7] unsignedLongLongValue]; 320 } 321 322 // --- update bandwidth graph 323 if(_bandwidthGraphView) { 324 inBytes = [[_status objectAtIndex:8] unsignedLongLongValue]; 325 outBytes = [[_status objectAtIndex:7] unsignedLongLongValue]; 367 326 inSpeed = 0; 368 327 outSpeed = 0; 369 328 370 if(loaded) { 371 if(inBytes > _inBytes) 329 if(_inBytes > 0 && _outBytes > 0) { 330 interval = [WCSettings doubleForKey:WCUpdateInterval]; 331 332 if(_inBytes > 0 && inBytes > _inBytes) 372 333 inSpeed = (inBytes - _inBytes) / interval; 373 334 … … 375 336 outSpeed = (outBytes - _outBytes) / interval; 376 337 } 377 378 // --- update bandwidth data 338 339 inSpeedString = [NSSWF:@"%@/s", [NSString humanReadableStringForSize:inSpeed]]; 340 outSpeedString = [NSSWF:@"%@/s", [NSString humanReadableStringForSize:outSpeed]]; 341 379 342 [_inData removeObjectAtIndex:0]; 380 343 [_inData addObject:[NSNumber numberWithUnsignedInt:inSpeed]]; 381 344 [_outData removeObjectAtIndex:0]; 382 345 [_outData addObject:[NSNumber numberWithUnsignedInt:outSpeed]]; 383 384 // --- get strings385 inSpeedString = [NSSWF:@"%@/s", [NSString humanReadableStringForSize:inSpeed]];386 outSpeedString = [NSSWF:@"%@/s", [NSString humanReadableStringForSize:outSpeed]];387 388 // --- update bandwidth graph389 346 [_bandwidthGraphView setInData:_inData]; 390 347 [_bandwidthGraphView setOutData:_outData]; … … 396 353 outSpeedString, 397 354 NSLS(@"out", @"Speed out")]]; 398 399 // --- update fields 400 [_currentUsersTextField setIntValue:users]; 401 [_totalUsersTextField setStringValue:[items objectAtIndex:2]]; 402 [_currentDownloadsTextField setIntValue:downloads]; 403 [_totalDownloadsTextField setStringValue:[items objectAtIndex:4]]; 404 [_currentUploadsTextField setIntValue:uploads]; 405 [_totalUploadsTextField setStringValue:[items objectAtIndex:6]]; 406 [_dataInTextField setStringValue:[NSString humanReadableStringForSize:inBytes]]; 407 [_dataOutTextField setStringValue:[NSString humanReadableStringForSize:outBytes]]; 408 [_dataInPerSecTextField setStringValue:inSpeedString]; 409 [_dataOutPerSecTextField setStringValue:outSpeedString]; 410 411 // --- reset 355 412 356 _inBytes = inBytes; 413 357 _outBytes = outBytes; 414 loaded = YES; 415 } 416 } 417 418 419 420 #pragma mark - 421 422 - (NSArray *)arguments { 423 NSMutableArray *arguments; 424 425 arguments = [NSMutableArray array]; 426 427 switch([[WCSettings objectForKey:WCLogMethod] intValue]) { 428 case WCLogMethodSyslog: 429 [arguments addObject:@"-s"]; 430 [arguments addObject:[WCSettings objectForKey:WCSyslogFacility]]; 431 break; 432 433 case WCLogMethodFile: 434 [arguments addObject:@"-L"]; 435 [arguments addObject:[WCSettings objectForKey:WCLogFile]]; 436 437 if([WCSettings boolForKey:WCLimitLogFile]) { 438 [arguments addObject:@"-i"]; 439 [arguments addObject:[NSSWF:@"%u", [WCSettings intForKey:WCLimitLogFileLines]]]; 440 } 441 break; 442 } 443 444 return arguments; 445 } 446 447 448 449 #pragma mark - 450 451 - (void)setRequiresAuthorization:(BOOL)value { 452 _requiresAuthorization = value; 453 } 454 455 456 457 - (BOOL)requiresAuthorization { 458 return _requiresAuthorization; 459 } 460 461 462 463 - (void)setLaunchesAtBoot:(BOOL)value { 464 NSFileManager *fileManager; 465 NSString *path, *owner, *temp, *string; 466 WCAuthorization *authorization; 467 WCConfig *config; 468 BOOL status; 469 470 authorization = [WCAuthorization authorization]; 471 fileManager = [NSFileManager defaultManager]; 472 path = WCExpandWiredPath(@"etc/wired.startup"); 473 474 if(value) { 475 string = [[self arguments] componentsJoinedByString:@" "]; 476 owner = [fileManager ownerAtPath:path]; 477 478 if(!owner) 479 owner = [fileManager ownerAtPath:[path stringByDeletingLastPathComponent]]; 480 481 if([owner isEqualToString:NSUserName()]) { 482 [string writeToFile:path atomically:YES]; 483 } 484 else if([authorization isAuthorized]) { 485 temp = [NSFileManager temporaryPathWithPrefix:@"wired" suffix:@"startup"]; 486 status = [string writeToFile:temp atomically:YES]; 487 488 if(status) 489 status = [authorization movePath:temp toPath:path]; 490 491 if(status) { 492 config = [[WCConfigController configController] config]; 493 status = [authorization changeOwnerOfPath:path 494 toOwner:[config stringForKey:@"user"] 495 group:[config stringForKey:@"group"]]; 496 } 497 } 498 } else { 499 if([[fileManager ownerAtPath:path] isEqualToString:NSUserName()]) 500 [fileManager removeFileAtPath:path]; 501 else if([authorization isAuthorized]) 502 [authorization removeFileAtPath:path]; 503 } 504 505 _launchesAtBoot = value; 506 } 507 508 509 510 - (BOOL)launchesAtBoot { 511 return _launchesAtBoot; 358 } 359 360 // --- update fields 361 [_currentUsersTextField setIntValue:users]; 362 [_totalUsersTextField setStringValue:[_status objectAtIndex:2]]; 363 [_currentDownloadsTextField setIntValue:downloads]; 364 [_totalDownloadsTextField setStringValue:[_status objectAtIndex:4]]; 365 [_currentUploadsTextField setIntValue:uploads]; 366 [_totalUploadsTextField setStringValue:[_status objectAtIndex:6]]; 367 [_dataInTextField setStringValue:[NSString humanReadableStringForSize:inBytes]]; 368 [_dataOutTextField setStringValue:[NSString humanReadableStringForSize:outBytes]]; 369 [_dataInPerSecTextField setStringValue:inSpeedString]; 370 [_dataOutPerSecTextField setStringValue:outSpeedString]; 512 371 } 513 372 … … 528 387 529 388 530 - (BOOL)isAuthorized { 531 return _authorized; 532 } 533 534 535 536 #pragma mark - 537 538 - (IBAction)start:(id)sender { 539 NSMutableArray *arguments; 540 541 arguments = [NSMutableArray array]; 542 543 if([self isRunning]) { 544 [arguments addObject:@"stop"]; 545 } else { 546 [arguments addObject:@"start"]; 547 [arguments addObjectsFromArray:[self arguments]]; 548 } 549 550 [[WCAuthorization authorization] launchTaskWithPath:WCExpandWiredPath(@"wiredctl") 551 arguments:arguments]; 552 } 553 554 555 556 - (IBAction)restart:(id)sender { 557 NSMutableArray *arguments; 558 559 arguments = [NSMutableArray array]; 560 [arguments addObject:@"restart"]; 561 [arguments addObjectsFromArray:[self arguments]]; 562 563 [[WCAuthorization authorization] launchTaskWithPath:WCExpandWiredPath(@"wiredctl") 564 arguments:arguments]; 565 } 566 567 568 569 - (IBAction)reload:(id)sender { 570 [[WCAuthorization authorization] launchTaskWithPath:WCExpandWiredPath(@"wiredctl") 571 arguments:[NSArray arrayWithObject:@"reload"]]; 572 } 573 574 575 576 - (IBAction)registerWithTracker:(id)sender { 577 [[WCAuthorization authorization] launchTaskWithPath:WCExpandWiredPath(@"wiredctl") 578 arguments:[NSArray arrayWithObject:@"register"]]; 579 } 580 581 582 583 - (IBAction)indexFiles:(id)sender { 584 [[WCAuthorization authorization] launchTaskWithPath:WCExpandWiredPath(@"wiredctl") 585 arguments:[NSArray arrayWithObject:@"index"]]; 389 - (NSArray *)status { 390 return [_status count] > 0 ? _status : NULL; 586 391 } 587 392
