Parse JSON network list into Network objects for the view

This commit is contained in:
Grant Limberg 2016-05-17 19:41:54 -07:00
parent da30d2898e
commit d5620288d5
5 changed files with 151 additions and 49 deletions

View File

@ -8,6 +8,7 @@
/* Begin PBXBuildFile section */
9330F1351CEAB4C400687EC8 /* ServiceCom.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9330F1341CEAB4C400687EC8 /* ServiceCom.swift */; };
9330F1371CEBF87200687EC8 /* Network.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9330F1361CEBF87200687EC8 /* Network.swift */; };
93326BDC1CE7C816005CA2AC /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93326BDB1CE7C816005CA2AC /* AppDelegate.swift */; };
93326BDE1CE7C816005CA2AC /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 93326BDD1CE7C816005CA2AC /* Assets.xcassets */; };
93326BE11CE7C816005CA2AC /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 93326BDF1CE7C816005CA2AC /* MainMenu.xib */; };
@ -19,6 +20,7 @@
/* Begin PBXFileReference section */
9330F1341CEAB4C400687EC8 /* ServiceCom.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ServiceCom.swift; sourceTree = "<group>"; };
9330F1361CEBF87200687EC8 /* Network.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Network.swift; sourceTree = "<group>"; };
93326BD81CE7C816005CA2AC /* ZeroTier One.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "ZeroTier One.app"; sourceTree = BUILT_PRODUCTS_DIR; };
93326BDB1CE7C816005CA2AC /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
93326BDD1CE7C816005CA2AC /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
@ -62,6 +64,7 @@
children = (
93326BDB1CE7C816005CA2AC /* AppDelegate.swift */,
93326BE81CE7D9B9005CA2AC /* JoinNetworkViewController.swift */,
9330F1361CEBF87200687EC8 /* Network.swift */,
93326BEC1CE7DA30005CA2AC /* ShowNetworksViewController.swift */,
9330F1341CEAB4C400687EC8 /* ServiceCom.swift */,
93326BDD1CE7C816005CA2AC /* Assets.xcassets */,
@ -145,6 +148,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
9330F1371CEBF87200687EC8 /* Network.swift in Sources */,
93326BDC1CE7C816005CA2AC /* AppDelegate.swift in Sources */,
93326BEA1CE7D9B9005CA2AC /* JoinNetworkViewController.swift in Sources */,
93326BEE1CE7DA30005CA2AC /* ShowNetworksViewController.swift in Sources */,

112
ZeroTier One/Network.swift Normal file
View File

@ -0,0 +1,112 @@
//
// Network.swift
// ZeroTier One
//
// Created by Grant Limberg on 5/17/16.
// Copyright © 2016 ZeroTier, Inc. All rights reserved.
//
import Cocoa
enum NetworkStatus : CustomStringConvertible {
case REQUESTING_CONFIGURATION
case OK
case ACCESS_DENIED
case NOT_FOUND
case PORT_ERROR
case CLIENT_TOO_OLD
var description: String {
switch self {
case .REQUESTING_CONFIGURATION: return "REQUESTING_CONFIGURATION"
case .OK: return "OK"
case .ACCESS_DENIED: return "ACCESS_DENIED"
case .NOT_FOUND: return "NOT_FOUND"
case .PORT_ERROR: return "PORT_ERROR"
case .CLIENT_TOO_OLD: return "CLIENT_TOO_OLD"
}
}
}
enum NetworkType: CustomStringConvertible {
case PUBLIC
case PRIVATE
var description: String {
switch self {
case .PUBLIC: return "PUBLIC"
case .PRIVATE: return "PRIVATE"
}
}
}
class Network: NSObject {
var assignedAddresses: [String] = [String]()
var bridge: Bool = false
var broadcastEnabled: Bool = false
var dhcp: Bool = false
var mac: String = ""
var mtu: Int = 0
var multicastSubscriptions: [String] = [String]()
var name: String = ""
var netconfRevision: Int = 232
var nwid: UInt64 = 0
var portDeviceName: String = ""
var portError: Int = 0
var status: NetworkStatus = .REQUESTING_CONFIGURATION
var type: NetworkType = .PRIVATE
init(jsonData: [String: AnyObject]) {
super.init()
let aa = jsonData["assignedAddresses"] as! [String]
for a in aa {
assignedAddresses.append(a)
}
bridge = (jsonData["bridge"] as! NSNumber).boolValue
broadcastEnabled = (jsonData["broadcastEnabled"] as! NSNumber).boolValue
dhcp = (jsonData["dhcp"] as! NSNumber).boolValue
mac = jsonData["mac"] as! String
mtu = (jsonData["mtu"] as! NSNumber).integerValue
let multSubs = jsonData["multicastSubscriptions"] as! [String]
for ms in multSubs {
multicastSubscriptions.append(ms)
}
name = jsonData["name"] as! String
netconfRevision = (jsonData["netconfRevision"] as! NSNumber).integerValue
nwid = UInt64((jsonData["nwid"] as! String), radix: 16)!
portDeviceName = jsonData["portDeviceName"] as! String
portError = (jsonData["portError"] as! NSNumber).integerValue
let statusStr = jsonData["status"] as! String
switch statusStr {
case "REQUESTING_CONFIGURATION":
status = .REQUESTING_CONFIGURATION
case "OK":
status = .OK
case "ACCESS_DENIED":
status = .ACCESS_DENIED
case "NOT_FOUND":
status = .NOT_FOUND
case "PORT_ERROR":
status = .PORT_ERROR
case "CLIENT_TOO_OLD":
status = .CLIENT_TOO_OLD
default:
break
}
let typeStr = jsonData["type"] as! String
switch typeStr {
case "PRIVATE":
type = .PRIVATE
case "PUBLIC":
type = .PUBLIC
default:
break
}
}
}

View File

@ -12,7 +12,7 @@ class ServiceCom: NSObject {
static let baseURL = "http://localhost:9993"
static var key: NSString? = "ddeb3b1e6996b6b4f2d12d10"
static func getNetworkList() {
static func getNetworkList(completionHandler: ([Network]) -> Void) {
let urlString = baseURL + "/network?auth=\(ServiceCom.key!)"
@ -27,11 +27,16 @@ class ServiceCom: NSObject {
if status == 200 {
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions())
print("\(json)")
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions()) as! [[String: AnyObject]]
var networks = [Network]()
for jobj in json {
networks.append(Network(jsonData: jobj))
}
completionHandler(networks)
}
catch {
print("JSON Error: \(error)")
}
}
}

View File

@ -8,19 +8,36 @@
import Cocoa
class ShowNetworksViewController: NSViewController {
class ShowNetworksViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource {
@IBOutlet var tableView: NSTableView!
var networkList: [Network] = [Network]()
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
tableView.setDelegate(self)
tableView.setDataSource(self)
}
override func viewWillAppear() {
super.viewWillAppear()
ServiceCom.getNetworkList()
ServiceCom.getNetworkList() { (networkList) -> Void in
NSOperationQueue.mainQueue().addOperationWithBlock() { () -> Void in
self.networkList = networkList
self.tableView.reloadData()
}
}
}
// NSTableViewDataSource
func numberOfRowsInTableView(tableView: NSTableView) -> Int {
return networkList.count
}
// end NSTableViewDataSource
}

View File

@ -20,17 +20,17 @@
<scrollView fixedFrame="YES" autohidesScrollers="YES" horizontalLineScroll="19" horizontalPageScroll="10" verticalLineScroll="19" verticalPageScroll="10" usesPredominantAxisScrolling="NO" translatesAutoresizingMaskIntoConstraints="NO" id="miH-DQ-i4L">
<rect key="frame" x="0.0" y="0.0" width="480" height="272"/>
<clipView key="contentView" ambiguous="YES" id="KXW-dX-mx6">
<rect key="frame" x="1" y="23" width="478" height="248"/>
<rect key="frame" x="1" y="1" width="478" height="270"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<tableView focusRingType="none" verticalHuggingPriority="750" allowsExpansionToolTips="YES" columnAutoresizingStyle="lastColumnOnly" columnSelection="YES" multipleSelection="NO" autosaveColumns="NO" rowSizeStyle="automatic" headerView="Vk4-NS-T5F" viewBased="YES" id="w5O-vn-cYB">
<rect key="frame" x="0.0" y="0.0" width="478" height="248"/>
<tableView focusRingType="none" verticalHuggingPriority="750" allowsExpansionToolTips="YES" columnAutoresizingStyle="lastColumnOnly" selectionHighlightStyle="none" columnReordering="NO" multipleSelection="NO" autosaveColumns="NO" rowSizeStyle="automatic" viewBased="YES" id="w5O-vn-cYB">
<rect key="frame" x="0.0" y="0.0" width="478" height="270"/>
<autoresizingMask key="autoresizingMask"/>
<size key="intercellSpacing" width="3" height="2"/>
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
<color key="gridColor" name="gridColor" catalog="System" colorSpace="catalog"/>
<tableColumns>
<tableColumn width="116" minWidth="40" maxWidth="1000" id="ztK-JB-A6Q">
<tableColumn width="475" minWidth="40" maxWidth="1000" id="ztK-JB-A6Q">
<tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border">
<font key="font" metaFont="smallSystem"/>
<color key="textColor" name="headerTextColor" catalog="System" colorSpace="catalog"/>
@ -44,7 +44,7 @@
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
<prototypeCellViews>
<tableCellView id="j2t-oK-WVh">
<rect key="frame" x="1" y="1" width="116" height="17"/>
<rect key="frame" x="1" y="1" width="475" height="17"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="rgC-R0-GUv">
@ -62,44 +62,12 @@
</tableCellView>
</prototypeCellViews>
</tableColumn>
<tableColumn width="356" minWidth="40" maxWidth="1000" id="gp2-yI-hbi">
<tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border">
<font key="font" metaFont="smallSystem"/>
<color key="textColor" name="headerTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="headerColor" catalog="System" colorSpace="catalog"/>
</tableHeaderCell>
<textFieldCell key="dataCell" lineBreakMode="truncatingTail" selectable="YES" editable="YES" title="Text Cell" id="0Oh-Cz-89i">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
<prototypeCellViews>
<tableCellView id="NLE-q5-NvF">
<rect key="frame" x="120" y="1" width="356" height="17"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="pqD-85-mZt">
<rect key="frame" x="0.0" y="0.0" width="100" height="17"/>
<textFieldCell key="cell" lineBreakMode="truncatingTail" sendsActionOnEndEditing="YES" title="Table View Cell" id="5Vv-q1-uNw">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
</subviews>
<connections>
<outlet property="textField" destination="pqD-85-mZt" id="0rP-8V-ny1"/>
</connections>
</tableCellView>
</prototypeCellViews>
</tableColumn>
</tableColumns>
</tableView>
</subviews>
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
</clipView>
<scroller key="horizontalScroller" hidden="YES" verticalHuggingPriority="750" doubleValue="0.5" horizontal="YES" id="ySn-XX-fde">
<scroller key="horizontalScroller" hidden="YES" verticalHuggingPriority="750" horizontal="YES" id="ySn-XX-fde">
<rect key="frame" x="1" y="256" width="478" height="15"/>
<autoresizingMask key="autoresizingMask"/>
</scroller>
@ -107,10 +75,6 @@
<rect key="frame" x="224" y="17" width="15" height="102"/>
<autoresizingMask key="autoresizingMask"/>
</scroller>
<tableHeaderView key="headerView" id="Vk4-NS-T5F">
<rect key="frame" x="0.0" y="0.0" width="478" height="23"/>
<autoresizingMask key="autoresizingMask"/>
</tableHeaderView>
</scrollView>
</subviews>
</customView>