source: launchers/macosx/I2PLauncher/routermgmt/RouterProcessStatus.swift @ e8decff5

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

Mac OS X Launcher: more cleanup and some bugfixes.

  • 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
37extension RouterProcessStatus {
38  static var isRouterRunning : Bool = false
39  static var isRouterChildProcess : Bool = false
40  static var routerVersion : String? = Optional.none
41  static var routerUptime : String? = Optional.none{
42    //Called before the change
43    willSet(newValue){
44      print("RouterProcessStatus.routerUptime will change from ", (self.routerUptime ?? "nil"), " to "+(newValue ?? "nil"))
45    }
46   
47    //Called after the change
48    didSet{
49      print("RouterProcessStatus.routerUptime did change to "+self.routerUptime!)
50    }
51  }
52  static var routerStartedAt : Date? = Optional.none
53  static var knownJavaBinPath : String? = Optional.none
54  static var i2pDirectoryPath : String = NSHomeDirectory() + "/Library/I2P"
55 
56  static var knownRouterSubTaskRef : I2PSubprocess? = Optional.none
57 
58}
59
60
61
62
63extension RouterProcessStatus {
64  static func checkTcpPortForListen(port: in_port_t) -> (Bool, descr: String){
65   
66    let socketFileDescriptor = socket(AF_INET, SOCK_STREAM, 0)
67    if socketFileDescriptor == -1 {
68      return (false, "SocketCreationFailed, \(descriptionOfLastError())")
69    }
70   
71    var addr = sockaddr_in()
72    let sizeOfSockkAddr = MemoryLayout<sockaddr_in>.size
73    addr.sin_len = __uint8_t(sizeOfSockkAddr)
74    addr.sin_family = sa_family_t(AF_INET)
75    addr.sin_port = Int(OSHostByteOrder()) == OSLittleEndian ? _OSSwapInt16(port) : port
76    addr.sin_addr = in_addr(s_addr: inet_addr("0.0.0.0"))
77    addr.sin_zero = (0, 0, 0, 0, 0, 0, 0, 0)
78    var bind_addr = sockaddr()
79    memcpy(&bind_addr, &addr, Int(sizeOfSockkAddr))
80   
81    if Darwin.bind(socketFileDescriptor, &bind_addr, socklen_t(sizeOfSockkAddr)) == -1 {
82      let details = descriptionOfLastError()
83      release(socket: socketFileDescriptor)
84      return (false, "\(port), BindFailed, \(details)")
85    }
86    if listen(socketFileDescriptor, SOMAXCONN ) == -1 {
87      let details = descriptionOfLastError()
88      release(socket: socketFileDescriptor)
89      return (false, "\(port), ListenFailed, \(details)")
90    }
91    release(socket: socketFileDescriptor)
92    return (true, "\(port) is free for use")
93  }
94 
95  static func release(socket: Int32) {
96    Darwin.shutdown(socket, SHUT_RDWR)
97    close(socket)
98  }
99  static func descriptionOfLastError() -> String {
100    return String(cString: UnsafePointer(strerror(errno)))
101  }
102}
103
Note: See TracBrowser for help on using the repository browser.