diff --git a/ZeroTier One/AppDelegate.swift b/ZeroTier One/AppDelegate.swift index 298363aa1..0bcea7415 100644 --- a/ZeroTier One/AppDelegate.swift +++ b/ZeroTier One/AppDelegate.swift @@ -87,6 +87,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate { } monitor.updateNetworkInfo() + monitor.start() } func applicationWillTerminate(aNotification: NSNotification) { @@ -243,10 +244,10 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate { let id = String(network.nwid, radix: 16) if network.connected { - ServiceCom.leaveNetwork(id) + ServiceCom.sharedInstance.leaveNetwork(id) } else { - ServiceCom.joinNetwork(id) + ServiceCom.sharedInstance.joinNetwork(id) } } @@ -257,11 +258,10 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate { } func menuWillOpen(menu: NSMenu) { - monitor.start() + //monitor.updateNetworkInfo() } func menuDidClose(menu: NSMenu) { - monitor.stop() } } diff --git a/ZeroTier One/JoinNetworkViewController.swift b/ZeroTier One/JoinNetworkViewController.swift index ecab8f6c6..446e87199 100644 --- a/ZeroTier One/JoinNetworkViewController.swift +++ b/ZeroTier One/JoinNetworkViewController.swift @@ -69,10 +69,10 @@ class JoinNetworkViewController: NSViewController, NSComboBoxDelegate, NSComboBo @IBAction func onJoinClicked(sender: AnyObject?) { let networkString = network.stringValue - ServiceCom.joinNetwork(networkString, - allowManaged: allowManagedCheckBox.state == NSOnState, - allowGlobal: allowGlobalCheckBox.state == NSOnState, - allowDefault: allowDefaultCheckBox.state == NSOnState) + ServiceCom.sharedInstance.joinNetwork(networkString, + allowManaged: allowManagedCheckBox.state == NSOnState, + allowGlobal: allowGlobalCheckBox.state == NSOnState, + allowDefault: allowDefaultCheckBox.state == NSOnState) network.stringValue = "" diff --git a/ZeroTier One/NetworkInfoCell.swift b/ZeroTier One/NetworkInfoCell.swift index 12e719480..2341b298f 100644 --- a/ZeroTier One/NetworkInfoCell.swift +++ b/ZeroTier One/NetworkInfoCell.swift @@ -56,14 +56,14 @@ class NetworkInfoCell: NSTableCellView { } func joinNetwork(nwid: String) { - ServiceCom.joinNetwork(nwid, - allowManaged: allowManaged.state == NSOnState, - allowGlobal: allowGlobal.state == NSOnState, - allowDefault: !defaultRouteExists(parent.networkList) && (allowDefault.state == NSOnState)) + ServiceCom.sharedInstance.joinNetwork(nwid, + allowManaged: allowManaged.state == NSOnState, + allowGlobal: allowGlobal.state == NSOnState, + allowDefault: !defaultRouteExists(parent.networkList) && (allowDefault.state == NSOnState)) } func leaveNetwork(nwid: String) { - ServiceCom.leaveNetwork(nwid) + ServiceCom.sharedInstance.leaveNetwork(nwid) } @IBAction func onAllowStatusChanged(sender: NSButton) { diff --git a/ZeroTier One/NetworkMonitor.swift b/ZeroTier One/NetworkMonitor.swift index c744e6e82..e96d7aac0 100644 --- a/ZeroTier One/NetworkMonitor.swift +++ b/ZeroTier One/NetworkMonitor.swift @@ -50,6 +50,7 @@ class NetworkMonitor: NSObject { } func updateNetworkInfo() { + //NSLog("updateNetworkInfo") let filePath = dataFile() if NSFileManager.defaultManager().fileExistsAtPath(filePath) { @@ -62,7 +63,7 @@ class NetworkMonitor: NSObject { } } - ServiceCom.getNetworkList() { (networkList) -> Void in + ServiceCom.sharedInstance.getNetworkList() { (networkList) -> Void in self.receivedNetworks = networkList NSOperationQueue.mainQueue().addOperationWithBlock() { () -> Void in @@ -70,7 +71,7 @@ class NetworkMonitor: NSObject { } } - ServiceCom.getNodeStatus() { nodeStatus -> Void in + ServiceCom.sharedInstance.getNodeStatus() { nodeStatus -> Void in NSOperationQueue.mainQueue().addOperationWithBlock() { () -> Void in let nc = NSNotificationCenter.defaultCenter() diff --git a/ZeroTier One/ServiceCom.swift b/ZeroTier One/ServiceCom.swift index 6ebf0986e..af5fb07a9 100644 --- a/ZeroTier One/ServiceCom.swift +++ b/ZeroTier One/ServiceCom.swift @@ -9,9 +9,17 @@ import Cocoa class ServiceCom: NSObject { - static let baseURL = "http://localhost:9993" + static let sharedInstance = ServiceCom() - private static func getKey() -> String { + let baseURL = "http://localhost:9993" + let session = NSURLSession(configuration: NSURLSessionConfiguration.ephemeralSessionConfiguration()) + + private override init() { + super.init() + } + + + private func getKey() -> String { struct Holder { static var key: String? = nil } @@ -77,16 +85,14 @@ class ServiceCom: NSObject { } } - static func getNetworkList(completionHandler: ([Network]) -> Void) { + func getNetworkList(completionHandler: ([Network]) -> Void) { - let urlString = baseURL + "/network?auth=\(ServiceCom.getKey())" + let urlString = baseURL + "/network?auth=\(getKey())" let url = NSURL(string: urlString) if let u = url { - let request = NSMutableURLRequest(URL: u) - let session = NSURLSession.sharedSession() - let task = session.dataTaskWithRequest(request) { (data, response, error) in + let task = session.dataTaskWithURL(u) { (data, response, error) in if error != nil{ NSLog("\(error)") return @@ -118,13 +124,11 @@ class ServiceCom: NSObject { } } - static func getNodeStatus(completionHandler: (NodeStatus -> Void)) { - let urlString = baseURL + "/status?auth=\(ServiceCom.getKey())" + func getNodeStatus(completionHandler: (NodeStatus -> Void)) { + let urlString = baseURL + "/status?auth=\(getKey())" if let u = NSURL(string: urlString) { - let request = NSMutableURLRequest(URL: u) - let session = NSURLSession.sharedSession() - let task = session.dataTaskWithRequest(request) { (data, response, error) in + let task = session.dataTaskWithURL(u) { (data, response, error) in if error != nil{ NSLog("\(error)") return @@ -153,8 +157,8 @@ class ServiceCom: NSObject { } } - static func joinNetwork(network: String, allowManaged: Bool = true, allowGlobal: Bool = false, allowDefault: Bool = false) { - let urlString = baseURL + "/network/\(network)?auth=\(ServiceCom.getKey())" + func joinNetwork(network: String, allowManaged: Bool = true, allowGlobal: Bool = false, allowDefault: Bool = false) { + let urlString = baseURL + "/network/\(network)?auth=\(getKey())" let url = NSURL(string: urlString) var jsonDict = [String: AnyObject]() @@ -171,7 +175,6 @@ class ServiceCom: NSObject { request.HTTPBody = json request.setValue("application/json", forHTTPHeaderField: "Content-Type") - let session = NSURLSession.sharedSession() let task = session.dataTaskWithRequest(request) { (data, response, error) in let httpResponse = response as! NSHTTPURLResponse let status = httpResponse.statusCode @@ -193,14 +196,13 @@ class ServiceCom: NSObject { } - static func leaveNetwork(network: String) { - let urlString = baseURL + "/network/\(network)?auth=\(ServiceCom.getKey())" + func leaveNetwork(network: String) { + let urlString = baseURL + "/network/\(network)?auth=\(getKey())" if let u = NSURL(string: urlString) { let request = NSMutableURLRequest(URL: u) request.HTTPMethod = "DELETE" - let session = NSURLSession.sharedSession() let task = session.dataTaskWithRequest(request) { (data, response, error) in let httpResponse = response as! NSHTTPURLResponse let status = httpResponse.statusCode diff --git a/ZeroTier One/ShowNetworksViewController.swift b/ZeroTier One/ShowNetworksViewController.swift index 41d088de3..7c155bf6f 100644 --- a/ZeroTier One/ShowNetworksViewController.swift +++ b/ZeroTier One/ShowNetworksViewController.swift @@ -35,8 +35,6 @@ class ShowNetworksViewController: NSViewController, NSTableViewDelegate, NSTable override func viewWillAppear() { super.viewWillAppear() visible = true - - netMonitor.start() } override func viewWillDisappear() { @@ -45,8 +43,6 @@ class ShowNetworksViewController: NSViewController, NSTableViewDelegate, NSTable let filePath = dataFile() NSKeyedArchiver.archiveRootObject(self.networkList, toFile: filePath) visible = false - - netMonitor.stop() } func deleteNetworkFromList(nwid: String) {