Changeset e2d2264 for launchers/macosx


Ignore:
Timestamp:
Oct 13, 2018 3:51:22 AM (21 months ago)
Author:
meeh <meeh@…>
Branches:
master
Children:
a9bf9e06
Parents:
5f07789
Message:

OSX Launcher:

  • UI Bugfixes/features
    • restart might work
    • open console button should work
    • cleanup
Location:
launchers/macosx/I2PLauncher/userinterface
Files:
2 edited

Legend:

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

    r5f07789 re2d2264  
    2525  @IBOutlet var routerPIDLabel: NSTextField?
    2626 
     27 
    2728  @IBOutlet var quickControlView: NSView?
    2829  @IBOutlet var routerStartStopButton: NSButton?
     30  @IBOutlet var restartRouterButton: NSButton?
    2931  @IBOutlet var openConsoleButton: NSButton?
    3032 
     33 
     34  @objc func actionBtnOpenConsole(_ sender: Any?) {
     35    SwiftMainDelegate.openLink(url: "http://localhost:7657")
     36  }
     37 
    3138  @objc func actionBtnStartRouter(_ sender: Any?) {
    32     NSLog("START ROUTER")
     39    NSLog("Router start clicked")
    3340    /*if (RouterManager.shared().getRouterTask() == nil) {
    3441      SBridge.sharedInstance().startupI2PRouter(RouterProcessStatus.i2pDirectoryPath)
     
    3643    (sender as! NSButton).isTransparent = true
    3744    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)
     45    DispatchQueue(label: "background_start").async {
     46      switch routerStatus {
     47      case .loaded?:
     48        RouterManager.shared().routerRunner.StartAgent(RouterRunner.launchAgent)
     49      case .unloaded?:
     50        do {
     51          try LaunchAgentManager.shared.load(RouterRunner.launchAgent!)
     52          RouterManager.shared().routerRunner.StartAgent(RouterRunner.launchAgent)
     53        } catch {
     54          RouterManager.shared().eventManager.trigger(eventName: "router_exception", information: error)
     55        }
     56        break
     57      default:
     58        break
    4759      }
    48       break
    49     default:
    50       break
    51     }
    52     self.reEnableButton()
     60      DispatchQueue.main.async {
     61        self.reEnableButton()
     62      }
     63    }
    5364  }
    5465 
    5566  @objc func actionBtnStopRouter(_ sender: Any?) {
    56     NSLog("STOP ROUTER")
    57     let routerStatus = RouterRunner.launchAgent?.status()
    58     switch routerStatus {
    59     case .running?:
    60       NSLog("Found running router")
    61       RouterManager.shared().routerRunner.StopAgent()
    62       break
    63     default:
    64       break
    65     }
    66     self.reEnableButton()
    67   }
    68  
    69   @objc func actionBtnRestartRouter(sender: Any?) {
    70     if (RouterManager.shared().getRouterTask() != nil) {
    71       //RouterManager.shared().getRouterTask()?.requestRestart()
     67    NSLog("Router stop clicked")
     68    DispatchQueue(label: "background_shutdown").async {
     69      RouterManager.shared().routerRunner.StopAgent({
     70        RouterProcessStatus.isRouterRunning = false
     71        RouterProcessStatus.isRouterChildProcess = false
     72        NSLog("Router should be stopped by now.")
     73      })
     74      // Wait for it to die.
     75     
     76    }
     77    RouterManager.shared().eventManager.trigger(eventName: "toggle_popover")
     78    self.reEnableButton()
     79  }
     80 
     81  func restartFn() {
     82    RouterManager.shared().routerRunner.StopAgent({
     83      sleep(30)
     84      RouterManager.shared().routerRunner.StartAgent()
     85    })
     86  }
     87 
     88  @objc func actionBtnRestartRouter(_ sender: Any?) {
     89    RouterManager.shared().eventManager.trigger(eventName: "toggle_popover")
     90    let currentStatus : AgentStatus = RouterRunner.launchAgent?.status() ?? AgentStatus.unloaded
     91    if currentStatus != AgentStatus.loaded && currentStatus != AgentStatus.unloaded {
     92      NSLog("Found a running router, will unload it from launchd")
     93      // OK, router seems to be running
     94      DispatchQueue(label: "background_restart").async {
     95        self.restartFn()
     96        // Report done to main thread
     97        DispatchQueue.main.async {
     98          self.reEnableButton()
     99        }
     100      }
    72101    } else {
    73102      NSLog("Can't restart a non running router, start it however...")
    74       //SBridge.sharedInstance().startupI2PRouter(RouterProcessStatus.i2pDirectoryPath)
     103      RouterManager.shared().routerRunner.StartAgent()
    75104    }
    76105  }
    77106 
    78107  func handlerRouterStart(information:Any?) {
    79     print("Triggered handlerRouterStart")
     108    NSLog("Triggered handlerRouterStart")
    80109    NSLog("PID2! %@", information as! String)
    81110    routerPIDLabel?.cell?.stringValue = "Router PID: "+(information as! String)
    82111    routerPIDLabel?.needsDisplay = true
    83112    routerStatusLabel?.cell?.stringValue = "Router status: Running"
     113    RouterManager.shared().lastRouterPid = (information as? String)
    84114    self.toggleSetButtonStop()
    85115    self.reEnableButton()
     
    113143    }
    114144    self.reEnableButton()
     145    openConsoleButton!.cell!.action = #selector(self.actionBtnOpenConsole(_:))
     146    openConsoleButton!.cell!.target = self
     147    restartRouterButton!.cell!.action = #selector(self.actionBtnRestartRouter(_:))
     148    restartRouterButton!.cell!.target = self
     149   
    115150  }
    116151 
    117152  func handleRouterStop() {
    118153    routerPIDLabel?.cell?.stringValue = "Router PID: Not running"
     154    RouterManager.shared().lastRouterPid = nil
    119155    self.toggleSetButtonStart()
    120156    reEnableButton()
     
    134170 
    135171  func setRouterStatusLabelText() {
    136     routerStartStopButton?.needsDisplay = true
    137172    routerStartStopButton?.target = self
    138     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     }
    151    
    152     let staticStartedByLabelText = "Router started by launcher?"
     173    let staticStartedByLabelText = "Router started by launcher? "
     174    let staticIsRunningLabelText = "Router status: "
     175    let staticRouterVersionLabelText = "Router version: "
     176    let staticRouterPidLabelText = "Router PID: "
     177   
     178    // Use default here to avoid any potential crashes with force unwrapping
     179    let currentStatus : AgentStatus = RouterRunner.launchAgent?.status() ?? AgentStatus.unloaded
     180    if currentStatus == AgentStatus.loaded || currentStatus == AgentStatus.unloaded  {
     181      routerStatusLabel?.cell?.stringValue = staticIsRunningLabelText+"Not running"
     182    } else {
     183      routerStatusLabel?.cell?.stringValue = staticIsRunningLabelText+"Running"
     184    }
     185   
    153186    if RouterProcessStatus.isRouterChildProcess {
    154       routerStartedByLabel?.cell?.stringValue = staticStartedByLabelText+" Yes"
    155     } else {
    156       routerStartedByLabel?.cell?.stringValue = staticStartedByLabelText+" No"
    157     }
    158     routerStartedByLabel?.needsDisplay = true
     187      routerStartedByLabel?.cell?.stringValue = staticStartedByLabelText+"Yes"
     188    } else {
     189      routerStartedByLabel?.cell?.stringValue = staticStartedByLabelText+"No"
     190    }
     191   
     192    // Try to display PID - if not, the string behind ?? is used as "default"
     193    let tmpPidText = RouterManager.shared().lastRouterPid ?? "Not running"
     194    routerPIDLabel?.cell?.stringValue = staticRouterPidLabelText+tmpPidText
    159195   
    160196    if let version = RouterProcessStatus.routerVersion {
    161       routerVersionLabel?.cell?.stringValue = "Router version: " + version
    162     } else {
    163       routerVersionLabel?.cell?.stringValue = "Router version: Still unknown"
    164     }
     197      routerVersionLabel?.cell?.stringValue = staticRouterVersionLabelText + version
     198    } else {
     199      routerVersionLabel?.cell?.stringValue = staticRouterVersionLabelText + "Still unknown"
     200    }
     201   
    165202    if let routerStartTime = RouterProcessStatus.routerStartedAt {
    166203      routerUptimeLabel?.cell?.stringValue = "Uptime: Router started " + DateTimeUtils.timeAgoSinceDate(date: NSDate(date: routerStartTime), numericDates: false)
     
    168205      routerUptimeLabel?.cell?.stringValue = "Uptime: Router isn't running"
    169206    }
     207   
     208    // Needs display function alerts the rendrerer that the UI parts need to be re-drawed.
     209    routerStartStopButton?.needsDisplay = true
     210    quickControlView?.needsDisplay = true
    170211    routerUptimeLabel?.needsDisplay = true
     212    routerVersionLabel?.needsDisplay = true
     213    routerStartedByLabel?.needsDisplay = true
     214    routerPIDLabel?.needsDisplay = true
    171215  }
    172216 
  • launchers/macosx/I2PLauncher/userinterface/StatusBarController.swift

    r5f07789 re2d2264  
    4545    NSLog("PID! %@", pidStr)
    4646    showPopover(sender: nil)
    47     //self.ctrl?.getRouterStatusView()?.handlerRouterStart(information: pidStr)
     47    RouterManager.shared().lastRouterPid = pidStr
    4848    self.ctrl?.getRouterStatusView()?.needsDisplay = true
     49  }
     50 
     51  func event_toggle(information:Any?) {
     52    self.togglePopover(sender: self)
    4953  }
    5054
     
    5458    self.ctrl = PopoverViewController.freshController()
    5559    popover.contentViewController = self.ctrl
    56     //updateObjectRef = SUUpdater.shared()
    57     //updateObjectRef?.checkForUpdatesInBackground()
    58    RouterManager.shared().eventManager.listenTo(eventName: "router_pid", action: pidReaction)
     60    RouterManager.shared().eventManager.listenTo(eventName: "router_pid", action: pidReaction)
    5961   
     62    RouterManager.shared().eventManager.listenTo(eventName: "toggle_popover", action: event_toggle)
    6063   
    6164    if let button = statusItem.button {
Note: See TracChangeset for help on using the changeset viewer.