-
Notifications
You must be signed in to change notification settings - Fork 5
/
Server.java
203 lines (176 loc) · 4.9 KB
/
Server.java
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
import java.io.*;
import java.net.*;
public class Server extends Thread {
private ServerSocket server;
private Socket client1, client2, last;
private boolean done = false;
private int port;
public enum States {
X, O, NONE
}
public static void clearGrid(States[][] grid) {
for(int i = 0; i < grid.length; i++) {
for(int j = 0; j < grid[i].length; j++) {
grid[i][j] = States.NONE;
}
}
}
private States[][] grid = new States[3][3];
public Server() {
this(Protocol.DEFAULTPORT);
}
public Server(int port) {
this.port = port;
}
private void write(int tobothclients) throws IOException {
write(tobothclients, tobothclients);
}
private void write(int toclient1, int toclient2) throws IOException {
client1.getOutputStream().write(toclient1);
client2.getOutputStream().write(toclient2);
}
private synchronized void set(Socket client, int x, int y) throws IOException {
if (grid[x][y] == States.NONE && x < grid.length && y < grid[0].length && client != last) {
byte b = 0;
if (client == client1) {
grid[x][y] = States.X;
b = Protocol.SETX << 4;
} else if (client == client2) {
grid[x][y] = States.O;
b = Protocol.SETO << 4;
}
b |= (x&3) << 2;
b |= (y&3);
write((int)b);
// check for win
for (int i = 0; i < grid.length; i++) {
if (grid[i][0] != States.NONE && grid[i][0] == grid[i][1] && grid[i][0] == grid[i][2] // waagerecht
|| grid[0][i] != States.NONE && grid[0][i] == grid[1][i] && grid[0][i] == grid[2][i]) // senkrecht
sendDone(client, false);
}
if (grid[1][1] != States.NONE && (grid[0][0] == grid[1][1] && grid[0][0] == grid[2][2]
|| grid[2][0] == grid[1][1] && grid[2][0] == grid[0][2]))
sendDone(client, false);
if (!done) {
// check for tie
boolean tie = true;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == States.NONE)
tie = false;
}
}
if (tie)
sendDone(client, true);
}
last = client;
}
}
private void sendDone(Socket client, boolean tie) throws IOException {
final byte win = Protocol.WIN << 4;
final byte lose = Protocol.LOSS << 4;
if (tie) {
write(Protocol.TIE << 4);
System.err.println("Tie!");
} else if (client == client1) {
write(win, lose);
System.err.println("X has won!");
} else {
write(lose, win);
System.err.println("O has won!");
}
try {
Thread.sleep(2500);
} catch (InterruptedException e) {}
clearGrid(grid);
write(Protocol.NEWROUND << 4);
System.err.println("Starting new round ...");
}
@Override
public void run() {
while (!isInterrupted()) {
System.err.println("Server waiting for connections ...");
clearGrid(grid);
client1 = client2 = null;
try {
server = new ServerSocket(port);
} catch (IOException e) {
System.err.println(e.getMessage());
break;
}
while ((client1 == null || client2 == null) && !done) {
Socket client = null;
try {
client = server.accept();
if (client1 == null)
client1 = client;
else
client2 = client;
System.err.println("New connection from " + client.getInetAddress());
} catch (IOException e) {}
}
try {
server.close();
} catch (IOException e) {}
// pick the beginner
last = (Math.random() >= 0.5) ? client1 : client2;
if (done)
break;
// remove data which was already sent
try {
client1.getInputStream().skip(client1.getInputStream().available());
client2.getInputStream().skip(client2.getInputStream().available());
} catch (IOException e) {}
Client c1 = new Client(client1);
Client c2 = new Client(client2);
c1.start();
c2.start();
try {
c1.join();
c2.join();
} catch (InterruptedException e) { break; }
}
}
@Override
public void interrupt() {
super.interrupt();
done = true;
try {
if (server != null)
server.close();
} catch (IOException e) {}
killclient(client1);
killclient(client2);
System.err.println("Server terminated!");
}
private synchronized void killclient(Socket client) {
if (client != null) {
try { client.getOutputStream().write(Protocol.FAIL << 4); } catch (IOException io) {}
try { client.close(); } catch (IOException io) {}
}
client = null;
}
private class Client extends Thread {
private Socket client;
public Client(Socket client) {
super("Client");
this.client = client;
}
@Override
public void run() {
while (!done) {
try {
int got = client.getInputStream().read();
if (got == -1/* || client == null*/)
throw new IOException();
if (Protocol.opcode(got) == Protocol.SET)
set(client, Protocol.x(got), Protocol.y(got));
} catch (IOException e) {
killclient(client1);
killclient(client2);
break;
}
}
}
}
}