Skip to content

Commit

Permalink
Kitura/Kitura#33 Part two of the rename of WebSocketClient to WebSock…
Browse files Browse the repository at this point in the history
…etConnection
  • Loading branch information
shmuelk committed Jan 4, 2017
1 parent f602556 commit fd9e3f2
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 38 deletions.
8 changes: 4 additions & 4 deletions Sources/KituraWebSocket/WSConnectionUpgradeFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ public class WSConnectionUpgradeFactory: ConnectionUpgradeFactory {
[sha1Data.base64EncodedString(options: .lineLength64Characters)]
response.headers["Sec-WebSocket-Protocol"] = request.headers["Sec-WebSocket-Protocol"]

let client = WebSocketClient()
let processor = WSSocketProcessor(client: client)
client.processor = processor
client.service = service
let connection = WebSocketConnection()
let processor = WSSocketProcessor(connection: connection)
connection.processor = processor
connection.service = service

return (processor, nil)
}
Expand Down
12 changes: 6 additions & 6 deletions Sources/KituraWebSocket/WSSocketProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ class WSSocketProcessor: IncomingSocketProcessor {

private var byteIndex = 0

private let client: WebSocketClient
private let connection: WebSocketConnection

init(client: WebSocketClient) {
self.client = client
init(connection: WebSocketConnection) {
self.connection = connection
}

/// Process data read from the socket.
Expand All @@ -58,7 +58,7 @@ class WSSocketProcessor: IncomingSocketProcessor {
guard error == nil else {
// What should be done if there is an error?
Log.error("Error parsing frame. \(error!)")
client.close(reason: .protocolError, description: error?.description)
connection.close(reason: .protocolError, description: error?.description)
return true
}

Expand All @@ -69,7 +69,7 @@ class WSSocketProcessor: IncomingSocketProcessor {
byteIndex += bytesConsumed

if completed {
client.received(frame: parser.frame)
connection.received(frame: parser.frame)
parser.reset()
}
}
Expand Down Expand Up @@ -107,6 +107,6 @@ class WSSocketProcessor: IncomingSocketProcessor {

/// Called by the `IncomingSocketHandler` to tell us that the socket has been closed.
public func socketClosed() {
client.connectionClosed(reason: .noReasonCodeSent)
connection.connectionClosed(reason: .noReasonCodeSent)
}
}
18 changes: 9 additions & 9 deletions Sources/KituraWebSocket/WebSocketConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ import Foundation
import Darwin
#endif

/// Represents the client on a specific WebSocket connection. Provides a unique
/// identifier for the client and APIs to send messages and control commands
/// to the client.
public class WebSocketClient {
/// Represents a specific WebSocket connection. Provides a unique identifier for
/// the connection and APIs to send messages and control commands to the client
/// at the other end of the connection.
public class WebSocketConnection {
weak var processor: WSSocketProcessor?

weak var service: WebSocketService? {
didSet {
guard let service = service else { return }
DispatchQueue.global().async { [unowned self] in
service.connected(client: self)
service.connected(connection: self)
}
}
}
Expand All @@ -51,14 +51,14 @@ public class WebSocketClient {
case binary, text, unknown
}

/// Unique identifier for this `WebSocketClient`
/// Unique identifier for this `WebSocketConnection`
public let id: String

private var messageState: MessageStates = .unknown

init() {
id = UUID().uuidString
buffer = NSMutableData(capacity: WebSocketClient.bufferSize) ?? NSMutableData()
buffer = NSMutableData(capacity: WebSocketConnection.bufferSize) ?? NSMutableData()
}

/// Close a WebSocket connection by sending a close control command to the client optionally
Expand Down Expand Up @@ -169,7 +169,7 @@ public class WebSocketClient {
closeConnection(reason: reasonTosend, description: description, hard: true)

DispatchQueue.global().async { [unowned self] in
self.service?.disconnected(client: self, reason: reason)
self.service?.disconnected(connection: self, reason: reason)
}
}
else {
Expand Down Expand Up @@ -291,7 +291,7 @@ public class WebSocketClient {
WSFrame.createFrameHeader(finalFrame: true, opCode: withOpCode, payloadLength: payloadLength, buffer: buffer)

if let realPayload = payload {
if WebSocketClient.bufferSize >= buffer.length + payloadLength {
if WebSocketConnection.bufferSize >= buffer.length + payloadLength {
buffer.append(realPayload, length: payloadLength)
processor.write(from: buffer)
}
Expand Down
24 changes: 12 additions & 12 deletions Sources/KituraWebSocket/WebSocketService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,28 @@ public protocol WebSocketService: class {
/// Called when a WebSocket client connects to the server and is connected to a specific
/// `WebSocketService`.
///
/// - Parameter client: The `WebSocketClient` object that represents the client that
/// connected to this `WebSocketService`
func connected(client: WebSocketClient)
/// - Parameter connection: The `WebSocketConnection` object that represents the client's
/// connection to this `WebSocketService`
func connected(connection: WebSocketConnection)

/// Called when a WebSocket client disconnects from the server.
///
/// - Parameter client: The `WebSocketClient` object that represents the client that
/// disconnected from this `WebSocketService`.
/// - Parameter connection: The `WebSocketConnection` object that represents the connection that
/// was disconnected from this `WebSocketService`.
/// - Paramater reason: The `WebSocketCloseReasonCode` that describes why the client disconnected.
func disconnected(client: WebSocketClient, reason: WebSocketCloseReasonCode)
func disconnected(connection: WebSocketConnection, reason: WebSocketCloseReasonCode)

/// Called when a WebSocket client sent a binary message to the server to this `WebSocketService`.
///
/// - Parameter message: A Data struct containing the bytes of the binary message sent by the client.
/// - Parameter client: The `WebSocketClient` object that represents the client that
/// sent the message to this `WebSocketService`
func received(message: Data, from: WebSocketClient)
/// - Parameter client: The `WebSocketConnection` object that represents the connection over which
/// the client sent the message to this `WebSocketService`
func received(message: Data, from: WebSocketConnection)

/// Called when a WebSocket client sent a text message to the server to this `WebSocketService`.
///
/// - Parameter message: A String containing the text message sent by the client.
/// - Parameter client: The `WebSocketClient` object that represents the client that
/// sent the message to this `WebSocketService`
func received(message: String, from: WebSocketClient)
/// - Parameter client: The `WebSocketConnection` object that represents the connection over which
/// the client sent the message to this `WebSocketService`
func received(message: String, from: WebSocketConnection)
}
14 changes: 7 additions & 7 deletions Tests/KituraWebSocketTests/TestWebSocketService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,28 @@ import Foundation
@testable import KituraWebSocket

class TestWebSocketService: WebSocketService {
var clientId = ""
var connectionId = ""
let closeReason: WebSocketCloseReasonCode

public init(closeReason: WebSocketCloseReasonCode) {
self.closeReason = closeReason
}

public func connected(client: WebSocketClient) {
clientId = client.id
public func connected(connection: WebSocketConnection) {
connectionId = connection.id
}

public func disconnected(client: WebSocketClient, reason: WebSocketCloseReasonCode) {
XCTAssertEqual(clientId, client.id, "Client ID from connect wasn't client ID from disconnect")
public func disconnected(connection: WebSocketConnection, reason: WebSocketCloseReasonCode) {
XCTAssertEqual(connectionId, connection.id, "Client ID from connect wasn't client ID from disconnect")
XCTAssertEqual(Int(closeReason.code()), Int(reason.code()), "Excpected close reason code of \(closeReason) received \(reason)")
}

public func received(message: Data, from: WebSocketClient) {
public func received(message: Data, from: WebSocketConnection) {
print("Received a binary message of length \(message.count)")
from.send(message: message)
}

public func received(message: String, from: WebSocketClient) {
public func received(message: String, from: WebSocketConnection) {
print("Received a String message of length \(message.characters.count)")
from.send(message: message)

Expand Down

0 comments on commit fd9e3f2

Please sign in to comment.