-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.java
407 lines (369 loc) · 16.6 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import java.net.*;
import java.io.*;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.PriorityQueue;
import java.util.Comparator;
import java.util.concurrent.*;
import java.nio.file.Files;
import java.net.InetAddress;
import java.sql.*;
import java.util.concurrent.TimeUnit;
import static java.lang.System.out;
public class Server implements Runnable {
private static boolean systemDebug = true;
private static Semaphore tokenSemaphore, pqSemaphore;
private static ServerSocket serverSocket;
private static String serverID;
private Socket clientSocket;
private static Socket parentConnectionSocket;
private static int debug = 0;
private static Integer myPort;
public static int activeClients = 2;
public static boolean killMain = false;
public static int killTotal = 5;
private static int Request = 1;
private static int Release = 2;
private static int Relinquish = 3;
private static int Grant = 4;
private static int Wait = 5;
private static int Acknowledgement = 6;
private static int ChildShutdown = 10;
private boolean waitForGrant;
private int clientResponseCode;
private Server parentServer;
private static int outGoingMessages = 0;
private static int incomingMessages = 0;
private static PriorityQueue<Request> requestQueue = new PriorityQueue<Request>(100,new Comparator<Request>() {
@Override
public int compare(Request rone, Request rtwo) {
return Long.compare(rone.requestTimestamp.getTime(), rtwo.requestTimestamp.getTime());
}
});
public void setWaitForGrant(boolean val) {waitForGrant = val;}
public void setClientResponseCode(int val) {clientResponseCode = val;}
public Server initThreadStatus(boolean val1, int val2) {
this.waitForGrant = val1;
this.clientResponseCode = val2;
return this;
}
// Create ServerSocket on the given port
public void startServer(int port) {
long currentThread = Thread.currentThread().getId();
System.out.println("Thread: \033[1m\033[32m" + currentThread + "\033[0m maintaining Server");
try {
serverSocket = new ServerSocket(port);
System.out.println("Server started: " + serverSocket.getLocalPort());
// Accept and manage clients
while (!killMain) {
try {
final Socket receiveClientSocket = serverSocket.accept();
String clientAddress = receiveClientSocket.getInetAddress().getHostName().toString().split("\\.")[0];
final int clientID = Integer.parseInt(clientAddress.substring(2,4));
final Server myNewServer = new Server();
Thread clientThread = new Thread(new Runnable() {
@Override
public void run() {
if (clientID > 1 && clientID <= 7) {
myNewServer.manageChild(receiveClientSocket);
} else {
myNewServer.initThreadStatus(true, 0);
myNewServer.manageClients(receiveClientSocket);
}
}
});
clientThread.start();
} catch (IOException except) {
break;
}
}
serverSocket.close();
return;
} catch (IOException e) {
if (systemDebug) {
System.err.println("Server creation failed !");
e.printStackTrace();
}
}
System.out.println("\n\n\nEnding Server");
}
public void connectTreeParent() {
int numParentPort = Integer.parseInt(serverID.substring(2,4));
String parentAddress = String.format("%02d", numParentPort / 2);
if (parentAddress.equals("00")) {
out.println("\033[1m\033[32m[S1]\033[0m");
} else {
parentAddress = "dc" + parentAddress + ".utdallas.edu";
int parentPort = 9037 + numParentPort/2;
System.out.println("Resolved parent address " + parentAddress + " | " + parentPort);
int retryConnection = 2;
while (retryConnection > 0) {
try {
parentConnectionSocket = new Socket(parentAddress, parentPort);
out.println(".... connected to parent.");
break;
} catch (IOException exc) {
try {TimeUnit.SECONDS.sleep(3);} catch (InterruptedException e) {e.printStackTrace();}
retryConnection--;
out.println("retrying connection to parent ....");
}
}
try {
DataInputStream inputDataStream = new DataInputStream(parentConnectionSocket.getInputStream());
while (killTotal > 0) {
killTotal = inputDataStream.readInt();
}
} catch (IOException except) {
System.err.println("Failed connecting to parent: " + except);
}
}
while (activeClients > 0) {
try {
TimeUnit.MICROSECONDS.sleep(1);
} catch (InterruptedException exc) {exc.printStackTrace();}
}
if (!parentAddress.equals("00")) {
try {
DataOutputStream outputDataStream = new DataOutputStream(parentConnectionSocket.getOutputStream());
killMain = true;
outputDataStream.writeInt(ChildShutdown);
System.out.println("Sending shutdown confirmation !");
} catch (IOException e) {
e.printStackTrace();
}
} else {
if (systemDebug) {out.println("\033[1m\033[32m Main KILLED \033[0m");}
killMain = true;
}
return;
}
public Server assignSocket(Socket socket) {
clientSocket = socket;
return this;
}
public Server assignParent(Server parent) {
this.parentServer = parent; return this;
}
public void interruptionListener(DataInputStream inputDataStream, Request curReq) {
try {
clientResponseCode = inputDataStream.readInt(); incomingMessages++;
if (clientResponseCode == Release) {
pqSemaphore.acquire();
requestQueue.remove(curReq);
pqSemaphore.release();
parentServer.initThreadStatus(false, clientResponseCode);
out.println("Ending interruptionListener: " + clientResponseCode);
} else {
out.println("interruptionListener got unknown code");
}
} catch (IOException | InterruptedException e) {
try {
pqSemaphore.acquire();
requestQueue.remove(curReq);
pqSemaphore.release();
parentServer.initThreadStatus(false, clientResponseCode);
} catch (InterruptedException ex) { ex.printStackTrace(); }
out.println("\033[1m\033[33mInterruptionListener terminated! " + curReq.clientID + "\033[0m");
// e.printStackTrace();
}
return;
}
public void manageClients(Socket clientSocket) {
long currentThread = Thread.currentThread().getId();
System.out.println("Thread: " + currentThread + " handling\033[1m\033[32m client:\033[0m " + clientSocket.getInetAddress().getHostName().toString() + "[" + waitForGrant + " " + clientResponseCode +"]");
try {
final DataInputStream inputDataStream = new DataInputStream(clientSocket.getInputStream());
byte[] getRequestBytes = new byte[1024];
int reqSize = inputDataStream.read(getRequestBytes);
byte[] getRequestBytesFinal = new byte[reqSize];
System.arraycopy(getRequestBytes, 0, getRequestBytesFinal, 0, reqSize);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(getRequestBytesFinal);
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
DataOutputStream outputDataStream = new DataOutputStream(clientSocket.getOutputStream());
final Request clientRequest = (Request) objectInputStream.readObject();incomingMessages++;
if (clientRequest.status) {
killTotal --;
outputDataStream.writeInt(Acknowledgement); outGoingMessages++;
System.out.println("Thread: " + currentThread + " \033[1m\033[32mcomplete\033[0m, client: " + clientSocket.getInetAddress().getHostName().toString());
return;
}
requestQueue.add(clientRequest);
System.out.println("Received a request: " + clientRequest.clientID + " @ " + clientRequest.requestTimestamp + " => " + clientRequest.status);
final Server newListner = new Server();
newListner.assignParent(this);
Thread interruptionListenerThread = new Thread(new Runnable() {
@Override
public void run() {
newListner.interruptionListener(inputDataStream, clientRequest);
}
});
interruptionListenerThread.start();
while ((clientRequest.status == false) & waitForGrant) {
if (false) {System.out.println("Status: " + clientRequest.status);}
try {
TimeUnit.MICROSECONDS.sleep(1);
} catch (InterruptedException exc) {
exc.printStackTrace();
}
}
// try {
// TimeUnit.SECONDS.sleep(6);
// } catch (InterruptedException exc) {
// exc.printStackTrace();
// }
if (waitForGrant) {
outputDataStream.writeInt(Grant); outGoingMessages++;
System.out.println(String.format("=> Request \033[1m\033[33m%2d\033[0m Granted !", clientRequest.clientID));
while (true) {
if (clientResponseCode == Release) {
out.println("Got release code, breaking now " + clientRequest.clientID);
break;
} else if (!interruptionListenerThread.isAlive()) {
out.println("\033[1m\033[33mILThread dead\033[0m, breaking now " + clientRequest.clientID);
break;
} else {
try {
TimeUnit.MICROSECONDS.sleep(3);
// out.println("response code not fulfilled: " + clientResponseCode);
} catch (InterruptedException exc) {
exc.printStackTrace();
}
}
}
tokenSemaphore.release();
out.println("Semaphore release !");
out.println("\033[1m\033[33mReleased by "+clientRequest.clientID+"\033[0m");
} else {
out.println("\033[1m\033[33m======>>Else statement..."+clientRequest.clientID+"\033[0m");
if (clientRequest.status == true) {
tokenSemaphore.release();
out.println("\033[1m\033[33mReleased by "+clientRequest.clientID+"\033[0m");
}
}
System.out.println("Thread: " + currentThread + " \033[1m\033[32mcomplete\033[0m, client: " + clientSocket.getInetAddress().getHostName().toString());
} catch (IOException | ClassNotFoundException except) {
except.printStackTrace();
}
}
public void manageChild(Socket clientSocket) {
long currentThread = Thread.currentThread().getId();
String childID = clientSocket.getInetAddress().getHostName().toString();
System.out.println("Thread: " + currentThread + " handling\033[1m\033[34m child:\033[0m " + childID);
try {
DataOutputStream outputDataStream = new DataOutputStream(clientSocket.getOutputStream());
DataInputStream inputDataStream = new DataInputStream(clientSocket.getInputStream());
int currentKillVal = killTotal;
while (true) {
try {
while (currentKillVal == killTotal) {
TimeUnit.MICROSECONDS.sleep(1);
}
outputDataStream.writeInt(killTotal);
currentKillVal = killTotal;
if (killTotal == 0) {break;}
} catch (InterruptedException exc) {
exc.printStackTrace();
}
}
int childShutdown = inputDataStream.readInt();
activeClients--;
if (childShutdown == ChildShutdown) {System.out.println(childID + " shutting down !");}
return;
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
long currentThread = Thread.currentThread().getId();
System.out.println("Thread: " + currentThread + " in run() method !");
}
public void grantOperations() {
while (!killMain) {
if (requestQueue.size() > 0) {
// out.println("SOMETHING IN Q: " + requestQueue.toString());
// out.println("\033[1m\033[33mAcquire...\033[0m");
boolean acquire = tokenSemaphore.tryAcquire();
if (acquire) {
out.println("\033[1m\033[33mAcquired!\033[0m");
try {
pqSemaphore.acquire();
Request currentGrant = requestQueue.poll();
currentGrant.status = true;
out.println("\033[1m\033[33mGranted request: "+currentGrant.clientID+"\033[0m");
pqSemaphore.release();
} catch (InterruptedException e) {e.printStackTrace();}
} else {
// out.println("[no acq] SOMETHING IN Q: " + requestQueue.toString());
try {
TimeUnit.MICROSECONDS.sleep(1);
} catch (InterruptedException exc) {
exc.printStackTrace();
}
}
} else {
try {
TimeUnit.MICROSECONDS.sleep(1);
// out.println("EMPTY Q: " + requestQueue.toString());
} catch (InterruptedException exc) {
exc.printStackTrace();
}
}
}
return;
}
public static void main(String[] args) {
try {
InetAddress localAddress = InetAddress.getLocalHost();
serverID = localAddress.toString().split("\\.")[0];
myPort = 9037 + Integer.parseInt(serverID.substring(2,4));
System.out.println("Server | Port ===> " + serverID + " | " + myPort);
if (Integer.parseInt(serverID.substring(2,4)) >= 4) {
activeClients = 0;
}
} catch (UnknownHostException except) {
System.err.println("Host Unknown");
except.printStackTrace();
}
tokenSemaphore = new Semaphore(1);
pqSemaphore = new Semaphore(1);
// Create and start a server thread
// Thread serverThread = new Thread(() -> new Server().startServer(myPort));
Thread serverThread = new Thread(new Runnable() {
@Override
public void run() {
new Server().startServer(myPort);
}
});
serverThread.start();
Thread connectParentThread = new Thread(new Runnable() {
@Override
public void run() {
new Server().connectTreeParent();
}
});
connectParentThread.start();
Thread grantOperationsThread = new Thread(new Runnable() {
@Override
public void run() {
new Server().grantOperations();
}
});
grantOperationsThread.start();
try {
grantOperationsThread.join();
connectParentThread.join();
serverSocket.close();
serverThread.join();
System.out.println("\033[1;31mAll threads joined, completing process!\033[0m");
out.println("Total incoming messages: " + incomingMessages + ", outgoing messagest: " + outGoingMessages + ".");
System.exit(0);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}
class Request implements Serializable {
int clientID;
Timestamp requestTimestamp;
boolean status = false;
}