summaryrefslogtreecommitdiffstats
path: root/src/main/java/derms/client
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/derms/client')
-rw-r--r--src/main/java/derms/client/CLI.java86
-rw-r--r--src/main/java/derms/client/Client.java8
-rw-r--r--src/main/java/derms/client/ResponderClient.java95
3 files changed, 170 insertions, 19 deletions
diff --git a/src/main/java/derms/client/CLI.java b/src/main/java/derms/client/CLI.java
new file mode 100644
index 0000000..05f4e35
--- /dev/null
+++ b/src/main/java/derms/client/CLI.java
@@ -0,0 +1,86 @@
+package derms.client;
+
+import java.util.*;
+
+abstract class CLI implements Runnable {
+ protected Map<String, Command> commands = new HashMap<String, Command>();
+ protected List<Description> cmdDescriptions = new ArrayList<Description>();
+ protected List<Description> argDescriptions = new ArrayList<Description>();
+
+ protected CLI() {
+ commands.put("quit", new Quit());
+ cmdDescriptions.add(new Description("quit", "Exit the program"));
+
+ commands.put("help", new Help());
+ cmdDescriptions.add(new Description("help", "List commands"));
+ }
+
+ @Override
+ public void run() {
+ Scanner scanner = new Scanner(System.in);
+ System.out.println("Type 'help' for a list of commands.");
+ for (;;) {
+ System.out.print("Command: ");
+ String input = scanner.nextLine();
+ String[] fields = input.split(" ");
+ if (fields.length < 1 || fields[0] == "") {
+ continue;
+ }
+ Command cmd = commands.get(fields[0]);
+ if (cmd == null) {
+ System.out.println("Invalid command '"+fields[0]+"'");
+ System.out.println("Type 'help' for a list of commands.");
+ continue;
+ }
+ String[] args = null;
+ if (fields.length < 2) {
+ args = new String[0];
+ } else {
+ args = Arrays.copyOfRange(fields, 1, fields.length);
+ }
+ cmd.exec(args);
+ }
+ }
+
+ protected interface Command {
+ public void exec(String[] args);
+ }
+
+ protected class Quit implements Command {
+ @Override
+ public void exec(String[] args) {
+ System.out.println("Shutting down...");
+ System.exit(1);
+ }
+ }
+
+ protected class Help implements Command {
+ @Override
+ public void exec(String[] args) {
+ System.out.println("\nCommands:");
+ for (Description d : cmdDescriptions) {
+ System.out.println(d);
+ }
+ System.out.println("\nArguments:");
+ for (Description d : argDescriptions) {
+ System.out.println(d);
+ }
+ System.out.println();
+ }
+ }
+
+ protected class Description {
+ String object; /// The thing being described
+ String description;
+
+ protected Description(String object, String description) {
+ this.object = object;
+ this.description = description;
+ }
+
+ @Override
+ public String toString() {
+ return object+"\n\t"+description;
+ }
+ }
+}
diff --git a/src/main/java/derms/client/Client.java b/src/main/java/derms/client/Client.java
index 11e863f..7069f54 100644
--- a/src/main/java/derms/client/Client.java
+++ b/src/main/java/derms/client/Client.java
@@ -9,14 +9,12 @@ import javax.xml.ws.Service;
import java.net.MalformedURLException;
import java.net.URL;
-public abstract class Client {
+class Client {
public static final String namespace = "frontend.derms";
public static final QName qname = new QName("http://"+namespace+"/", DERMSServerImpl.class.getSimpleName()+"Service");
- protected final DERMSInterface server;
-
- protected Client(String FEhost) throws MalformedURLException {
+ static DERMSInterface connectToServer(String FEhost) throws MalformedURLException {
URL url = new URL(FE.endpointURL(FEhost) + "?wsdl");
- this.server = Service.create(url, qname).getPort(DERMSInterface.class);
+ return Service.create(url, qname).getPort(DERMSInterface.class);
}
}
diff --git a/src/main/java/derms/client/ResponderClient.java b/src/main/java/derms/client/ResponderClient.java
index af5dc3d..57bb132 100644
--- a/src/main/java/derms/client/ResponderClient.java
+++ b/src/main/java/derms/client/ResponderClient.java
@@ -1,36 +1,103 @@
package derms.client;
+import derms.frontend.DERMSInterface;
+
import java.net.MalformedURLException;
-public class ResponderClient extends Client {
- public static final String usage = "Usage: java derms.client.ResponderClienet <FE host>";
+public class ResponderClient extends CLI {
+ public static final String usage = "Usage: java derms.client.ResponderClientCLI <FE host>";
+
+ private final DERMSInterface server;
+
+ private 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."));
- public ResponderClient(String FEhost) throws MalformedURLException {
- super(FEhost);
+ 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("Missing argument 'FE host'");
+ System.err.println(usage);
System.exit(1);
}
String FEhost = args[0];
- ResponderClient client = null;
try {
- client = new ResponderClient(FEhost);
+ (new ResponderClient(FEhost)).run();
} catch (MalformedURLException e) {
- System.err.println(e.getMessage());
- System.exit(1);
+ e.printStackTrace();
+ }
+ }
+
+ private 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]);
}
- System.out.println("Adding resource...");
- String response = client.addResource("MTL1234", "AMBULANCE", 100);
- System.out.println("Response: " + response);
+ 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);
+ }
+ }
}
- public String addResource(String resourceID, String resourceName, int duration) {
- return server.addResource(resourceID, resourceName, 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);
+ }
}
}