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

Last change on this file since e3ab9f8e was e3ab9f8e, checked in by meeh <meeh@…>, 20 months ago

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

  • Property mode set to 100644
File size: 6.5 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  @IBOutlet var quickControlView: NSView?
28  @IBOutlet var routerStartStopButton: NSButton?
29  @IBOutlet var openConsoleButton: NSButton?
30 
31  @objc func actionBtnStartRouter(_ sender: Any?) {
32    NSLog("START ROUTER")
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
51    }
52    self.reEnableButton()
53  }
54 
55  @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()
72    } else {
73      NSLog("Can't restart a non running router, start it however...")
74      //SBridge.sharedInstance().startupI2PRouter(RouterProcessStatus.i2pDirectoryPath)
75    }
76  }
77 
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  }
87 
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  }
108 
109  override func viewWillDraw() {
110    super.viewWillDraw()
111    if (RouterStatusView.instance != nil) {
112      RouterStatusView.instance = self
113    }
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(_:))
133  }
134 
135  func setRouterStatusLabelText() {
136    routerStartStopButton?.needsDisplay = true
137    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?"
153    if RouterProcessStatus.isRouterChildProcess {
154      routerStartedByLabel?.cell?.stringValue = staticStartedByLabelText+" Yes"
155    } else {
156      routerStartedByLabel?.cell?.stringValue = staticStartedByLabelText+" No"
157    }
158    routerStartedByLabel?.needsDisplay = true
159   
160    if let version = RouterProcessStatus.routerVersion {
161      routerVersionLabel?.cell?.stringValue = "Router version: " + version
162    } else {
163      routerVersionLabel?.cell?.stringValue = "Router version: Still unknown"
164    }
165    if let routerStartTime = RouterProcessStatus.routerStartedAt {
166      routerUptimeLabel?.cell?.stringValue = "Uptime: Router started " + DateTimeUtils.timeAgoSinceDate(date: NSDate(date: routerStartTime), numericDates: false)
167    } else {
168      routerUptimeLabel?.cell?.stringValue = "Uptime: Router isn't running"
169    }
170    routerUptimeLabel?.needsDisplay = true
171  }
172 
173 
174  init() {
175    let c = NSCoder()
176    super.init(coder: c)!
177    self.setupObservers()
178    self.toggleSetButtonStart()
179    self.reEnableButton()
180  }
181 
182  required init?(coder decoder: NSCoder) {
183    super.init(coder: decoder)
184    self.setupObservers()
185    self.toggleSetButtonStart()
186    self.reEnableButton()
187  }
188 
189}
Note: See TracBrowser for help on using the repository browser.