source: launchers/macosx/RouterTask.mm @ e046418f

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

Cleaning up the code base, remove dead code and failed attempts.

  • Property mode set to 100644
File size: 4.5 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#include "include/PidWatcher.h"
13
14#import <AppKit/AppKit.h>
15#import <Foundation/Foundation.h>
16
17@implementation RTaskOptions
18@end
19
20@implementation I2PRouterTask
21
22
23- (void)routerStdoutData:(NSNotification *)notification
24{
25    NSLog(@"%@", [[NSString alloc] initWithData:[notification.object availableData] encoding:NSUTF8StringEncoding]);
26    [notification.object waitForDataInBackgroundAndNotify];
27}
28
29- (instancetype) initWithOptions : (RTaskOptions*) options
30{
31  self.userRequestedRestart = NO;
32  self.isRouterRunning = NO;
33  self.input = [NSFileHandle fileHandleWithStandardInput];
34  self.routerTask = [NSTask new];
35  self.processPipe = [NSPipe new];
36  [self.routerTask setLaunchPath:options.binPath];
37  [self.routerTask setArguments:options.arguments];
38  NSDictionary *envDict = @{
39    @"I2PBASE": options.i2pBaseDir
40  };
41  [self.routerTask setEnvironment: envDict];
42  NSLog(@"Using environment variables: %@", envDict);
43  [self.routerTask setStandardOutput:self.processPipe];
44        [self.routerTask setStandardError:self.processPipe];
45
46    NSFileHandle *stdoutFileHandle = [self.processPipe fileHandleForReading];
47    [[NSNotificationCenter defaultCenter] addObserver:self
48        selector:@selector(routerStdoutData:)
49        name:NSFileHandleDataAvailableNotification
50        object:stdoutFileHandle];
51
52  [stdoutFileHandle waitForDataInBackgroundAndNotify];
53
54  [self.routerTask setTerminationHandler:^(NSTask* task) {
55    NSLog(@"termHandler triggered!");
56    auto swiftRouterStatus = [[RouterProcessStatus alloc] init];
57    [swiftRouterStatus setRouterStatus: true];
58    sendUserNotification(APP_IDSTR, @"I2P Router has stopped");
59    // Cleanup
60    //self.isRouterRunning = NO;
61  }];
62/*
63    self.readLogHandle = [self.processPipe fileHandleForReading];
64    NSData *inData = nil;
65    self.totalLogData = [[[NSMutableData alloc] init] autorelease];
66
67    while ((inData = [self.readLogHandle availableData]) &&
68        [inData length]) {
69        [self.totalLogData appendData:inData];
70    }
71*/
72    return self;
73}
74
75- (void) requestShutdown
76{
77    [self.routerTask interrupt];
78}
79
80- (void) requestRestart
81{
82    self.userRequestedRestart = YES;
83    kill([self.routerTask processIdentifier], SIGHUP);
84}
85
86- (BOOL) isRunning
87{
88    return self.routerTask.running;
89}
90
91- (int) execute
92{
93    @try {
94        [self.routerTask launch];
95        watchPid([self.routerTask processIdentifier]);
96        self.isRouterRunning = YES;
97        return 1;
98    }
99    @catch (NSException *e)
100        {
101                NSLog(@"Expection occurred %@", [e reason]);
102        return 0;
103        }
104}
105
106- (int) getPID
107{
108    return [self.routerTask processIdentifier];
109}
110
111@end
112
113#ifdef __cplusplus
114
115
116const std::vector<NSString*> JavaRunner::defaultStartupFlags {
117    @"-Xmx512M",
118    @"-Xms128m",
119    @"-Djava.awt.headless=true",
120    @"-Dwrapper.logfile=/tmp/router.log",
121    @"-Dwrapper.logfile.loglevel=DEBUG",
122    @"-Dwrapper.java.pidfile=/tmp/routerjvm.pid",
123    @"-Dwrapper.console.loglevel=DEBUG"
124};
125
126const std::vector<std::string> JavaRunner::defaultFlagsForExtractorJob {
127    "-Xmx512M",
128    "-Xms128m",
129    "-Djava.awt.headless=true"
130};
131
132JavaRunner::JavaRunner(std::string& javaBin, std::string& arguments, std::string& i2pBaseDir, const fp_proc_t& execFn, const fp_t& cb)
133  : javaBinaryPath(javaBin), javaRouterArgs(arguments), _i2pBaseDir(i2pBaseDir), executingFn(execFn), exitCallbackFn(cb)
134{
135  execLine = javaBinaryPath;
136  execLine += " " + std::string(javaRouterArgs.c_str());
137  printf("CLI: %s\n",execLine.c_str());
138  javaProcess = std::shared_ptr<Popen>(new Popen(execLine, environment{{
139            {"I2PBASE", _i2pBaseDir},
140            {"JAVA_OPTS", getenv("JAVA_OPTS")}
141        }}, defer_spawn{true}));
142}
143
144void JavaRunner::requestRouterShutdown()
145{
146    // SIGHUP
147    javaProcess->kill(1);
148}
149
150std::future<int> JavaRunner::execute()
151{
152  try {
153    auto executingFn = dispatch_block_create(DISPATCH_BLOCK_INHERIT_QOS_CLASS, ^{
154      this->executingFn(this);
155    });
156    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), executingFn);
157    dispatch_block_wait(executingFn, DISPATCH_TIME_FOREVER);
158
159    // Here, the process is done executing.
160
161    printf("Finished executingFn - Runs callbackFn\n");
162    this->exitCallbackFn();
163    return std::async(std::launch::async, []{ return 0; });
164  } catch (std::exception* ex) {
165    printf("ERROR: %s\n", ex->what());
166    return std::async(std::launch::async, []{ return 1; });
167  }
168}
169
170#endif
Note: See TracBrowser for help on using the repository browser.