123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- //
- // 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"
- let statusUpdateKey = "com.zerotier.one.status"
- class NetworkMonitor: NSObject {
- var timer: NSTimer? = nil
- var savedNetworks: [Network] = [Network]()
- var receivedNetworks: [Network] = [Network]()
- var allNetworks: [Network] = [Network]()
- override init() {
- super.init()
- }
- deinit {
- timer?.invalidate()
- }
- func start() {
- NSLog("ZeroTier monitor started")
- timer = NSTimer.scheduledTimerWithTimeInterval(1.0,
- target: self,
- selector: #selector(updateNetworkInfo),
- userInfo: nil,
- repeats: true)
- }
- func stop() {
- NSLog("ZeroTier monitor stopped")
- timer?.invalidate()
- timer = nil
- }
- 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() {
- //NSLog("updateNetworkInfo")
- let filePath = dataFile()
- if NSFileManager.defaultManager().fileExistsAtPath(filePath) {
- let networks = NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as! [Network]
- self.savedNetworks.removeAll()
- for n in networks {
- self.savedNetworks.append(n)
- }
- }
- ServiceCom.sharedInstance().getNetworklist() { (networkList) -> Void in
- self.receivedNetworks = networkList
- NSOperationQueue.mainQueue().addOperationWithBlock() { () -> Void in
- self.internal_updateNetworkInfo()
- }
- }
- ServiceCom.sharedInstance().getNodeStatus() { nodeStatus -> Void in
- NSOperationQueue.mainQueue().addOperationWithBlock() { () -> Void in
- let nc = NSNotificationCenter.defaultCenter()
- nc.postNotificationName(statusUpdateKey, object: nil, userInfo: ["status": nodeStatus])
- }
- }
- }
- func deleteSavedNetwork(nwid: String) {
- if let nwid = UInt64(nwid, radix: 16) {
- let index = findNetworkWithID(nwid)
- if index != NSNotFound {
- allNetworks.removeAtIndex(index)
- }
- let index2 = findSavedNetworkWithID(nwid)
- if index2 != NSNotFound {
- savedNetworks.removeAtIndex(index2)
- }
- }
- saveNetworks()
- }
- // Only to be called by updateNetworkInfo()
- private func internal_updateNetworkInfo() {
- var networks = self.savedNetworks
- for nw in receivedNetworks {
- let index = findSavedNetworkWithID(nw.nwid)
- if index != NSNotFound {
- networks[index] = nw
- }
- else {
- networks.append(nw)
- }
- }
- networks.sortInPlace({ (left, right) -> Bool in
- if left.nwid < right.nwid {
- return true
- }
- return false
- })
- objc_sync_enter(allNetworks)
- allNetworks = networks
- objc_sync_exit(allNetworks)
- saveNetworks()
- let nc = NSNotificationCenter.defaultCenter()
- nc.postNotificationName(networkUpdateKey, object: nil, userInfo: ["networks": networks])
- }
- private func findNetworkWithID(nwid: UInt64) -> Int {
- for (index, element) in allNetworks.enumerate() {
- if element.nwid == nwid {
- return index
- }
- }
- return NSNotFound
- }
- private func findSavedNetworkWithID(nwid: UInt64) -> Int {
- for (index, element) in savedNetworks.enumerate() {
- if element.nwid == nwid {
- return index
- }
- }
- return NSNotFound
- }
- private func saveNetworks() {
- let file = dataFile()
- objc_sync_enter(allNetworks)
- NSKeyedArchiver.archiveRootObject(self.allNetworks, toFile: file)
- objc_sync_exit(allNetworks)
- }
- }
|