-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathChatServer.java
More file actions
393 lines (327 loc) · 12.3 KB
/
ChatServer.java
File metadata and controls
393 lines (327 loc) · 12.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import java.io.IOException;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.*;
import java.util.logging.*;
/**
* Simple UDP-based chat server demonstrating datagram networking.
*
* WARNING: This is a teaching example. UDP is unsuitable for production chat
* because it provides no reliability, ordering, or delivery guarantees.
* Production systems should use TCP or implement reliability on top of UDP.
*
* Protocol:
* - @JOIN nickname - Register with server
* - @QUIT - Leave chat
* - any other text - Broadcast to all clients
*
* Limitations:
* - Messages limited to 512 bytes
* - No reliability (UDP can drop packets)
* - No message ordering guarantees
* - No encryption
* - Basic nickname collision handling
*/
public class ChatServer implements AutoCloseable {
private static final Logger LOGGER = Logger.getLogger(ChatServer.class.getName());
private static final int PACKET_SIZE = 512;
private static final int MAX_NICKNAME_LENGTH = 20;
private static final int RECEIVE_BUFFER_SIZE = 65536; // OS buffer for incoming packets
private final DatagramSocket serverSocket;
private final ExecutorService executor;
private final Map<ClientAddress, Client> clients; // Thread-safe map
private volatile boolean running = true;
/**
* Represents a registered chat client.
*/
private static class Client {
final String nickname;
final InetAddress address;
final int port;
final Instant joinedAt;
Client(String nickname, InetAddress address, int port) {
this.nickname = nickname;
this.address = address;
this.port = port;
this.joinedAt = Instant.now();
}
@Override
public String toString() {
return nickname + "@" + address.getHostAddress() + ":" + port;
}
}
/**
* Composite key for identifying clients by address and port.
*/
private static class ClientAddress {
final InetAddress address;
final int port;
ClientAddress(InetAddress address, int port) {
this.address = address;
this.port = port;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof ClientAddress)) return false;
ClientAddress other = (ClientAddress) o;
return address.equals(other.address) && port == other.port;
}
@Override
public int hashCode() {
return Objects.hash(address, port);
}
}
/**
* Creates a chat server on the specified port.
*/
public ChatServer(int port) throws IOException {
// Create UDP socket
this.serverSocket = new DatagramSocket(port);
// Increase receive buffer to handle bursts (default is often too small)
this.serverSocket.setReceiveBufferSize(RECEIVE_BUFFER_SIZE);
// Use concurrent map for thread-safe client management
this.clients = new ConcurrentHashMap<>();
// Single thread for receiving packets (UDP is sequential)
// Could use multiple threads, but need careful synchronization
this.executor = Executors.newSingleThreadExecutor(r -> {
Thread t = new Thread(r, "ChatServer-Receiver");
t.setDaemon(false); // Want to process remaining packets on shutdown
return t;
});
LOGGER.info("ChatServer started on port " + port);
// Start receiving packets
executor.submit(this::receiveLoop);
}
/**
* Main packet receiving loop.
*/
private void receiveLoop() {
byte[] buffer = new byte[PACKET_SIZE];
while (running) {
try {
// Create fresh packet for each receive (avoid buffer reuse issues)
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
// Block waiting for packet
serverSocket.receive(packet);
// Process packet
handlePacket(packet);
} catch (SocketException e) {
// Socket closed during shutdown - expected
if (running) {
LOGGER.log(Level.SEVERE, "Socket error", e);
}
break;
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Error receiving packet", e);
} catch (Exception e) {
// Catch-all to prevent loop from crashing
LOGGER.log(Level.SEVERE, "Unexpected error in receive loop", e);
}
}
LOGGER.info("Receive loop terminated");
}
/**
* Process a received datagram packet.
*/
private void handlePacket(DatagramPacket packet) {
try {
// Extract message with explicit encoding
byte[] data = Arrays.copyOfRange(packet.getData(), 0, packet.getLength());
String message = new String(data, StandardCharsets.UTF_8).trim();
// Validate message
if (message.isEmpty()) {
LOGGER.fine("Received empty packet");
return;
}
InetAddress senderAddress = packet.getAddress();
int senderPort = packet.getPort();
ClientAddress clientAddr = new ClientAddress(senderAddress, senderPort);
LOGGER.fine("Received from " + senderAddress + ":" + senderPort + ": " + message);
// Handle commands
if (message.startsWith("@JOIN ")) {
handleJoin(clientAddr, message.substring(6).trim());
}
else if (message.equals("@QUIT")) {
handleQuit(clientAddr);
}
else if (message.startsWith("@")) {
// Unknown command
sendToClient(clientAddr, "ERROR: Unknown command");
}
else {
// Regular chat message
handleMessage(clientAddr, message);
}
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Error handling packet", e);
}
}
/**
* Handle client join request.
*/
private void handleJoin(ClientAddress clientAddr, String nickname) throws IOException {
// Validate nickname
if (nickname.isEmpty()) {
sendToClient(clientAddr, "ERROR: Nickname cannot be empty");
return;
}
if (nickname.length() > MAX_NICKNAME_LENGTH) {
sendToClient(clientAddr, "ERROR: Nickname too long (max " + MAX_NICKNAME_LENGTH + ")");
return;
}
if (!nickname.matches("[a-zA-Z0-9_-]+")) {
sendToClient(clientAddr, "ERROR: Nickname must be alphanumeric");
return;
}
// Check if client already registered
if (clients.containsKey(clientAddr)) {
sendToClient(clientAddr, "ERROR: You are already joined as " + clients.get(clientAddr).nickname);
return;
}
// Check for nickname collision
boolean nicknameTaken = clients.values().stream()
.anyMatch(c -> c.nickname.equalsIgnoreCase(nickname));
if (nicknameTaken) {
sendToClient(clientAddr, "ERROR: Nickname '" + nickname + "' is already taken");
return;
}
// Register client
Client client = new Client(nickname, clientAddr.address, clientAddr.port);
clients.put(clientAddr, client);
LOGGER.info("Client joined: " + client);
// Send confirmation to client
sendToClient(clientAddr, "OK: Joined as " + nickname);
// Broadcast to all clients
broadcast(nickname + " has joined the chat", null);
}
/**
* Handle client quit request.
*/
private void handleQuit(ClientAddress clientAddr) throws IOException {
Client client = clients.remove(clientAddr);
if (client != null) {
LOGGER.info("Client quit: " + client);
sendToClient(clientAddr, "OK: Goodbye");
broadcast(client.nickname + " has left the chat", clientAddr);
} else {
sendToClient(clientAddr, "ERROR: You are not registered");
}
}
/**
* Handle regular chat message.
*/
private void handleMessage(ClientAddress clientAddr, String message) throws IOException {
Client client = clients.get(clientAddr);
if (client == null) {
sendToClient(clientAddr, "ERROR: You must JOIN before sending messages");
return;
}
// Broadcast message with sender's nickname
String formattedMessage = client.nickname + ": " + message;
broadcast(formattedMessage, null);
LOGGER.fine("Broadcasted: " + formattedMessage);
}
/**
* Send message to specific client.
*/
private void sendToClient(ClientAddress clientAddr, String message) throws IOException {
byte[] data = message.getBytes(StandardCharsets.UTF_8);
// Check message size
if (data.length > PACKET_SIZE) {
LOGGER.warning("Message too large to send: " + data.length + " bytes");
data = "ERROR: Message too large".getBytes(StandardCharsets.UTF_8);
}
DatagramPacket packet = new DatagramPacket(
data, data.length,
clientAddr.address, clientAddr.port
);
serverSocket.send(packet);
}
/**
* Broadcast message to all clients except excludeAddr.
*/
private void broadcast(String message, ClientAddress excludeAddr) {
byte[] data = message.getBytes(StandardCharsets.UTF_8);
if (data.length > PACKET_SIZE) {
LOGGER.warning("Broadcast message too large, truncating");
data = Arrays.copyOf(data, PACKET_SIZE);
}
// Send to all clients
for (Map.Entry<ClientAddress, Client> entry : clients.entrySet()) {
ClientAddress addr = entry.getKey();
// Skip excluded address
if (excludeAddr != null && addr.equals(excludeAddr)) {
continue;
}
try {
DatagramPacket packet = new DatagramPacket(
data, data.length,
addr.address, addr.port
);
serverSocket.send(packet);
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Failed to send to " + entry.getValue(), e);
}
}
}
/**
* Gracefully shutdown the server.
*/
@Override
public void close() {
LOGGER.info("Shutting down ChatServer...");
running = false;
try {
// Notify all clients
broadcast("Server is shutting down", null);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Error notifying clients of shutdown", e);
}
// Close socket (interrupts receive())
serverSocket.close();
// Shutdown executor
executor.shutdown();
try {
if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
executor.shutdownNow();
}
} catch (InterruptedException e) {
executor.shutdownNow();
Thread.currentThread().interrupt();
}
LOGGER.info("ChatServer shutdown complete");
}
/**
* Get current client count (for monitoring).
*/
public int getClientCount() {
return clients.size();
}
/**
* Main method for demonstration.
*/
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("USAGE: java ChatServer <port>");
System.exit(1);
}
int port = Integer.parseInt(args[0]);
try (ChatServer server = new ChatServer(port)) {
// Add shutdown hook for graceful termination
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
LOGGER.info("Shutdown hook triggered");
server.close();
}));
LOGGER.info("ChatServer running. Press Ctrl+C to stop.");
// Keep main thread alive
Thread.sleep(Long.MAX_VALUE);
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Failed to start server", e);
System.exit(1);
} catch (InterruptedException e) {
LOGGER.info("Server interrupted");
}
}
}