Repository navigation
Expand file tree
/
Copy pathServer.java
More file actions
155 lines (134 loc) · 5.8 KB
/
Copy pathServer.java
File metadata and controls
155 lines (134 loc) · 5.8 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
import java.io.*;
import java.net.*;
import java.util.*;
public class Server {
private static final List<ClientHandler> clients = new ArrayList<ClientHandler>();
private static Sudoku sudoku = new Sudoku();
private static int clientIdCounter = 1;
private static Map<Integer, Integer> clientUpdateCounts = new HashMap<>();
private static Map<String, Integer> boardUpdates = new HashMap<>();
public static void main(String[] args) throws IOException {
if (args.length != 1) {
System.err.println("Usage: java EchoServer <port number>");
System.exit(1);
}
int portNumber = Integer.parseInt(args[0]);
sudoku.fillValues();
System.out.println("Sudoku board Created: ");
System.out.println(sudoku.getSudokuString());
try (ServerSocket serverSocket = new ServerSocket(portNumber)) {
System.out.println("Server is listening on port " + portNumber);
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("New Client Connection");
int clientId = clientIdCounter++;
clientUpdateCounts.put(clientId, 0);
ClientHandler clientHandler = new ClientHandler(clientSocket);
clients.add(clientHandler);
new Thread(clientHandler).start();
}
} catch (IOException e) {
System.out.println("Exception caught when trying to listen on port "
+ portNumber + " or listening for a connection");
System.out.println(e.getMessage());
}
} // main
static void broadcast(String message) {
synchronized (clients) {
for (ClientHandler client : clients) {
client.sendMessage(message);
}
}
}
static void removeClient(ClientHandler clientHandler) {
clients.remove(clientHandler);
}
static boolean handleUpdate(int i, int j, int n) {
String key = i + "-" + j;
if (sudoku.enterNumber(i, j, n)) {
if(boardUpdates.containsKey(key)) {
int previousClientId = boardUpdates.get(key);
clientUpdateCounts.put(previousClientId, clientUpdateCounts.get(previousClientId) - 1);
}
boardUpdates.put(key, clientId);
clientUpdateCounts.put(clientId, clientUpdateCounts.get(clientId) + 1);
broadcast("Board Updated:\n" + sudoku.getSudokuString());
if (sudoku.isBoardFull()) {
announceWinner();
System.exit(0);
}
return true;
}
return false;
}
static void announceWinner() {
int maxUpdates = 0;
int winnerId = -1;
for(Map.Entry<Integer, Integer> entry : clientUpdateCounts.entrySet()) {
if (entry.getValue() > maxUpdates) {
maxUpdates = entry.getValue();
winnerId = entry.getKey();
}
}
if(winnerId != -1) {
broadcast("Game Complete! The winner is Client " + winnerId + " with " + maxUpdates + " updates.");
} else {
broadcast("Game Complete! No winner determined.");
}
}
private static class ClientHandler implements Runnable {
private final Socket socket;
private final int clientId;
private PrintWriter out;
public ClientHandler(Socket socket, int clientId) {
this.socket = socket;
this.clientId = clientId;
}
@Override
public void run() {
try (BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
out = new PrintWriter(socket.getOutputStream(), true);
out.println("Welcome to the Sudoku server! Your client ID is " + clientId);
out.println("Current Sudoku board:\n" + sudoku.getSudokuString());
String inputLine;
while ((inputLine = in.readLine())!= null) {
String[] parts = inputLine.split(" ");
if (parts[0].equalsIgnoreCase("show")) {
out.println("Current Sudoku board:\n" + sudoku.getSudokuString());
} else if (parts[0].equalsIgnoreCase("update") && parts.length == 4) {
try {
int i = Integer.parseInt(parts[1]);
int j = Integer.parseInt(parts[2]);
int n = Integer.parseInt(parts[3]);
if (handleUpdate(i, j, n)) {
out.println("Update successful!");
} else {
out.println("Update failed! Invalid move.");
}
} catch (NumberFormatException e){
out.println("Invalid input. Use: update <i> <j> <n>");
}
} else {
out.println("Invalid command. Use 'show' or 'update <i> <j> <n>'");
}
}
} catch (IOException e) {
System.err.println("Error occurred with a client connection.");
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
System.err.println("Error closing client socket.");
}
removeClient(this);
System.out.println("Client disconnected.");
}
}
void sendMessage (String message) {
if (out != null) {
out.println(message);
}
}
}
}