Browse Source

Only poll the ZeroTier service when the menu or Network List are visible. Reduces CPU idle prevention by a a LOT

Grant Limberg 9 years ago
parent
commit
78e5a00a68

+ 12 - 2
ZeroTier One/AppDelegate.swift

@@ -9,7 +9,7 @@
 import Cocoa
 import Cocoa
 
 
 @NSApplicationMain
 @NSApplicationMain
-class AppDelegate: NSObject, NSApplicationDelegate {
+class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
 
 
     @IBOutlet weak var window: NSWindow!
     @IBOutlet weak var window: NSWindow!
 
 
@@ -85,6 +85,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
                 self.showAbout()
                 self.showAbout()
             }
             }
         }
         }
+
+        monitor.updateNetworkInfo()
     }
     }
 
 
     func applicationWillTerminate(aNotification: NSNotification) {
     func applicationWillTerminate(aNotification: NSNotification) {
@@ -181,6 +183,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
 
 
     func buildMenu() {
     func buildMenu() {
         let menu = NSMenu()
         let menu = NSMenu()
+        menu.delegate = self
 
 
         if let s = self.status {
         if let s = self.status {
             menu.addItem(NSMenuItem(title: "Node ID: \(s.address)", action: #selector(AppDelegate.copyNodeID), keyEquivalent: ""))
             menu.addItem(NSMenuItem(title: "Node ID: \(s.address)", action: #selector(AppDelegate.copyNodeID), keyEquivalent: ""))
@@ -230,7 +233,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {
         menu.addItem(NSMenuItem(title: "Quit ZeroTier One", action: #selector(AppDelegate.quit), keyEquivalent: "q"))
         menu.addItem(NSMenuItem(title: "Quit ZeroTier One", action: #selector(AppDelegate.quit), keyEquivalent: "q"))
 
 
         statusItem.menu = menu
         statusItem.menu = menu
-
     }
     }
 
 
     func toggleNetwork(sender: NSMenuItem) {
     func toggleNetwork(sender: NSMenuItem) {
@@ -253,5 +255,13 @@ class AppDelegate: NSObject, NSApplicationDelegate {
             pasteboard.setString(s.address, forType: NSPasteboardTypeString)
             pasteboard.setString(s.address, forType: NSPasteboardTypeString)
         }
         }
     }
     }
+
+    func menuWillOpen(menu: NSMenu) {
+        monitor.start()
+    }
+
+    func menuDidClose(menu: NSMenu) {
+        monitor.stop()
+    }
 }
 }
 
 

+ 11 - 1
ZeroTier One/NetworkMonitor.swift

@@ -21,7 +21,14 @@ class NetworkMonitor: NSObject {
 
 
     override init() {
     override init() {
         super.init()
         super.init()
+    }
 
 
+    deinit {
+        timer?.invalidate()
+    }
+
+    func start() {
+        NSLog("ZeroTier monitor started")
         timer = NSTimer.scheduledTimerWithTimeInterval(1.0,
         timer = NSTimer.scheduledTimerWithTimeInterval(1.0,
                                                        target: self,
                                                        target: self,
                                                        selector: #selector(updateNetworkInfo),
                                                        selector: #selector(updateNetworkInfo),
@@ -29,8 +36,11 @@ class NetworkMonitor: NSObject {
                                                        repeats: true)
                                                        repeats: true)
     }
     }
 
 
-    deinit {
+
+    func stop() {
+        NSLog("ZeroTier monitor stopped")
         timer?.invalidate()
         timer?.invalidate()
+        timer = nil
     }
     }
 
 
     private func  dataFile() -> String {
     private func  dataFile() -> String {

+ 4 - 2
ZeroTier One/ShowNetworksViewController.swift

@@ -35,6 +35,8 @@ class ShowNetworksViewController: NSViewController, NSTableViewDelegate, NSTable
     override func viewWillAppear() {
     override func viewWillAppear() {
         super.viewWillAppear()
         super.viewWillAppear()
         visible = true
         visible = true
+
+        netMonitor.start()
     }
     }
 
 
     override func viewWillDisappear() {
     override func viewWillDisappear() {
@@ -43,6 +45,8 @@ class ShowNetworksViewController: NSViewController, NSTableViewDelegate, NSTable
         let filePath = dataFile()
         let filePath = dataFile()
         NSKeyedArchiver.archiveRootObject(self.networkList, toFile: filePath)
         NSKeyedArchiver.archiveRootObject(self.networkList, toFile: filePath)
         visible = false
         visible = false
+
+        netMonitor.stop()
     }
     }
 
 
     func deleteNetworkFromList(nwid: String) {
     func deleteNetworkFromList(nwid: String) {
@@ -55,8 +59,6 @@ class ShowNetworksViewController: NSViewController, NSTableViewDelegate, NSTable
         if visible {
         if visible {
             tableView?.reloadData()
             tableView?.reloadData()
         }
         }
-
-        NSLog("Visible? \(visible)")
     }
     }
 
 
     // NSTableViewDataSource
     // NSTableViewDataSource