/
ServerMain.java
249 lines (231 loc) · 6.88 KB
/
ServerMain.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
248
249
import java.io.*;
import java.net.*;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.*;
public class ServerMain extends Thread{
private Socket clientSocket;
private OutputStream os;
private DataOutputStream dos;
private InputStream is;
private DataInputStream dis;
static ArrayList<ServerMain> threadPool = new ArrayList<ServerMain>();
static ArrayList<Socket> sockets = new ArrayList<Socket>();
private static final int listenPort = 12345;
private int threadIndex;
private int socketIndex;
MySQLManager manager;
public ServerMain(Socket clientSocket, int threadIndex, int socketIndex) throws ClassNotFoundException {
this.threadIndex = threadIndex;
this.socketIndex = socketIndex;
try {
this.clientSocket = clientSocket;
os = clientSocket.getOutputStream();
dos = new DataOutputStream(os);
is = clientSocket.getInputStream();
dis = new DataInputStream(is);
Class.forName("com.mysql.jdbc.Driver");
manager = new MySQLManager("jdbc:mysql://localhost:3306/RestaurantDatabase", "root");
} catch (IOException e) {
System.err.println("Failed to construct new thread");
e.printStackTrace();
} catch (SQLException e) {
System.err.println("Failed to connect to database");
e.printStackTrace();
}
}
public void run() {
int connectionType = determineConnection();
try {
if(connectionType == 10) {
dos.writeInt(connectionType);
int status = mobileConnect();
dos.writeInt(status);
} else if(connectionType == 20) {
dos.writeInt(connectionType);
int status = resturauntConnect();
dos.writeInt(status);
} else if(connectionType == -1) {
dos.writeInt(connectionType);
}
} catch(IOException e) {
System.err.println("Could not write to client");
e.printStackTrace();
}
endConnection();
}
private void endConnection() {
try {
dos.close();
os.close();
dis.close();
is.close();
manager.close();
clientSocket.close();
synchronized(threadPool) {
sockets.remove(socketIndex);
threadPool.remove(threadIndex);
}
} catch (IOException e) {
System.err.println("Failed to close streams");
e.printStackTrace();
} catch (SQLException e) {
System.err.println("Failed to close MySQLManager");
e.printStackTrace();
}
}
private int determineConnection() {
try {
return dis.readInt();
} catch (IOException e) {
System.err.println("Could not read connection type from client.");
e.printStackTrace();
return -1;
}
}
private int resturauntConnect() {
try {
int code = dis.readInt();
if(code == 10) { //Only read new data
String query = "SELECT * FROM Orders WHERE Read_Flag = 0 AND TotalPrice IS NOT NULL";
ArrayList<String[]> result = manager.query(query);
int numCol = result.get(0).length;
dos.writeInt(numCol);
for(int j=0; j<result.size(); j++) {
for(int i=0; i<numCol; i++) {
dos.writeUTF(result.get(j)[i]);
}
}
dos.writeUTF("done"); //Done writing data
return 0;
} else if(code == 20) { //Read all data
String query = "SELECT * FROM Orders";
ArrayList<String[]> result = manager.query(query);
int numCol = result.get(0).length;
dos.writeInt(numCol);
for(int j=0; j<result.size(); j++) {
for(int i=0; i<numCol; i++) {
dos.writeUTF(result.get(j)[i]);
}
}
dos.writeUTF("done"); //Done writing data
return 0;
} else if(code == 30) { //Get Special Offer
String offer = dis.readUTF();
String update = "INSERT INTO special_offers VALUES '" + offer +"'";
manager.update(update);
return 0;
} else if(code == 40) { //Get only old orders
String query = "SELECT * FROM Orders WHERE Read_Flag = 1 AND TotalPrice IS NOT NULL";
ArrayList<String[]> result = manager.query(query);
int numCol = result.get(0).length;
dos.writeInt(numCol);
for(int j=0; j<result.size(); j++) {
for(int i=0; i<numCol; i++) {
dos.writeUTF(result.get(j)[i]);
}
}
dos.writeUTF("done"); //Done writing data
return 0;
} else if(code == 50) { //Send menu to server
} else if(code == 60) { //Send menu to restaurant
} else if(code == 70) { //Flip read_flag for order
int index = dis.readInt();
String update = "UPDATE Orders SET read_flag=1 WHERE PrimaryIndex=" + index;
manager.update(update);
return 0;
}
} catch(IOException e) {
System.err.println("Could not read code from resuraunt");
e.printStackTrace();
return -10;
} catch (SQLException e) {
System.err.println("Error reading from database");
e.printStackTrace();
return -20;
}
return -1;
}
private int mobileConnect() {
int code = 0;
try {
code = dis.readInt();
System.out.println(code);
} catch (IOException e1) {
System.err.println("Unable to communicate with mobile device");
e1.printStackTrace();
}
if(code == 10) { //read order from mobile device
boolean done = false;
ArrayList<String> values = new ArrayList<String>();
while(!done) {
try {
String temp = dis.readUTF();
if(!temp.equals( "done")) {
values.add(temp);
} else {
String update = "";
update += "INSERT INTO orders VALUES (";
for(int i=0; i<values.size(); i++) {
if(i != values.size()-1)
update += "'" + values.get(i) + "',";
else
update += "'" + values.get(i) + "',";
}
update += "0)";
int status = manager.update(update);
System.out.println(status);
done = true;
return 0;
}
} catch(IOException e) {
System.err.println("Error reading String!");
e.printStackTrace();
return -10;
} catch (SQLException e) {
System.err.println("Error updating database!");
e.printStackTrace();
return -20;
}
}
} else if(code == 20) { //send mobile device special offers
try {
String query = "SELECT offers FROM special_offers WHERE valid=0 AND TotalPrice IS NOT NULL";
ArrayList<String[]> rs = manager.query(query);
for(int i=0; i<rs.size(); i++) {
dos.writeUTF(rs.get(i)[0]);
}
dos.writeUTF("done");
return 0;
} catch(SQLException e) {
System.err.println("Failed to query database");
e.printStackTrace();
return -10;
} catch (IOException e) {
System.err.println("Failed to write to mobile client");
e.printStackTrace();
return -20;
}
}
return -1;
}
public static void main(String args[]) throws ClassNotFoundException {
try {
@SuppressWarnings("resource")
ServerSocket serverSocket = new ServerSocket(listenPort);
while(true) {
Socket temp = serverSocket.accept();
synchronized(threadPool) {
sockets.add(temp);
ServerMain tempRef = new ServerMain(temp, threadPool.size(), sockets.size()-1);
threadPool.add(tempRef);
tempRef.start();
}
}
} catch (IOException e) {
System.err.println("Server failed");
e.printStackTrace();
}
}
}