Changeset 5304709


Ignore:
Timestamp:
Oct 13, 2018 3:54:01 AM (22 months ago)
Author:
meeh <meeh@…>
Branches:
master
Children:
4f8af55
Parents:
a9bf9e06
Message:

OSX Launcher: general cleanup, responsibility delegation, and fixes

Location:
launchers/macosx/I2PLauncher
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • launchers/macosx/I2PLauncher/SwiftMainDelegate.swift

    ra9bf9e06 r5304709  
    9494 
    9595  @objc static func openLink(url: String) {
    96     SBridge.sharedInstance().openUrl(url)
     96    NSLog("Trying to open \(url)")
     97    NSWorkspace.shared().open(NSURL(string: url)! as URL)
    9798  }
    9899 
  • launchers/macosx/I2PLauncher/logger_c.h

    ra9bf9e06 r5304709  
    8383
    8484
     85
     86#define MMLog(format_string,...) ((MLog(1, [NSString stringWithFormat:format_string,##__VA_ARGS__])))
     87
    8588#endif /* logger_c_h */
  • launchers/macosx/I2PLauncher/routermgmt/RouterManager.swift

    ra9bf9e06 r5304709  
    2626 
    2727  var logViewStorage: NSTextStorage?
     28  var lastRouterPid : String? = nil
    2829 
    2930  private static func handleRouterException(information:Any?) {
     
    9596    routerManager.eventManager.listenTo(eventName: "router_can_start", action: routerManager.routerRunner.StartAgent)
    9697    routerManager.eventManager.listenTo(eventName: "router_can_setup", action: routerManager.routerRunner.SetupAgent)
     98   
     99    //routerManager.eventManager.listenTo(eventName: "launch_agent_running", action: routerManager.routerRunner.onLoadedAgent)
     100   
     101    routerManager.eventManager.listenTo(eventName: "launch_agent_running", action: {
     102      let agent = RouterRunner.launchAgent!
     103      let agentStatus = agent.status()
     104      switch agentStatus {
     105      case .running(let pid):
     106        DispatchQueue.main.async {
     107          routerManager.eventManager.trigger(eventName: "router_start", information: String(pid))
     108          routerManager.routerRunner.routerStatus.setRouterStatus(true)
     109          routerManager.routerRunner.routerStatus.setRouterRanByUs(true)
     110          routerManager.eventManager.trigger(eventName: "router_pid", information: String(pid))
     111        }
     112        break
     113       
     114      default:
     115        NSLog("wtf, agent status = \(agentStatus)")
     116        break
     117      }
     118    })
    97119    return routerManager
    98120  }()
  • launchers/macosx/I2PLauncher/routermgmt/RouterRunner.swift

    ra9bf9e06 r5304709  
    2323  let domainLabel = "net.i2p.macosx.I2PRouter"
    2424 
    25   let plistName = "net.i2p.macosx.I2PRouterAgent.plist"
     25  let plistName = "net.i2p.macosx.I2PRouter.plist"
    2626 
    2727  let defaultStartupCommand:String = "/usr/libexec/java_home"
     
    3535 
    3636  let appSupportPath = FileManager.default.urls(for: FileManager.SearchPathDirectory.applicationSupportDirectory, in: FileManager.SearchPathDomainMask.userDomainMask)
     37 
     38  override init() {
     39    super.init()
     40  }
    3741 
    3842  func SetupAgent() {
     
    107111    agent.runAtLoad = shouldStartupAtLogin
    108112    agent.keepAlive = true
    109    
    110     do {
    111      
    112       try LaunchAgentManager.shared.write(agent, called: self.plistName)
    113       sleep(1)
    114       try LaunchAgentManager.shared.load(agent)
    115       sleep(1)
    116      
    117       let agentStatus = LaunchAgentManager.shared.status(agent)
    118       switch agentStatus {
    119       case .running:
    120         break
    121       case .loaded:
    122         break
    123       case .unloaded:
    124         sleep(2)
    125         break
    126       }
    127      
    128      
    129       RouterManager.shared().eventManager.trigger(eventName: "router_can_start", information: agent)
    130     } catch {
    131       RouterManager.shared().eventManager.trigger(eventName: "router_setup_error", information: "\(error)")
     113    DispatchQueue(label: "background_starter").async {
     114      do {
     115        try LaunchAgentManager.shared.write(agent, called: self.plistName)
     116        sleep(1)
     117        try LaunchAgentManager.shared.load(agent)
     118        sleep(1)
     119       
     120        let agentStatus = LaunchAgentManager.shared.status(agent)
     121        switch agentStatus {
     122        case .running:
     123          break
     124        case .loaded:
     125          DispatchQueue.main.async {
     126            RouterManager.shared().eventManager.trigger(eventName: "router_can_start", information: agent)
     127          }
     128          break
     129        case .unloaded:
     130          break
     131        }
     132      } catch {
     133        DispatchQueue.main.async {
     134          RouterManager.shared().eventManager.trigger(eventName: "router_setup_error", information: "\(error)")
     135        }
     136      }
    132137    }
    133138    return agent
    134139  }
    135140 
    136   func StartAgent(information:Any?) {
    137     let agent = RouterRunner.launchAgent!
    138     LaunchAgentManager.shared.start(agent)
    139     sleep(1)
    140     let agentStatus = agent.status()
    141     switch agentStatus {
    142     case .running(let pid):
    143       RouterManager.shared().eventManager.trigger(eventName: "router_start", information: String(pid))
    144       routerStatus.setRouterStatus(true)
    145       routerStatus.setRouterRanByUs(true)
    146       DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
    147         // Delayed message to ensure UI has been initialized.
    148         RouterManager.shared().eventManager.trigger(eventName: "router_pid", information: String(pid))
    149       }
    150       break
    151      
    152     default: break
    153     }
    154   }
    155  
    156   func StopAgent() {
    157     var agentStatus = LaunchAgentManager.shared.status(RouterRunner.launchAgent!)
    158     switch agentStatus {
    159     case .running:
    160       LaunchAgentManager.shared.stop(RouterRunner.launchAgent!)
    161       break
    162     case .loaded, .unloaded:
    163       try! LaunchAgentManager.shared.load(RouterRunner.launchAgent!)
    164       routerStatus.setRouterStatus(false)
    165       routerStatus.setRouterRanByUs(false)
    166       RouterManager.shared().eventManager.trigger(eventName: "router_stop", information: "ok")
    167       return;
    168       break
    169     default: break
    170     }
    171     sleep(1)
    172     agentStatus = LaunchAgentManager.shared.status(RouterRunner.launchAgent!)
    173     switch agentStatus {
    174     case .loaded, .unloaded:
    175       try! LaunchAgentManager.shared.load(RouterRunner.launchAgent!)
    176       routerStatus.setRouterStatus(false)
    177       routerStatus.setRouterRanByUs(false)
    178       RouterManager.shared().eventManager.trigger(eventName: "router_stop", information: "ok")
    179       break
    180     default: break
     141 
     142  func StartAgent(_ information:Any? = nil) {
     143    let agent = RouterRunner.launchAgent ?? information as! LaunchAgent
     144    DispatchQueue(label: "background_block").async {
     145      LaunchAgentManager.shared.start(agent, { (proc) in
     146        NSLog("Will call onLaunchdStarted")
     147      })
     148    }
     149  }
     150 
     151  func StopAgent(_ callback: @escaping () -> () = {}) {
     152    let agentStatus = LaunchAgentManager.shared.status(RouterRunner.launchAgent!)
     153    DispatchQueue(label: "background_block").async {
     154      do {
     155        switch agentStatus {
     156        case .running:
     157          // For now we need to use unload to stop it.
     158          try LaunchAgentManager.shared.unload(RouterRunner.launchAgent!, { (proc) in
     159            // Called when stop is actually executed
     160            proc.waitUntilExit()
     161            DispatchQueue.main.async {
     162              RouterManager.shared().eventManager.trigger(eventName: "router_stop", information: "ok")
     163              callback()
     164            }
     165          })
     166          try LaunchAgentManager.shared.load(RouterRunner.launchAgent!)
     167          break
     168        case .unloaded:
     169          // Seems it sometimes get unloaded on stop, we load it again.
     170          try! LaunchAgentManager.shared.load(RouterRunner.launchAgent!)
     171          return
     172        default: break
     173        }
     174      } catch {
     175        NSLog("Error \(error)")
     176      }
    181177    }
    182178  }
Note: See TracChangeset for help on using the changeset viewer.