summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2024-12-03 09:57:56 -0500
committerSam Anthony <sam@samanthony.xyz>2024-12-03 09:57:56 -0500
commit0123ebaac66527b0e17d89cec8b41e073afba885 (patch)
treef13f44da185d68cb0c0b41b7c8addeacb5d88b4b
parent33732b560ddc12e2a9e99729666ad88286013668 (diff)
downloadsoen423-0123ebaac66527b0e17d89cec8b41e073afba885.zip
separate responder client from cli
-rw-r--r--src/main/java/derms/client/ResponderClient.java97
-rw-r--r--src/main/java/derms/client/ResponderClientCLI.java103
-rw-r--r--src/test/java/derms/test/SystemTest.java15
3 files changed, 115 insertions, 100 deletions
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/test/java/derms/test/SystemTest.java b/src/test/java/derms/test/SystemTest.java
index 3ae83a8..59b08fa 100644
--- a/src/test/java/derms/test/SystemTest.java
+++ b/src/test/java/derms/test/SystemTest.java
@@ -78,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));
@@ -96,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));
@@ -114,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));
@@ -146,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);
}
}
});
@@ -160,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);
}
}
});