source: launchers/macosx/I2PLauncher/SwiftMainDelegate.swift @ 281c5f57

Last change on this file since 281c5f57 was 281c5f57, checked in by meeh <meeh@…>, 2 years ago

Bugfixes, improvements, more error handling and stability to the osx launcher.

  • Property mode set to 100644
File size: 3.1 KB
Line 
1//
2//  SwiftMainDelegate.swift
3//  I2PLauncher
4//
5//  Created by Mikal Villa on 17/09/2018.
6//  Copyright © 2018 The I2P Project. All rights reserved.
7//
8
9import Foundation
10import Cocoa
11
12@objc class SwiftMainDelegate : NSObject {
13 
14  //let statusItem = NSStatusBar.system().statusItem(withLength: NSSquareStatusItemLength )
15  let statusBarController = StatusBarController()
16  let javaDetector = DetectJava()
17  static let objCBridge = SBridge()
18 
19  override init() {
20    super.init()
21   
22    self.javaDetector.findIt()
23    if (!javaDetector.isJavaFound()) {
24      print("Could not find java....")
25      terminate("No java..")
26    }
27    let javaBinPath = self.javaDetector.javaHome
28    RouterProcessStatus.knownJavaBinPath = javaBinPath
29    print("Found java home = ", javaBinPath)
30   
31    let (portIsNotTaken, _) = RouterProcessStatus.checkTcpPortForListen(port: 7657)
32    if (!portIsNotTaken) {
33      RouterProcessStatus.isRouterRunning = true
34      RouterProcessStatus.isRouterChildProcess = false
35      print("I2P Router seems to be running")
36    } else {
37      RouterProcessStatus.isRouterRunning = false
38      print("I2P Router seems to NOT be running")
39     
40    }
41   
42   
43  } // End of init()
44 
45  @objc func findInstalledI2PVersion() {
46    var i2pPath = NSHomeDirectory()
47    i2pPath += "/Library/I2P"
48    var jExecPath:String = RouterProcessStatus.knownJavaBinPath!
49   
50    // Sometimes, home will return the binary, sometimes the actual home dir. This fixes the diverge.
51    // If JRE is detected, binary follows - if it's JDK, home follows.
52    if (jExecPath.hasSuffix("Home")) {
53      jExecPath += "/jre/bin/java"
54    }
55   
56    let jarPath = i2pPath + "/lib/i2p.jar"
57   
58    let subCmd = jExecPath + " -cp " + jarPath + " net.i2p.CoreVersion"
59   
60    let cmdArgs:[String] = ["-c", subCmd]
61    print(cmdArgs)
62    let sub:Subprocess = Subprocess.init(executablePath: "/bin/sh", arguments: cmdArgs)
63    let results:ExecutionResult = sub.execute(captureOutput: true)!
64    if (results.didCaptureOutput) {
65      print("captured output")
66      let i2pVersion = results.outputLines.first?.replace(target: "I2P Core version: ", withString: "")
67      NSLog("I2P version detected: %@",i2pVersion ?? "Unknown")
68      RouterProcessStatus.routerVersion = i2pVersion
69    } else {
70      print("did NOT captured output")
71     
72    }
73  }
74 
75  @objc func applicationDidFinishLaunching() {
76    print("Hello from swift!")
77    var i2pPath = NSHomeDirectory()
78    i2pPath += "/Library/I2P"
79   
80    //let javaBinPath = self.javaDetector.javaHome.replace(target: " ", withString: "\\ ")
81   
82    let fileManager = FileManager()
83    var ok = ObjCBool(true)
84    let doesI2PDirExists = fileManager.fileExists(atPath: i2pPath, isDirectory: &ok)
85   
86    if (!doesI2PDirExists) {
87      // Deploy
88    }
89   
90    //let i2pJarPath = i2pPath + "/lib/i2p.jar"
91   
92    findInstalledI2PVersion()
93  }
94 
95  @objc static func openLink(url: String) {
96    objCBridge.openUrl(url)
97  }
98 
99  @objc func applicationWillTerminate() {
100    // Shutdown stuff
101  }
102 
103  @objc func terminate(_ why: Any?) {
104    print("Stopping cause of ", why!)
105  }
106}
107
Note: See TracBrowser for help on using the repository browser.