/Volumes/Plantain/MyDocuments/Projects/MacTrek/MacTrek/App/ServerController.m

00001 //
00002 //  ServerController.m
00003 //  MacTrek
00004 //
00005 //  Created by Aqua on 21/04/2006.
00006 //  Copyright 2006 __MyCompanyName__. All rights reserved.
00007 //
00008 
00009 #import "ServerController.h"
00010 
00011 
00012 @implementation ServerController
00013 
00014 
00015 - (id) init {
00016     self = [super init];
00017     if (self != nil) {
00018         serverTask = nil;
00019         logTask = nil;
00020         logDestination = nil;
00021         restartServer = NO;
00022         restartLog = NO;
00023         serverPid = -1;
00024     }
00025     return self;
00026 }
00027 
00028 - (id)initWithTextView:(NSTextView *) destination {
00029     self = [self init];
00030     logDestination = destination;
00031     return self;
00032 }
00033 
00034 - (void)startServer {
00035     
00036     NSString *pathToResources = [[NSBundle mainBundle] resourcePath];
00037     NSString *pathToServer = [NSString stringWithFormat:@"%@/netrek-server-vanilla/bin/netrekd", pathToResources];
00038     NSString *pathToLog = [NSString stringWithFormat:@"%@/netrek-server-vanilla/var/log", pathToResources];
00039     
00040     if (serverTask != nil) {
00041         // still running? stop the server with restart, this will
00042         // start the server again after it is properly stoped
00043         [serverTask stopProcess];
00044         restartServer = YES;        
00045     } else {
00046          // clean start
00047         serverTask = [[TaskWrapper alloc] initWithController:self 
00048                                                    arguments:[NSArray arrayWithObjects:pathToServer, nil] ];
00049         [serverTask startProcess];
00050     }
00051     
00052     if (logTask != nil) {
00053         [logTask stopProcess];
00054         restartLog = YES;        
00055     } else {
00056         logTask = [[TaskWrapper alloc] initWithController:self 
00057                                                 arguments:[NSArray arrayWithObjects:@"/usr/bin/tail",
00058                                                     @"-f", pathToLog, nil] ];
00059         [logTask startProcess];         
00060     }
00061 
00062 }
00063 
00064 - (void) stopServer {
00065     if (serverTask != nil) {
00066         [serverTask stopProcess];
00067     }
00068     if (logTask != nil) {
00069         [logTask stopProcess];
00070     }
00071     if (serverPid > 0) {
00072         // won't run very long so no clean up needed?
00073         TaskWrapper *killTask = [[[TaskWrapper alloc] initWithController:self 
00074             arguments:[NSArray arrayWithObjects:@"/bin/kill", 
00075             [NSString stringWithFormat: @"%d", serverPid], nil] ] autorelease];
00076         [killTask startProcess];
00077         
00078         NSLog(@"ServerController.stopServer kill PID %d", serverPid);
00079         serverPid = -1;
00080     }
00081 }
00082 
00083 - (void)appendOutput:(NSString *)output fromTask:(id) task {
00084     
00085     // scan the text for "started, pid xxxx,"
00086     NSRange myRange = [output rangeOfString:@"started, pid "];
00087     if (myRange.location != NSNotFound) {
00088         // take the rest of the string minus the comma
00089         myRange.location += myRange.length;
00090         myRange.length = [output length] - myRange.location - 1;
00091         serverPid = [[output substringWithRange:myRange] intValue];
00092     }
00093     
00094     // check also for "already running as pid 4623"
00095     myRange = [output rangeOfString:@"already running as pid "];
00096     if (myRange.location != NSNotFound) {
00097         // take the rest of the string minus the comma
00098         myRange.location += myRange.length;
00099         myRange.length = [output length] - myRange.location - 1;
00100         serverPid = [[output substringWithRange:myRange] intValue];
00101     }
00102     
00103     if (logDestination != nil) {
00104         [logDestination setEditable:YES];
00105         [logDestination insertText: output];
00106         [logDestination setEditable:NO];
00107     } else {
00108         NSLog([NSString stringWithFormat: @"ServerController.appendOutput %@", output]);
00109     }
00110 
00111 }
00112 
00113 - (void)processStarted:(id)task {
00114     if (task == serverTask) {
00115         NSLog(@"ServerController.processStarted: serverTask");
00116     } else if (task == logTask) {
00117         NSLog(@"ServerController.processStarted: logTask");
00118     } else {
00119         NSLog(@"ServerController.processStarted: unknown task");
00120     }
00121 }
00122 
00123 
00124 - (void)processFinished:(id)task {
00125     if (task == serverTask) {
00126         //[serverTask release];
00127         serverTask = nil;
00128         NSLog(@"ServerController.processFinished: serverTask done");
00129         // is this stop part of a restart?
00130         if (restartServer) {
00131             serverTask = [[TaskWrapper alloc] initWithController:self 
00132                                                        arguments:[NSArray arrayWithObjects:@"/usr/local/games/netrek-server-vanilla/bin/netrekd", nil] ];
00133             [serverTask startProcess];
00134             restartServer = NO;
00135         }    
00136             
00137     } else if (task == logTask) {
00138         //[logTask release];
00139         logTask = nil;
00140         NSLog(@"ServerController.processFinished: logTask done");
00141         if (restartLog) {
00142             logTask = [[TaskWrapper alloc] initWithController:self 
00143                                                     arguments:[NSArray arrayWithObjects:@"/usr/bin/tail",
00144                                                         @"-f", @"/usr/local/games/netrek-server-vanilla/var/log", nil] ];
00145             [logTask startProcess];  
00146             restartLog = NO;
00147         }    
00148     } else {
00149         NSLog(@"ServerController.processFinished: unknown task done");
00150     }
00151 }
00152 
00153 @end

Generated on Fri Jul 28 19:15:14 2006 for MacTrek by  doxygen 1.4.7