source: launchers/macosx/SBridge.mm @ e046418f

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

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

  • Property mode set to 100644
File size: 4.9 KB
Line 
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
29std::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    //setGlobalRouterObject(instance);
37    //NSThread *thr = [[NSThread alloc] initWithTarget:instance selector:@selector(execute) object:nil];
38    [instance execute];
39    sendUserNotification(APP_IDSTR, @"The I2P router is starting up.");
40    auto pid = [instance getPID];
41    return std::async(std::launch::async, [&pid]{
42      return pid;
43    });
44  }
45  @catch (NSException *e)
46  {
47    auto errStr = [NSString stringWithFormat:@"Expection occurred %@",[e reason]];
48    NSLog(@"%@", errStr);
49    sendUserNotification(APP_IDSTR, errStr);
50    return std::async(std::launch::async, [&]{
51      return 0;
52    });
53  }
54}
55
56namespace osx {
57  inline void openUrl(NSString* url)
58  {
59    [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString: url]];
60  }
61}
62
63inline std::vector<std::string> globVector(const std::string& pattern){
64  glob_t glob_result;
65  glob(pattern.c_str(),GLOB_TILDE,NULL,&glob_result);
66  std::vector<std::string> files;
67  for(unsigned int i=0;i<glob_result.gl_pathc;++i){
68    files.push_back(std::string(glob_result.gl_pathv[i]));
69  }
70  globfree(&glob_result);
71  return files;
72}
73
74inline std::string buildClassPathForObjC(std::string basePath)
75{
76  NSBundle *launcherBundle = [NSBundle mainBundle];
77  auto jarList = globVector(basePath+std::string("/lib/*.jar"));
78 
79  std::string classpathStrHead = "-classpath";
80  std::string classpathStr = "";
81  classpathStr += [[launcherBundle pathForResource:@"launcher" ofType:@"jar"] UTF8String];
82  std::string prefix(basePath);
83  prefix += "/lib/";
84  for_each(jarList, [&classpathStr](std::string str){ classpathStr += std::string(":") + str; });
85  return classpathStr;
86}
87
88
89@implementation SBridge
90
91
92- (void) openUrl:(NSString*)url
93{
94  osx::openUrl(url);
95}
96
97- (NSString*) buildClassPath:(NSString*)i2pPath
98{
99  const char * basePath = [i2pPath UTF8String];
100  auto jarList = buildClassPathForObjC(basePath);
101  const char * classpath = jarList.c_str();
102  NSLog(@"Classpath from ObjC = %s", classpath);
103  return [[NSString alloc] initWithUTF8String:classpath];
104}
105
106
107
108- (void)startupI2PRouter:(NSString*)i2pRootPath javaBinPath:(NSString*)javaBinPath
109{
110  std::string basePath([i2pRootPath UTF8String]);
111 
112  // Get paths
113  //NSBundle *launcherBundle = [NSBundle mainBundle];
114  auto classPathStr = buildClassPathForObjC(basePath);
115 
116  RouterProcessStatus* routerStatus = [[RouterProcessStatus alloc] init];
117  try {
118    std::vector<NSString*> argList = {
119      @"-Xmx512M",
120      @"-Xms128m",
121      @"-Djava.awt.headless=true",
122      @"-Dwrapper.logfile=/tmp/router.log",
123      @"-Dwrapper.logfile.loglevel=DEBUG",
124      @"-Dwrapper.java.pidfile=/tmp/routerjvm.pid",
125      @"-Dwrapper.console.loglevel=DEBUG"
126    };
127   
128    std::string baseDirArg("-Di2p.dir.base=");
129    baseDirArg += basePath;
130    std::string javaLibArg("-Djava.library.path=");
131    javaLibArg += basePath;
132    // TODO: pass this to JVM
133    //auto java_opts = getenv("JAVA_OPTS");
134   
135    std::string cpString = std::string("-cp");
136   
137    argList.push_back([NSString stringWithUTF8String:baseDirArg.c_str()]);
138    argList.push_back([NSString stringWithUTF8String:javaLibArg.c_str()]);
139    argList.push_back([NSString stringWithUTF8String:cpString.c_str()]);
140    argList.push_back([NSString stringWithUTF8String:classPathStr.c_str()]);
141    argList.push_back(@"net.i2p.router.Router");
142    auto javaBin = std::string([javaBinPath UTF8String]);
143   
144   
145    sendUserNotification(APP_IDSTR, @"I2P Router is starting up!");
146    auto nsJavaBin = javaBinPath;
147    auto nsBasePath = i2pRootPath;
148    NSArray* arrArguments = [NSArray arrayWithObjects:&argList[0] count:argList.size()];
149    // We don't really know yet, but per now a workaround
150    [routerStatus setRouterStatus: true];
151    NSLog(@"Trying to run command: %@", javaBinPath);
152    NSLog(@"With I2P Base dir: %@", i2pRootPath);
153    NSLog(@"And Arguments: %@", arrArguments);
154    startupRouter(nsJavaBin, arrArguments, nsBasePath);
155  } catch (std::exception &err) {
156    auto errMsg = [NSString stringWithUTF8String:err.what()];
157    NSLog(@"Exception: %@", errMsg);
158    sendUserNotification(APP_IDSTR, [NSString stringWithFormat:@"Error: %@", errMsg]);
159    [routerStatus setRouterStatus: false];
160    [routerStatus setRouterRanByUs: false];
161  }
162}
163@end
164
165
166#endif
Note: See TracBrowser for help on using the repository browser.