App/RobotsController.m

00001 //
00002 //  RobotsController.m
00003 //  MacTrek
00004 //
00005 //  Created by Aqua on 02/05/2006.
00006 //  Copyright 2006 Luky Soft. All rights reserved.
00007 //
00008 
00009 #import "RobotsController.h"
00010 
00011 
00012 @implementation RobotsController
00013 
00014 
00015 - (id) init {
00016     self = [super init];
00017     if (self != nil) {
00018         robots = [[NSMutableArray alloc] init];
00019         restart = NO;
00020         NSString *pathToResources = [[NSBundle mainBundle] resourcePath];
00021         pathToRobot = [NSString stringWithFormat:@"%@/netrek-server-vanilla/lib/og/robot", pathToResources];
00022         fedNames = [NSArray arrayWithObjects:@"Kirk", @"McCoy", @"Jobs", @"Spock", @"Picard", @"Riker", 
00023             @"T'Poll", @"Kevin", nil];
00024         kliNames = [NSArray arrayWithObjects: @"Scott", @"Kahless", @"Worf", @"Gowron", @"Martok", @"K'mpec",
00025             @"Kor", @"Gates", nil];
00026         // need to retain?
00027         [fedNames retain];
00028         [kliNames retain];
00029         [pathToRobot retain];
00030     }
00031     return self;
00032 }
00033 
00034 - (id)initWithTextView:(NSTextView *) destination {
00035     self = [self init];
00036     logDestination = destination;
00037     return self;
00038 }
00039 
00040 - (void)startRobots:(int)numberOfRobots {
00041     
00042     if ([robots count] > 0) {
00043         // still running? stop the server with restart, this will
00044         // start the server again after it is properly stoped
00045         [self stopRobots];
00046         [self startRobots:numberOfRobots];
00047     } else {
00048         // clean start
00049         NSString *botName;
00050         NSString *team;
00051         for (int i = 0; i < numberOfRobots; i++) {
00052             if ((i % 2) == 0) {
00053                 botName = [fedNames objectAtIndex:(i/2)];
00054                 team = @"-Tf";
00055             } else {
00056                 botName = [kliNames objectAtIndex:i/2];
00057                 team = @"-To";
00058             }
00059             
00060             TaskWrapper *robotTask = [[TaskWrapper alloc] initWithController:self 
00061                                                                    arguments:[NSArray arrayWithObjects: pathToRobot, 
00062                                                                        team, @"-h",  @"localhost", @"-p", @"2592", @"-n"
00063                                                                        , botName, @"-b", @"-O", @"-I", nil] ];
00064             [robotTask startProcess];
00065             [robots addObject:robotTask];
00066         }
00067     }
00068 }
00069 
00070 - (void) stopRobots {
00071     for (int i = 0; i < [robots count]; i++) {
00072         TaskWrapper *robot = [robots objectAtIndex:i];
00073         [robots removeObjectAtIndex:i];
00074         if (robot != nil) {
00075             [robot stopProcess];
00076             [robot release]; 
00077         }       
00078     }
00079 }
00080 
00081 - (void)appendOutput:(NSString *)output fromTask:(id) task {
00082      
00083     if (logDestination != nil) {
00084         [logDestination setEditable:YES];
00085         [logDestination insertText: output];
00086         [logDestination setEditable:NO];
00087     } else {
00088         NSLog([NSString stringWithFormat: @"RobotsController.appendOutput %@", output]);
00089     }
00090     
00091 }
00092 
00093 - (void)processStarted:(id)task {
00094     NSLog(@"RobotsController.processStarted");
00095 }
00096 
00097 - (void)processFinished:(id)task {
00098     NSLog(@"RobotsController.processFinished:  done");
00099 }
00100 
00101 @end

Generated on Sat Aug 26 21:14:07 2006 for MacTrek by  doxygen 1.4.7