-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNettyHandler.java
More file actions
190 lines (167 loc) · 6.83 KB
/
NettyHandler.java
File metadata and controls
190 lines (167 loc) · 6.83 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
// Owen Gregson
// Artificial Intelligence
// TTT Checkpoint #3
// Dec 18, 2024
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.concurrent.atomic.AtomicBoolean;
public class NettyHandler {
private HttpServer server;
private Game game;
private MoveHandler moveHandler;
private RestartHandler restartHandler;
private StatsHandler statsHandler;
private final AtomicBoolean restartRequested = new AtomicBoolean(false);
private TTT3.GameConfig config;
public NettyHandler(Game game, int port, TTT3.GameConfig config) throws IOException {
this.game = game;
this.config = config;
this.server = HttpServer.create(new InetSocketAddress(port), 0);
initializeContexts();
}
private void initializeContexts() {
// Serve static files
server.createContext("/", new StaticFileHandler());
// Handle game state requests
GameStateHandler gameStateHandler = new GameStateHandler(game);
server.createContext("/gamestate", gameStateHandler);
// Handle move submissions
this.moveHandler = new MoveHandler();
server.createContext("/move", this.moveHandler);
// restarting
this.restartHandler = new RestartHandler(restartRequested);
server.createContext("/restart", this.restartHandler);
// choosing side
server.createContext("/choose-x", new ChooseSideHandler(Player.X));
server.createContext("/choose-o", new ChooseSideHandler(Player.O));
// game stats
this.statsHandler = new StatsHandler(game);
server.createContext("/stats", this.statsHandler);
server.createContext("/sounds", exchange -> {
if ("GET".equalsIgnoreCase(exchange.getRequestMethod())) {
String requested = exchange.getRequestURI().getPath().replace("/sounds/", "");
java.nio.file.Path filePath = Paths.get(requested);
if (Files.exists(filePath)) {
byte[] fileBytes = Files.readAllBytes(filePath);
exchange.getResponseHeaders().set("Content-Type", "audio/wav");
exchange.sendResponseHeaders(200, fileBytes.length);
try (OutputStream os = exchange.getResponseBody()) {
os.write(fileBytes);
}
} else {
exchange.sendResponseHeaders(404, -1);
}
} else {
exchange.sendResponseHeaders(405, -1);
}
});
}
public void start() {
server.setExecutor(null);
server.start();
System.out.println("Server started at http://localhost:" + server.getAddress().getPort() + "/");
}
public void stop(int delay) {
server.stop(delay);
System.out.println("Server stopped.");
}
public int getSelectedMove() {
return moveHandler.getSelectedMove();
}
public void resetSelectedMove() {
moveHandler.resetSelectedMove();
}
public void waitForRestart() {
System.out.println("Waiting for restart request...");
synchronized (restartRequested) {
while (!restartRequested.get()) {
try {
restartRequested.wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
}
restartRequested.set(false);
System.out.println("Restart request received.");
}
static class RestartHandler implements HttpHandler {
private final AtomicBoolean restartRequested;
public RestartHandler(AtomicBoolean restartRequested) {
this.restartRequested = restartRequested;
}
@Override
public void handle(HttpExchange exchange) throws IOException {
if ("POST".equalsIgnoreCase(exchange.getRequestMethod())) {
synchronized (restartRequested) {
restartRequested.set(true);
restartRequested.notifyAll();
}
String response = "Restart requested.";
exchange.sendResponseHeaders(200, response.length());
try (OutputStream os = exchange.getResponseBody()) {
os.write(response.getBytes());
}
} else {
exchange.sendResponseHeaders(405, -1);
}
}
}
class ChooseSideHandler implements HttpHandler {
private final Player chosenSide;
public ChooseSideHandler(Player chosenSide) {
this.chosenSide = chosenSide;
}
@Override
public void handle(HttpExchange exchange) throws IOException {
if ("POST".equalsIgnoreCase(exchange.getRequestMethod())) {
if (config.isPlayingAgainstAI && config.humanPlayer == null) {
config.humanPlayer = chosenSide;
Player aiSide = config.humanPlayer.other();
if (aiSide == Player.X && config.aiPlayerX == null) {
config.aiPlayerX = new AIPlayer(Player.X);
} else if (aiSide == Player.O && config.aiPlayerO == null) {
config.aiPlayerO = new AIPlayer(Player.O);
}
game.humanPlayer = config.humanPlayer;
game.aiPlayerX = config.aiPlayerX;
game.aiPlayerO = config.aiPlayerO;
game.aiSide = aiSide;
System.out.println("Human player chosen: " + chosenSide);
}
String response = "Side chosen: " + chosenSide;
exchange.sendResponseHeaders(200, response.length());
try (OutputStream os = exchange.getResponseBody()) {
os.write(response.getBytes());
}
} else {
exchange.sendResponseHeaders(405, -1);
}
}
}
static class StatsHandler implements HttpHandler {
private final Game game;
public StatsHandler(Game game) {
this.game = game;
}
@Override
public void handle(HttpExchange exchange) throws IOException {
if ("GET".equalsIgnoreCase(exchange.getRequestMethod())) {
String response = game.stats();
exchange.sendResponseHeaders(200, response.length());
try (OutputStream os = exchange.getResponseBody()) {
os.write(response.getBytes());
}
} else {
exchange.sendResponseHeaders(405, -1);
}
}
}
}