-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
81 lines (71 loc) · 3.71 KB
/
Server.java
File metadata and controls
81 lines (71 loc) · 3.71 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
import java.io.*;
import java.net.*;
import java.util.*;
public class Server {
//Information needed to establish a socket connection
private static final String HOST = "localhost";
private static final int PORT = 9999;
public static void main(String[] args) {
ServerSocket serverSocket = null;
JavaSqlCon sql = new JavaSqlCon();
try {
serverSocket = new ServerSocket(PORT);
} catch (IOException ex) {
System.out.println("Could not create a ServerSocket" + ex.getMessage());
}
//This loop checks for all of the commands in the protocol and executes accordingly
try {
while (true) {
Socket socket = serverSocket.accept();
Scanner scanner = new Scanner(socket.getInputStream());
PrintWriter out = new PrintWriter(socket.getOutputStream());
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.startsWith("ADD")) {
String[] attributes = line.substring(4).split(",");
AddRunnable add = new AddRunnable(sql, attributes[0], attributes[1], attributes[2], attributes[3], attributes[4], attributes[5], socket);
Thread a = new Thread(add);
a.start();
//sql.addBooking();
} else if (line.startsWith("LISTALL")) {
ListAllRunnable listAll = new ListAllRunnable(sql, socket);
Thread la = new Thread(listAll);
la.start();
//sql.listAllBookings();
} else if (line.startsWith("LISTPT")) {
ListPtRunnable listPt = new ListPtRunnable(sql, line.substring(7), socket);
Thread lp = new Thread(listPt);
lp.start();
//sql.listBookingsByStaff(line.substring(7));
} else if (line.startsWith("LISTCLIENT")) {
ListClientRunnable listC = new ListClientRunnable(sql, line.substring(11), socket);
Thread lc = new Thread(listC);
lc.start();
//sql.listBookingsByClient(line.substring(11));
} else if (line.startsWith("LISTDAY")) {
ListDayRunnable listD = new ListDayRunnable(sql, line.substring(8), socket);
Thread ld = new Thread(listD);
ld.start();
//sql.listBookingsByDate(line.substring(8));
} else if (line.startsWith("UPDATE")) {
String[] attributes = line.substring(7).split(",");
UpdateRunnable update = new UpdateRunnable(sql, attributes[0], attributes[1], attributes[2], attributes[3], attributes[4], attributes[5], attributes[6], socket);
Thread u = new Thread(update);
u.start();
//sql.listBookingByClinet(line.substring(8));
} else if (line.startsWith("DELETE")) {
DeleteRunnable delete = new DeleteRunnable(sql, line.substring(7), socket);
Thread del = new Thread(delete);
del.start();
//sql.deleteBooking(line.substring(7));
} else {
out.println("Try Again" + " \n" + "");
out.flush();
}
}
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}