source: launchers/macosx/I2PLauncher/routermgmt/RouterProcessStatus.swift @ 7a72049

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

Mac OS X Launcher:

  • Bugfixes as always
  • Added Sparkle (native updater, https://sparkle-project.org/ )
  • The launcher will now extract and overwrite older versions if found
  • Rewrite of the java extraction part (to enable overwrite)
  • Move more functionality to use EventManager? as it works quite well
  • Added check for updates menu item
  • Property mode set to 100644
File size: 3.1 KB
Line 
1//
2//  RouterProcessStatus.swift
3//  I2PLauncher
4//
5//  Created by Mikal Villa on 18/09/2018.
6//  Copyright © 2018 The I2P Project. All rights reserved.
7//
8
9import Foundation
10import AppKit
11
12@objc class RouterProcessStatus : NSObject {
13 
14  /**
15   *
16   * Why the functions bellow? Because the Objective-C bridge is limited, it can't do Swift "static's" over it.
17   *
18   **/
19 
20  @objc func setRouterStatus(_ isRunning: Bool = false) {
21    RouterProcessStatus.isRouterRunning = isRunning
22  }
23 
24  @objc func setRouterRanByUs(_ ranByUs: Bool = false) {
25    RouterProcessStatus.isRouterChildProcess = ranByUs
26  }
27 
28  @objc func getRouterIsRunning() -> Bool {
29    return RouterProcessStatus.isRouterRunning
30  }
31 
32  @objc func getJavaHome() -> String {
33    return RouterProcessStatus.knownJavaBinPath!
34  }
35 
36  @objc func triggerEvent(en: String, details: String? = nil) {
37    RouterManager.shared().eventManager.trigger(eventName: en, information: details)
38  }
39
40  @objc func listenForEvent(eventName: String, callbackActionFn: @escaping ((Any?)->()) ) {
41    RouterManager.shared().eventManager.listenTo(eventName: eventName, action: callbackActionFn )
42  }
43}
44
45extension RouterProcessStatus {
46  static var isRouterRunning : Bool = (RouterManager.shared().getRouterTask() != nil)
47  static var isRouterChildProcess : Bool = (RouterManager.shared().getRouterTask() != nil)
48  static var routerVersion : String? = Optional.none
49  static var routerStartedAt : Date? = Optional.none
50  static var knownJavaBinPath : String? = Optional.none
51  static var i2pDirectoryPath : String = NSHomeDirectory() + "/Library/I2P"
52 
53  static var knownRouterSubTaskRef : I2PSubprocess? = Optional.none
54 
55}
56
57
58
59
60extension RouterProcessStatus {
61  static func checkTcpPortForListen(port: in_port_t) -> (Bool, descr: String){
62   
63    let socketFileDescriptor = socket(AF_INET, SOCK_STREAM, 0)
64    if socketFileDescriptor == -1 {
65      return (false, "SocketCreationFailed, \(descriptionOfLastError())")
66    }
67   
68    var addr = sockaddr_in()
69    let sizeOfSockkAddr = MemoryLayout<sockaddr_in>.size
70    addr.sin_len = __uint8_t(sizeOfSockkAddr)
71    addr.sin_family = sa_family_t(AF_INET)
72    addr.sin_port = Int(OSHostByteOrder()) == OSLittleEndian ? _OSSwapInt16(port) : port
73    addr.sin_addr = in_addr(s_addr: inet_addr("0.0.0.0"))
74    addr.sin_zero = (0, 0, 0, 0, 0, 0, 0, 0)
75    var bind_addr = sockaddr()
76    memcpy(&bind_addr, &addr, Int(sizeOfSockkAddr))
77   
78    if Darwin.bind(socketFileDescriptor, &bind_addr, socklen_t(sizeOfSockkAddr)) == -1 {
79      let details = descriptionOfLastError()
80      release(socket: socketFileDescriptor)
81      return (false, "\(port), BindFailed, \(details)")
82    }
83    if listen(socketFileDescriptor, SOMAXCONN ) == -1 {
84      let details = descriptionOfLastError()
85      release(socket: socketFileDescriptor)
86      return (false, "\(port), ListenFailed, \(details)")
87    }
88    release(socket: socketFileDescriptor)
89    return (true, "\(port) is free for use")
90  }
91 
92  static func release(socket: Int32) {
93    Darwin.shutdown(socket, SHUT_RDWR)
94    close(socket)
95  }
96  static func descriptionOfLastError() -> String {
97    return String(cString: UnsafePointer(strerror(errno)))
98  }
99}
100
Note: See TracBrowser for help on using the repository browser.