#import "BOINCController.h" #import "Preferences.h" #import "GKTask.h" #import "GKStringEncryption.h" @implementation BOINCController NSTask *boinc = nil; + (void)startBOINC { if(!boinc) { boinc = [[[NSTask init] alloc] retain]; [boinc setEnvironment:[BOINCController createEnvironmentDictionary]]; [boinc setStandardOutput:[NSPipe pipe]]; NSFileHandle *outputHandle = [[boinc standardOutput] fileHandleForReading]; [boinc setStandardError:[NSPipe pipe]]; [boinc setCurrentDirectoryPath:[[Preferences prefs] objectForKey:@"boincDataDirectory"]]; [boinc setLaunchPath:[[NSBundle mainBundle] pathForResource:@"boinc" ofType:nil]]; [boinc launch]; [BOINCController waitUntilConnectionIsEstablished]; [[NSNotificationCenter defaultCenter] postNotificationName:@"boincToggled" object:nil]; [outputHandle readInBackgroundAndNotify]; } } + (void)waitUntilConnectionIsEstablished { NSString *output; do { GKTask *boinc_cmd = [GKTask launchedTaskSquelchingOutputWithLaunchPath:[[NSBundle mainBundle] pathForResource:@"boinc_cmd" ofType:nil] arguments:[NSArray arrayWithObject:@"--get_state"]]; NSFileHandle *outputHandle = [[boinc_cmd standardOutput] fileHandleForReading]; output = [[[NSString alloc] initWithData:[outputHandle availableData] encoding:NSASCIIStringEncoding] autorelease]; } while ([output rangeOfString:@"======== Projects ========"].location == NSNotFound); } + (BOOL)boincIsRunning { return boinc != nil; } + (BOOL)attachProjectWithURL:(NSString *)url accountID:(NSString *)accountID { BOOL shouldStopBOINCAfterAttach = NO; if(!boinc) { [BOINCController startBOINC]; [BOINCController waitUntilConnectionIsEstablished]; shouldStopBOINCAfterAttach = YES; } [GKTask launchedTaskSquelchingOutputWithLaunchPath:[[NSBundle mainBundle] pathForResource:@"boinc_cmd" ofType:nil] arguments:[NSArray arrayWithObjects:@"--project_attach", url, accountID, nil]]; if(shouldStopBOINCAfterAttach) [BOINCController stopBOINC]; return YES; //TEMP } + (BOOL)updateProjectWithURL:(NSString *)url { BOOL shouldStopBOINCAfterAttach = NO; if(!boinc) { [BOINCController startBOINC]; [BOINCController waitUntilConnectionIsEstablished]; shouldStopBOINCAfterAttach = YES; } [GKTask launchedTaskSquelchingOutputWithLaunchPath:[[NSBundle mainBundle] pathForResource:@"boinc_cmd" ofType:nil] arguments:[NSArray arrayWithObjects:@"--project", url, @"update", nil]]; if(shouldStopBOINCAfterAttach) [BOINCController stopBOINC]; return YES; //TEMP } + (BOOL)resetProjectWithURL:(NSString *)url { BOOL shouldStopBOINCAfterAttach = NO; if(!boinc) { [BOINCController startBOINC]; [BOINCController waitUntilConnectionIsEstablished]; shouldStopBOINCAfterAttach = YES; } [GKTask launchedTaskSquelchingOutputWithLaunchPath:[[NSBundle mainBundle] pathForResource:@"boinc_cmd" ofType:nil] arguments:[NSArray arrayWithObjects:@"--project", url, @"reset", nil]]; if(shouldStopBOINCAfterAttach) [BOINCController stopBOINC]; return YES; //TEMP } + (BOOL)removeProjectWithURL:(NSString *)url { BOOL shouldStopBOINCAfterAttach = NO; if(!boinc) { [BOINCController startBOINC]; [BOINCController waitUntilConnectionIsEstablished]; shouldStopBOINCAfterAttach = YES; } [GKTask launchedTaskSquelchingOutputWithLaunchPath:[[NSBundle mainBundle] pathForResource:@"boinc_cmd" ofType:nil] arguments:[NSArray arrayWithObjects:@"--project", url, @"detach", nil]]; if(shouldStopBOINCAfterAttach) [BOINCController stopBOINC]; return YES; //TEMP } + (void)stopBOINC { [GKTask launchedTaskSquelchingOutputWithLaunchPath:[[NSBundle mainBundle] pathForResource:@"boinc_cmd" ofType:nil] arguments:[NSArray arrayWithObject:@"--quit"]]; [boinc waitUntilExit]; [boinc release]; boinc = nil; [[NSNotificationCenter defaultCenter] postNotificationName:@"boincToggled" object:nil]; } + (NSDictionary *)createEnvironmentDictionary { NSMutableDictionary *environmentDictionary = [NSMutableDictionary dictionary]; // Creates dictionary of environment variables for an http proxy if([[[Preferences prefs] objectForKey:@"proxyType"] isEqualToString:@"HTTP"]) { [environmentDictionary setObject:[[Preferences prefs] objectForKey:@"proxyAddress"] forKey:@"HTTP_PROXY"]; [environmentDictionary setObject:[[Preferences prefs] objectForKey:@"proxyUserName"] forKey:@"HTTP_USER_NAME"]; [environmentDictionary setObject:cipher([[Preferences prefs] objectForKey:@"proxyPassword"]) forKey:@"HTTP_USER_PASSWD"]; } // Creates dictionary of environment variables for a socks proxy else if([[[Preferences prefs] objectForKey:@"proxyType"] isEqualToString:@"SOCKS"]) { [environmentDictionary setObject:[[Preferences prefs] objectForKey:@"proxyAddress"] forKey:@"SOCKS5_SERVER"]; [environmentDictionary setObject:[[Preferences prefs] objectForKey:@"proxyUserName"] forKey:@"SOCKS5_USER"]; [environmentDictionary setObject:cipher([[Preferences prefs] objectForKey:@"proxyPassword"]) forKey:@"SOCKS5_PASSWD"]; } return environmentDictionary; } @end