source: launchers/macosx/RouterTask.mm @ e8decff5

Last change on this file since e8decff5 was e8decff5, checked in by meeh <meeh@…>, 21 months ago

Mac OS X Launcher: more cleanup and some bugfixes.

  • Property mode set to 100644
File size: 2.7 KB
Line 
1#include "RouterTask.h"
2
3#include <dispatch/dispatch.h>
4#include <future>
5#include <stdlib.h>
6
7#ifdef __cplusplus
8#include "include/subprocess.hpp"
9#import "I2PLauncher-Swift.h"
10#include "AppDelegate.h"
11#endif
12
13#include "include/PidWatcher.h"
14
15#import <AppKit/AppKit.h>
16#import <Foundation/Foundation.h>
17
18@implementation RTaskOptions
19@end
20
21@implementation I2PRouterTask
22
23
24- (void)routerStdoutData:(NSNotification *)notification
25{
26    NSLog(@"%@", [[NSString alloc] initWithData:[notification.object availableData] encoding:NSUTF8StringEncoding]);
27    [notification.object waitForDataInBackgroundAndNotify];
28}
29
30- (instancetype) initWithOptions : (RTaskOptions*) options
31{
32  self.userRequestedRestart = NO;
33  self.isRouterRunning = NO;
34  self.input = [NSFileHandle fileHandleWithStandardInput];
35  self.routerTask = [NSTask new];
36  self.processPipe = [NSPipe new];
37  [self.routerTask setLaunchPath:options.binPath];
38  [self.routerTask setArguments:options.arguments];
39  NSDictionary *envDict = @{
40    @"I2PBASE": options.i2pBaseDir
41  };
42  [self.routerTask setEnvironment: envDict];
43  NSLog(@"Using environment variables: %@", envDict);
44  [self.routerTask setStandardOutput:self.processPipe];
45        [self.routerTask setStandardError:self.processPipe];
46
47    NSFileHandle *stdoutFileHandle = [self.processPipe fileHandleForReading];
48    [[NSNotificationCenter defaultCenter] addObserver:self
49        selector:@selector(routerStdoutData:)
50        name:NSFileHandleDataAvailableNotification
51        object:stdoutFileHandle];
52
53  [stdoutFileHandle waitForDataInBackgroundAndNotify];
54
55  [self.routerTask setTerminationHandler:^(NSTask* task) {
56    // Cleanup
57    NSLog(@"termHandler triggered!");
58    auto swiftRouterStatus = [[RouterProcessStatus alloc] init];
59    [swiftRouterStatus setRouterStatus: false];
60    [swiftRouterStatus setRouterRanByUs: false];
61    sendUserNotification(APP_IDSTR, @"I2P Router has stopped");
62  }];
63    return self;
64}
65
66- (void) requestShutdown
67{
68    [self.routerTask interrupt];
69}
70
71- (void) requestRestart
72{
73    self.userRequestedRestart = YES;
74    kill([self.routerTask processIdentifier], SIGHUP);
75}
76
77- (BOOL) isRunning
78{
79    return self.routerTask.running;
80}
81
82- (int) execute
83{
84    @try {
85      [self.routerTask launch];
86      watchPid([self.routerTask processIdentifier]);
87      self.isRouterRunning = YES;
88      return 1;
89    }
90    @catch (NSException *e)
91        {
92                NSLog(@"Expection occurred %@", [e reason]);
93    auto swiftRouterStatus = [[RouterProcessStatus alloc] init];
94    [swiftRouterStatus setRouterStatus: false];
95    [swiftRouterStatus setRouterRanByUs: false];
96    sendUserNotification(@"An error occured, can't start the I2P Router", [e reason]);
97    return 0;
98        }
99}
100
101- (int) getPID
102{
103  return [self.routerTask processIdentifier];
104}
105
106@end
Note: See TracBrowser for help on using the repository browser.