NetworkMonitor.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. let statusUpdateKey = "com.zerotier.one.status"
  11. class NetworkMonitor: NSObject {
  12. var timer: NSTimer? = nil
  13. var savedNetworks: [Network] = [Network]()
  14. var receivedNetworks: [Network] = [Network]()
  15. var allNetworks: [Network] = [Network]()
  16. override init() {
  17. super.init()
  18. }
  19. deinit {
  20. timer?.invalidate()
  21. }
  22. func start() {
  23. NSLog("ZeroTier monitor started")
  24. timer = NSTimer.scheduledTimerWithTimeInterval(1.0,
  25. target: self,
  26. selector: #selector(updateNetworkInfo),
  27. userInfo: nil,
  28. repeats: true)
  29. }
  30. func stop() {
  31. NSLog("ZeroTier monitor stopped")
  32. timer?.invalidate()
  33. timer = nil
  34. }
  35. private func dataFile() -> String {
  36. var appSupport = NSFileManager.defaultManager().URLsForDirectory(.ApplicationSupportDirectory, inDomains: .UserDomainMask)[0]
  37. appSupport = appSupport.URLByAppendingPathComponent("ZeroTier").URLByAppendingPathComponent("One").URLByAppendingPathComponent("networks.dat")
  38. return appSupport.path!
  39. }
  40. func updateNetworkInfo() {
  41. //NSLog("updateNetworkInfo")
  42. let filePath = dataFile()
  43. if NSFileManager.defaultManager().fileExistsAtPath(filePath) {
  44. let networks = NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as! [Network]
  45. self.savedNetworks.removeAll()
  46. for n in networks {
  47. self.savedNetworks.append(n)
  48. }
  49. }
  50. ServiceCom.sharedInstance().getNetworklist() { (networkList) -> Void in
  51. self.receivedNetworks = networkList
  52. NSOperationQueue.mainQueue().addOperationWithBlock() { () -> Void in
  53. self.internal_updateNetworkInfo()
  54. }
  55. }
  56. ServiceCom.sharedInstance().getNodeStatus() { nodeStatus -> Void in
  57. NSOperationQueue.mainQueue().addOperationWithBlock() { () -> Void in
  58. let nc = NSNotificationCenter.defaultCenter()
  59. nc.postNotificationName(statusUpdateKey, object: nil, userInfo: ["status": nodeStatus])
  60. }
  61. }
  62. }
  63. func deleteSavedNetwork(nwid: String) {
  64. if let nwid = UInt64(nwid, radix: 16) {
  65. let index = findNetworkWithID(nwid)
  66. if index != NSNotFound {
  67. allNetworks.removeAtIndex(index)
  68. }
  69. let index2 = findSavedNetworkWithID(nwid)
  70. if index2 != NSNotFound {
  71. savedNetworks.removeAtIndex(index2)
  72. }
  73. }
  74. saveNetworks()
  75. }
  76. // Only to be called by updateNetworkInfo()
  77. private func internal_updateNetworkInfo() {
  78. var networks = self.savedNetworks
  79. for nw in receivedNetworks {
  80. let index = findSavedNetworkWithID(nw.nwid)
  81. if index != NSNotFound {
  82. networks[index] = nw
  83. }
  84. else {
  85. networks.append(nw)
  86. }
  87. }
  88. networks.sortInPlace({ (left, right) -> Bool in
  89. if left.nwid < right.nwid {
  90. return true
  91. }
  92. return false
  93. })
  94. objc_sync_enter(allNetworks)
  95. allNetworks = networks
  96. objc_sync_exit(allNetworks)
  97. saveNetworks()
  98. let nc = NSNotificationCenter.defaultCenter()
  99. nc.postNotificationName(networkUpdateKey, object: nil, userInfo: ["networks": networks])
  100. }
  101. private func findNetworkWithID(nwid: UInt64) -> Int {
  102. for (index, element) in allNetworks.enumerate() {
  103. if element.nwid == nwid {
  104. return index
  105. }
  106. }
  107. return NSNotFound
  108. }
  109. private func findSavedNetworkWithID(nwid: UInt64) -> Int {
  110. for (index, element) in savedNetworks.enumerate() {
  111. if element.nwid == nwid {
  112. return index
  113. }
  114. }
  115. return NSNotFound
  116. }
  117. private func saveNetworks() {
  118. let file = dataFile()
  119. objc_sync_enter(allNetworks)
  120. NSKeyedArchiver.archiveRootObject(self.allNetworks, toFile: file)
  121. objc_sync_exit(allNetworks)
  122. }
  123. }