diff options
| author | BMatajsz <90217645+BMatajsz@users.noreply.github.com> | 2024-12-03 10:27:54 -0500 |
|---|---|---|
| committer | BMatajsz <90217645+BMatajsz@users.noreply.github.com> | 2024-12-03 10:27:54 -0500 |
| commit | 298eb839a8ccdd625978cf51476a6c412d11902b (patch) | |
| tree | 99571b2234f546b6a5b536d27dac086d1ec99433 /src/main/java/derms/client/ResponderClientCLI.java | |
| parent | d0b0b50f18c4713814c565af2f3146ec82aa6b0f (diff) | |
| parent | 1a2c519e5e70cc1e31eaef5cd770da75d75d6654 (diff) | |
| download | soen423-298eb839a8ccdd625978cf51476a6c412d11902b.zip | |
Merge branch 'test' of https://github.com/sam-rba/soen423 into test
Diffstat (limited to 'src/main/java/derms/client/ResponderClientCLI.java')
| -rw-r--r-- | src/main/java/derms/client/ResponderClientCLI.java | 103 |
1 files changed, 103 insertions, 0 deletions
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); + } + } +} |