// // SBridge.m // I2PLauncher // // Created by Mikal Villa on 18/09/2018. // Copyright © 2018 The I2P Project. All rights reserved. // #import "SBridge.h" #ifdef __cplusplus #include #include #include #include #include #include #include #include #import #import "I2PLauncher-Swift.h" #include "AppDelegate.h" #include "include/fn.h" std::future startupRouter(NSString* javaBin, NSArray* arguments, NSString* i2pBaseDir) { @try { RTaskOptions* options = [RTaskOptions alloc]; options.binPath = javaBin; options.arguments = arguments; options.i2pBaseDir = i2pBaseDir; auto instance = [[I2PRouterTask alloc] initWithOptions: options]; //setGlobalRouterObject(instance); //NSThread *thr = [[NSThread alloc] initWithTarget:instance selector:@selector(execute) object:nil]; [instance execute]; sendUserNotification(APP_IDSTR, @"The I2P router is starting up."); auto pid = [instance getPID]; return std::async(std::launch::async, [&pid]{ return pid; }); } @catch (NSException *e) { auto errStr = [NSString stringWithFormat:@"Expection occurred %@",[e reason]]; NSLog(@"%@", errStr); sendUserNotification(APP_IDSTR, errStr); return std::async(std::launch::async, [&]{ return 0; }); } } namespace osx { inline void openUrl(NSString* url) { [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString: url]]; } } inline std::vector globVector(const std::string& pattern){ glob_t glob_result; glob(pattern.c_str(),GLOB_TILDE,NULL,&glob_result); std::vector files; for(unsigned int i=0;i argList = { @"-Xmx512M", @"-Xms128m", @"-Djava.awt.headless=true", @"-Dwrapper.logfile=/tmp/router.log", @"-Dwrapper.logfile.loglevel=DEBUG", @"-Dwrapper.java.pidfile=/tmp/routerjvm.pid", @"-Dwrapper.console.loglevel=DEBUG" }; std::string baseDirArg("-Di2p.dir.base="); baseDirArg += basePath; std::string javaLibArg("-Djava.library.path="); javaLibArg += basePath; // TODO: pass this to JVM //auto java_opts = getenv("JAVA_OPTS"); std::string cpString = std::string("-cp"); argList.push_back([NSString stringWithUTF8String:baseDirArg.c_str()]); argList.push_back([NSString stringWithUTF8String:javaLibArg.c_str()]); argList.push_back([NSString stringWithUTF8String:cpString.c_str()]); argList.push_back([NSString stringWithUTF8String:classPathStr.c_str()]); argList.push_back(@"net.i2p.router.Router"); auto javaBin = std::string([javaBinPath UTF8String]); sendUserNotification(APP_IDSTR, @"I2P Router is starting up!"); auto nsJavaBin = javaBinPath; auto nsBasePath = i2pRootPath; NSArray* arrArguments = [NSArray arrayWithObjects:&argList[0] count:argList.size()]; // We don't really know yet, but per now a workaround [routerStatus setRouterStatus: true]; NSLog(@"Trying to run command: %@", javaBinPath); NSLog(@"With I2P Base dir: %@", i2pRootPath); NSLog(@"And Arguments: %@", arrArguments); startupRouter(nsJavaBin, arrArguments, nsBasePath); } catch (std::exception &err) { auto errMsg = [NSString stringWithUTF8String:err.what()]; NSLog(@"Exception: %@", errMsg); sendUserNotification(APP_IDSTR, [NSString stringWithFormat:@"Error: %@", errMsg]); [routerStatus setRouterStatus: false]; [routerStatus setRouterRanByUs: false]; } } @end #endif