1 | // |
---|
2 | // SBridge.m |
---|
3 | // I2PLauncher |
---|
4 | // |
---|
5 | // Created by Mikal Villa on 18/09/2018. |
---|
6 | // Copyright © 2018 The I2P Project. All rights reserved. |
---|
7 | // |
---|
8 | |
---|
9 | #import "SBridge.h" |
---|
10 | |
---|
11 | #ifdef __cplusplus |
---|
12 | #include <functional> |
---|
13 | #include <memory> |
---|
14 | #include <glob.h> |
---|
15 | #include <string> |
---|
16 | #include <list> |
---|
17 | #include <stdlib.h> |
---|
18 | #include <future> |
---|
19 | #include <vector> |
---|
20 | |
---|
21 | #import <AppKit/AppKit.h> |
---|
22 | #import "I2PLauncher-Swift.h" |
---|
23 | |
---|
24 | #include "AppDelegate.h" |
---|
25 | #include "include/fn.h" |
---|
26 | |
---|
27 | |
---|
28 | |
---|
29 | std::future<int> startupRouter(NSString* javaBin, NSArray<NSString*>* arguments, NSString* i2pBaseDir) { |
---|
30 | @try { |
---|
31 | RTaskOptions* options = [RTaskOptions alloc]; |
---|
32 | options.binPath = javaBin; |
---|
33 | options.arguments = arguments; |
---|
34 | options.i2pBaseDir = i2pBaseDir; |
---|
35 | auto instance = [[I2PRouterTask alloc] initWithOptions: options]; |
---|
36 | [instance execute]; |
---|
37 | sendUserNotification(APP_IDSTR, @"The I2P router is starting up."); |
---|
38 | auto pid = [instance getPID]; |
---|
39 | return std::async(std::launch::async, [&pid]{ |
---|
40 | return pid; |
---|
41 | }); |
---|
42 | } |
---|
43 | @catch (NSException *e) |
---|
44 | { |
---|
45 | auto errStr = [NSString stringWithFormat:@"Expection occurred %@",[e reason]]; |
---|
46 | NSLog(@"%@", errStr); |
---|
47 | sendUserNotification(APP_IDSTR, errStr); |
---|
48 | return std::async(std::launch::async, [&]{ |
---|
49 | return 0; |
---|
50 | }); |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | |
---|
55 | |
---|
56 | @implementation SBridge |
---|
57 | |
---|
58 | |
---|
59 | - (void) openUrl:(NSString*)url |
---|
60 | { |
---|
61 | osx::openUrl(url); |
---|
62 | } |
---|
63 | |
---|
64 | - (NSString*) buildClassPath:(NSString*)i2pPath |
---|
65 | { |
---|
66 | const char * basePath = [i2pPath UTF8String]; |
---|
67 | auto jarList = buildClassPathForObjC(basePath); |
---|
68 | const char * classpath = jarList.c_str(); |
---|
69 | NSLog(@"Classpath from ObjC = %s", classpath); |
---|
70 | return [[NSString alloc] initWithUTF8String:classpath]; |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | |
---|
75 | - (void)startupI2PRouter:(NSString*)i2pRootPath javaBinPath:(NSString*)javaBinPath |
---|
76 | { |
---|
77 | std::string basePath([i2pRootPath UTF8String]); |
---|
78 | |
---|
79 | // Get paths |
---|
80 | //NSBundle *launcherBundle = [NSBundle mainBundle]; |
---|
81 | auto classPathStr = buildClassPathForObjC(basePath); |
---|
82 | |
---|
83 | RouterProcessStatus* routerStatus = [[RouterProcessStatus alloc] init]; |
---|
84 | try { |
---|
85 | std::vector<NSString*> argList = { |
---|
86 | @"-Xmx512M", |
---|
87 | @"-Xms128m", |
---|
88 | @"-Djava.awt.headless=true", |
---|
89 | @"-Dwrapper.logfile=/tmp/router.log", |
---|
90 | @"-Dwrapper.logfile.loglevel=DEBUG", |
---|
91 | @"-Dwrapper.java.pidfile=/tmp/routerjvm.pid", |
---|
92 | @"-Dwrapper.console.loglevel=DEBUG" |
---|
93 | }; |
---|
94 | |
---|
95 | std::string baseDirArg("-Di2p.dir.base="); |
---|
96 | baseDirArg += basePath; |
---|
97 | std::string javaLibArg("-Djava.library.path="); |
---|
98 | javaLibArg += basePath; |
---|
99 | // TODO: pass this to JVM |
---|
100 | //auto java_opts = getenv("JAVA_OPTS"); |
---|
101 | |
---|
102 | std::string cpString = std::string("-cp"); |
---|
103 | |
---|
104 | argList.push_back([NSString stringWithUTF8String:baseDirArg.c_str()]); |
---|
105 | argList.push_back([NSString stringWithUTF8String:javaLibArg.c_str()]); |
---|
106 | argList.push_back([NSString stringWithUTF8String:cpString.c_str()]); |
---|
107 | argList.push_back([NSString stringWithUTF8String:classPathStr.c_str()]); |
---|
108 | argList.push_back(@"net.i2p.router.Router"); |
---|
109 | auto javaBin = std::string([javaBinPath UTF8String]); |
---|
110 | |
---|
111 | |
---|
112 | sendUserNotification(APP_IDSTR, @"I2P Router is starting up!"); |
---|
113 | auto nsJavaBin = javaBinPath; |
---|
114 | auto nsBasePath = i2pRootPath; |
---|
115 | NSArray* arrArguments = [NSArray arrayWithObjects:&argList[0] count:argList.size()]; |
---|
116 | // We don't really know yet, but per now a workaround |
---|
117 | [routerStatus setRouterStatus: true]; |
---|
118 | NSLog(@"Trying to run command: %@", javaBinPath); |
---|
119 | NSLog(@"With I2P Base dir: %@", i2pRootPath); |
---|
120 | NSLog(@"And Arguments: %@", arrArguments); |
---|
121 | startupRouter(nsJavaBin, arrArguments, nsBasePath); |
---|
122 | } catch (std::exception &err) { |
---|
123 | auto errMsg = [NSString stringWithUTF8String:err.what()]; |
---|
124 | NSLog(@"Exception: %@", errMsg); |
---|
125 | sendUserNotification(APP_IDSTR, [NSString stringWithFormat:@"Error: %@", errMsg]); |
---|
126 | [routerStatus setRouterStatus: false]; |
---|
127 | [routerStatus setRouterRanByUs: false]; |
---|
128 | } |
---|
129 | } |
---|
130 | @end |
---|
131 | |
---|
132 | |
---|
133 | #endif |
---|