-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.java
247 lines (206 loc) · 7.97 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
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
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import com.google.gson.Gson;
public class Server implements Runnable {
private int clientNo = 0;
private ArrayList<Integer> highScoreList;
private ArrayList<Integer> mineList;
private ArrayList<Integer> revealList;
private ArrayList<Integer> flagList;
private Connection conn;
private PreparedStatement queryStmtScores;
private PreparedStatement querySavedGames;
private PreparedStatement scoreInsertStatement;
private PreparedStatement gameInsertStatement;
public Server() {
Thread t = new Thread(this);
t.start();
}
public void run() {
try {
// Create a server socket
ServerSocket serverSocket = new ServerSocket(8000);
System.out.println("MultiThreadServer started at " + new Date() + '\n');
highScoreList = new ArrayList<Integer>();
while (true) {
// Listen for a new connection request
Socket socket = serverSocket.accept();
// Increment clientNo
clientNo++;
System.out.println("Starting thread for client " + clientNo + " at " + new Date() + '\n');
// Find the client's host name, and IP address
InetAddress inetAddress = socket.getInetAddress();
System.out.println("Client " + clientNo + "'s host name is " + inetAddress.getHostName() + "\n");
System.out.println("Client " + clientNo + "'s IP Address is " + inetAddress.getHostAddress() + "\n");
// Create and start a new thread for the connection
new Thread(new HandleAClient(socket, clientNo)).start();
}
}
catch(IOException ex) {
System.err.println(ex);
}
}
// Define the thread class for handling new connection
class HandleAClient implements Runnable {
private Socket socket; // A connected socket
private int clientNum;
/** Construct a thread */
public HandleAClient(Socket socket, int clientNum) {
try {
this.socket = socket;
this.clientNum = clientNum;
conn = DriverManager.getConnection("jdbc:sqlite:game.db");
queryStmtScores = conn.prepareStatement("Select score from tbl_scores");
} catch (SQLException e) {
System.err.println("Connection error: " + e);
System.exit(1);
}
/* sets up prepared statements for SQL inserts */
String scoreInsertSQL = "INSERT INTO tbl_scores(gameSize, score) VALUES(?,?)";
String gameInsertSQL = "INSERT INTO tbl_savedgames(name, timer, gameSize, flagsRemaining, mineList, revealList, flagList) VALUES(?,?,?,?,?,?,?)";
try {
scoreInsertStatement = conn.prepareStatement(scoreInsertSQL);
gameInsertStatement = conn.prepareStatement(gameInsertSQL);
} catch (SQLException e) {
e.printStackTrace();
System.exit(0);
}
}
/** Run a thread */
public void run() {
try {
// Create data input and output streams
DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream());
// Continuously serve the client
while (true) {
int request = inputFromClient.readInt();
buildArr(highScoreList);
sortArr(highScoreList);
// Request = 0 means the client is fetching the leaderboard
if (request == 0) {
for (int i = 0; i < 5; i++) {
outputToClient.writeInt(highScoreList.get(i));
}
}
// Request = 1 means client wants to attempt to commit a score to the leaderboard
else if (request == 1) {
Integer gameSize = inputFromClient.readInt();
Integer score = inputFromClient.readInt();
try {
scoreInsertStatement.setString(1, gameSize.toString());
scoreInsertStatement.setString(2, score.toString());
scoreInsertStatement.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
// Request = 2 means client wants to save a game to the database
else if (request == 2) {
Integer nameLength = inputFromClient.readInt();
String name = "";
for (int i = 0; i < nameLength; i++) {
name += inputFromClient.readChar();
}
Integer timer = inputFromClient.readInt();
Integer gameSize = inputFromClient.readInt();
Integer flagsRemaining = inputFromClient.readInt();
Integer mineJsonLength = inputFromClient.readInt();
String mineJson = "";
for (int i = 0; i < mineJsonLength; i++) {
mineJson += inputFromClient.readChar();
}
Integer revealJsonLength = inputFromClient.readInt();
String revealJson = "";
for (int i = 0; i < revealJsonLength; i++) {
revealJson += inputFromClient.readChar();
}
Integer flagJsonLength = inputFromClient.readInt();
String flagJson = "";
for (int i = 0; i < flagJsonLength; i++) {
flagJson += inputFromClient.readChar();
}
try {
gameInsertStatement.setString(1, name);
gameInsertStatement.setString(2, timer.toString());
gameInsertStatement.setString(3, gameSize.toString());
gameInsertStatement.setString(4, flagsRemaining.toString());
gameInsertStatement.setString(5, mineJson);
gameInsertStatement.setString(6, revealJson);
gameInsertStatement.setString(7, flagJson);
gameInsertStatement.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
// Request = 3 means client wants to load a game from the database
else if (request == 3) {
Integer nameLength = inputFromClient.readInt();
String name = "";
for (int i = 0; i < nameLength; i++) {
name += inputFromClient.readChar();
}
try {
querySavedGames = conn.prepareStatement("Select * from tbl_savedgames where name = \"" + name + "\"");
PreparedStatement stmt = querySavedGames;
ResultSet rset = stmt.executeQuery();
int timerInt = rset.getInt(2);
int gameSizeInt = rset.getInt(3);
int flagsRemainingInt = rset.getInt(4);
Object o1 = rset.getObject(5);
String mineListString = o1.toString();
Object o2 = rset.getObject(6);
String revealListString = o2.toString();
Object o3 = rset.getObject(7);
String flagListString = o3.toString();
outputToClient.writeInt(timerInt);
outputToClient.writeInt(gameSizeInt);
outputToClient.writeInt(flagsRemainingInt);
outputToClient.writeInt(mineListString.length());
outputToClient.writeChars(mineListString);
outputToClient.writeInt(revealListString.length());
outputToClient.writeChars(revealListString);
outputToClient.writeInt(flagListString.length());
outputToClient.writeChars(flagListString);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
} catch(IOException ex) {
ex.printStackTrace();
}
}
private void buildArr(ArrayList<Integer> arr) {
try {
arr.clear();
PreparedStatement stmt = queryStmtScores;
ResultSet rset = stmt.executeQuery();
while (rset.next()) {
arr.add(rset.getInt("score"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private void sortArr(ArrayList<Integer> arr) {
Collections.sort(arr, Collections.reverseOrder());
}
}
public static void main(String[] args) {
Server serv = new Server();
}
}