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 | |
---|
9 | import Foundation |
---|
10 | import Cocoa |
---|
11 | |
---|
12 | @objc class SwiftMainDelegate : NSObject { |
---|
13 | |
---|
14 | let statusBarController = StatusBarController() |
---|
15 | let sharedRouterMgmr = RouterManager.shared() |
---|
16 | static let javaDetector = DetectJava() |
---|
17 | |
---|
18 | override init() { |
---|
19 | super.init() |
---|
20 | if (!SwiftMainDelegate.javaDetector.isJavaFound()) { |
---|
21 | SwiftMainDelegate.javaDetector.findIt() |
---|
22 | if (!SwiftMainDelegate.javaDetector.isJavaFound()) { |
---|
23 | print("Could not find java....") |
---|
24 | terminate("No java..") |
---|
25 | } |
---|
26 | } |
---|
27 | let javaBinPath = SwiftMainDelegate.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 | } // End of init() |
---|
41 | |
---|
42 | @objc func findInstalledI2PVersion() { |
---|
43 | var i2pPath = NSHomeDirectory() |
---|
44 | i2pPath += "/Library/I2P" |
---|
45 | var jExecPath:String = RouterProcessStatus.knownJavaBinPath! |
---|
46 | |
---|
47 | // Sometimes, home will return the binary, sometimes the actual home dir. This fixes the diverge. |
---|
48 | // If JRE is detected, binary follows - if it's JDK, home follows. |
---|
49 | if (jExecPath.hasSuffix("Home")) { |
---|
50 | jExecPath += "/jre/bin/java" |
---|
51 | } |
---|
52 | |
---|
53 | let jarPath = i2pPath + "/lib/i2p.jar" |
---|
54 | |
---|
55 | let subCmd = jExecPath + " -cp " + jarPath + " net.i2p.CoreVersion" |
---|
56 | |
---|
57 | let cmdArgs:[String] = ["-c", subCmd] |
---|
58 | print(cmdArgs) |
---|
59 | let sub:Subprocess = Subprocess.init(executablePath: "/bin/sh", arguments: cmdArgs) |
---|
60 | let results:ExecutionResult = sub.execute(captureOutput: true)! |
---|
61 | if (results.didCaptureOutput) { |
---|
62 | if (results.status == 0) { |
---|
63 | let i2pVersion = results.outputLines.first?.replace(target: "I2P Core version: ", withString: "") |
---|
64 | NSLog("I2P version detected: %@",i2pVersion ?? "Unknown") |
---|
65 | RouterProcessStatus.routerVersion = i2pVersion |
---|
66 | RouterManager.shared().eventManager.trigger(eventName: "router_version", information: i2pVersion) |
---|
67 | RouterManager.shared().eventManager.trigger(eventName: "router_can_start", information: i2pVersion) |
---|
68 | } else { |
---|
69 | NSLog("Non zero exit code from subprocess while trying to detect version number!") |
---|
70 | for line in results.errorsLines { |
---|
71 | NSLog(line) |
---|
72 | } |
---|
73 | } |
---|
74 | } else { |
---|
75 | print("Warning: Version Detection did NOT captured output") |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | @objc func applicationDidFinishLaunching() { |
---|
80 | var i2pPath = NSHomeDirectory() |
---|
81 | i2pPath += "/Library/I2P" |
---|
82 | |
---|
83 | } |
---|
84 | |
---|
85 | @objc func listenForEvent(eventName: String, callbackActionFn: @escaping ((Any?)->()) ) { |
---|
86 | RouterManager.shared().eventManager.listenTo(eventName: eventName, action: callbackActionFn ) |
---|
87 | } |
---|
88 | |
---|
89 | @objc func triggerEvent(en: String, details: String? = nil) { |
---|
90 | RouterManager.shared().eventManager.trigger(eventName: en, information: details) |
---|
91 | } |
---|
92 | |
---|
93 | @objc static func openLink(url: String) { |
---|
94 | SBridge.sharedInstance().openUrl(url) |
---|
95 | } |
---|
96 | |
---|
97 | @objc func applicationWillTerminate() { |
---|
98 | // Shutdown stuff |
---|
99 | } |
---|
100 | |
---|
101 | @objc func terminate(_ why: Any?) { |
---|
102 | print("Stopping cause of ", why!) |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|