the quest for leaks is over.

Why a singleton doesn't leak, vs calling static methods on a class is beyond me
This commit is contained in:
Grant Limberg 2016-08-02 20:30:09 -07:00
parent 78e5a00a68
commit f54d5e9e8f
6 changed files with 36 additions and 37 deletions

View File

@ -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()
}
}

View File

@ -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 = ""

View File

@ -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) {

View File

@ -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()

View File

@ -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

View File

@ -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) {