Skip to content

Commit 981c915

Browse files
authored
Merge pull request #1 from Local-Connectivity-Lab/zhennan_fix_linux
Fix build on Linux and various refactoring
2 parents 76b2a6b + 2f38202 commit 981c915

14 files changed

+606
-322
lines changed

Package.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ let package = Package(
2929
.product(name: "NIOConcurrencyHelpers", package: "swift-nio"),
3030
.product(name: "NIOWebSocket", package: "swift-nio"),
3131
.product(name: "NIOHTTP1", package: "swift-nio"),
32+
.product(name: "NIOFoundationCompat", package: "swift-nio"),
3233
.product(name: "NIOSSL", package: "swift-nio-ssl"),
3334
.product(
3435
name: "NIOTransportServices",
@@ -43,6 +44,14 @@ let package = Package(
4344
name: "LCLWebSocketTests",
4445
dependencies: ["LCLWebSocket"]
4546
),
47+
.executableTarget(
48+
name: "AutobahnClient",
49+
dependencies: ["LCLWebSocket"]
50+
),
51+
.executableTarget(
52+
name: "AutobahnServer",
53+
dependencies: ["LCLWebSocket"]
54+
),
4655
.executableTarget(name: "Client", dependencies: ["LCLWebSocket"]),
4756
.executableTarget(name: "Server", dependencies: ["LCLWebSocket"]),
4857
]
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//
2+
// This source file is part of the LCL open source project
3+
//
4+
// Copyright (c) 2021-2024 Local Connectivity Lab and the project authors
5+
// Licensed under Apache License v2.0
6+
//
7+
// See LICENSE for license information
8+
// See CONTRIBUTORS for the list of project authors
9+
//
10+
// SPDX-License-Identifier: Apache-2.0
11+
//
12+
13+
import Foundation
14+
import LCLWebSocket
15+
import NIOCore
16+
import NIOPosix
17+
18+
@main
19+
struct TestWebSocketClient {
20+
21+
static let config = LCLWebSocket.Configuration(
22+
maxFrameSize: 1 << 16,
23+
autoPingConfiguration: .disabled,
24+
leftoverBytesStrategy: .forwardBytes
25+
)
26+
27+
static let serverAddress = "127.0.0.1"
28+
static let serverPort = 9001
29+
static let agentName = "LCLWebSocketClient"
30+
31+
public static func main() throws {
32+
33+
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1)
34+
var client = LCLWebSocket.client(on: elg)
35+
let totalTestCount = elg.any().makePromise(of: Int.self)
36+
37+
client.onText { websocket, text in
38+
guard let total = Int(text) else {
39+
fatalError()
40+
}
41+
totalTestCount.succeed(total)
42+
}
43+
44+
try client.connect(to: "ws://\(Self.serverAddress):\(Self.serverPort)/getCaseCount", configuration: Self.config)
45+
.wait()
46+
47+
let total = try totalTestCount.futureResult.wait()
48+
print("Running total tests: \(total)")
49+
50+
for i in 1...total {
51+
var client = LCLWebSocket.client(on: elg)
52+
client.onText { ws, text in
53+
ws.send(.init(string: text), opcode: .text)
54+
}
55+
client.onBinary { ws, binary in
56+
ws.send(binary, opcode: .binary)
57+
}
58+
try client.connect(
59+
to: "ws://\(Self.serverAddress):\(Self.serverPort)/runCase?case=\(i)&agent=\(Self.agentName)",
60+
configuration: Self.config
61+
).wait()
62+
}
63+
64+
let closeClient = LCLWebSocket.client()
65+
do {
66+
try closeClient.connect(
67+
to: "ws://\(Self.serverAddress):\(Self.serverPort)/updateReports?agent=\(Self.agentName)",
68+
configuration: Self.config
69+
).wait()
70+
} catch {
71+
print("Error closing client: \(error)")
72+
}
73+
74+
}
75+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// This source file is part of the LCL open source project
3+
//
4+
// Copyright (c) 2021-2024 Local Connectivity Lab and the project authors
5+
// Licensed under Apache License v2.0
6+
//
7+
// See LICENSE for license information
8+
// See CONTRIBUTORS for the list of project authors
9+
//
10+
// SPDX-License-Identifier: Apache-2.0
11+
//
12+
13+
import Foundation
14+
import LCLWebSocket
15+
import NIOCore
16+
import NIOPosix
17+
18+
@main
19+
struct AutohahnServer {
20+
21+
static let config = LCLWebSocket.Configuration(
22+
maxFrameSize: 1 << 16,
23+
autoPingConfiguration: .disabled,
24+
leftoverBytesStrategy: .forwardBytes
25+
)
26+
27+
static let serverAddress = "127.0.0.1"
28+
static let serverPort = 9000
29+
30+
static func main() throws {
31+
32+
let args = CommandLine.arguments
33+
var port: Int = Self.serverPort
34+
precondition(args.count == 1 || args.count == 3, "Usage: \(args[0]) [--port <port>]")
35+
let portCmdIndex = args.firstIndex(where: { $0 == "--port" })
36+
if let portCmdIndex = portCmdIndex {
37+
let portIndex = portCmdIndex + 1
38+
port = Int(args[portIndex]) ?? port
39+
}
40+
41+
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 4)
42+
var server = LCLWebSocket.server(on: elg)
43+
44+
server.onBinary { ws, buffer in
45+
ws.send(buffer, opcode: .binary)
46+
}
47+
48+
server.onText { ws, text in
49+
ws.send(.init(string: text), opcode: .text)
50+
}
51+
52+
try server.listen(host: Self.serverAddress, port: port, configuration: Self.config).wait()
53+
}
54+
}

0 commit comments

Comments
 (0)