-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.swift
110 lines (96 loc) · 3.35 KB
/
main.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#if os(macOS)
import Darwin
#elseif os(Linux)
import Glibc
#else
#error("Unsupported platform!")
#endif
/// The name of the executable.
let executableName = "simple-client"
/// The version number of the executable.
let versionNumber = "1.1.0"
/// The default IP address or name of the host to connect to.
let defaultHost = "127.0.0.1"
/// The default port used for the connection.
let defaultPort: UInt16 = 13050
/// The IP address or name of the host to connect to.
var host = defaultHost
/// The port used for the connection.
var port = defaultPort
/// The reservation code to join a prepared game.
var reservation = ""
/// The strategy used for the game.
var strategy = ""
/// Prints the help message into the standard output.
func printHelpMessage() {
print("""
Usage: \(executableName) [options]
-h, --host:
The IP address or name of the host to connect to (default: \(defaultHost)).
-p, --port:
The port used for the connection (default: \(defaultPort)).
-r, --reservation:
The reservation code to join a prepared game.
-s, --strategy:
The strategy used for the game.
--help:
Print this help message.
--version:
Print the version number.
""")
}
/// Exits the program with the given error message.
///
/// - Parameter error: The error message to print into the standard output.
func exit(withError error: String) -> Never {
if !error.isEmpty {
print("ERROR: \(error)")
}
printHelpMessage()
exit(EXIT_FAILURE)
}
/// The iterator for the command-line arguments.
var argvIterator = CommandLine.arguments.dropFirst().makeIterator()
// Parse the command-line arguments.
while let carg = argvIterator.next() {
switch carg {
case "--help":
printHelpMessage()
exit(EXIT_SUCCESS)
case "--version":
print("\(executableName) version \(versionNumber)")
exit(EXIT_SUCCESS)
case let arg where arg.hasPrefix("-"):
guard let argValue = argvIterator.next() else {
exit(withError: #"Missing value for the option "\#(arg)"!"#)
}
switch arg {
case "-h", "--host":
host = argValue
case "-p", "--port":
guard let portValue = UInt16(argValue) else {
exit(withError: #"The value "\#(argValue)" can not be converted to a port number!"#)
}
port = portValue
case "-r", "--reservation":
reservation = argValue
case "-s", "--strategy":
strategy = argValue
default:
exit(withError: #"Unrecognized option "\#(arg)"!"#)
}
case let arg:
exit(withError: #"Unrecognized argument "\#(arg)"!"#)
}
}
// Create a TCP socket.
let tcpSocket = SCSocket()
// Connect to the game server.
if tcpSocket.connect(toHost: host, withPort: port) {
print("Connected to the game server!")
// Handle the game and the communication with the game server.
SCGameHandler(socket: tcpSocket, reservation: reservation, strategy: strategy).handleGame()
}
// Close the socket and the connection with the game server.
tcpSocket.close()
print("Terminating the client!")