summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBMatajsz <90217645+BMatajsz@users.noreply.github.com>2024-12-03 10:27:54 -0500
committerBMatajsz <90217645+BMatajsz@users.noreply.github.com>2024-12-03 10:27:54 -0500
commit298eb839a8ccdd625978cf51476a6c412d11902b (patch)
tree99571b2234f546b6a5b536d27dac086d1ec99433
parentd0b0b50f18c4713814c565af2f3146ec82aa6b0f (diff)
parent1a2c519e5e70cc1e31eaef5cd770da75d75d6654 (diff)
downloadsoen423-298eb839a8ccdd625978cf51476a6c412d11902b.zip
Merge branch 'test' of https://github.com/sam-rba/soen423 into test
-rw-r--r--MTL_log.txt7
-rw-r--r--src/main/java/derms/client/ResponderClient.java97
-rw-r--r--src/main/java/derms/client/ResponderClientCLI.java103
-rw-r--r--src/main/java/derms/replica1/CoordinatorClient.java4
-rw-r--r--src/main/java/derms/replica1/DERMSServer.java23
-rw-r--r--src/main/java/derms/replica1/DERMSServerPublisher.java22
-rw-r--r--src/main/java/derms/replica1/Replica1.java69
-rw-r--r--src/main/java/derms/replica1/ResponderClient.java4
-rw-r--r--src/test/java/derms/test/SystemTest.java24
9 files changed, 219 insertions, 134 deletions
diff --git a/MTL_log.txt b/MTL_log.txt
new file mode 100644
index 0000000..36d7665
--- /dev/null
+++ b/MTL_log.txt
@@ -0,0 +1,7 @@
+2024-12-03T08:07:04.053 - listResourceAvailability - ambulance - []
+2024-12-03T08:07:44.514 - listResourceAvailability - ambulance - []
+2024-12-03T08:36:28.057 - listResourceAvailability - ambulance - []
+2024-12-03T08:36:42.466 - listResourceAvailability - ambulance - []
+2024-12-03T08:37:05.489 - listResourceAvailability - ambulance - []
+2024-12-03T08:57:57.627 - addResource - MTL1233 - ambulance - 10 - Invalid resource type.
+2024-12-03T08:58:39.878 - addResource - MTL1223 - AMBULANCE - 10 - derms.Resource added successfully.
diff --git a/src/main/java/derms/client/ResponderClient.java b/src/main/java/derms/client/ResponderClient.java
index 43be3f3..4db30c8 100644
--- a/src/main/java/derms/client/ResponderClient.java
+++ b/src/main/java/derms/client/ResponderClient.java
@@ -6,105 +6,22 @@ import derms.util.TestLogger;
import java.net.MalformedURLException;
import java.util.Objects;
-public class ResponderClient extends CLI {
- public static final String usage = "Usage: java derms.client.ResponderClient <FE host>";
-
+public class ResponderClient {
private final DERMSInterface server;
public ResponderClient(String FEhost) throws MalformedURLException {
server = Client.connectToServer(FEhost);
-
- commands.put("add", new Add());
- cmdDescriptions.add(new Description(
- "add <resource ID> <resource type> <duration>",
- "Add ad resource to the server"));
-
- commands.put("remove", new Remove());
- cmdDescriptions.add(new Description(
- "remove <resource ID> <duration>",
- "Decrease the duration of a resource. If duration is negative, the resource is removed entirely."));
-
- commands.put("list", new List());
- cmdDescriptions.add(new Description(
- "list <resource name>",
- "List available resources"));
}
- public static void main(String[] args) {
- if (args.length < 1) {
- System.err.println(usage);
- System.exit(1);
- }
-
- String FEhost = args[0];
-
- try {
- (new ResponderClient(FEhost)).run();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
+ public String addResource(String resourceID, String resourceName, int duration) {
+ return server.addResource(resourceID, resourceName, duration);
}
- public class Add implements Command {
- @Override
- public void exec(String[] args) {
- if (args.length < 3)
- System.out.println("invalid arguments for 'add'");
- else
- add(args[0], args[1], args[2]);
- }
-
- public void add(String resourceID, String resourceName, String durationStr) {
- try {
- int duration = Integer.parseInt(durationStr);
- if (duration < 0) {
- throw new NumberFormatException("duration less than 0");
- }
- String response = server.addResource(resourceID, resourceName, duration);
- System.out.println(response);
- if (response.contains("Fail")) {
- TestLogger.log("[FAILED: " + response + "]");
- } else {
- TestLogger.log("[SUCCESS: " + response + "]");
- }
- } catch (NumberFormatException e) {
- System.out.println("invalid duration: " + durationStr);
- }
- }
+ public String removeResource(String resourceID, int duration) {
+ return server.removeResource(resourceID, duration);
}
- private class Remove implements Command {
- @Override
- public void exec(String[] args) {
- if (args.length < 2)
- System.out.println("invalid arguments for 'remove'");
- else
- remove(args[0], args[1]);
- }
-
- private void remove(String resourceID, String durationStr) {
- try {
- int duration = Integer.parseInt(durationStr);
- String response = server.removeResource(resourceID, duration);
- System.out.println(response);
- } catch (NumberFormatException e) {
- System.out.println("invalid duration: " + durationStr);
- }
- }
- }
-
- private class List implements Command {
- @Override
- public void exec(String[] args) {
- if (args.length < 1)
- System.out.println("invalid arguments for 'list'");
- else
- list(args[0]);
- }
-
- private void list(String resourceName) {
- String response = server.listResourceAvailability(resourceName);
- System.out.println(response);
- }
+ public String listResourceAvailability(String resourceName) {
+ return server.listResourceAvailability(resourceName);
}
}
diff --git a/src/main/java/derms/client/ResponderClientCLI.java b/src/main/java/derms/client/ResponderClientCLI.java
new file mode 100644
index 0000000..86a5a9a
--- /dev/null
+++ b/src/main/java/derms/client/ResponderClientCLI.java
@@ -0,0 +1,103 @@
+package derms.client;
+
+import derms.frontend.DERMSInterface;
+
+import java.net.MalformedURLException;
+
+public class ResponderClientCLI extends CLI {
+ public static final String usage = "Usage: java derms.client.ResponderClient <FE host>";
+
+ private final DERMSInterface server;
+
+ private ResponderClientCLI(String FEhost) throws MalformedURLException {
+ server = Client.connectToServer(FEhost);
+
+ commands.put("add", new Add());
+ cmdDescriptions.add(new CLI.Description(
+ "add <resource ID> <resource type> <duration>",
+ "Add ad resource to the server"));
+
+ commands.put("remove", new Remove());
+ cmdDescriptions.add(new CLI.Description(
+ "remove <resource ID> <duration>",
+ "Decrease the duration of a resource. If duration is negative, the resource is removed entirely."));
+
+ commands.put("list", new List());
+ cmdDescriptions.add(new CLI.Description(
+ "list <resource name>",
+ "List available resources"));
+ }
+
+ public static void main(String[] args) {
+ if (args.length < 1) {
+ System.err.println(usage);
+ System.exit(1);
+ }
+
+ String FEhost = args[0];
+
+ try {
+ (new ResponderClientCLI(FEhost)).run();
+ } catch (MalformedURLException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private class Add implements CLI.Command {
+ @Override
+ public void exec(String[] args) {
+ if (args.length < 3)
+ System.out.println("invalid arguments for 'add'");
+ else
+ add(args[0], args[1], args[2]);
+ }
+
+ private void add(String resourceID, String resourceName, String durationStr) {
+ try {
+ int duration = Integer.parseInt(durationStr);
+ if (duration < 0) {
+ throw new NumberFormatException("duration less than 0");
+ }
+ String response = server.addResource(resourceID, resourceName, duration);
+ System.out.println(response);
+ } catch (NumberFormatException e) {
+ System.out.println("invalid duration: " + durationStr);
+ }
+ }
+ }
+
+ private class Remove implements CLI.Command {
+ @Override
+ public void exec(String[] args) {
+ if (args.length < 2)
+ System.out.println("invalid arguments for 'remove'");
+ else
+ remove(args[0], args[1]);
+ }
+
+ private void remove(String resourceID, String durationStr) {
+ try {
+ int duration = Integer.parseInt(durationStr);
+ String response = server.removeResource(resourceID, duration);
+ System.out.println(response);
+ } catch (NumberFormatException e) {
+ System.out.println("invalid duration: " + durationStr);
+ }
+ }
+ }
+
+ private class List implements CLI.Command {
+ @Override
+ public void exec(String[] args) {
+ if (args.length < 1)
+ System.out.println("invalid arguments for 'list'");
+ else
+ list(args[0]);
+ }
+
+ private void list(String resourceName) {
+ String response = server.listResourceAvailability(resourceName);
+ System.out.println(response);
+ }
+ }
+}
diff --git a/src/main/java/derms/replica1/CoordinatorClient.java b/src/main/java/derms/replica1/CoordinatorClient.java
index 23b5a01..3793542 100644
--- a/src/main/java/derms/replica1/CoordinatorClient.java
+++ b/src/main/java/derms/replica1/CoordinatorClient.java
@@ -56,13 +56,15 @@ public class CoordinatorClient {
return Collections.emptyList();
}
- public void returnResource(String coordinatorID, String resourceID) {
+ public String returnResource(String coordinatorID, String resourceID) {
try {
String response = server.returnResource(coordinatorID, resourceID);
System.out.println(response);
logOperation("returnResource", coordinatorID, resourceID, response);
+ return response;
} catch (Exception e) {
e.printStackTrace();
+ throw new RuntimeException(e);
}
}
public String swapResource(String coordinatorID, String oldResourceID, String oldResourceType, String newResourceID, String newResourceType) {
diff --git a/src/main/java/derms/replica1/DERMSServer.java b/src/main/java/derms/replica1/DERMSServer.java
index 72849a1..585a946 100644
--- a/src/main/java/derms/replica1/DERMSServer.java
+++ b/src/main/java/derms/replica1/DERMSServer.java
@@ -22,17 +22,22 @@ public class DERMSServer implements DERMSInterface {
private static List<String> cities = Arrays.asList("MTL", "QUE", "SHE");
private static List<String> resourceNames = Arrays.asList("AMBULANCE", "FIRETRUCK", "PERSONNEL");
+ private final Random r = new Random();
+ private final Map<String, Integer> portsMap = new HashMap<String, Integer>() {{
+ put("MTL", r.nextInt(60000-8000) + 8000);
+ put("QUE", r.nextInt(60000-8000) + 8000);
+ put("SHE", r.nextInt(60000-8000) + 8000);
+ }};
public DERMSServer() {
// Default constructor to support JAX-WS
- super();
- this.serverID = "MTL";
- resources = new HashMap<>();
- new Thread(this::listenForMessages).start();
+// super();
+// this.serverID = "MTL";
+// resources = new HashMap<>();
+// new Thread(this::listenForMessages).start();
}
public DERMSServer(String serverID) throws InterruptedException {
- super();
this.serverID = serverID;
resources = new HashMap<>();
new Thread(this::listenForMessages).start();
@@ -40,7 +45,7 @@ public class DERMSServer implements DERMSInterface {
}
private void listenForMessages() {
- try (DatagramSocket socket = new DatagramSocket()) {
+ try (DatagramSocket socket = new DatagramSocket(getServerPortsFromCentralRepo().get(serverID))) {
this.serverPort.set(socket.getLocalPort());
byte[] buffer = new byte[1024];
@@ -216,11 +221,7 @@ public class DERMSServer implements DERMSInterface {
private Map<String, Integer> getServerPortsFromCentralRepo() {
// Mocking response as web services since no derms.CentralRepoInterface.
- return new HashMap<String, Integer>() {{
- put("MTL", 4321);
- put("QUE", 4322);
- put("SHE", 4323);
- }};
+ return new HashMap<>(portsMap);
}
@Override
diff --git a/src/main/java/derms/replica1/DERMSServerPublisher.java b/src/main/java/derms/replica1/DERMSServerPublisher.java
index 9b5ba91..442f844 100644
--- a/src/main/java/derms/replica1/DERMSServerPublisher.java
+++ b/src/main/java/derms/replica1/DERMSServerPublisher.java
@@ -2,22 +2,26 @@ package derms.replica1;
import javax.xml.ws.Endpoint;
-import derms.frontend.DERMSServerImpl;
-
public class DERMSServerPublisher {
- private static Endpoint endpoint;
+ private static Endpoint[] endpoints = new Endpoint[3];
public static void main(String[] args) {
- // Publish the web service
- endpoint = Endpoint.publish("http://127.0.0.1:8387/ws/derms", new DERMSServerImpl());
- System.out.println("DERMS Server is published at http://127.0.0.1:8387/ws/derms");
+ try {
+ endpoints[0] = Endpoint.publish("http://localhost:8387/ws/derms", new DERMSServer("MTL"));
+ endpoints[1] = Endpoint.publish("http://localhost:8081/ws/derms", new DERMSServer("QUE"));
+ endpoints[3] = Endpoint.publish("http://localhost:8082/ws/derms", new DERMSServer("SHE"));
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
}
public static void stop() {
- if (endpoint != null && endpoint.isPublished()) {
- endpoint.stop();
- System.out.println("DERMS Server is stopped.");
+ for (Endpoint endpoint : endpoints) {
+ if (endpoint != null && endpoint.isPublished()) {
+ endpoint.stop();
+ System.out.println("DERMS Server is stopped.");
+ }
}
}
} \ No newline at end of file
diff --git a/src/main/java/derms/replica1/Replica1.java b/src/main/java/derms/replica1/Replica1.java
index dd5b545..c9e080d 100644
--- a/src/main/java/derms/replica1/Replica1.java
+++ b/src/main/java/derms/replica1/Replica1.java
@@ -22,7 +22,11 @@ public class Replica1 implements Replica {
private final Logger log;
private final InetAddress localAddr;
private final ResponderClient responderClient;
+ private final CoordinatorClient coordinatorClient;
+ private final String responderClientID = "MTL";
+ private final String coordinatorClientID = "MTLC1111";
private final ReplicaManager replicaManager;
+ private DERMSServer server;
private boolean byzFailure;
public Replica1(ReplicaManager replicaManager) {
@@ -33,7 +37,8 @@ public class Replica1 implements Replica {
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
- responderClient = new ResponderClient("MTL");
+ responderClient = new ResponderClient(responderClientID);
+ coordinatorClient = new CoordinatorClient(coordinatorClientID);
try {
this.log = DermsLogger.getLogger(getClass());
} catch (IOException e) {
@@ -62,8 +67,22 @@ public class Replica1 implements Replica {
byzFailure = false;
}
- pool.execute(DERMSServer::new);
- //alive = true;
+ try {
+ server = new DERMSServer("MTL");
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ try {
+ new DERMSServer("SHE");
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ try {
+ new DERMSServer("QUE");
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+
log.info(getClass().getSimpleName() + " started.");
log.config("Local address is "+localAddr.toString());
}
@@ -77,12 +96,44 @@ public class Replica1 implements Replica {
return;
}
- String status = responderClient.addResource(
- request.getResourceID(),
- request.getResourceType(),
- request.getDuration()
- );
- Response response = new Response(request.getSequenceNumber(), status);
+ log.info(request.toString());
+
+ String status = "";
+ boolean isSuccess = true;
+ try {
+ switch (request.getFunction()) {
+ case "addResource":
+ status = server.addResource(request.getResourceID(), request.getResourceType(), request.getDuration());
+ break;
+ case "removeResource":
+ status = server.removeResource(request.getResourceID(), request.getDuration());
+ break;
+ case "listResourceAvailability":
+// status = String.join(",", responderClient.listResourceAvailability(request.getResourceType()));
+ status = String.join(",", server.listResourceAvailability(request.getResourceType()));
+ break;
+ case "requestResource":
+ status = server.requestResource(coordinatorClientID, request.getResourceID(), request.getDuration());
+ break;
+ case "findResource":
+ status = String.join(",", server.findResource(coordinatorClientID, request.getResourceType()));
+ break;
+ case "returnResource":
+ status = server.returnResource(coordinatorClientID, request.getResourceID());
+ break;
+ case "swapResource":
+ status = server.swapResource(coordinatorClientID, 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();
+ isSuccess = false;
+ }
+
+ Response response = new Response(request, replicaManager.getReplicaId(), status, isSuccess); // TODO: isSuccess flag
log.info("Processed request " + request + "; response: " + response);
replicaManager.sendResponseToFE(response);
}
diff --git a/src/main/java/derms/replica1/ResponderClient.java b/src/main/java/derms/replica1/ResponderClient.java
index c022fd3..13adc73 100644
--- a/src/main/java/derms/replica1/ResponderClient.java
+++ b/src/main/java/derms/replica1/ResponderClient.java
@@ -59,13 +59,15 @@ public class ResponderClient {
}
}
- public void removeResource(String resourceID, int duration) {
+ public String removeResource(String resourceID, int duration) {
try {
String response = server.removeResource(resourceID, duration);
System.out.println(response);
logOperation("removeResource", resourceID, duration, response);
+ return response;
} catch (Exception e) {
e.printStackTrace();
+ throw new RuntimeException(e);
}
}
diff --git a/src/test/java/derms/test/SystemTest.java b/src/test/java/derms/test/SystemTest.java
index 2112ce9..59b08fa 100644
--- a/src/test/java/derms/test/SystemTest.java
+++ b/src/test/java/derms/test/SystemTest.java
@@ -9,6 +9,8 @@ import derms.replica1.DERMSServerPublisher;
import org.junit.jupiter.api.*;
import java.io.*;
import java.net.MalformedURLException;
+import java.net.InetAddress;
+import java.net.NetworkInterface;
import java.nio.file.*;
import java.util.*;
import derms.util.*;
@@ -27,7 +29,6 @@ class SystemTest {
// [TODO]
// input IP and NET config
private static String IP = "127.0.0.1";
- private static String NET = "en0";
@BeforeEach
void clearLogFile() throws IOException {
@@ -37,7 +38,6 @@ class SystemTest {
@BeforeAll
static void runMains() throws IOException {
String[] argsFE = {IP, IP};
- String[] argsSQ = {IP, NET};
Thread feThread = new Thread(() -> {
try {
@@ -50,7 +50,10 @@ class SystemTest {
Thread sequencerThread = new Thread(() -> {
try {
- Sequencer.main(argsSQ);
+ InetAddress ip = InetAddress.getByName(IP);
+ NetworkInterface netIfc = NetworkInterface.getByInetAddress(ip);
+ Sequencer sequencer = new Sequencer(ip, netIfc);
+ sequencer.run();
} catch (Exception e) {
e.printStackTrace();
}
@@ -75,8 +78,7 @@ class SystemTest {
ReplicaManager.main(argsRM);
ResponderClient responderClient = new ResponderClient(IP);
- ResponderClient.Add addCommand = responderClient.new Add();
- addCommand.add("MTL1001", "ambulance", "10");
+ responderClient.addResource("MTL1001", "ambulance", 10);
// Compare the number of lines in the log files, to determine if they match or not
assertTrue(LogComparator.compareFiles(TEST_LOG_PATH, EXPECTED_LOG_PATH_NORM));
@@ -93,8 +95,7 @@ class SystemTest {
ReplicaManager.main(argsRM);
ResponderClient responderClient = new ResponderClient(IP);
- ResponderClient.Add addCommand = responderClient.new Add();
- addCommand.add("MTL1001", "ambulance", "10");
+ responderClient.addResource("MTL1001", "ambulance", 10);
// Compare the number of lines in the log files, to determine if they match or not
assertTrue(LogComparator.compareFiles(TEST_LOG_PATH, EXPECTED_LOG_PATH_BYZ));
@@ -111,8 +112,7 @@ class SystemTest {
ReplicaManager.main(argsRM);
ResponderClient responderClient = new ResponderClient(IP);
- ResponderClient.Add addCommand = responderClient.new Add();
- addCommand.add("MTL1001", "ambulance", "10");
+ responderClient.addResource("MTL1001", "ambulance", 10);
// Compare the number of lines in the log files, to determine if they match or not
assertTrue(LogComparator.compareFiles(TEST_LOG_PATH, EXPECTED_LOG_PATH_CRASH));
@@ -143,8 +143,7 @@ class SystemTest {
e.printStackTrace();
} finally {
if (responderClient != null) {
- ResponderClient.Add addCommand = responderClient.new Add();
- addCommand.add("MTL1001", "ambulance", "10");
+ responderClient.addResource("MTL1001", "ambulance", 10);
}
}
});
@@ -157,8 +156,7 @@ class SystemTest {
e.printStackTrace();
} finally {
if (responderClient2 != null) {
- ResponderClient.Add addCommand2 = responderClient2.new Add();
- addCommand2.add("MTL1002", "ambulance", "11");
+ responderClient2.addResource("MTL1002", "ambulance", 11);
}
}
});