Ind.ie is now Small Technology Foundation.
Commit ae95c09b authored by Dapperstout's avatar Dapperstout

Refactor: extract parameter object for remote device parameters.

parent 6ad26b31
......@@ -2,6 +2,18 @@ import Foundation
import SwiftBytes
import CocoaAsyncSocket
public class RemoteDevice {
public let host: NSString
public let port: UInt16
public let id: DeviceId
public init(host: NSString, port: UInt16 = 22000, id: DeviceId) {
self.host = host
self.port = port
self.id = id
}
}
public class Connector: NSObject {
public typealias ConnectionHandler = (Connection?) -> ()
......@@ -14,7 +26,7 @@ public class Connector: NSObject {
self.localIdentity = localIdentity
}
public func connect(host: NSString, port: UInt16 = 22000 , remoteDeviceId: DeviceId,
public func connect(remoteDevice: RemoteDevice,
onConnection: ConnectionHandler = { _ in },
onMessage: MessageHandler = { _ in },
onError: ErrorHandler = { _ in }
......@@ -22,8 +34,8 @@ public class Connector: NSObject {
socket.synchronouslySetDelegateQueue(queue)
socket.synchronouslySetDelegate(self) // don't actually need delegate here, but without it, connectToHost doesn't work...
do {
try socket.connectToHost(host as String, onPort: port)
secure(socket, remoteDeviceId: remoteDeviceId, onConnection: onConnection, onMessage: onMessage, onError: onError)
try socket.connectToHost(remoteDevice.host as String, onPort: remoteDevice.port)
secure(socket, remoteDeviceId: remoteDevice.id, onConnection: onConnection, onMessage: onMessage, onError: onError)
}
catch _ {
onConnection(nil)
......
......@@ -15,6 +15,7 @@ class ConnectionTests: QuickSpec {
let host = "1.2.3.4"
let port = UInt16(1234)
let deviceId = DeviceId.example
let remoteDevice = RemoteDevice(host: host, port: port, id: deviceId)
beforeEach {
socket = SocketSpy()
......@@ -29,7 +30,7 @@ class ConnectionTests: QuickSpec {
it("opens a connection") {
var connection: Connection?
connector.connect(host, port: port, remoteDeviceId: deviceId, onConnection: {
connector.connect(remoteDevice, onConnection: {
connection = $0
})
......@@ -42,7 +43,7 @@ class ConnectionTests: QuickSpec {
socket.connectShouldSucceed = false
var callbackCalled = false
connector.connect(host, remoteDeviceId: deviceId, onConnection: {
connector.connect(remoteDevice, onConnection: {
expect($0).to(beNil())
callbackCalled = true
})
......@@ -51,7 +52,7 @@ class ConnectionTests: QuickSpec {
}
it("can disconnect") {
connector.connect(host, remoteDeviceId: deviceId, onConnection: {
connector.connect(remoteDevice, onConnection: {
$0!.disconnect()
})
......@@ -62,7 +63,7 @@ class ConnectionTests: QuickSpec {
describe("securing the connection") {
it("uses TLS to secure the connection") {
connector.connect(host, remoteDeviceId: deviceId)
connector.connect(remoteDevice)
expect(tls.secureSocketWasCalled).to(beTrue())
expect(tls.latestSocket) == socket
......@@ -74,7 +75,7 @@ class ConnectionTests: QuickSpec {
tls.secureShouldSucceed = false
var callbackCalled = false
connector.connect(host, remoteDeviceId: deviceId, onConnection: {
connector.connect(remoteDevice, onConnection: {
expect($0).to(beNil())
callbackCalled = true
})
......@@ -89,7 +90,7 @@ class ConnectionTests: QuickSpec {
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)
connector.queue = queue
connector.connect(host, remoteDeviceId: deviceId)
connector.connect(remoteDevice)
expect(socket.latestDelegateQueue) === queue
}
......@@ -110,7 +111,7 @@ class ConnectionTests: QuickSpec {
let checkIncomingMessage: ((Message) -> ()) -> () = {
check in
connector.connect(host, remoteDeviceId: deviceId, onMessage: {
connector.connect(remoteDevice, onMessage: {
check($0)
});
}
......@@ -118,7 +119,7 @@ class ConnectionTests: QuickSpec {
it("sends messages") {
let ping = Ping()
connector.connect(host, remoteDeviceId: deviceId, onConnection: {
connector.connect(remoteDevice, onConnection: {
$0!.send(ping)
})
......@@ -166,7 +167,7 @@ class ConnectionTests: QuickSpec {
socket.availableBytes = [concatenateNibbles(1, 0), 0, 0, 0, 0, 0, 0, 0]
var error: NSError?
connector.connect(host, remoteDeviceId: deviceId, onError: {
connector.connect(remoteDevice, onError: {
error = $0
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment