source: launchers/macosx/RouterTask.mm @ 22a0f39

Last change on this file since 22a0f39 was 22a0f39, checked in by meeh <meeh@…>, 2 years ago

Mac OS X Launcher:

  • Enabled Apple's "Hardened Runtime", however unsecure memory had to be allowed to spawn java etc.
  • Updated docs about Event Manager code
  • Make the launcher handle cases where extract is incomplete or invalid
  • Bugfixes as always
  • Property mode set to 100644
File size: 2.6 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  [self.routerTask setTerminationHandler:^(NSTask* task) {
48    // Cleanup
49    NSLog(@"termHandler triggered!");
50    auto swiftRouterStatus = [[RouterProcessStatus alloc] init];
51    [swiftRouterStatus setRouterStatus: false];
52    [swiftRouterStatus setRouterRanByUs: false];
53    [swiftRouterStatus triggerEventWithEn:@"router_stop" details:@"normal shutdown"];
54    [[SBridge sharedInstance] setCurrentRouterInstance:nil];
55    sendUserNotification(APP_IDSTR, @"I2P Router has stopped");
56  }];
57    return self;
58}
59
60- (void) requestShutdown
61{
62    [self.routerTask interrupt];
63}
64
65- (void) requestRestart
66{
67    self.userRequestedRestart = YES;
68    kill([self.routerTask processIdentifier], SIGHUP);
69}
70
71- (BOOL) isRunning
72{
73    return self.routerTask.running;
74}
75
76- (int) execute
77{
78    @try {
79      [self.routerTask launch];
80      self.isRouterRunning = YES;
81      return 1;
82    }
83    @catch (NSException *e)
84        {
85                NSLog(@"Expection occurred %@", [e reason]);
86    auto swiftRouterStatus = [[RouterProcessStatus alloc] init];
87    self.isRouterRunning = NO;
88    [swiftRouterStatus triggerEventWithEn:@"router_exception" details:[e reason]];
89    [[SBridge sharedInstance] setCurrentRouterInstance:nil];
90    sendUserNotification(@"An error occured, can't start the I2P Router", [e reason]);
91    return 0;
92        }
93}
94
95- (int) getPID
96{
97  return [self.routerTask processIdentifier];
98}
99
100@end
Note: See TracBrowser for help on using the repository browser.