NetworkMonitor.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // NetworkMonitor.swift
  3. // ZeroTier One
  4. //
  5. // Created by Grant Limberg on 6/16/16.
  6. // Copyright © 2016 ZeroTier, Inc. All rights reserved.
  7. //
  8. import Cocoa
  9. let networkUpdateKey = "com.zerotier.one.network-list"
  10. class NetworkMonitor: NSObject {
  11. var timer: NSTimer? = nil
  12. var savedNetworks: [Network] = [Network]()
  13. var receivedNetworks: [Network] = [Network]()
  14. var allNetworks: [Network] = [Network]()
  15. override init() {
  16. super.init()
  17. timer = NSTimer.scheduledTimerWithTimeInterval(1.0,
  18. target: self,
  19. selector: #selector(updateNetworkInfo),
  20. userInfo: nil,
  21. repeats: true)
  22. }
  23. deinit {
  24. timer?.invalidate()
  25. }
  26. private func dataFile() -> String {
  27. var appSupport = NSFileManager.defaultManager().URLsForDirectory(.ApplicationSupportDirectory, inDomains: .UserDomainMask)[0]
  28. appSupport = appSupport.URLByAppendingPathComponent("ZeroTier").URLByAppendingPathComponent("One").URLByAppendingPathComponent("networks.dat")
  29. return appSupport.path!
  30. }
  31. func updateNetworkInfo() {
  32. let filePath = dataFile()
  33. if NSFileManager.defaultManager().fileExistsAtPath(filePath) {
  34. self.savedNetworks = NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as! [Network]
  35. }
  36. ServiceCom.getNetworkList() { (networkList) -> Void in
  37. self.receivedNetworks = networkList
  38. NSOperationQueue.mainQueue().addOperationWithBlock() { () -> Void in
  39. self.internal_updateNetworkInfo()
  40. }
  41. }
  42. }
  43. func deleteSavedNetwork(nwid: String) {
  44. if let nwid = UInt64(nwid, radix: 16) {
  45. let index = findNetworkWithID(nwid)
  46. if index != NSNotFound {
  47. allNetworks.removeAtIndex(index)
  48. }
  49. }
  50. }
  51. // Only to be called by updateNetworkInfo()
  52. private func internal_updateNetworkInfo() {
  53. var networks = self.savedNetworks
  54. for nw in receivedNetworks {
  55. let index = findNetworkWithID(nw.nwid)
  56. if index != NSNotFound {
  57. networks[index] = nw
  58. }
  59. networks.sortInPlace({ (left, right) -> Bool in
  60. if left.nwid < right.nwid {
  61. return true
  62. }
  63. return false
  64. })
  65. objc_sync_enter(allNetworks)
  66. allNetworks = networks
  67. objc_sync_exit(allNetworks)
  68. saveNetworks()
  69. let nc = NSNotificationCenter.defaultCenter()
  70. nc.postNotificationName(networkUpdateKey, object: nil, userInfo: ["networks": networks])
  71. }
  72. }
  73. private func findNetworkWithID(nwid: UInt64) -> Int {
  74. for (index, element) in allNetworks.enumerate() {
  75. if element.nwid == nwid {
  76. return index
  77. }
  78. }
  79. return NSNotFound
  80. }
  81. private func saveNetworks() {
  82. let file = dataFile()
  83. objc_sync_enter(allNetworks)
  84. NSKeyedArchiver.archiveRootObject(self.allNetworks, toFile: file)
  85. objc_sync_exit(allNetworks)
  86. }
  87. }