Ignore:
Timestamp:
Oct 11, 2018 5:02:42 PM (20 months ago)
Author:
meeh <meeh@…>
Branches:
master
Children:
8e988c39
Parents:
96d31995
Message:

OSX Launcher: Swift GUI code updates, most related to the new launch method.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • launchers/macosx/I2PLauncher/userinterface/RouterStatusView.swift

    r96d31995 re3ab9f8e  
    2323  @IBOutlet var routerStartedByLabel: NSTextField?
    2424  @IBOutlet var routerUptimeLabel: NSTextField?
     25  @IBOutlet var routerPIDLabel: NSTextField?
    2526 
    2627  @IBOutlet var quickControlView: NSView?
    2728  @IBOutlet var routerStartStopButton: NSButton?
     29  @IBOutlet var openConsoleButton: NSButton?
    2830 
    2931  @objc func actionBtnStartRouter(_ sender: Any?) {
    3032    NSLog("START ROUTER")
    31     if (RouterManager.shared().getRouterTask() == nil) {
    32       SBridge.sharedInstance().startupI2PRouter(RouterProcessStatus.i2pDirectoryPath, javaBinPath: RouterProcessStatus.knownJavaBinPath!)
     33    /*if (RouterManager.shared().getRouterTask() == nil) {
     34      SBridge.sharedInstance().startupI2PRouter(RouterProcessStatus.i2pDirectoryPath)
     35    }*/
     36    (sender as! NSButton).isTransparent = true
     37    let routerStatus = RouterRunner.launchAgent?.status()
     38    switch routerStatus {
     39    case .loaded?:
     40      RouterManager.shared().routerRunner.StartAgent(information: RouterRunner.launchAgent)
     41    case .unloaded?:
     42      do {
     43        try LaunchAgentManager.shared.load(RouterRunner.launchAgent!)
     44        RouterManager.shared().routerRunner.StartAgent(information: RouterRunner.launchAgent)
     45      } catch {
     46        RouterManager.shared().eventManager.trigger(eventName: "router_exception", information: error)
     47      }
     48      break
     49    default:
     50      break
    3351    }
    34     RouterManager.shared().updateState()
     52    self.reEnableButton()
    3553  }
    3654 
    3755  @objc func actionBtnStopRouter(_ sender: Any?) {
    3856    NSLog("STOP ROUTER")
    39     if (RouterManager.shared().getRouterTask() != nil) {
     57    let routerStatus = RouterRunner.launchAgent?.status()
     58    switch routerStatus {
     59    case .running?:
    4060      NSLog("Found running router")
    41       RouterManager.shared().getRouterTask()?.requestShutdown()
    42       RouterManager.shared().updateState()
     61      RouterManager.shared().routerRunner.StopAgent()
     62      break
     63    default:
     64      break
    4365    }
     66    self.reEnableButton()
    4467  }
    4568 
    4669  @objc func actionBtnRestartRouter(sender: Any?) {
    4770    if (RouterManager.shared().getRouterTask() != nil) {
    48       RouterManager.shared().getRouterTask()?.requestRestart()
     71      //RouterManager.shared().getRouterTask()?.requestRestart()
    4972    } else {
    5073      NSLog("Can't restart a non running router, start it however...")
    51       SBridge.sharedInstance().startupI2PRouter(RouterProcessStatus.i2pDirectoryPath, javaBinPath: RouterProcessStatus.knownJavaBinPath!)
     74      //SBridge.sharedInstance().startupI2PRouter(RouterProcessStatus.i2pDirectoryPath)
    5275    }
    53     RouterManager.shared().updateState()
    5476  }
    5577 
     78  func handlerRouterStart(information:Any?) {
     79    print("Triggered handlerRouterStart")
     80    NSLog("PID2! %@", information as! String)
     81    routerPIDLabel?.cell?.stringValue = "Router PID: "+(information as! String)
     82    routerPIDLabel?.needsDisplay = true
     83    routerStatusLabel?.cell?.stringValue = "Router status: Running"
     84    self.toggleSetButtonStop()
     85    self.reEnableButton()
     86  }
    5687 
     88  func reEnableButton() {
     89    let currentStatus : AgentStatus = RouterRunner.launchAgent?.status() ?? AgentStatus.unloaded
     90    if currentStatus != AgentStatus.loaded && currentStatus != AgentStatus.unloaded  {
     91      self.toggleSetButtonStop()
     92    } else {
     93      self.toggleSetButtonStart()
     94    }
     95    routerStartStopButton?.isTransparent = false
     96    routerStartStopButton?.needsDisplay = true
     97    self.setRouterStatusLabelText()
     98  }
     99 
     100  func setupObservers() {
     101    RouterManager.shared().eventManager.listenTo(eventName: "router_start", action: handlerRouterStart)
     102    RouterManager.shared().eventManager.listenTo(eventName: "router_stop", action: handleRouterStop)
     103    RouterManager.shared().eventManager.listenTo(eventName: "router_pid", action: handlerRouterStart)
     104    RouterManager.shared().eventManager.listenTo(eventName: "launch_agent_running", action: reEnableButton)
     105    RouterManager.shared().eventManager.listenTo(eventName: "launch_agent_unloaded", action: reEnableButton)
     106    RouterManager.shared().eventManager.listenTo(eventName: "launch_agent_loaded", action: reEnableButton)
     107  }
    57108 
    58109  override func viewWillDraw() {
     
    61112      RouterStatusView.instance = self
    62113    }
    63     self.setRouterStatusLabelText()
     114    self.reEnableButton()
     115  }
     116 
     117  func handleRouterStop() {
     118    routerPIDLabel?.cell?.stringValue = "Router PID: Not running"
     119    self.toggleSetButtonStart()
     120    reEnableButton()
     121  }
     122 
     123  private func toggleSetButtonStart() {
     124    routerStatusLabel?.cell?.stringValue = "Router status: Not running"
     125    routerStartStopButton?.title = "Start Router"
     126    routerStartStopButton?.action = #selector(self.actionBtnStartRouter(_:))
     127  }
     128 
     129  private func toggleSetButtonStop() {
     130    routerStatusLabel?.cell?.stringValue = "Router status: Running"
     131    routerStartStopButton?.title = "Stop Router"
     132    routerStartStopButton?.action = #selector(self.actionBtnStopRouter(_:))
    64133  }
    65134 
    66135  func setRouterStatusLabelText() {
    67     if (RouterProcessStatus.isRouterRunning) {
    68       routerStatusLabel?.cell?.stringValue = "Router status: Running"
    69       routerStartStopButton?.title = "Stop Router"
    70       routerStartStopButton?.action = #selector(self.actionBtnStopRouter(_:))
    71     } else {
    72       routerStatusLabel?.cell?.stringValue = "Router status: Not running"
    73       routerStartStopButton?.title = "Start Router"
    74       routerStartStopButton?.action = #selector(self.actionBtnStartRouter(_:))
    75     }
    76136    routerStartStopButton?.needsDisplay = true
    77137    routerStartStopButton?.target = self
    78138    quickControlView?.needsDisplay = true
     139   
     140    do {
     141      let currentStatus : AgentStatus = RouterRunner.launchAgent!.status()
     142      if currentStatus == AgentStatus.loaded || currentStatus == AgentStatus.unloaded  {
     143        routerStatusLabel?.cell?.stringValue = "Router status: Not running"
     144      } else {
     145        routerStatusLabel?.cell?.stringValue = "Router status: Running"
     146      }
     147    } catch {
     148      // Ensure it's set even AgentStatus is nil (uninitialized yet..)
     149      routerStatusLabel?.cell?.stringValue = "Router status: Not running"
     150    }
    79151   
    80152    let staticStartedByLabelText = "Router started by launcher?"
     
    103175    let c = NSCoder()
    104176    super.init(coder: c)!
    105     self.setRouterStatusLabelText()
     177    self.setupObservers()
     178    self.toggleSetButtonStart()
     179    self.reEnableButton()
    106180  }
    107181 
    108182  required init?(coder decoder: NSCoder) {
    109183    super.init(coder: decoder)
    110     self.setRouterStatusLabelText()
     184    self.setupObservers()
     185    self.toggleSetButtonStart()
     186    self.reEnableButton()
    111187  }
    112188 
Note: See TracChangeset for help on using the changeset viewer.