Commit d3c60da9 authored by James Hughes's avatar James Hughes
Browse files

Added Kinetic Discovery

parent b8bddde5
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -26,6 +26,10 @@
		3EFB7F2B1B7B963800988886 /* Channel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EFB7F2A1B7B963800988886 /* Channel.swift */; };
		3EFB7F2D1B7BAF8D00988886 /* NetworkChannel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EFB7F2C1B7BAF8D00988886 /* NetworkChannel.swift */; };
		3EFB7F2F1B7BB4E700988886 /* Authentication.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EFB7F2E1B7BB4E700988886 /* Authentication.swift */; };
		7A53ED3B1B99E85B00558A3D /* Discovery.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A53ED3A1B99E85B00558A3D /* Discovery.swift */; };
		7A53ED3C1B99E85B00558A3D /* Discovery.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A53ED3A1B99E85B00558A3D /* Discovery.swift */; };
		7A53ED3D1B99E85B00558A3D /* Discovery.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A53ED3A1B99E85B00558A3D /* Discovery.swift */; };
		7A53ED3F1B9A1C1900558A3D /* KineticDiscoveryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A53ED3E1B9A1C1900558A3D /* KineticDiscoveryTests.swift */; };
		7ACC0F461B97D218005BD8BE /* Kinetic.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3EDAAB3D1B66D32D00F30808 /* Kinetic.framework */; };
		7ACC0F4D1B97D264005BD8BE /* KineticTestMulticast.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7ACC0F4C1B97D264005BD8BE /* KineticTestMulticast.swift */; };
		921636AE408C3549DE7A1141 /* Pods.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 382D1C00FC0EC4F1BF676411 /* Pods.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
@@ -76,6 +80,8 @@
		3EFB7F2A1B7B963800988886 /* Channel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Channel.swift; sourceTree = "<group>"; };
		3EFB7F2C1B7BAF8D00988886 /* NetworkChannel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NetworkChannel.swift; sourceTree = "<group>"; };
		3EFB7F2E1B7BB4E700988886 /* Authentication.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Authentication.swift; sourceTree = "<group>"; };
		7A53ED3A1B99E85B00558A3D /* Discovery.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Discovery.swift; sourceTree = "<group>"; };
		7A53ED3E1B9A1C1900558A3D /* KineticDiscoveryTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KineticDiscoveryTests.swift; sourceTree = "<group>"; };
		7ACC0F411B97D218005BD8BE /* KineticTestsMulticast.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = KineticTestsMulticast.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
		7ACC0F4C1B97D264005BD8BE /* KineticTestMulticast.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KineticTestMulticast.swift; sourceTree = "<group>"; };
		80777030020DBD5A301CA196 /* Pods.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.debug.xcconfig; path = "Pods/Target Support Files/Pods/Pods.debug.xcconfig"; sourceTree = "<group>"; };
@@ -152,6 +158,7 @@
				3EFB7F2C1B7BAF8D00988886 /* NetworkChannel.swift */,
				3EFB7F2E1B7BB4E700988886 /* Authentication.swift */,
				3EDAAB571B66D47200F30808 /* Session.swift */,
				7A53ED3A1B99E85B00558A3D /* Discovery.swift */,
				3EFB7F231B7A951000988886 /* Commands */,
				3EDAAB401B66D32D00F30808 /* Kinetic.h */,
				3EDAAB421B66D32D00F30808 /* Info.plist */,
@@ -164,6 +171,7 @@
			children = (
				3EDAAB4C1B66D32D00F30808 /* KineticTests.swift */,
				7ACC0F4C1B97D264005BD8BE /* KineticTestMulticast.swift */,
				7A53ED3E1B9A1C1900558A3D /* KineticDiscoveryTests.swift */,
				3EDAAB4E1B66D32D00F30808 /* Info.plist */,
			);
			path = KineticTests;
@@ -381,6 +389,7 @@
				3EFB7F1E1B7A6A9800988886 /* Put.swift in Sources */,
				3ED681EB1B7CEA0600AFDF79 /* Encoding.swift in Sources */,
				3EFB7F291B7AB06800988886 /* Delete.swift in Sources */,
				7A53ED3B1B99E85B00558A3D /* Discovery.swift in Sources */,
				3EFB7F2D1B7BAF8D00988886 /* NetworkChannel.swift in Sources */,
				3EDAAB581B66D47200F30808 /* Session.swift in Sources */,
				3EDB8E8A1B871C5C00503F9A /* Batch.swift in Sources */,
@@ -398,6 +407,8 @@
			isa = PBXSourcesBuildPhase;
			buildActionMask = 2147483647;
			files = (
				7A53ED3F1B9A1C1900558A3D /* KineticDiscoveryTests.swift in Sources */,
				7A53ED3C1B99E85B00558A3D /* Discovery.swift in Sources */,
				7ACC0F4D1B97D264005BD8BE /* KineticTestMulticast.swift in Sources */,
				3EDAAB4D1B66D32D00F30808 /* KineticTests.swift in Sources */,
			);
@@ -407,6 +418,7 @@
			isa = PBXSourcesBuildPhase;
			buildActionMask = 2147483647;
			files = (
				7A53ED3D1B99E85B00558A3D /* Discovery.swift in Sources */,
			);
			runOnlyForDeploymentPostprocessing = 0;
		};
@@ -582,18 +594,21 @@
		7ACC0F491B97D218005BD8BE /* Debug */ = {
			isa = XCBuildConfiguration;
			buildSettings = {
				CLANG_ENABLE_MODULES = YES;
				COMBINE_HIDPI_IMAGES = YES;
				INFOPLIST_FILE = KineticTestsMulticast/Info.plist;
				LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
				MACOSX_DEPLOYMENT_TARGET = 10.11;
				PRODUCT_BUNDLE_IDENTIFIER = com.DeeHughes.KineticTestsMulticast;
				PRODUCT_NAME = "$(TARGET_NAME)";
				SWIFT_OPTIMIZATION_LEVEL = "-Onone";
			};
			name = Debug;
		};
		7ACC0F4A1B97D218005BD8BE /* Release */ = {
			isa = XCBuildConfiguration;
			buildSettings = {
				CLANG_ENABLE_MODULES = YES;
				COMBINE_HIDPI_IMAGES = YES;
				INFOPLIST_FILE = KineticTestsMulticast/Info.plist;
				LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
@@ -640,6 +655,7 @@
				7ACC0F4A1B97D218005BD8BE /* Release */,
			);
			defaultConfigurationIsVisible = 0;
			defaultConfigurationName = Release;
		};
/* End XCConfigurationList section */
	};
+83 −0
Original line number Diff line number Diff line
//
//  Discovery.swift
//  Kinetic
//
//  Created by James Hughes on 9/4/15.
//  Copyright © 2015 Seagate. All rights reserved.
//

import Foundation
import Socket


public class KineticDiscovery {
    
    enum jroot:String {
        case firmware_version = "firmware_version"
        case manufacturer = "manufacturer"
        case model = "model"
        case network_interfaces = "network_interfaces"
        case port = "port"
        case protocol_version = "protocol_version"
        case serial_number = "serial_number"
        case tlsPort = "tlsPort"
        case world_wide_name = "world_wide_name"
    }
    
    enum jnet:String {
        case ipv4_addr = "ipv4_addr"
        case ipv6_addr = "ipv6_addr"
        case mac_addr = "mac_addr"
        case name = "name"
    }
    

    
    
    public private(set) var discoverRunning: Bool = false
    public private(set) var error:ErrorType? = nil
    
    private var discoverStopping = false
    private var s:Datagram? = nil
    
    var done = dispatch_semaphore_create(0)

    
    init (multicast mAddr:String = "239.1.2.3", port mPort:String = "8123", timeout:Double = 0, f:(AnyObject)->()) throws {
        
        s = try Datagram(multicast: mAddr, port: mPort)
        
        discoverRunning = true
        
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {
            do {
                defer {
                    self.discoverRunning = false
                    dispatch_semaphore_signal(self.done)
                }
    
                while true {
                    // recieve it on the multicast socket
                    var (_, _, bytes) = try self.s!.recv(65535, timeout:timeout) // assumes you have access to
                    if self.discoverStopping {
                        return
                    }
                    let data = NSData(bytesNoCopy: &bytes, length: bytes.count, freeWhenDone:false)
                    let json = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments)
                    f(json)
                }
                
            } catch let x {
                self.error = x
                print(String(x))
            }
        }
        
    }
    
    public func stop() {
        discoverStopping = true
        s!.sockClose()
    }
}
 No newline at end of file
+57 −0
Original line number Diff line number Diff line
//
//  KineticDiscovery.swift
//  Kinetic
//
//  Created by James Hughes on 9/4/15.
//  Copyright © 2015 Seagate. All rights reserved.
//

import XCTest
import Socket

class KineticDiscoveryTests: XCTestCase {

    override func setUp() {
        super.setUp()
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }
    
    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
    }
    
    func delay(x: Double) {
        NSThread.sleepForTimeInterval(x)
    }

    func testMulticastTimeout() {
        do {
            let session = try KineticDiscovery(port: "8125", timeout:0.1) { _ in
                XCTFail("Should not happen")
            }
            delay(0.5)
            XCTAssertFalse(session.discoverRunning)
            XCTAssertTrue(session.error  != nil)
            XCTAssertTrue(session.error! as? Datagram.Error == Datagram.Error.timeout)
        } catch let x {
            XCTFail(String(x))
        }
    }
    
    func testPrintAllWWN() {
        var WWNs = Set<String>()
        do {
            let s = try KineticDiscovery() { j in
                WWNs.insert(j["world_wide_name"]!! as! String)
            }
            delay(6)
            s.stop()
            print(WWNs)
            XCTAssertGreaterThan(WWNs.count, 0) 
        } catch let x {
            XCTFail(String(x))
        }
    }
    
}
+7 −13
Original line number Diff line number Diff line
@@ -13,16 +13,9 @@ import BrightFutures

class KineticTests: XCTestCase {
    
    var c: KineticSession? = nil
    
    override func setUp() {
        super.setUp()
        do {
            c = try Kinetic.connect("127.0.0.1")
            print("Open Successful, \(c!.connectionId!)")
        } catch let x {
            fatalError(String(x)) // don't continue on failure. None of the tests will work.
        }
    }
    
    func delay(x: Double) {
@@ -30,24 +23,25 @@ class KineticTests: XCTestCase {
    }
    
    override func tearDown() {
        c!.close()
        XCTAssertFalse(c!.connected)
        super.tearDown()
    }
    
    func testExample() {
        do {
            let c = try Kinetic.connect("127.0.0.1")
            print("Open Successful, \(c.connectionId!)")

            //: Write a key/value pair
            try c!.put("hello", value: "world")
            try c.put("hello", value: "world")
            
            //: Read the value back
            let x = try c!.get("hello")
            let x = try c.get("hello")
            
            //: The Strings on the methods are just for convenience
            //: the actual values are byte arrays `[UInt8]`
            print("Received: \(x.value!.toUtf8String())")
            
            c.close()
        }catch let x {
            XCTFail(String(x))
        }
+1 −1
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ CHECKOUT OPTIONS:
    :commit: ad4db3095a9ccc034c1eb65934a89c670012e6a0
    :git: https://github.com/krzyzanowskim/CryptoSwift
  Socket:
    :commit: f1fd41d73bddd744fddba0110a10862e4fe70346
    :commit: e747f958de049373dc21e9c87f26b5d5cd4f76ef
    :git: https://github.com/jphughes/socket-swift

SPEC CHECKSUMS:
Loading