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.updateNetworkInfo()
monitor.start()
} }
func applicationWillTerminate(aNotification: NSNotification) { func applicationWillTerminate(aNotification: NSNotification) {
@ -243,10 +244,10 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
let id = String(network.nwid, radix: 16) let id = String(network.nwid, radix: 16)
if network.connected { if network.connected {
ServiceCom.leaveNetwork(id) ServiceCom.sharedInstance.leaveNetwork(id)
} }
else { else {
ServiceCom.joinNetwork(id) ServiceCom.sharedInstance.joinNetwork(id)
} }
} }
@ -257,11 +258,10 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
} }
func menuWillOpen(menu: NSMenu) { func menuWillOpen(menu: NSMenu) {
monitor.start() //monitor.updateNetworkInfo()
} }
func menuDidClose(menu: NSMenu) { func menuDidClose(menu: NSMenu) {
monitor.stop()
} }
} }

View File

@ -69,7 +69,7 @@ class JoinNetworkViewController: NSViewController, NSComboBoxDelegate, NSComboBo
@IBAction func onJoinClicked(sender: AnyObject?) { @IBAction func onJoinClicked(sender: AnyObject?) {
let networkString = network.stringValue let networkString = network.stringValue
ServiceCom.joinNetwork(networkString, ServiceCom.sharedInstance.joinNetwork(networkString,
allowManaged: allowManagedCheckBox.state == NSOnState, allowManaged: allowManagedCheckBox.state == NSOnState,
allowGlobal: allowGlobalCheckBox.state == NSOnState, allowGlobal: allowGlobalCheckBox.state == NSOnState,
allowDefault: allowDefaultCheckBox.state == NSOnState) allowDefault: allowDefaultCheckBox.state == NSOnState)

View File

@ -56,14 +56,14 @@ class NetworkInfoCell: NSTableCellView {
} }
func joinNetwork(nwid: String) { func joinNetwork(nwid: String) {
ServiceCom.joinNetwork(nwid, ServiceCom.sharedInstance.joinNetwork(nwid,
allowManaged: allowManaged.state == NSOnState, allowManaged: allowManaged.state == NSOnState,
allowGlobal: allowGlobal.state == NSOnState, allowGlobal: allowGlobal.state == NSOnState,
allowDefault: !defaultRouteExists(parent.networkList) && (allowDefault.state == NSOnState)) allowDefault: !defaultRouteExists(parent.networkList) && (allowDefault.state == NSOnState))
} }
func leaveNetwork(nwid: String) { func leaveNetwork(nwid: String) {
ServiceCom.leaveNetwork(nwid) ServiceCom.sharedInstance.leaveNetwork(nwid)
} }
@IBAction func onAllowStatusChanged(sender: NSButton) { @IBAction func onAllowStatusChanged(sender: NSButton) {

View File

@ -50,6 +50,7 @@ class NetworkMonitor: NSObject {
} }
func updateNetworkInfo() { func updateNetworkInfo() {
//NSLog("updateNetworkInfo")
let filePath = dataFile() let filePath = dataFile()
if NSFileManager.defaultManager().fileExistsAtPath(filePath) { 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 self.receivedNetworks = networkList
NSOperationQueue.mainQueue().addOperationWithBlock() { () -> Void in 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 NSOperationQueue.mainQueue().addOperationWithBlock() { () -> Void in
let nc = NSNotificationCenter.defaultCenter() let nc = NSNotificationCenter.defaultCenter()

View File

@ -9,9 +9,17 @@
import Cocoa import Cocoa
class ServiceCom: NSObject { 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 { struct Holder {
static var key: String? = nil 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) let url = NSURL(string: urlString)
if let u = url { if let u = url {
let request = NSMutableURLRequest(URL: u) let task = session.dataTaskWithURL(u) { (data, response, error) in
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request) { (data, response, error) in
if error != nil{ if error != nil{
NSLog("\(error)") NSLog("\(error)")
return return
@ -118,13 +124,11 @@ class ServiceCom: NSObject {
} }
} }
static func getNodeStatus(completionHandler: (NodeStatus -> Void)) { func getNodeStatus(completionHandler: (NodeStatus -> Void)) {
let urlString = baseURL + "/status?auth=\(ServiceCom.getKey())" let urlString = baseURL + "/status?auth=\(getKey())"
if let u = NSURL(string: urlString) { if let u = NSURL(string: urlString) {
let request = NSMutableURLRequest(URL: u) let task = session.dataTaskWithURL(u) { (data, response, error) in
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request) { (data, response, error) in
if error != nil{ if error != nil{
NSLog("\(error)") NSLog("\(error)")
return return
@ -153,8 +157,8 @@ class ServiceCom: NSObject {
} }
} }
static func joinNetwork(network: String, allowManaged: Bool = true, allowGlobal: Bool = false, allowDefault: Bool = false) { func joinNetwork(network: String, allowManaged: Bool = true, allowGlobal: Bool = false, allowDefault: Bool = false) {
let urlString = baseURL + "/network/\(network)?auth=\(ServiceCom.getKey())" let urlString = baseURL + "/network/\(network)?auth=\(getKey())"
let url = NSURL(string: urlString) let url = NSURL(string: urlString)
var jsonDict = [String: AnyObject]() var jsonDict = [String: AnyObject]()
@ -171,7 +175,6 @@ class ServiceCom: NSObject {
request.HTTPBody = json request.HTTPBody = json
request.setValue("application/json", forHTTPHeaderField: "Content-Type") request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request) { (data, response, error) in let task = session.dataTaskWithRequest(request) { (data, response, error) in
let httpResponse = response as! NSHTTPURLResponse let httpResponse = response as! NSHTTPURLResponse
let status = httpResponse.statusCode let status = httpResponse.statusCode
@ -193,14 +196,13 @@ class ServiceCom: NSObject {
} }
static func leaveNetwork(network: String) { func leaveNetwork(network: String) {
let urlString = baseURL + "/network/\(network)?auth=\(ServiceCom.getKey())" let urlString = baseURL + "/network/\(network)?auth=\(getKey())"
if let u = NSURL(string: urlString) { if let u = NSURL(string: urlString) {
let request = NSMutableURLRequest(URL: u) let request = NSMutableURLRequest(URL: u)
request.HTTPMethod = "DELETE" request.HTTPMethod = "DELETE"
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request) { (data, response, error) in let task = session.dataTaskWithRequest(request) { (data, response, error) in
let httpResponse = response as! NSHTTPURLResponse let httpResponse = response as! NSHTTPURLResponse
let status = httpResponse.statusCode let status = httpResponse.statusCode

View File

@ -35,8 +35,6 @@ 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() {
@ -45,8 +43,6 @@ 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) {