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
RevLine 
[6d0f80f]1#include "RouterTask.h"
2
3#include <dispatch/dispatch.h>
4#include <future>
5#include <stdlib.h>
6
[f76874a]7#ifdef __cplusplus
8#include "include/subprocess.hpp"
9#import "I2PLauncher-Swift.h"
10#include "AppDelegate.h"
11#endif
[e8decff5]12
[f76874a]13#include "include/PidWatcher.h"
[6d0f80f]14
15#import <AppKit/AppKit.h>
[f76874a]16#import <Foundation/Foundation.h>
[6d0f80f]17
18@implementation RTaskOptions
19@end
20
[f9fbc76]21@implementation I2PRouterTask
[6d0f80f]22
[c8490a3]23
[f9fbc76]24- (void)routerStdoutData:(NSNotification *)notification
25{
26    NSLog(@"%@", [[NSString alloc] initWithData:[notification.object availableData] encoding:NSUTF8StringEncoding]);
27    [notification.object waitForDataInBackgroundAndNotify];
28}
29
[6d0f80f]30- (instancetype) initWithOptions : (RTaskOptions*) options
31{
[f76874a]32  self.userRequestedRestart = NO;
33  self.isRouterRunning = NO;
[3b38f5a]34  //self.input = [NSFileHandle fileHandleWithStandardInput];
[f76874a]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];
[6d0f80f]45        [self.routerTask setStandardError:self.processPipe];
[f9fbc76]46
[e046418f]47  [self.routerTask setTerminationHandler:^(NSTask* task) {
[e8decff5]48    // Cleanup
[e046418f]49    NSLog(@"termHandler triggered!");
50    auto swiftRouterStatus = [[RouterProcessStatus alloc] init];
[e8decff5]51    [swiftRouterStatus setRouterStatus: false];
52    [swiftRouterStatus setRouterRanByUs: false];
[3b38f5a]53    [swiftRouterStatus triggerEventWithEn:@"router_stop" details:@"normal shutdown"];
54    [[SBridge sharedInstance] setCurrentRouterInstance:nil];
[e046418f]55    sendUserNotification(APP_IDSTR, @"I2P Router has stopped");
56  }];
[6d0f80f]57    return self;
58}
59
[c8490a3]60- (void) requestShutdown
61{
62    [self.routerTask interrupt];
63}
64
65- (void) requestRestart
66{
[f9fbc76]67    self.userRequestedRestart = YES;
68    kill([self.routerTask processIdentifier], SIGHUP);
[c8490a3]69}
70
71- (BOOL) isRunning
72{
73    return self.routerTask.running;
74}
75
[6d0f80f]76- (int) execute
77{
[f9fbc76]78    @try {
[e8decff5]79      [self.routerTask launch];
80      self.isRouterRunning = YES;
81      return 1;
[f9fbc76]82    }
[6d0f80f]83    @catch (NSException *e)
84        {
85                NSLog(@"Expection occurred %@", [e reason]);
[e8decff5]86    auto swiftRouterStatus = [[RouterProcessStatus alloc] init];
[3b38f5a]87    self.isRouterRunning = NO;
[22a0f39]88    [swiftRouterStatus triggerEventWithEn:@"router_exception" details:[e reason]];
[3b38f5a]89    [[SBridge sharedInstance] setCurrentRouterInstance:nil];
[e8decff5]90    sendUserNotification(@"An error occured, can't start the I2P Router", [e reason]);
91    return 0;
[f9fbc76]92        }
[6d0f80f]93}
94
95- (int) getPID
96{
[e8decff5]97  return [self.routerTask processIdentifier];
[6d0f80f]98}
99
100@end
Note: See TracBrowser for help on using the repository browser.