2018-09-19 17:00:17 +00:00
|
|
|
//
|
|
|
|
// 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 <functional>
|
|
|
|
#include <memory>
|
|
|
|
#include <glob.h>
|
|
|
|
#include <string>
|
|
|
|
#include <list>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <future>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#import <AppKit/AppKit.h>
|
|
|
|
#import "I2PLauncher-Swift.h"
|
|
|
|
|
|
|
|
#include "AppDelegate.h"
|
|
|
|
#include "include/fn.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-09-23 03:33:29 +00:00
|
|
|
std::future<int> startupRouter(NSString* javaBin, NSArray<NSString*>* arguments, NSString* i2pBaseDir, RouterProcessStatus* routerStatus) {
|
2018-09-19 17:00:17 +00:00
|
|
|
@try {
|
|
|
|
RTaskOptions* options = [RTaskOptions alloc];
|
|
|
|
options.binPath = javaBin;
|
|
|
|
options.arguments = arguments;
|
|
|
|
options.i2pBaseDir = i2pBaseDir;
|
|
|
|
auto instance = [[I2PRouterTask alloc] initWithOptions: options];
|
2018-09-22 22:13:40 +00:00
|
|
|
|
|
|
|
[[SBridge sharedInstance] setCurrentRouterInstance:instance];
|
2018-09-19 17:00:17 +00:00
|
|
|
[instance execute];
|
2018-09-26 20:42:58 +00:00
|
|
|
sendUserNotification(APP_IDSTR, @"The I2P router is starting up.");
|
|
|
|
auto pid = [instance getPID];
|
|
|
|
NSLog(@"Got pid: %d", pid);
|
2018-09-23 03:33:29 +00:00
|
|
|
if (routerStatus != nil) {
|
|
|
|
[routerStatus setRouterStatus: true];
|
|
|
|
[routerStatus setRouterRanByUs: true];
|
|
|
|
[routerStatus triggerEventWithEn:@"router_start" details:@"normal start"];
|
2018-09-26 20:42:58 +00:00
|
|
|
[routerStatus triggerEventWithEn:@"router_pid" details:[NSString stringWithFormat:@"%d", pid]];
|
2018-09-23 03:33:29 +00:00
|
|
|
}
|
2018-09-22 22:13:40 +00:00
|
|
|
|
2018-09-19 17:00:17 +00:00
|
|
|
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);
|
2018-09-22 22:13:40 +00:00
|
|
|
[[SBridge sharedInstance] setCurrentRouterInstance:nil];
|
|
|
|
|
2018-09-23 03:33:29 +00:00
|
|
|
if (routerStatus != nil) {
|
|
|
|
[routerStatus triggerEventWithEn:@"router_exception" details:errStr];
|
|
|
|
}
|
2018-09-22 22:13:40 +00:00
|
|
|
|
2018-09-19 17:00:17 +00:00
|
|
|
return std::async(std::launch::async, [&]{
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@implementation SBridge
|
|
|
|
|
2018-09-22 22:13:40 +00:00
|
|
|
// this makes it a singleton
|
|
|
|
+ (instancetype)sharedInstance {
|
|
|
|
static SBridge *sharedInstance = nil;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
sharedInstance = [[SBridge alloc] init];
|
|
|
|
});
|
|
|
|
return sharedInstance;
|
|
|
|
}
|
2018-09-19 17:00:17 +00:00
|
|
|
|
|
|
|
- (void) openUrl:(NSString*)url
|
|
|
|
{
|
|
|
|
osx::openUrl(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) buildClassPath:(NSString*)i2pPath
|
|
|
|
{
|
|
|
|
const char * basePath = [i2pPath UTF8String];
|
|
|
|
auto jarList = buildClassPathForObjC(basePath);
|
|
|
|
const char * classpath = jarList.c_str();
|
|
|
|
NSLog(@"Classpath from ObjC = %s", classpath);
|
|
|
|
return [[NSString alloc] initWithUTF8String:classpath];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- (void)startupI2PRouter:(NSString*)i2pRootPath javaBinPath:(NSString*)javaBinPath
|
|
|
|
{
|
|
|
|
std::string basePath([i2pRootPath UTF8String]);
|
|
|
|
|
|
|
|
auto classPathStr = buildClassPathForObjC(basePath);
|
|
|
|
|
|
|
|
RouterProcessStatus* routerStatus = [[RouterProcessStatus alloc] init];
|
|
|
|
try {
|
|
|
|
std::vector<NSString*> 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()];
|
2018-09-23 03:33:29 +00:00
|
|
|
|
2018-09-19 17:00:17 +00:00
|
|
|
NSLog(@"Trying to run command: %@", javaBinPath);
|
|
|
|
NSLog(@"With I2P Base dir: %@", i2pRootPath);
|
|
|
|
NSLog(@"And Arguments: %@", arrArguments);
|
2018-09-23 03:33:29 +00:00
|
|
|
startupRouter(nsJavaBin, arrArguments, nsBasePath, routerStatus);
|
2018-09-19 17:00:17 +00:00
|
|
|
} 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];
|
2018-09-22 22:13:40 +00:00
|
|
|
[routerStatus triggerEventWithEn:@"router_exception" details:[NSString stringWithFormat:@"Error: %@", errMsg]];
|
2018-09-19 17:00:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|