Browse Source

Base functionality complete. Can join/leave/list networks. Can disconnect from networks without removing them from the network list.

Grant Limberg 9 years ago
parent
commit
b085329bca
2 changed files with 143 additions and 8 deletions
  1. 59 3
      ZeroTier One/Network.swift
  2. 84 5
      ZeroTier One/ShowNetworksViewController.swift

+ 59 - 3
ZeroTier One/Network.swift

@@ -8,7 +8,7 @@
 
 
 import Cocoa
 import Cocoa
 
 
-enum NetworkStatus : CustomStringConvertible {
+enum NetworkStatus : Int, CustomStringConvertible {
     case REQUESTING_CONFIGURATION
     case REQUESTING_CONFIGURATION
     case OK
     case OK
     case ACCESS_DENIED
     case ACCESS_DENIED
@@ -28,7 +28,7 @@ enum NetworkStatus : CustomStringConvertible {
     }
     }
 }
 }
 
 
-enum NetworkType: CustomStringConvertible {
+enum NetworkType: Int, CustomStringConvertible {
     case PUBLIC
     case PUBLIC
     case PRIVATE
     case PRIVATE
 
 
@@ -40,7 +40,25 @@ enum NetworkType: CustomStringConvertible {
     }
     }
 }
 }
 
 
-class Network: NSObject {
+
+struct PropertyKeys {
+    static let addressesKey = "addresses"
+    static let bridgeKey = "bridge"
+    static let broadcastKey = "broadcast"
+    static let dhcpKey = "dhcp"
+    static let macKey = "mac"
+    static let mtuKey = "mtu"
+    static let multicastKey = "multicast"
+    static let nameKey = "name"
+    static let netconfKey = "netconf"
+    static let nwidKey = "nwid"
+    static let portNameKey = "port"
+    static let portErrorKey = "portError"
+    static let statusKey = "status"
+    static let typeKey = "type"
+}
+
+class Network: NSObject, NSCoding  {
     var assignedAddresses: [String] = [String]()
     var assignedAddresses: [String] = [String]()
     var bridge: Bool = false
     var bridge: Bool = false
     var broadcastEnabled: Bool = false
     var broadcastEnabled: Bool = false
@@ -55,6 +73,7 @@ class Network: NSObject {
     var portError: Int = 0
     var portError: Int = 0
     var status: NetworkStatus = .REQUESTING_CONFIGURATION
     var status: NetworkStatus = .REQUESTING_CONFIGURATION
     var type: NetworkType = .PRIVATE
     var type: NetworkType = .PRIVATE
+    var connected: Bool = false // NOT PERSISTED.  Set to true if loaded via JSON
 
 
     init(jsonData: [String: AnyObject]) {
     init(jsonData: [String: AnyObject]) {
         super.init()
         super.init()
@@ -108,5 +127,42 @@ class Network: NSObject {
         default:
         default:
             break
             break
         }
         }
+
+        // if it's being initialized via JSON, it's connected
+        connected = true
+    }
+
+    required init?(coder aDecoder: NSCoder) {
+        self.assignedAddresses = aDecoder.decodeObjectForKey(PropertyKeys.addressesKey) as! [String]
+        self.bridge = aDecoder.decodeBoolForKey(PropertyKeys.bridgeKey)
+        self.broadcastEnabled = aDecoder.decodeBoolForKey(PropertyKeys.broadcastKey)
+        self.dhcp = aDecoder.decodeBoolForKey(PropertyKeys.dhcpKey)
+        self.mac = aDecoder.decodeObjectForKey(PropertyKeys.macKey) as! String
+        self.mtu = aDecoder.decodeIntegerForKey(PropertyKeys.mtuKey)
+        self.multicastSubscriptions = aDecoder.decodeObjectForKey(PropertyKeys.multicastKey) as! [String]
+        self.name = aDecoder.decodeObjectForKey(PropertyKeys.nameKey) as! String
+        self.netconfRevision = aDecoder.decodeIntegerForKey(PropertyKeys.netconfKey)
+        self.nwid = (aDecoder.decodeObjectForKey(PropertyKeys.nwidKey) as! NSNumber).unsignedLongLongValue
+        self.portDeviceName = aDecoder.decodeObjectForKey(PropertyKeys.portNameKey) as! String
+        self.portError = aDecoder.decodeIntegerForKey(PropertyKeys.portErrorKey)
+        self.status = NetworkStatus(rawValue: aDecoder.decodeIntegerForKey(PropertyKeys.statusKey))!
+        self.type = NetworkType(rawValue: aDecoder.decodeIntegerForKey(PropertyKeys.typeKey))!
+    }
+
+    func encodeWithCoder(aCoder: NSCoder) {
+        aCoder.encodeObject(self.assignedAddresses, forKey: PropertyKeys.addressesKey)
+        aCoder.encodeBool(self.bridge, forKey: PropertyKeys.bridgeKey)
+        aCoder.encodeBool(self.broadcastEnabled, forKey: PropertyKeys.broadcastKey)
+        aCoder.encodeBool(self.dhcp, forKey: PropertyKeys.dhcpKey)
+        aCoder.encodeObject(self.mac, forKey: PropertyKeys.macKey)
+        aCoder.encodeInteger(self.mtu, forKey: PropertyKeys.mtuKey)
+        aCoder.encodeObject(self.multicastSubscriptions, forKey: PropertyKeys.multicastKey)
+        aCoder.encodeObject(self.name, forKey: PropertyKeys.nameKey)
+        aCoder.encodeInteger(self.netconfRevision, forKey: PropertyKeys.netconfKey)
+        aCoder.encodeObject(NSNumber(unsignedLongLong: self.nwid), forKey: PropertyKeys.nwidKey)
+        aCoder.encodeObject(self.portDeviceName, forKey: PropertyKeys.portNameKey)
+        aCoder.encodeInteger(self.portError, forKey: PropertyKeys.portErrorKey)
+        aCoder.encodeInteger(self.status.rawValue, forKey: PropertyKeys.statusKey)
+        aCoder.encodeInteger(self.type.rawValue, forKey: PropertyKeys.typeKey)
     }
     }
 }
 }

+ 84 - 5
ZeroTier One/ShowNetworksViewController.swift

@@ -23,19 +23,92 @@ class ShowNetworksViewController: NSViewController, NSTableViewDelegate, NSTable
         tableView.backgroundColor = NSColor.clearColor()
         tableView.backgroundColor = NSColor.clearColor()
     }
     }
 
 
+    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!
+    }
+
+
+    private func findNetworkWithID(id: UInt64) -> Int {
+
+        for (index, element) in networkList.enumerate() {
+
+            if element.nwid == id {
+                return index
+            }
+        }
+
+        return NSNotFound
+    }
+
     override func viewWillAppear() {
     override func viewWillAppear() {
         super.viewWillAppear()
         super.viewWillAppear()
 
 
-        ServiceCom.getNetworkList() { (networkList) -> Void in
-            NSOperationQueue.mainQueue().addOperationWithBlock() { () -> Void in
-                self.networkList = networkList
-                self.tableView.reloadData()
+        let filePath = dataFile()
+
+        if NSFileManager.defaultManager().fileExistsAtPath(filePath) {
+            networkList = NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as! [Network]
+
+            ServiceCom.getNetworkList() { (networkList) -> Void in
+
+                for nw in networkList {
+                    let index = self.findNetworkWithID(nw.nwid)
+
+                    if index != NSNotFound {
+                        self.networkList[index] = nw
+                    }
+                    else {
+                        self.networkList.append(nw)
+                    }
+                }
+
+                NSOperationQueue.mainQueue().addOperationWithBlock() { () -> Void in
+                    self.networkList.sortInPlace({ (left, right) -> Bool in
+                        if left.nwid < right.nwid {
+                            return true
+                        }
+
+                        return false
+                    })
+                    self.tableView.reloadData()
+                }
+            }
+        }
+        else {
+            ServiceCom.getNetworkList() { (networkList) -> Void in
+                NSOperationQueue.mainQueue().addOperationWithBlock() { () -> Void in
+                    self.networkList.sortInPlace({ (left, right) -> Bool in
+                        if left.nwid < right.nwid {
+                            return true
+                        }
+
+                        return false
+                    })
+                    
+                    self.networkList = networkList
+                    self.tableView.reloadData()
+                }
             }
             }
         }
         }
     }
     }
 
 
+    override func viewWillDisappear() {
+        super.viewWillDisappear()
+
+        let filePath = dataFile()
+        NSKeyedArchiver.archiveRootObject(self.networkList, toFile: filePath)
+    }
+
     func deleteNetworkFromList(nwid: String) {
     func deleteNetworkFromList(nwid: String) {
-        
+        if let nwid = UInt64(nwid, radix: 16) {
+            let index = findNetworkWithID(nwid)
+
+            if index != NSNotFound {
+                networkList.removeAtIndex(index)
+                tableView.reloadData()
+            }
+        }
     }
     }
 
 
     // NSTableViewDataSource
     // NSTableViewDataSource
@@ -71,6 +144,12 @@ class ShowNetworksViewController: NSViewController, NSTableViewDelegate, NSTable
                 cell.addressesField.stringValue += "\n"
                 cell.addressesField.stringValue += "\n"
             }
             }
 
 
+            if network.connected {
+                cell.connectedCheckbox.state = NSOnState
+            }
+            else {
+                cell.connectedCheckbox.state = NSOffState
+            }
 
 
             return cell
             return cell
         }
         }