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 | JavaRunner::JavaRunner(std::string& javaBin, std::string& arguments, std::string& i2pBaseDir, const fp_proc_t& execFn, const fp_t& cb) |
---|
116 | : javaBinaryPath(javaBin), javaRouterArgs(arguments), _i2pBaseDir(i2pBaseDir), executingFn(execFn), exitCallbackFn(cb) |
---|
117 | { |
---|
118 | execLine = javaBinaryPath; |
---|
119 | execLine += " " + std::string(javaRouterArgs.c_str()); |
---|
120 | printf("CLI: %s\n",execLine.c_str()); |
---|
121 | javaProcess = std::shared_ptr<Popen>(new Popen(execLine, environment{{ |
---|
122 | {"I2PBASE", _i2pBaseDir}, |
---|
123 | {"JAVA_OPTS", getenv("JAVA_OPTS")} |
---|
124 | }}, defer_spawn{true})); |
---|
125 | } |
---|
126 | |
---|
127 | void JavaRunner::requestRouterShutdown() |
---|
128 | { |
---|
129 | // SIGHUP |
---|
130 | javaProcess->kill(1); |
---|
131 | } |
---|
132 | |
---|
133 | std::future<int> JavaRunner::execute() |
---|
134 | { |
---|
135 | try { |
---|
136 | auto executingFn = dispatch_block_create(DISPATCH_BLOCK_INHERIT_QOS_CLASS, ^{ |
---|
137 | this->executingFn(this); |
---|
138 | }); |
---|
139 | dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), executingFn); |
---|
140 | dispatch_block_wait(executingFn, DISPATCH_TIME_FOREVER); |
---|
141 | |
---|
142 | // Here, the process is done executing. |
---|
143 | |
---|
144 | printf("Finished executingFn - Runs callbackFn\n"); |
---|
145 | this->exitCallbackFn(); |
---|
146 | return std::async(std::launch::async, []{ return 0; }); |
---|
147 | } catch (std::exception* ex) { |
---|
148 | printf("ERROR: %s\n", ex->what()); |
---|
149 | return std::async(std::launch::async, []{ return 1; }); |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | #endif |
---|