Skip to content

Commit

Permalink
Rename node -> address, undeprecate and avoid duplicate functions
Browse files Browse the repository at this point in the history
  • Loading branch information
djones6 committed May 17, 2019
1 parent 2df3d80 commit 46a097c
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 84 deletions.
45 changes: 9 additions & 36 deletions Sources/KituraNet/FastCGI/FastCGIServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class FastCGIServer: Server {
public private(set) var port: Int?

/// Has the same meaning as in `getaddrinfo()`.
public private(set) var node: String?
public private(set) var address: String?

/**
A server state.
Expand Down Expand Up @@ -111,16 +111,16 @@ public class FastCGIServer: Server {
Listens for connections on a socket

- Parameter on: port number for new connections
- Parameter node: has the same meaning as in `getaddrinfo()`
- Parameter address: has the same meaning as in `getaddrinfo()`

### Usage Example: ###
````swift
try server.listen(on: port, node: "localhost")
try server.listen(on: port, address: "localhost")
````
*/
public func listen(on port: Int, node: String? = nil) throws {
public func listen(on port: Int, address: String? = nil) throws {
self.port = port
self.node = node
self.address = address
do {
let socket = try Socket.create()
self.listenSocket = socket
Expand All @@ -147,54 +147,27 @@ public class FastCGIServer: Server {
}
}

/**
Listens for connections on a socket
- Parameter on: port number for new connections
*/
@available(*, deprecated, message: "use 'listen(on:node) throws' instead")
public func listen(on port: Int) throws {
return try listen(on: port, node: nil)
}

/**
Static method to create a new `FastCGIServer` and have it listen for conenctions

- Parameter on: port number for accepting new connections
- Parameter node: has the same meaning as in `getaddrinfo()`
- Parameter address: has the same meaning as in `getaddrinfo()`
- Parameter delegate: the delegate handler for FastCGI/HTTP connections

- Returns: a new `FastCGIServer` instance

### Usage Example: ###
````swift
let server = try FastCGIServer.listen(on: port, node: "localhost", delegate: delegate)
let server = try FastCGIServer.listen(on: port, address: "localhost", delegate: delegate)
````
*/
public static func listen(on port: Int, node: String?, delegate: ServerDelegate?) throws -> FastCGIServer {
public static func listen(on port: Int, address: String? = nil, delegate: ServerDelegate?) throws -> FastCGIServer {
let server = FastCGI.createServer()
server.delegate = delegate
try server.listen(on: port, node: node)
try server.listen(on: port, address: address)
return server
}

/**
Static method to create a new `FastCGIServer` and have it listen for conenctions

- Parameter on: port number for accepting new connections
- Parameter delegate: the delegate handler for FastCGI/HTTP connections

- Returns: a new `FastCGIServer` instance

### Usage Example: ###
````swift
let server = try FastCGIServer.listen(on: port, delegate: delegate)
````
*/
@available(*, deprecated, message: "use 'listen(on:node:delegate) throws' instead")
public static func listen(on port: Int, delegate: ServerDelegate?) throws -> FastCGIServer {
return try listen(on: port, node: nil, delegate: delegate)
}

/**
Listens for connections on a socket

Expand Down
43 changes: 10 additions & 33 deletions Sources/KituraNet/HTTP/HTTPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class HTTPServer: Server {
public private(set) var port: Int?

/// Has the same meaning as in `getaddrinfo()`.
public private(set) var node: String?
public private(set) var address: String?

/// The Unix domain socket path on which this server listens for new connections. If `nil`, this server does not listen on a Unix socket.
public private(set) var unixDomainSocketPath: String?
Expand Down Expand Up @@ -154,21 +154,16 @@ public class HTTPServer: Server {

### Usage Example: ###
````swift
try server.listen(on: 8080, node: "localhost")
try server.listen(on: 8080, address: "localhost")
````

- Parameter port: Port number for new connections, e.g. 8080
- Parameter node: has the same meaning as in `getaddrinfo()`
- Parameter address: has the same meaning as in `getaddrinfo()`
*/
public func listen(on port: Int, node: String?) throws {
public func listen(on port: Int, address: String? = nil) throws {
self.port = port
self.node = node
try listen(.inet(port, node))
}

@available(*, deprecated, message: "use 'listen(on:node) throws' instead")
public func listen(on port: Int) throws {
return try listen(on: port, node: nil)
self.address = address
try listen(.inet(port, address))
}

/**
Expand Down Expand Up @@ -264,40 +259,22 @@ public class HTTPServer: Server {

### Usage Example: ###
````swift
let server = HTTPServer.listen(on: 8080, node: "localhost", delegate: self)
let server = HTTPServer.listen(on: 8080, address: "localhost", delegate: self)
````

- Parameter on: Port number for accepting new connections.
- Parameter node: has the same meaning as in `getaddrinfo()`
- Parameter address: has the same meaning as in `getaddrinfo()`
- Parameter delegate: The delegate handler for HTTP connections.

- Returns: A new instance of a `HTTPServer`.
*/
public static func listen(on port: Int, node: String?, delegate: ServerDelegate?) throws -> HTTPServer {
public static func listen(on port: Int, address: String? = nil, delegate: ServerDelegate?) throws -> HTTPServer {
let server = HTTP.createServer()
server.delegate = delegate
try server.listen(on: port, node: node)
try server.listen(on: port, address: address)
return server
}

/**
Static method to create a new HTTP server and have it listen for connections.

### Usage Example: ###
````swift
let server = HTTPServer.listen(on: 8080, delegate: self)
````

- Parameter on: Port number for accepting new connections.
- Parameter delegate: The delegate handler for HTTP connections.

- Returns: A new instance of a `HTTPServer`.
*/
@available(*, deprecated, message: "use 'listen(on:node:delegate) throws' instead")
public static func listen(on port: Int, delegate: ServerDelegate?) throws -> HTTPServer {
return try listen(on: port, node: nil, delegate: delegate)
}

/**
Static method to create a new HTTP server and have it listen for connections on a Unix domain socket.

Expand Down
21 changes: 14 additions & 7 deletions Sources/KituraNet/Server/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,39 +27,37 @@ public protocol Server {
var port: Int? { get }

/// Has the same meaning as in `getaddrinfo()`.
var node: String? { get }
var address: String? { get }

/// A server state.
var state: ServerState { get }

/// Listen for connections on a socket.
///
/// - Parameter on: port number for new connections (eg. 8080)
/// - Parameter node: has the same meaning as in `getaddrinfo()`
func listen(on port: Int, node: String?) throws
/// - Parameter address: has the same meaning as in `getaddrinfo()`
func listen(on port: Int, address: String?) throws

/// Listen for connections on a socket.
///
/// - Parameter on: port number for new connections (eg. 8080)
@available(*, deprecated, message: "use 'listen(on:node) throws' with instead")
func listen(on port: Int) throws

/// Static method to create a new Server and have it listen for connections.
///
/// - Parameter on: port number for accepting new connections
/// - Parameter node: has the same meaning as in `getaddrinfo()`
/// - Parameter address: has the same meaning as in `getaddrinfo()`
/// - Parameter delegate: the delegate handler for HTTP connections
///
/// - Returns: a new Server instance
static func listen(on port: Int, node: String?, delegate: ServerDelegate?) throws -> ServerType
static func listen(on port: Int, address: String?, delegate: ServerDelegate?) throws -> ServerType

/// Static method to create a new Server and have it listen for connections.
///
/// - Parameter on: port number for accepting new connections
/// - Parameter delegate: the delegate handler for HTTP connections
///
/// - Returns: a new Server instance
@available(*, deprecated, message: "use 'listen(on:node:delegate) throws' with instead")
static func listen(on port: Int, delegate: ServerDelegate?) throws -> ServerType

/// Listen for connections on a socket.
Expand Down Expand Up @@ -114,3 +112,12 @@ public protocol Server {
@discardableResult
func clientConnectionFailed(callback: @escaping (Swift.Error) -> Void) -> Self
}

extension Server {
public func listen(on port: Int) throws {
try listen(on: port, address: nil)
}
public static func listen(on port: Int, delegate: ServerDelegate?) throws -> ServerType {
return try Self.listen(on: port, address: nil, delegate: delegate)
}
}
2 changes: 1 addition & 1 deletion Tests/KituraNetTests/ClientE2ETests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ class ClientE2ETests: KituraNetTest {

func testEphemeralListeningPort() {
do {
let server = try HTTPServer.listen(on: 0, node: "localhost", delegate: delegate)
let server = try HTTPServer.listen(on: 0, address: "localhost", delegate: delegate)
_ = HTTP.get("http://localhost:\(server.port!)") { response in
XCTAssertNotNil(response, "ERROR!!! ClientRequest response object was nil")
XCTAssertEqual(response?.statusCode, HTTPStatusCode.OK, "HTTP Status code was \(String(describing: response?.statusCode))")
Expand Down
4 changes: 2 additions & 2 deletions Tests/KituraNetTests/KituraNetTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class KituraNetTest: XCTestCase {
if let unixDomainSocketPath = unixDomainSocketPath {
try server.listen(unixDomainSocketPath: unixDomainSocketPath)
} else {
try server.listen(on: port, node: "localhost")
try server.listen(on: port, address: "localhost")
}
return server
}
Expand Down Expand Up @@ -163,7 +163,7 @@ class KituraNetTest: XCTestCase {
do {
self.port = port

let server = try FastCGIServer.listen(on: port, node: "localhost", delegate: delegate)
let server = try FastCGIServer.listen(on: port, address: "localhost", delegate: delegate)
server.allowPortReuse = allowPortReuse
defer {
server.stop()
Expand Down
8 changes: 4 additions & 4 deletions Tests/KituraNetTests/LifecycleListenerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class LifecycleListenerTests: KituraNetTest {
}

do {
try server.listen(on: self.port, node: "localhost")
try server.listen(on: self.port, address: "localhost")

self.waitForExpectations(timeout: 5) { error in
XCTAssertNil(error)
Expand Down Expand Up @@ -93,7 +93,7 @@ class LifecycleListenerTests: KituraNetTest {
}

do {
try server.listen(on: self.port, node: "localhost")
try server.listen(on: self.port, address: "localhost")

self.waitForExpectations(timeout: 5) { error in
XCTAssertNil(error)
Expand Down Expand Up @@ -127,7 +127,7 @@ class LifecycleListenerTests: KituraNetTest {
}

do {
try server.listen(on: self.port, node: "localhost")
try server.listen(on: self.port, address: "localhost")

self.waitForExpectations(timeout: 5) { error in
XCTAssertNil(error)
Expand Down Expand Up @@ -155,7 +155,7 @@ class LifecycleListenerTests: KituraNetTest {
})

do {
try server.listen(on: -1, node: nil)
try server.listen(on: -1, address: nil)
} catch {
// Do NOT fail the test if an error is thrown.
// In this test case an error should be thrown.
Expand Down
2 changes: 1 addition & 1 deletion Tests/KituraNetTests/MonitoringTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class MonitoringTests: KituraNetTest {
}

do {
try server.listen(on: self.port, node: nil)
try server.listen(on: self.port, address: nil)

self.waitForExpectations(timeout: 10) { error in
server.stop()
Expand Down

0 comments on commit 46a097c

Please sign in to comment.