2016-06-17 03:53:55 +00:00
|
|
|
//
|
|
|
|
// NetworkMonitor.swift
|
|
|
|
// ZeroTier One
|
|
|
|
//
|
|
|
|
// Created by Grant Limberg on 6/16/16.
|
|
|
|
// Copyright © 2016 ZeroTier, Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
|
|
|
|
let networkUpdateKey = "com.zerotier.one.network-list"
|
2016-07-01 04:24:45 +00:00
|
|
|
let statusUpdateKey = "com.zerotier.one.status"
|
2016-06-17 03:53:55 +00:00
|
|
|
|
|
|
|
class NetworkMonitor: NSObject {
|
|
|
|
|
|
|
|
var timer: NSTimer? = nil
|
|
|
|
|
|
|
|
var savedNetworks: [Network] = [Network]()
|
|
|
|
var receivedNetworks: [Network] = [Network]()
|
|
|
|
var allNetworks: [Network] = [Network]()
|
|
|
|
|
|
|
|
override init() {
|
|
|
|
super.init()
|
|
|
|
|
|
|
|
timer = NSTimer.scheduledTimerWithTimeInterval(1.0,
|
|
|
|
target: self,
|
|
|
|
selector: #selector(updateNetworkInfo),
|
|
|
|
userInfo: nil,
|
|
|
|
repeats: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
deinit {
|
|
|
|
timer?.invalidate()
|
|
|
|
}
|
|
|
|
|
|
|
|
private func dataFile() -> String {
|
|
|
|
var appSupport = NSFileManager.defaultManager().URLsForDirectory(.ApplicationSupportDirectory, inDomains: .UserDomainMask)[0]
|
|
|
|
appSupport = appSupport.URLByAppendingPathComponent("ZeroTier").URLByAppendingPathComponent("One").URLByAppendingPathComponent("networks.dat")
|
|
|
|
return appSupport.path!
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateNetworkInfo() {
|
|
|
|
let filePath = dataFile()
|
|
|
|
|
|
|
|
if NSFileManager.defaultManager().fileExistsAtPath(filePath) {
|
2016-07-01 02:42:23 +00:00
|
|
|
let networks = NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as! [Network]
|
2016-06-17 03:53:55 +00:00
|
|
|
|
2016-07-01 02:42:23 +00:00
|
|
|
self.savedNetworks.removeAll()
|
|
|
|
|
|
|
|
for n in networks {
|
|
|
|
self.savedNetworks.append(n)
|
|
|
|
}
|
2016-06-17 03:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ServiceCom.getNetworkList() { (networkList) -> Void in
|
|
|
|
self.receivedNetworks = networkList
|
|
|
|
|
|
|
|
NSOperationQueue.mainQueue().addOperationWithBlock() { () -> Void in
|
|
|
|
self.internal_updateNetworkInfo()
|
|
|
|
}
|
2016-07-01 04:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ServiceCom.getNodeStatus() { nodeStatus -> Void in
|
|
|
|
NSOperationQueue.mainQueue().addOperationWithBlock() { () -> Void in
|
|
|
|
let nc = NSNotificationCenter.defaultCenter()
|
|
|
|
|
|
|
|
nc.postNotificationName(statusUpdateKey, object: nil, userInfo: ["status": nodeStatus])
|
|
|
|
}
|
2016-06-17 03:53:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteSavedNetwork(nwid: String) {
|
|
|
|
if let nwid = UInt64(nwid, radix: 16) {
|
|
|
|
let index = findNetworkWithID(nwid)
|
|
|
|
|
|
|
|
if index != NSNotFound {
|
|
|
|
allNetworks.removeAtIndex(index)
|
|
|
|
}
|
2016-06-28 00:54:54 +00:00
|
|
|
|
|
|
|
let index2 = findSavedNetworkWithID(nwid)
|
|
|
|
|
|
|
|
if index2 != NSNotFound {
|
|
|
|
savedNetworks.removeAtIndex(index2)
|
|
|
|
}
|
2016-06-17 03:53:55 +00:00
|
|
|
}
|
2016-06-28 00:54:54 +00:00
|
|
|
|
|
|
|
saveNetworks()
|
2016-06-17 03:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Only to be called by updateNetworkInfo()
|
|
|
|
private func internal_updateNetworkInfo() {
|
|
|
|
var networks = self.savedNetworks
|
|
|
|
|
|
|
|
for nw in receivedNetworks {
|
2016-06-27 01:18:59 +00:00
|
|
|
let index = findSavedNetworkWithID(nw.nwid)
|
2016-06-17 03:53:55 +00:00
|
|
|
|
|
|
|
if index != NSNotFound {
|
|
|
|
networks[index] = nw
|
|
|
|
}
|
2016-06-27 01:18:59 +00:00
|
|
|
else {
|
|
|
|
networks.append(nw)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
networks.sortInPlace({ (left, right) -> Bool in
|
|
|
|
if left.nwid < right.nwid {
|
|
|
|
return true
|
|
|
|
}
|
2016-06-17 03:53:55 +00:00
|
|
|
|
2016-06-27 01:18:59 +00:00
|
|
|
return false
|
|
|
|
})
|
2016-06-17 03:53:55 +00:00
|
|
|
|
2016-06-27 01:18:59 +00:00
|
|
|
objc_sync_enter(allNetworks)
|
|
|
|
allNetworks = networks
|
|
|
|
objc_sync_exit(allNetworks)
|
2016-06-17 03:53:55 +00:00
|
|
|
|
2016-06-27 01:18:59 +00:00
|
|
|
saveNetworks()
|
2016-06-17 03:53:55 +00:00
|
|
|
|
2016-06-27 01:18:59 +00:00
|
|
|
let nc = NSNotificationCenter.defaultCenter()
|
2016-06-17 03:53:55 +00:00
|
|
|
|
2016-06-27 01:18:59 +00:00
|
|
|
nc.postNotificationName(networkUpdateKey, object: nil, userInfo: ["networks": networks])
|
2016-06-17 03:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func findNetworkWithID(nwid: UInt64) -> Int {
|
|
|
|
for (index, element) in allNetworks.enumerate() {
|
|
|
|
|
|
|
|
if element.nwid == nwid {
|
|
|
|
return index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NSNotFound
|
|
|
|
}
|
|
|
|
|
2016-06-27 01:18:59 +00:00
|
|
|
private func findSavedNetworkWithID(nwid: UInt64) -> Int {
|
|
|
|
for (index, element) in savedNetworks.enumerate() {
|
|
|
|
|
|
|
|
if element.nwid == nwid {
|
|
|
|
return index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NSNotFound
|
|
|
|
}
|
|
|
|
|
2016-06-17 03:53:55 +00:00
|
|
|
private func saveNetworks() {
|
|
|
|
let file = dataFile()
|
|
|
|
|
|
|
|
objc_sync_enter(allNetworks)
|
|
|
|
NSKeyedArchiver.archiveRootObject(self.allNetworks, toFile: file)
|
|
|
|
objc_sync_exit(allNetworks)
|
|
|
|
}
|
|
|
|
}
|