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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
|
package derms.replica3;
import derms.*;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import derms.replica3.Logger;
public class Replica3 implements Replica{
static final InetSocketAddress announceGroup = new InetSocketAddress("225.5.5.5", 5555);
private String serverID;
private HashMap<String, HashMap<String, Resource>> resources;
private HashMap<String, List<Resource>> coordinatorResources ;
private HashMap<String, List<String>> resourceWaitingQueues;
private List<String> serverList = Arrays.asList(Constants.MONTREAL, Constants.QUEBEC, Constants.SHERBROOKE);
private Logger activityLogger;
// final InetAddress localAddr;
// private final Logger log;
private boolean alive;
private final ReplicaManager replicaManager;
public Replica3(City city, ReplicaManager replicaManager) throws IOException {
this.serverID = city.getCode();
this.resources = new HashMap<>();
coordinatorResources = new HashMap<>();
resourceWaitingQueues = new HashMap<>();
// this.log = DermsLogger.getLogger(getClass());
this.replicaManager = replicaManager;
this.activityLogger = new Logger( serverID + "Server.log");
// log.info("Running");
// log.config("Local address is "+localAddr.toString());
this.alive = true;
}
public Replica3(String city, ReplicaManager replicaManager) throws IOException {
this(new City(city), replicaManager);
}
@Override
public boolean isAlive() { return alive; }
@Override
public void startProcess(int byzantine, int crash) {
// TODO
// log.info(getClass().getSimpleName() + " started.");
System.out.println("process started");
}
@Override
public void processRequest(Request request) {
// log.info(request.toString());
System.out.println("process request and good");
String status = "";
try {
switch (request.getFunction()) {
case "addResource":
status = addResource(request.getResourceID(), request.getResourceType(), request.getDuration());
break;
case "removeResource":
status = removeResource(request.getResourceID(), request.getDuration());
break;
case "listResourceAvailability":
status = listResourceAvailability(request.getResourceType());
break;
case "requestResource":
status = requestResource(request.getClientID(), request.getResourceID(), request.getDuration());
break;
case "findResource":
status = findResource(request.getClientID(), request.getResourceType());
break;
case "returnResource":
status = returnResource(request.getClientID(), request.getResourceID());
break;
case "swapResource":
status = swapResource(request.getClientID(),request.getOldResourceID(), request.getOldResourceType(), request.getResourceID(), request.getResourceType());
break;
default:
status = "Failure: unknown function '" + request.getFunction() + "'";
}
} catch (Exception e) {
// log.warning(e.getMessage());
status = "Failure: " + request.getFunction() + ": " + e.getMessage();
}
Response response = new Response(request, replicaManager.getReplicaId(), status, false); // TODO
// log.info("Processed request " + request + "; response: " + response);
replicaManager.sendResponseToFE(response);
}
@Override
public void restart() {
// TODO
shutdown();
startProcess(0, 0);
}
@Override
public int getId() { return 3; }
private void shutdown() {
// TODO
}
public synchronized String addResource(String resourceID, String resourceName, int duration) {
// Check if the resource name already exists
String message = "";
// boolean isIdValid = ValidationService.isResourceIDValid(resourceID);
// boolean isNameValid = ValidationService.isResourceNameValid(resourceName);
//
// if(!(isIdValid && isNameValid)) {
// message = "Input is not valid for ID: " + resourceID + " " + resourceName + " " + duration;
// activityLogger.log(" ADD RESOURCE (" + resourceID + ", " + resourceName + ", " + duration + ") " , " FAILED ", message);
// return message;
// }
HashMap<String, Resource> resourceMap = resources.get(resourceName);
if (resourceMap == null) {
resourceMap = new HashMap<>(); // Create a new inner HashMap if it doesn't exist
resources.put(resourceName, resourceMap);
}
Resource resource = resourceMap.get(resourceID);
if (resource != null) {
// If the resource already exists, update its duration
if (duration > resource.getDuration()) {
resource.setDuration(duration);
}
message = "Resource already exist! So duration is updated: " + resourceID + " : " + resourceName + " : " + duration;
} else {
// Add the new resource
resource = new Resource(resourceID, resourceName, duration);
resourceMap.put(resourceID, resource);
message = "New Resource of " + resourceName +" is added: " + resourceID + " : " + resourceName + " : " + duration;
}
// if(resourceWaitingQueues.containsKey(resourceID)) {
// List<String> coordinators = resourceWaitingQueues.get(resourceID);
// for(int i=0; i<coordinators.size(); i++) {
// if(coordinators.get(i).split("-").length > 1) {
// String crID = coordinators.get(i).split("-")[0];
// int dur = Integer.parseInt(coordinators.get(i).split("-")[1]);
// Resource res = resourceMap.get(resourceID);
//
// if(res.getDuration() >= dur) {
//// activityLogger.log(" ADD RESOURCE - Resource allocation to waiting coordinator", " COMPLETED ", "Resource " +
//// resourceID + " is allocated to waiting coordinator " + crID);
// res.setDuration(res.getDuration() - dur);
// resourceMap.put(resourceID, res);
// coordinators.remove(i);
// break;
// }
// }
//
// }
// }
// activityLogger.log(" ADD RESOURCE (" + resourceID + ", " + resourceName + ", " + duration + ") " , " COMPLETED ", message);
return message;
}
public synchronized String removeResource(String resourceID, int duration) {
String message = "";
// boolean isIdValid = ValidationService.isResourceIDValid(resourceID);
//
// if(!isIdValid ) {
// message = "Input is not valid for ID: " + resourceID + " " + duration;
// activityLogger.log(" ADD RESOURCE (" + resourceID + ", " + duration + ") " , " FAILED ", message);
// return message;
// }
for (HashMap<String, Resource> resourceMap : resources.values()) {
Resource resource = resourceMap.get(resourceID);
if (resource != null) {
if (duration >= resource.getDuration()) {
resourceMap.remove(resourceID); // Remove if duration is fully used
message = "Resource of ID: " + resourceID + " with duration " + resource.getDuration() + " is successfully removed.";
activityLogger.log(" REMOVE RESOURCE (" + resourceID + ", " + duration + ") " , " COMPLETED ", message);
return message;
} else {
resource.setDuration(resource.getDuration() - duration); // Reduce the duration
message = "Duration of the Resource of ID: " + resourceID + " with duration " + duration + " is successfully reduced.";
activityLogger.log(" REMOVE RESOURCE (" + resourceID + ", " + duration + ") " , " COMPLETED ", message);
return message;
}
}
}
message = "Resource with ID " + resourceID + " is not found!";
activityLogger.log(" REMOVE RESOURCE (" + resourceID + ", " + duration + ") " , " FAILED ", message);
return "Resource not found";
}
public synchronized String listResourceAvailability(String resourceName) {
String message = "";
// boolean isNameValid = ValidationService.isResourceNameValid(resourceName);
//
// if(!isNameValid ) {
// message = "Input is not valid for name: " + resourceName;
// activityLogger.log(" LIST RESOURCE AVAILABILITY (" + resourceName + ") " , " FAILED ", message);
// return message;
// }
final StringBuilder result = new StringBuilder();
result.append(resourceName.toUpperCase());
CountDownLatch latch = new CountDownLatch(serverList.size() - 1);
for(String server: serverList) {
System.out.println(serverID + " " + server);
if(serverID.equalsIgnoreCase(server)) {
result.append(getAvailableResources(resourceName));
System.out.println("listResourceAvailability same server:" + result);
}else {
System.out.println("listResourceAvailability request region:" + result);
new Thread(() -> {
System.out.println("listResourceAvailability Thread:" + result);
final String count = sendResourceAvailabilityRequest(server, resourceName);
System.out.println(count);
if(count != null)
synchronized(result) {
result.append(count);
}
latch.countDown();
}).start();
}
}
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
activityLogger.log(" LIST RESOURCE AVAILABILITY (" + resourceName + ") " , " COMPLETED ", result.toString());
System.out.println("printing: " + result.toString());
return result.toString().trim();
}
public String getAvailableResources(String resourceName) {
String result = "";
HashMap<String, Resource> resourceMap = resources.get(resourceName);
if (resourceMap == null) {
return result;
}
for (Resource resource : resourceMap.values()) {
result += " " + resource.getResourceID() + "-" + resource.getDuration();
}
return result;
}
private String sendResourceAvailabilityRequest(final String location, String resourceName) {
System.out.println("sendResourceAvailabilityRequest:");
String recordCount = "";
try {
final InetAddress inetAddress = InetAddress.getByName("localhost");
activityLogger.log(location, " UPD MESSAGE SENT ", "UPD Message sent for Resource Availability");
final DatagramSocket socket = new DatagramSocket();
byte[] data = (serverID + "-GET_AVAILABLE_RESOURCES" + "-" + resourceName ).toString().getBytes();
final DatagramPacket packet = new DatagramPacket(data, data.length, inetAddress, PortConstants.getUdpPort(location)) ;
socket.send(packet);
data = new byte[1000];
DatagramPacket responsePacket = new DatagramPacket(data, data.length);
socket.receive(responsePacket);
recordCount = " " + new String(responsePacket.getData(), 0, responsePacket.getLength()).trim();
socket.close();
} catch (Exception e) {
activityLogger.log("", "EXCEPTION", e.getMessage());
}
return recordCount;
}
public synchronized String requestResource(String coordinatorID, String resourceID, int duration) {
// Iterate through the outer HashMap to find the resource by its ID
String message = "";
final StringBuilder result = new StringBuilder();
// boolean isUserIdValid = ValidationService.isUserIDValid(coordinatorID);
// boolean isIdValid = ValidationService.isResourceIDValid(resourceID);
//
// if(!(isIdValid && isUserIdValid)) {
// message = "Input is not valid for ID: " + resourceID + " " + resourceID + " " + duration;
// activityLogger.log(" ADD RESOURCE (" + resourceID + ", " + resourceID + ", " + duration + ") " , " FAILED ", message);
// return message;
// }
CountDownLatch latch = new CountDownLatch(1);
for (HashMap<String, Resource> resourceMap : resources.values()) {
Resource resource = resourceMap.get(resourceID);
if (resource != null && resource.getDuration() >= duration) {
// If resource is found and has sufficient duration, reduce it
resource.setDuration(resource.getDuration() - duration);
if(coordinatorResources.containsKey(coordinatorID)) {
Resource rs = new Resource(resourceID, resource.getResourceName(), duration);
coordinatorResources.get(coordinatorID).add(rs);
}else {
Resource rs = new Resource(resourceID, resource.getResourceName(), duration);
List<Resource> lst = new ArrayList<Resource>();
lst.add(rs);
System.out.println(lst);
coordinatorResources.put(coordinatorID, lst);
System.out.println(coordinatorResources.keySet());
}
message = "Coordinator of ID " + coordinatorID + " borrowed resource of ID " + resourceID + " " + "from same server";
activityLogger.log(" REQUEST RESOURCE (" + coordinatorID + ", " + resourceID + ", " + duration + ") " , " COMPLETED ", message);
return message;
} else if (resource != null) {
message = "Insufficient resource duration for resource of ID " + resourceID + " " + " in the same server" ;
activityLogger.log(" REQUEST RESOURCE (" + coordinatorID + ", " + resourceID + ", " + duration + ") " , " FAILED ", message);
if(resourceWaitingQueues.containsKey(resourceID)) {
resourceWaitingQueues.get(resourceID).add(coordinatorID + "-" + duration);
}else {
List<String> lst = new ArrayList<String>();
lst.add(coordinatorID + "-" + duration);
resourceWaitingQueues.put(resourceID, lst);
}
return message;
}
}
for(String server: serverList) {
if(!server.equalsIgnoreCase(serverID) && resourceID.substring(0,3).equalsIgnoreCase(server)) {
new Thread(() -> {
final String count = sendResourceRequest(server, resourceID, duration);
if(count != null) {
if(count.trim().length() > 0) {
if(coordinatorResources.containsKey(coordinatorID)) {
Resource rs = new Resource(resourceID, "", duration);
coordinatorResources.get(coordinatorID).add(rs);
}else {
List<Resource> lst = new ArrayList<Resource>();
Resource rs = new Resource(resourceID, "", duration);
lst.add(rs);
coordinatorResources.put(coordinatorID, lst);
}
}else {
if(resourceWaitingQueues.containsKey(resourceID)) {
resourceWaitingQueues.get(resourceID).add(coordinatorID);
}else {
List<String> lst = new ArrayList<String>();
lst.add(coordinatorID);
resourceWaitingQueues.put(resourceID, lst);
}
}
synchronized(result) {
result.append(count);
}
}
latch.countDown();
}).start();
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println(coordinatorResources.toString());
// Request to other servers for getting specified resource
if(result.toString().trim().length() == 0) {
message = "Requested Resource of ID " + resourceID + " by coordinator " + coordinatorID + " is not found! so Coordinator is added to waiting queue!";
activityLogger.log(" REQUEST RESOURCE (" + coordinatorID + ", " + resourceID + ", " + duration + ") " , " COMPLETED ", message);
return message;
}
activityLogger.log(" REQUEST RESOURCE (" + coordinatorID + ", " + resourceID + ", " + duration + ") " , " COMPLETED ", message);
return result.toString();
}
private String sendResourceRequest(final String location, String resourceID, int duration) {
String recordCount = "";
try {
final InetAddress inetAddress = InetAddress.getByName("localhost");
// activityLogger.log(location, " UPD MESSAGE SENT ", "UPD Message sent for Request Resource");
final DatagramSocket socket = new DatagramSocket();
byte[] data = (serverID + "-GET_RESOURCE" + "-" + resourceID + "-" + duration).toString().getBytes();
final DatagramPacket packet = new DatagramPacket(data, data.length, inetAddress, PortConstants.getUdpPort(location)) ;
socket.send(packet);
System.out.println("inside the request udp: "+ location + " "+ data.toString());
data = new byte[1000];
DatagramPacket responsePacket = new DatagramPacket(data, data.length);
socket.receive(responsePacket);
recordCount = " " + new String(responsePacket.getData(), 0, responsePacket.getLength()).trim();
System.out.println("after received the request udp: "+ location + " "+ data.toString());
socket.close();
} catch (Exception e) {
}
return recordCount;
}
public String getRequestedResource(String id, int duration) {
String message = "";
for (HashMap<String, Resource> resourceMap : resources.values()) {
Resource resource = resourceMap.get(id);
if (resource != null) {
if(resource.getDuration() >= duration) {
resource.setDuration(resource.getDuration() - duration);
message = "Resource of ID: " + id + " is borrowed successfully and duration is updated to " + resource.getDuration() + " in " + serverID + "server" ;
activityLogger.log("REQUEST RESOURCE", " COMPLETED " , message);
return message;
}
}
}
message = "Resource of ID: " + id + " is not available to borrow!";
activityLogger.log("REQUEST RESOURCE", " FAILED " , message);
return "";
}
public String findResource(String coordinatorID, String resourceName){
// Retrieve the inner HashMap for the resource name (e.g., "AMBULANCE")
final StringBuilder result = new StringBuilder();
String message = "";
// boolean isUserIdValid = ValidationService.isUserIDValid(coordinatorID);
// boolean isNameValid = ValidationService.isResourceNameValid(resourceName);
//
// if(!(isNameValid && isUserIdValid)) {
// message = "Input is not valid for : " + coordinatorID + " " + resourceName;
// activityLogger.log(" FIND RESOURCE (" + coordinatorID + ", " + resourceName + ") " , " FAILED ", message);
// return message;
// }
result.append(resourceName.toUpperCase());
int startLen = result.length();
HashMap<String, Resource> specifiedResources = resources.get(resourceName);
if(specifiedResources != null) {
if(coordinatorResources.containsKey(coordinatorID)) {
for(Resource res: coordinatorResources.get(coordinatorID)) {
System.out.println("resID: " + res.getResourceID());
if(specifiedResources.containsKey(res.getResourceID())) {
result.append(" " + res.getResourceID() + "-" + res.getDuration());
}
}
}
}
CountDownLatch latch = new CountDownLatch(serverList.size() - 1);
final StringBuilder response = new StringBuilder();
for(String server: serverList) {
if(!serverID.equalsIgnoreCase(server)) {
new Thread(() -> {
final String count = sendResourceAvailabilityRequest(server, resourceName);
if(count != null)
synchronized(result) {
response.append(count);
}
latch.countDown();
}).start();
}
}
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
String responseString = response.toString().trim();
String[] resources = responseString.split(" ");
if (resources != null) {
for (String resource : resources) {
// Split each resource by '-'
String[] parts = resource.split("-");
if (parts.length == 2) {
String resourceID = parts[0];
String count = parts[1];
if(coordinatorResources.containsKey(coordinatorID)) {
for(Resource res: coordinatorResources.get(coordinatorID)) {
if(resourceID.equalsIgnoreCase(res.getResourceID())) {
result.append(" " + resourceID + "-" + res.getDuration());
}
}
}
}
}
}
if(startLen == result.length()) {
message = "Resources of " + resourceName+ " occupied by coordinator " + coordinatorID + " are not found!";
activityLogger.log(" FIND RESOURCE (" + coordinatorID + ") " , " COMPLETED ", message);
return message;
}
message = "Resources of " + resourceName+ " occupied by coordinator " + coordinatorID + " are returned successfully!";
activityLogger.log(" FIND RESOURCE (" + coordinatorID + ") " , " COMPLETED ", message);
System.out.println(result);
return result.toString();
}
public synchronized String returnResource(String coordinatorID, String resourceID){
// Iterate through the outer HashMap to find the resource by its ID
String message = "";
// boolean isUserIdValid = ValidationService.isUserIDValid(coordinatorID);
// boolean isIDValid = ValidationService.isResourceIDValid(resourceID);
//
// if(!(isIDValid && isUserIdValid)) {
// message = "Input is not valid for : " + coordinatorID + " " + resourceID;
// activityLogger.log(" RETURN RESOURCE (" + coordinatorID + ", " + resourceID + ") " , " FAILED ", message);
// return message;
// }
// for (HashMap<String, Resource> resourceMap : resources.values()) {
// Resource resource = resourceMap.get(resourceID);
// if (resource != null) {
// Perform any logic to return the resource
if(coordinatorResources.containsKey(coordinatorID)) {
for(Resource res: coordinatorResources.get(coordinatorID)) {
if(res.getResourceID().equalsIgnoreCase(resourceID)) {
message = "Resource " + resourceID + "-" + res.getDuration() + " returned successfully to coordinator " + coordinatorID;
activityLogger.log(" RETURN RESOURCE (" + coordinatorID + ", " + resourceID + ") " , " COMPLETED ", message);
return message;
}
}
}
// }
// }
message = "Resource of ID: " + resourceID + " not found.";
activityLogger.log(" RETURN RESOURCE (" + coordinatorID + ", " + resourceID + ") " , " FAILED ", message);
return message;
}
public synchronized String swapResource(String coordinatorID, String oldResourceID, String oldResourceType, String newResourceID, String newResourceType) {
String message = "";
int durationRequest = 0;
String returnedResource = returnResource(coordinatorID, oldResourceID);
if(returnedResource.contains("not found")) {
message = "Coordinator of ID: " + coordinatorID + " didn't acquired Resource of ID: " + oldResourceID + ".";
// activityLogger.log(" SWAP RESOURCE (" + coordinatorID + ", " + oldResourceID + ") " , " FAILED ", message);
return message;
}
if(coordinatorResources.containsKey(coordinatorID)) {
for(Resource res: coordinatorResources.get(coordinatorID)) {
if(res.getResourceID().equals(oldResourceID)) {
durationRequest = res.getDuration();
coordinatorResources.get(coordinatorID).remove(res);
break;
}
}
}
String requestedResource = requestResource(coordinatorID, newResourceID, durationRequest);
if(requestedResource.contains("not found")) {
message = "Coordinator of ID: " + coordinatorID + " couldn't acquire Resource of ID: " + newResourceID + ".";
activityLogger.log(" SWAP RESOURCE (" + coordinatorID + ", " + oldResourceID + ") " , " FAILED ", message);
return message;
}
message = "Coordinator of ID: " + coordinatorID + " Successfully swapped old resource " + oldResourceID + " with new resource " + newResourceID;
// activityLogger.log(" SWAP RESOURCE (" + coordinatorID + ", " + oldResourceID + ", " + oldResourceType + ", " + newResourceID + ", " + newResourceType + ", "+ ") ",
// " COMPLETED ", message);
return message;
}
}
|