source: launchers/macosx/RouterTask.mm @ d27000e

Last change on this file since d27000e was 7a72049, checked in by meeh <meeh@…>, 2 years ago

Mac OS X Launcher:

  • Bugfixes as always
  • Added Sparkle (native updater, https://sparkle-project.org/ )
  • The launcher will now extract and overwrite older versions if found
  • Rewrite of the java extraction part (to enable overwrite)
  • Move more functionality to use EventManager? as it works quite well
  • Added check for updates menu item
  • 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 setRouterStatus: false];
89    [swiftRouterStatus setRouterRanByUs: false];
90    [swiftRouterStatus triggerEventWithEn:@"router_stop" details:@"error shutdown"];
91    [[SBridge sharedInstance] setCurrentRouterInstance:nil];
92    sendUserNotification(@"An error occured, can't start the I2P Router", [e reason]);
93    return 0;
94        }
95}
96
97- (int) getPID
98{
99  return [self.routerTask processIdentifier];
100}
101
102@end
Note: See TracBrowser for help on using the repository browser.