Changeset 7dbf568 for launchers


Ignore:
Timestamp:
Oct 11, 2018 11:55:05 AM (2 years ago)
Author:
meeh <meeh@…>
Branches:
master
Children:
969bc2d
Parents:
7e9d193
Message:

OSX Launcher: Refactor deployment code to own file, + code cleanups.

Location:
launchers/macosx
Files:
1 added
1 deleted
3 edited

Legend:

Unmodified
Added
Removed
  • launchers/macosx/AppDelegate.h

    r7e9d193 r7dbf568  
    1919#include "RouterTask.h"
    2020
    21 
    22 #define DEF_I2P_VERSION "0.9.36"
    23 #define APPDOMAIN "net.i2p.launcher"
    24 #define NSAPPDOMAIN @APPDOMAIN
    25 #define CFAPPDOMAIN CFSTR(APPDOMAIN)
    26 #define APP_IDSTR @"I2P Launcher"
     21#include "version.h"
    2722
    2823@class SwiftMainDelegate;
     
    5651  auto homeDir = RealHomeDirectory();
    5752  const char* pathFromHome = "%s/Library/I2P";
     53  char buffer[strlen(homeDir)+strlen(pathFromHome)];
     54  sprintf(buffer, pathFromHome, homeDir);
     55  std::string i2pBaseDir(buffer);
     56  return i2pBaseDir;
     57}
     58
     59inline std::string getDefaultLogDir()
     60{
     61  // Figure out log directory
     62  auto homeDir = RealHomeDirectory();
     63  const char* pathFromHome = "%s/Library/Logs/I2P";
    5864  char buffer[strlen(homeDir)+strlen(pathFromHome)];
    5965  sprintf(buffer, pathFromHome, homeDir);
  • launchers/macosx/Deployer.h

    r7e9d193 r7dbf568  
    88
    99#import <Foundation/Foundation.h>
     10#import <Foundation/NSError.h>
     11#import "AppDelegate.h"
     12
     13@class ExtractMetaInfo;
    1014
    1115
    12 @interface NSObject ()
    13 
     16@interface I2PDeployer : NSObject
     17@property (assign) ExtractMetaInfo *metaInfo;
     18- (I2PDeployer *) initWithMetaInfo:(ExtractMetaInfo*)mi;
     19- (void) extractI2PBaseDir:(void(^)(BOOL success, NSError *error))completion;
    1420@end
    1521
  • launchers/macosx/main.mm

    r7e9d193 r7dbf568  
    2626#include "include/portcheck.h"
    2727#import "SBridge.h"
     28#import "Deployer.h"
     29#include "logger_c.h"
    2830
    2931#ifdef __cplusplus
     
    6264}
    6365
    64 #ifdef __cplusplus
    65 
    6666- (void)extractI2PBaseDir:(void(^)(BOOL success, NSError *error))completion
    6767{
    68  
    69   NSBundle *launcherBundle = [NSBundle mainBundle];
    70   auto homeDir = RealHomeDirectory();
    71   NSLog(@"Home directory is %s", homeDir);
    72  
    73   std::string basePath(homeDir);
    74   basePath.append("/Library/I2P");
    75  
    76   auto jarResPath = [launcherBundle pathForResource:@"launcher" ofType:@"jar"];
    77   NSLog(@"Trying to load launcher.jar from url = %@", jarResPath);
    78   self.metaInfo.jarFile = jarResPath;
    79   self.metaInfo.zipFile = [launcherBundle pathForResource:@"base" ofType:@"zip"];
    80  
    81   NSParameterAssert(basePath.c_str());
    82   NSError *error = NULL;
    83   BOOL success = NO;
    84   dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    85 
    86 
    87     try {
    88       std::string basearg("-Di2p.dir.base=");
    89       basearg += basePath;
    90 
    91       std::string zippath("-Di2p.base.zip=");
    92       zippath += [self.metaInfo.zipFile UTF8String];
    93 
    94       std::string jarfile("-cp ");
    95       jarfile += [self.metaInfo.jarFile UTF8String];
    96 
    97       // Create directory
    98       mkdir(basePath.c_str(), S_IRUSR | S_IWUSR | S_IXUSR);
    99 
    100       auto cli = defaultFlagsForExtractorJob;
    101       setenv("I2PBASE", basePath.c_str(), true);
    102       setenv("ZIPPATH", zippath.c_str(), true);
    103       //setenv("DYLD_LIBRARY_PATH",".:/usr/lib:/lib:/usr/local/lib", true);
    104 
    105       cli.push_back(basearg);
    106       cli.push_back(zippath);
    107       cli.push_back(jarfile);
    108       cli.push_back("net.i2p.launchers.BaseExtractor");
    109       auto rs = [[RouterProcessStatus alloc] init];
    110      
    111       std::string execStr = std::string([rs.getJavaHome UTF8String]);
    112       for_each(cli, [&execStr](std::string str){ execStr += std::string(" ") + str; });
    113 
    114       NSLog(@"Trying cmd: %@", [NSString stringWithUTF8String:execStr.c_str()]);
    115       sendUserNotification(APP_IDSTR, @"Please hold on while we extract I2P. You'll get a new message once done!");
    116       int extractStatus = Popen(execStr.c_str(), environment{{
    117         {"ZIPPATH", zippath.c_str()},
    118         {"I2PBASE", basePath.c_str()}
    119       }}).wait();
    120       NSLog(@"Extraction exit code %@",[NSString stringWithUTF8String:(std::to_string(extractStatus)).c_str()]);
    121       if (extractStatus == 0) {
    122         NSLog(@"Extraction process done");
    123       } else {
    124         NSLog(@"Something went wrong");
    125       }
    126 
    127       // All done. Assume success and error are already set.
    128       dispatch_async(dispatch_get_main_queue(), ^{
    129         if (completion) {
    130           completion(success, error);
    131         }
    132       });
    133      
    134      
    135     } catch (OSError &err) {
    136       auto errMsg = [NSString stringWithUTF8String:err.what()];
    137       NSLog(@"Exception: %@", errMsg);
    138       sendUserNotification(APP_IDSTR, [NSString stringWithFormat:@"Error: %@", errMsg]);
    139     }
    140   });
     68  auto deployer = [[I2PDeployer alloc] initWithMetaInfo:self.metaInfo];
     69  [deployer extractI2PBaseDir:completion];
    14170}
    14271
     
    14675    @"enableVerboseLogging": @YES,
    14776    @"autoStartRouter": @YES,
     77    @"startRouterAtLogin": @NO,
     78    @"startRouterAtStartup": @NO,
     79    @"letRouterLiveEvenLauncherDied": @NO,
    14880    @"i2pBaseDirectory": (NSString *)CFStringCreateWithCString(NULL, const_cast<const char *>(getDefaultBaseDir().c_str()), kCFStringEncodingUTF8)
    14981  }];
     
    15789  if (self.enableVerboseLogging) NSLog(@"Default preferences stored!");
    15890}
    159 
    160 #endif
    16191
    16292
     
    184114  RouterProcessStatus* routerStatus = [[RouterProcessStatus alloc] init];
    185115  std::string i2pBaseDir(getDefaultBaseDir());
    186   NSLog(@"i2pBaseDir = %s", i2pBaseDir.c_str());
     116  MLOG(INFO) << "i2pBaseDir = " << i2pBaseDir.c_str();
    187117  bool shouldAutoStartRouter = false;
     118 
     119  // Initialize the Swift environment (the UI components)
     120  [self.swiftRuntime applicationDidFinishLaunching];
    188121 
    189122  // TODO: Make the port a setting which defaults to 7657
     
    218151  // Might be hard to read if you're not used to Objective-C
    219152  // But this is a "function call" that contains a "callback function"
    220   [routerStatus listenForEventWithEventName:@"router_can_start" callbackActionFn:^(NSString* information) {
     153  /*[routerStatus listenForEventWithEventName:@"router_can_start" callbackActionFn:^(NSString* information) {
    221154    NSLog(@"Got signal, router can be started");
    222     [[SBridge sharedInstance] startupI2PRouter:self.metaInfo.i2pBase javaBinPath:self.metaInfo.javaBinary];
    223   }];
     155    [[SBridge sharedInstance] startupI2PRouter:self.metaInfo.i2pBase];
     156  }];*/
    224157 
    225158  // This will trigger the router start after an upgrade.
     
    227160    NSLog(@"Got signal, router must be deployed from base.zip");
    228161    [self extractI2PBaseDir:^(BOOL success, NSError *error) {
    229       if (success && error != nil) {
     162      if (success) {
    230163        sendUserNotification(@"I2P is done extracting", @"I2P is now installed and ready to run!");
    231164        NSLog(@"Done extracting I2P");
     
    238171  }];
    239172 
    240   // Initialize the Swift environment (the UI components)
    241   [self.swiftRuntime applicationDidFinishLaunching];
    242  
    243173  NSString *nsI2PBaseStr = [NSString stringWithUTF8String:i2pBaseDir.c_str()];
    244174
     175  [routerStatus listenForEventWithEventName:@"extract_completed" callbackActionFn:^(NSString* information) {
     176    NSLog(@"Time to detect I2P version in install directory");
     177    [self.swiftRuntime findInstalledI2PVersion];
     178  }];
    245179 
    246180  //struct stat sb;
     
    266200    } else {
    267201      // The directory exists, but not i2p.jar - most likely we're in mid-extraction state.
    268       [routerStatus listenForEventWithEventName:@"extract_completed" callbackActionFn:^(NSString* information) {
    269         NSLog(@"Time to detect I2P version in install directory");
    270         [self.swiftRuntime findInstalledI2PVersion];
    271       }];
     202      [routerStatus triggerEventWithEn:@"router_must_upgrade" details:@"deploy needed"];
    272203    }
    273204  }
     
    283214- (void)applicationWillTerminate:(NSNotification *)aNotification {
    284215  // Tear down here
     216  [self.swiftRuntime applicationWillTerminate];
    285217  NSString *string = @"applicationWillTerminate executed";
    286218  NSLog(@"%@", string);
Note: See TracChangeset for help on using the changeset viewer.