source: launchers/macosx/I2PLauncher/SwiftMainDelegate.swift @ 5304709

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

OSX Launcher: general cleanup, responsibility delegation, and fixes

  • Property mode set to 100644
File size: 4.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
12class Logger {
13  static func MLog<T>(level:Int32, _ object: T?,file:String = #file, function:String = #function, line:Int = #line) {
14    SBridge.logProxy(level, formattedMsg: "\(makeTag(function: function, file: file, line: line)) : \(object)")
15  }
16 
17  private static func makeTag(function: String, file: String, line: Int) -> String{
18    let url = NSURL(fileURLWithPath: file)
19    let className:String! = url.lastPathComponent == nil ? file: url.lastPathComponent!
20    return "\(className) \(function)[\(line)]"
21  }
22 
23}
24
25
26@objc class SwiftMainDelegate : NSObject {
27 
28  let statusBarController = StatusBarController()
29  let sharedRouterMgmr = RouterManager.shared()
30 
31  override init() {
32    super.init()
33    if (!DetectJava.shared().isJavaFound()) {
34    DetectJava.shared().findIt()
35      if (!DetectJava.shared().isJavaFound()) {
36        Logger.MLog(level:3, "Could not find java....")
37        terminate("No java..")
38      }
39    }
40    let javaBinPath = DetectJava.shared().javaBinary
41    Logger.MLog(level:1, "".appendingFormat("Found java home = %@", javaBinPath!))
42   
43    let (portIsNotTaken, _) = RouterProcessStatus.checkTcpPortForListen(port: 7657)
44    if (!portIsNotTaken) {
45      RouterProcessStatus.isRouterRunning = true
46      RouterProcessStatus.isRouterChildProcess = false
47      Logger.MLog(level:2, "I2P Router seems to be running")
48    } else {
49      RouterProcessStatus.isRouterRunning = false
50      Logger.MLog(level:2, "I2P Router seems to NOT be running")
51    }
52  } // End of init()
53 
54  @objc func findInstalledI2PVersion() {
55    var i2pPath = NSHomeDirectory()
56    i2pPath += "/Library/I2P"
57    let jExecPath:String = "/usr/libexec/java_home -v 1.7+ --exec java "
58   
59    let jarPath = i2pPath + "/lib/i2p.jar"
60   
61    let subCmd = jExecPath + "-cp " + jarPath + " net.i2p.CoreVersion"
62   
63    let cmdArgs:[String] = ["-c", subCmd]
64    print(cmdArgs)
65    let sub:Subprocess = Subprocess.init(executablePath: "/bin/sh", arguments: cmdArgs)
66    let results:ExecutionResult = sub.execute(captureOutput: true)!
67    if (results.didCaptureOutput) {
68      if (results.status == 0) {
69        let i2pVersion = results.outputLines.first?.replace(target: "I2P Core version: ", withString: "")
70        Logger.MLog(level: 1, "".appendingFormat("I2P version detected: %@",i2pVersion ?? "Unknown"))
71        RouterProcessStatus.routerVersion = i2pVersion
72        RouterManager.shared().eventManager.trigger(eventName: "router_version", information: i2pVersion)
73      } else {
74        Logger.MLog(level: 2, "Non zero exit code from subprocess while trying to detect version number!")
75        for line in results.errorsLines {
76          Logger.MLog(level: 2, line)
77        }
78      }
79    } else {
80      Logger.MLog(level: 1, "Warning: Version Detection did NOT captured output")
81    }
82  }
83 
84  @objc func applicationDidFinishLaunching() {
85  }
86 
87  @objc func listenForEvent(eventName: String, callbackActionFn: @escaping ((Any?)->()) ) {
88    RouterManager.shared().eventManager.listenTo(eventName: eventName, action: callbackActionFn )
89  }
90 
91  @objc func triggerEvent(en: String, details: String? = nil) {
92    RouterManager.shared().eventManager.trigger(eventName: en, information: details)
93  }
94 
95  @objc static func openLink(url: String) {
96    NSLog("Trying to open \(url)")
97    NSWorkspace.shared().open(NSURL(string: url)! as URL)
98  }
99 
100  @objc func applicationWillTerminate() {
101    // Shutdown stuff
102    let userPreferences = UserDefaults.standard
103    if (!userPreferences.bool(forKey: "letRouterLiveEvenLauncherDied")) {
104      RouterManager.shared().routerRunner.TeardownLaunchd()
105      sleep(2)
106      let status: AgentStatus? = RouterRunner.launchAgent?.status()
107      if status != .unloaded {
108        Logger.MLog(level:2, "Router service not yet stopped")
109        RouterManager.shared().routerRunner.TeardownLaunchd()
110        sleep(5)
111      }
112    }
113  }
114 
115  @objc func terminate(_ why: Any?) {
116    Logger.MLog(level:2, "".appendingFormat("Stopping cause of ", why! as! CVarArg))
117  }
118}
119
Note: See TracBrowser for help on using the repository browser.