source: launchers/macosx/I2PLauncher/userinterface/RouterStatusView.swift @ f8fb4a6

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

OSX Launcher: Remove restart button

  • Property mode set to 100644
File size: 7.4 KB
Line 
1//
2//  RouterStatusView.swift
3//  I2PLauncher
4//
5//  Created by Mikal Villa on 22/09/2018.
6//  Copyright © 2018 The I2P Project. All rights reserved.
7//
8
9import Cocoa
10
11@objc class RouterStatusView : NSView {
12  static var instance: RouterStatusView?
13 
14  static func getInstance() -> RouterStatusView? {
15    if (self.instance != Optional.none) {
16      return RouterStatusView.instance
17    }
18    return Optional.none
19  }
20 
21  @IBOutlet var routerStatusLabel: NSTextField?
22  @IBOutlet var routerVersionLabel: NSTextField?
23  @IBOutlet var routerStartedByLabel: NSTextField?
24  @IBOutlet var routerUptimeLabel: NSTextField?
25  @IBOutlet var routerPIDLabel: NSTextField?
26 
27 
28  @IBOutlet var quickControlView: NSView?
29  @IBOutlet var routerStartStopButton: NSButton?
30  @IBOutlet var openConsoleButton: NSButton?
31 
32 
33  @objc func actionBtnOpenConsole(_ sender: Any?) {
34    SwiftMainDelegate.openLink(url: "http://localhost:7657")
35  }
36 
37  @objc func actionBtnStartRouter(_ sender: Any?) {
38    NSLog("Router start clicked")
39    /*if (RouterManager.shared().getRouterTask() == nil) {
40      SBridge.sharedInstance().startupI2PRouter(RouterProcessStatus.i2pDirectoryPath)
41    }*/
42    (sender as! NSButton).isTransparent = true
43    let routerStatus = RouterRunner.launchAgent?.status()
44    DispatchQueue(label: "background_start").async {
45      switch routerStatus {
46      case .loaded?:
47        RouterManager.shared().routerRunner.StartAgent(RouterRunner.launchAgent)
48      case .unloaded?:
49        do {
50          try LaunchAgentManager.shared.load(RouterRunner.launchAgent!)
51          RouterManager.shared().routerRunner.StartAgent(RouterRunner.launchAgent)
52        } catch {
53          RouterManager.shared().eventManager.trigger(eventName: "router_exception", information: error)
54        }
55        break
56      default:
57        break
58      }
59      DispatchQueue.main.async {
60        self.reEnableButton()
61      }
62    }
63  }
64 
65  @objc func actionBtnStopRouter(_ sender: Any?) {
66    NSLog("Router stop clicked")
67    DispatchQueue(label: "background_shutdown").async {
68      RouterManager.shared().routerRunner.StopAgent({
69        RouterProcessStatus.isRouterRunning = false
70        RouterProcessStatus.isRouterChildProcess = false
71        NSLog("Router should be stopped by now.")
72      })
73      // Wait for it to die.
74     
75    }
76    RouterManager.shared().eventManager.trigger(eventName: "toggle_popover")
77    self.reEnableButton()
78  }
79 
80  func restartFn() {
81    RouterManager.shared().routerRunner.StopAgent({
82      sleep(30)
83      RouterManager.shared().routerRunner.StartAgent()
84    })
85  }
86 
87  func handlerRouterStart(information:Any?) {
88    NSLog("Triggered handlerRouterStart")
89    NSLog("PID2! %@", information as! String)
90    routerPIDLabel?.cell?.stringValue = "Router PID: "+(information as! String)
91    routerPIDLabel?.needsDisplay = true
92    routerStatusLabel?.cell?.stringValue = "Router status: Running"
93    RouterManager.shared().lastRouterPid = (information as? String)
94    self.toggleSetButtonStop()
95    self.reEnableButton()
96  }
97 
98  func reEnableButton() {
99    let currentStatus : AgentStatus = RouterRunner.launchAgent?.status() ?? AgentStatus.unloaded
100    if currentStatus != AgentStatus.loaded && currentStatus != AgentStatus.unloaded  {
101      self.toggleSetButtonStop()
102    } else {
103      self.toggleSetButtonStart()
104    }
105    routerStartStopButton?.isTransparent = false
106    routerStartStopButton?.needsDisplay = true
107    self.setRouterStatusLabelText()
108  }
109 
110  func setupObservers() {
111    RouterManager.shared().eventManager.listenTo(eventName: "router_start", action: handlerRouterStart)
112    RouterManager.shared().eventManager.listenTo(eventName: "router_stop", action: handleRouterStop)
113    RouterManager.shared().eventManager.listenTo(eventName: "router_pid", action: handlerRouterStart)
114    RouterManager.shared().eventManager.listenTo(eventName: "launch_agent_running", action: reEnableButton)
115    RouterManager.shared().eventManager.listenTo(eventName: "launch_agent_unloaded", action: reEnableButton)
116    RouterManager.shared().eventManager.listenTo(eventName: "launch_agent_loaded", action: reEnableButton)
117  }
118 
119  override func viewWillDraw() {
120    super.viewWillDraw()
121    if (RouterStatusView.instance != nil) {
122      RouterStatusView.instance = self
123    }
124    self.reEnableButton()
125    openConsoleButton!.cell!.action = #selector(self.actionBtnOpenConsole(_:))
126    openConsoleButton!.cell!.target = self
127   
128  }
129 
130  func handleRouterStop() {
131    routerPIDLabel?.cell?.stringValue = "Router PID: Not running"
132    RouterManager.shared().lastRouterPid = nil
133    self.toggleSetButtonStart()
134    reEnableButton()
135  }
136 
137  private func toggleSetButtonStart() {
138    routerStatusLabel?.cell?.stringValue = "Router status: Not running"
139    routerStartStopButton?.title = "Start Router"
140    routerStartStopButton?.action = #selector(self.actionBtnStartRouter(_:))
141  }
142 
143  private func toggleSetButtonStop() {
144    routerStatusLabel?.cell?.stringValue = "Router status: Running"
145    routerStartStopButton?.title = "Stop Router"
146    routerStartStopButton?.action = #selector(self.actionBtnStopRouter(_:))
147  }
148 
149  func setRouterStatusLabelText() {
150    routerStartStopButton?.target = self
151    let staticStartedByLabelText = "Router started by launcher? "
152    let staticIsRunningLabelText = "Router status: "
153    let staticRouterVersionLabelText = "Router version: "
154    let staticRouterPidLabelText = "Router PID: "
155   
156    // Use default here to avoid any potential crashes with force unwrapping
157    let currentStatus : AgentStatus = RouterRunner.launchAgent?.status() ?? AgentStatus.unloaded
158    if currentStatus == AgentStatus.loaded || currentStatus == AgentStatus.unloaded  {
159      routerStatusLabel?.cell?.stringValue = staticIsRunningLabelText+"Not running"
160    } else {
161      routerStatusLabel?.cell?.stringValue = staticIsRunningLabelText+"Running"
162    }
163   
164    if RouterProcessStatus.isRouterChildProcess {
165      routerStartedByLabel?.cell?.stringValue = staticStartedByLabelText+"Yes"
166    } else {
167      routerStartedByLabel?.cell?.stringValue = staticStartedByLabelText+"No"
168    }
169   
170    // Try to display PID - if not, the string behind ?? is used as "default"
171    let tmpPidText = RouterManager.shared().lastRouterPid ?? "Not running"
172    routerPIDLabel?.cell?.stringValue = staticRouterPidLabelText+tmpPidText
173   
174    if let version = RouterProcessStatus.routerVersion {
175      routerVersionLabel?.cell?.stringValue = staticRouterVersionLabelText + version
176    } else {
177      routerVersionLabel?.cell?.stringValue = staticRouterVersionLabelText + "Still unknown"
178    }
179   
180    if let routerStartTime = RouterProcessStatus.routerStartedAt {
181      routerUptimeLabel?.cell?.stringValue = "Uptime: Router started " + DateTimeUtils.timeAgoSinceDate(date: NSDate(date: routerStartTime), numericDates: false)
182    } else {
183      routerUptimeLabel?.cell?.stringValue = "Uptime: Router isn't running"
184    }
185   
186    // Needs display function alerts the rendrerer that the UI parts need to be re-drawed.
187    routerStartStopButton?.needsDisplay = true
188    quickControlView?.needsDisplay = true
189    routerUptimeLabel?.needsDisplay = true
190    routerVersionLabel?.needsDisplay = true
191    routerStartedByLabel?.needsDisplay = true
192    routerPIDLabel?.needsDisplay = true
193  }
194 
195 
196  init() {
197    let c = NSCoder()
198    super.init(coder: c)!
199    self.setupObservers()
200    self.toggleSetButtonStart()
201    self.reEnableButton()
202  }
203 
204  required init?(coder decoder: NSCoder) {
205    super.init(coder: decoder)
206    self.setupObservers()
207    self.toggleSetButtonStart()
208    self.reEnableButton()
209  }
210 
211}
Note: See TracBrowser for help on using the repository browser.