NetworkMonitor.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. let index2 = findSavedNetworkWithID(nwid)
  50. if index2 != NSNotFound {
  51. savedNetworks.removeAtIndex(index2)
  52. }
  53. }
  54. saveNetworks()
  55. }
  56. // Only to be called by updateNetworkInfo()
  57. private func internal_updateNetworkInfo() {
  58. var networks = self.savedNetworks
  59. for nw in receivedNetworks {
  60. let index = findSavedNetworkWithID(nw.nwid)
  61. if index != NSNotFound {
  62. networks[index] = nw
  63. }
  64. else {
  65. networks.append(nw)
  66. }
  67. }
  68. networks.sortInPlace({ (left, right) -> Bool in
  69. if left.nwid < right.nwid {
  70. return true
  71. }
  72. return false
  73. })
  74. objc_sync_enter(allNetworks)
  75. allNetworks = networks
  76. objc_sync_exit(allNetworks)
  77. saveNetworks()
  78. let nc = NSNotificationCenter.defaultCenter()
  79. nc.postNotificationName(networkUpdateKey, object: nil, userInfo: ["networks": networks])
  80. }
  81. private func findNetworkWithID(nwid: UInt64) -> Int {
  82. for (index, element) in allNetworks.enumerate() {
  83. if element.nwid == nwid {
  84. return index
  85. }
  86. }
  87. return NSNotFound
  88. }
  89. private func findSavedNetworkWithID(nwid: UInt64) -> Int {
  90. for (index, element) in savedNetworks.enumerate() {
  91. if element.nwid == nwid {
  92. return index
  93. }
  94. }
  95. return NSNotFound
  96. }
  97. private func saveNetworks() {
  98. let file = dataFile()
  99. objc_sync_enter(allNetworks)
  100. NSKeyedArchiver.archiveRootObject(self.allNetworks, toFile: file)
  101. objc_sync_exit(allNetworks)
  102. }
  103. }