-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
318 lines (285 loc) · 10.3 KB
/
Client.java
File metadata and controls
318 lines (285 loc) · 10.3 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Arrays;
import java.util.concurrent.Callable;
import java.util.function.Function;
class Client {
static String host = "127.0.0.1";
static int port = 9876;
static ClientState state = ClientState.CONNECTING;
static Socket socket = null;
static InputStream in = null;
static OutputStream out = null;
static Thread inputThread = null;
static ClientGame game = null;
/**
* Initializes the client, shutdown hook, client socket and client game state.
* <p>
* Listens for messages from the server on the main thread indefinitely; see {@link #receiveMessage()}.
* <p>
* Once a message fails to be received or is invalid, stops listening for messages and calls {@link #disconnect()}.
*
* @param args Takes in a host as argument #1, which defaults to {@link #host}. Takes in a port number as
* argument #2, which defaults to {@link #port}.
*/
public static void main(String[] args) {
clear();
if (args.length >= 1) {
host = args[0];
}
if (args.length >= 2) {
try {
port = Integer.parseInt(args[1]);
} catch (NumberFormatException e) {
System.out.printf("ERROR: Invalid port number \"%s\"\n", args[1]);
System.exit(-1);
}
}
Runtime.getRuntime().addShutdownHook(new Thread(Client::disconnect));
System.out.printf("Connecting to server at %s:%d...\n", host, port);
if (!connect()) {
System.out.print("ERROR: Failed to connect to server\n");
System.exit(-1);
}
System.out.print("Connected to server\n");
game = new ClientGame();
while (receiveMessage()) {
Thread.yield();
}
disconnect();
}
/**
* Attempt to clear the console using control sequences.
*/
static void clear() {
System.out.print("\033[H\033[2J\033[3J");
// System.out.print("\033\143");
}
/**
* Runs a thread that waits for input from the client.
* <p>
* Starts by calling {@link #stopReadingInput()} in case the client is already reading input.
* <p>
* I use a thread here so that we can wait for input in a non-blocking manner, so that the client can listen to
* messages while it waits on input.
* <p>
* Unfortunately, the way I currently do this will make it so that typed input doesn't display until the client
* submits it (by hitting enter) on Windows-based terminals; I either need to find a way around that, or just
* scrap this entirely.
*
* @param prompt The input prompt.
* @param condition The condition (callable) by which to continue waiting for input.
* @param action The action (function) to call once input has been obtained.
*/
static void readInput(String prompt, Callable<Boolean> condition, Function<Byte, Boolean> action) {
stopReadingInput();
inputThread = new Thread(() -> {
try {
while (!inputThread.isInterrupted() && condition.call()) {
System.in.read(new byte[System.in.available()]); // skip existing bytes
System.out.print(prompt);
while (System.in.available() < 1) { // wait for a byte to become available
Thread.yield();
if (inputThread.isInterrupted() || !condition.call()) {
return;
}
}
int input = System.in.read();
System.in.read(new byte[System.in.available()]); // skip remaining bytes
if (action.apply((byte) input)) {
break;
}
}
} catch (Exception e) {
// ignore
}
});
inputThread.start();
}
/**
* Interrupts the thread that is waiting for input and waits for that thread to die.
*/
static void stopReadingInput() {
if (inputThread != null && inputThread.isAlive()) {
do {
if (!inputThread.isInterrupted()) {
inputThread.interrupt();
}
Thread.yield();
} while (inputThread.isAlive());
System.out.print('\n');
}
}
/**
* Sets up the client socket and its input and output streams.
*
* @return Boolean indicating success.
*/
static boolean connect() {
state = ClientState.CONNECTING;
try {
socket = new Socket(host, port);
in = socket.getInputStream();
out = socket.getOutputStream();
} catch (IOException e) {
disconnect();
return false;
}
state = ClientState.CONNECTED;
return true;
}
/**
* Closes the client socket and its input and output streams.
* <p>
* Calls {@link #stopReadingInput()} to interrupt input before disconnecting.
*/
static void disconnect() {
if (state == ClientState.DISCONNECTING || state == ClientState.DISCONNECTED) {
return;
}
state = ClientState.DISCONNECTING;
if (in != null) {
try {
in.close();
} catch (IOException e) {
// ignore
}
in = null;
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
// ignore
}
out = null;
}
if (socket == null) {
return;
}
try {
socket.close();
} catch (IOException e) {
// ignore
}
stopReadingInput();
System.out.print("Disconnected from server\n");
socket = null;
state = ClientState.DISCONNECTED;
}
/**
* Waits for a byte from the server by calling {@link InputStream#read()}, then parses the received byte(s) and
* updates the client/game state accordingly.
* <p>
* Starts by calling {@link #stopReadingInput()} in case the client is reading input.
*
* @return Boolean indicating success.
*/
static boolean receiveMessage() {
try {
int nextByte;
if ((nextByte = in.read()) == -1) {
return false;
}
stopReadingInput();
// Waiting for another player to join the game
// `w` (lowercase)
// Only sent if there is not yet a second player
// IF second player arrives, send "game starting" message
if (nextByte == 'w') {
game.waitingForOpponent();
return true;
}
// Game starting
// `x` – Game starting, you are X
// `o` – Game starting, you are O
// Indicate for each player if they are `X` or `O`
if (nextByte == 'x' || nextByte == 'o') {
char role = Character.toUpperCase((char) nextByte);
game.gameStarting(role);
return true;
}
// How many people before the player in the queue
// `Q` followed by byte with value 0…254
// 255 = lots (255 or more) players ahead of you
if (nextByte == 'Q') {
if ((nextByte = in.read()) == -1) {
return false;
}
game.inQueue(nextByte);
return true;
}
// Current state of the board
// String – each of 9 characters represents one square on the board (`X`, `O` or ` `)
// Always sent "square 1", "square 2", …, "square 9"
if (nextByte == 'X' || nextByte == 'O' || nextByte == ' ') {
char[] board = new char[9];
int square = 0;
boolean eof = false;
do {
board[square] = (char) nextByte;
} while (++square < 9 && !(eof = ((nextByte = in.read()) == -1)));
if (eof) {
return false;
}
game.boardStateChanged(board);
return true;
}
// Indicate who plays next
// Boolean – `1` = your turn, `0` = other player`s turn
boolean yourTurn = nextByte == 1;
if (yourTurn || nextByte == 0) {
game.nextTurn(yourTurn);
return true;
}
// Incorrect move
// `I`
// Only sent if move is incorrect
// If correct, message with board and indicating other player`s move
if (nextByte == 'I') {
game.invalidMove();
return true;
}
// You won/lost/tied
// `W` = win (followed by byte with length of win streak)
// `L` = loss
// `T` = tie
if (nextByte == 'W') {
// Indicate winning streak to winner
// Send at end of each game to winner
// Number – indicates length of the streak
// In binary, one byte of 1..255
// Streaks longer than 255 will be reported as 255
if ((nextByte = in.read()) == -1) {
return false;
}
game.gameWon(nextByte);
return true;
}
boolean tie = nextByte == 'T';
if (tie || nextByte == 'L') {
game.gameLost(tie);
return true;
}
System.out.printf("ERROR: Received unrecognized byte from server: %s\n", nextByte);
} catch (IOException e) {
// ignore
}
return false;
}
/**
* Sends a message to the server.
*/
static void sendMessage(byte[] bytes) {
try {
out.write(bytes);
} catch (IOException e) {
if (state != ClientState.DISCONNECTING) {
System.out.printf("ERROR: Failed to send message to server: %s\n", Arrays.toString(bytes));
System.exit(-1);
}
}
}
}