Files
i2p.i2p/launchers/macosx/RouterTask.mm

107 lines
2.7 KiB
Plaintext
Raw Normal View History

#include "RouterTask.h"
#include <dispatch/dispatch.h>
#include <future>
#include <stdlib.h>
#ifdef __cplusplus
#include "include/subprocess.hpp"
#import "I2PLauncher-Swift.h"
#include "AppDelegate.h"
#endif
#include "include/PidWatcher.h"
#import <AppKit/AppKit.h>
#import <Foundation/Foundation.h>
@implementation RTaskOptions
@end
@implementation I2PRouterTask
- (void)routerStdoutData:(NSNotification *)notification
{
NSLog(@"%@", [[NSString alloc] initWithData:[notification.object availableData] encoding:NSUTF8StringEncoding]);
[notification.object waitForDataInBackgroundAndNotify];
}
- (instancetype) initWithOptions : (RTaskOptions*) options
{
self.userRequestedRestart = NO;
self.isRouterRunning = NO;
self.input = [NSFileHandle fileHandleWithStandardInput];
self.routerTask = [NSTask new];
self.processPipe = [NSPipe new];
[self.routerTask setLaunchPath:options.binPath];
[self.routerTask setArguments:options.arguments];
NSDictionary *envDict = @{
@"I2PBASE": options.i2pBaseDir
};
[self.routerTask setEnvironment: envDict];
NSLog(@"Using environment variables: %@", envDict);
[self.routerTask setStandardOutput:self.processPipe];
[self.routerTask setStandardError:self.processPipe];
NSFileHandle *stdoutFileHandle = [self.processPipe fileHandleForReading];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(routerStdoutData:)
name:NSFileHandleDataAvailableNotification
object:stdoutFileHandle];
[stdoutFileHandle waitForDataInBackgroundAndNotify];
[self.routerTask setTerminationHandler:^(NSTask* task) {
// Cleanup
NSLog(@"termHandler triggered!");
auto swiftRouterStatus = [[RouterProcessStatus alloc] init];
[swiftRouterStatus setRouterStatus: false];
[swiftRouterStatus setRouterRanByUs: false];
sendUserNotification(APP_IDSTR, @"I2P Router has stopped");
}];
return self;
}
- (void) requestShutdown
{
[self.routerTask interrupt];
}
- (void) requestRestart
{
self.userRequestedRestart = YES;
kill([self.routerTask processIdentifier], SIGHUP);
}
- (BOOL) isRunning
{
return self.routerTask.running;
}
- (int) execute
{
@try {
[self.routerTask launch];
watchPid([self.routerTask processIdentifier]);
self.isRouterRunning = YES;
return 1;
}
@catch (NSException *e)
{
NSLog(@"Expection occurred %@", [e reason]);
auto swiftRouterStatus = [[RouterProcessStatus alloc] init];
[swiftRouterStatus setRouterStatus: false];
[swiftRouterStatus setRouterRanByUs: false];
sendUserNotification(@"An error occured, can't start the I2P Router", [e reason]);
return 0;
}
}
- (int) getPID
{
return [self.routerTask processIdentifier];
}
@end