-
Notifications
You must be signed in to change notification settings - Fork 0
/
Barista.java
105 lines (91 loc) · 3.32 KB
/
Barista.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
import java.io.*;
import java.lang.reflect.Type;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.TreeMap;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import Helpers.CoffeeBar;
import Helpers.HandleCustomer;
import Helpers.ServerLog;
public class Barista {
private final static int port = 8888;
private final static String IDLE = "idle";
private static TreeMap<String, String> clientCount = new TreeMap<>(); // Format <portNumber, State>
private static TreeMap<String, ServerLog> currentLog = new TreeMap<>(); // Store Server Log
// Main method to run the program, a.k.a 'server', a.k.a 'barista'
public static void main(String[] args) {
// Check if "serverLog.json" file already exist
if (new File("serverLog.json").exists()) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
// Retrieve Previous Log in the file
String previousLog;
try {
previousLog = readPreviousLog();
Type mapType = currentLog.getClass();
TreeMap<String, ServerLog> previousData = gson.fromJson(previousLog, mapType);
currentLog = previousData;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
OpenShop();
}
// Method to run the 'server'
private static void OpenShop() {
// Initialize server socket to listen for customer connections
ServerSocket serverSocket = null;
final CoffeeBar coffeeBar = new CoffeeBar(clientCount, currentLog);
// Intercept when Server Shut Down, Wrtie Server Log to JSON file
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
System.out.println("\nWriting Server Log..");
try {
jsonWriter();
System.out.println("\nDone.. Bye Bye");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}));
try {
// Instantiate Server Socket
serverSocket = new ServerSocket(port);
System.out.println("Waiting for Customers...");
while (true) {
// Socket stays in blocked state untill a customer is connected
Socket socket = serverSocket.accept();
// Append new customer in Cafe
clientCount.put(Integer.toString(socket.getPort()), IDLE);
// Start independant thread for new joined customer
new Thread(new HandleCustomer(socket, coffeeBar, clientCount)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
// Read Previous Server Log Info
private static String readPreviousLog() throws FileNotFoundException, IOException {
try (BufferedReader reader = new BufferedReader(new FileReader("serverLog.json"))) {
StringBuilder content = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
content.append(line);
}
return content.toString();
}
}
// Write Server Log to JSON File
public static void jsonWriter() throws FileNotFoundException, IOException {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
// Write Log to JSON File
try (FileWriter writer = new FileWriter("serverLog.json")) {
gson.toJson(currentLog, writer);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}