diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2024-11-25 09:32:37 -0500 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2024-11-25 09:32:37 -0500 |
| commit | c001e711d390de43b86554a9e21780b154fe813e (patch) | |
| tree | 2f5fe6519f028c840fd3be89639cfe3b607b5e8e | |
| parent | 3c62b863509131e78c18ed13c6b83e4fc508848f (diff) | |
| download | soen423-c001e711d390de43b86554a9e21780b154fe813e.zip | |
replica1: remove old JAX code
11 files changed, 2 insertions, 619 deletions
diff --git a/src/main/java/derms/replica/replica1/CLI.java b/src/main/java/derms/replica/replica1/CLI.java deleted file mode 100644 index 88e166a..0000000 --- a/src/main/java/derms/replica/replica1/CLI.java +++ /dev/null @@ -1,91 +0,0 @@ -package derms.replica.replica1; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Scanner; - -public 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/replica/replica1/Client.java b/src/main/java/derms/replica/replica1/Client.java deleted file mode 100644 index 7ebbb68..0000000 --- a/src/main/java/derms/replica/replica1/Client.java +++ /dev/null @@ -1,30 +0,0 @@ -package derms.replica.replica1; - -import javax.xml.namespace.QName; -import javax.xml.ws.Service; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.UnknownHostException; - -public abstract class Client<I, C> { - public static final String namespace = "derms.samanthony.xyz"; - public static final int port = 8080; - - private final Class<I> endpointInterface; - private final QName qname; - - protected Client(Class<I> endpointInterface, Class<C> endpointClass) { - this.endpointInterface = endpointInterface; - this.qname = new QName("http://"+namespace+"/", endpointClass.getSimpleName()+"Service"); - } - - protected I connect(String host) throws MalformedURLException { - URL url = new URL("http://"+host+":"+port+"/"+endpointInterface.getSimpleName()+"?wsdl"); - return Service.create(url, qname).getPort(endpointInterface); - } - - protected I connect(City city) throws UnknownHostException, MalformedURLException { - String host = Hosts.get(city); - return connect(host); - } -} diff --git a/src/main/java/derms/replica/replica1/Coordinator.java b/src/main/java/derms/replica/replica1/Coordinator.java deleted file mode 100644 index b963d58..0000000 --- a/src/main/java/derms/replica/replica1/Coordinator.java +++ /dev/null @@ -1,22 +0,0 @@ -package derms.replica.replica1; - -import javax.jws.WebService; -import javax.jws.soap.SOAPBinding; - -@WebService -@SOAPBinding(style = SOAPBinding.Style.RPC) -public interface Coordinator { - void requestResource(CoordinatorID cid, ResourceID rid, int duration) - throws ServerCommunicationError, NoSuchResourceException, - AlreadyBorrowedException, InvalidDurationException; - - Resource[] findResource(CoordinatorID cid, ResourceName rname) - throws ServerCommunicationError; - - void returnResource(CoordinatorID cid, ResourceID rid) - throws ServerCommunicationError, NoSuchResourceException, - NotBorrowedException; - - void swapResource(CoordinatorID cid, ResourceID oldRID, ResourceID newRID) - throws ServerCommunicationError, NoSuchResourceException; -} diff --git a/src/main/java/derms/replica/replica1/CoordinatorClient.java b/src/main/java/derms/replica/replica1/CoordinatorClient.java deleted file mode 100644 index 6016c31..0000000 --- a/src/main/java/derms/replica/replica1/CoordinatorClient.java +++ /dev/null @@ -1,40 +0,0 @@ -package derms.replica.replica1; - -import java.net.MalformedURLException; -import java.net.UnknownHostException; - -public class CoordinatorClient extends Client<Coordinator, CoordinatorServer> { - public CoordinatorID id; - public Coordinator server; - - public CoordinatorClient(CoordinatorID id) throws UnknownHostException, MalformedURLException { - super(Coordinator.class, CoordinatorServer.class); - this.id = id; - this.server = connect(new City(id.city)); - } - - public CoordinatorClient(City city, short idNum) throws UnknownHostException, MalformedURLException { - this(new CoordinatorID(city.toString(), idNum)); - } - - public void requestResource(ResourceID resourceID, int duration) - throws ServerCommunicationError, NoSuchResourceException, - AlreadyBorrowedException, InvalidDurationException - { - server.requestResource(id, resourceID, duration); - } - - public void returnResource(ResourceID resourceID) - throws ServerCommunicationError, NoSuchResourceException, NotBorrowedException - { - server.returnResource(id, resourceID); - } - - public Resource[] findResource(ResourceName name) throws ServerCommunicationError { - return server.findResource(id, name); - } - - public void swapResource(ResourceID oldRID, ResourceID newRID) throws ServerCommunicationError, NoSuchResourceException { - server.swapResource(id, oldRID, newRID); - } -}
\ No newline at end of file diff --git a/src/main/java/derms/replica/replica1/CoordinatorClientCLI.java b/src/main/java/derms/replica/replica1/CoordinatorClientCLI.java deleted file mode 100644 index c1ac48a..0000000 --- a/src/main/java/derms/replica/replica1/CoordinatorClientCLI.java +++ /dev/null @@ -1,212 +0,0 @@ -package derms.replica.replica1; - -import java.net.MalformedURLException; -import java.net.UnknownHostException; - -public class CoordinatorClientCLI extends CLI { - public static final String usage = "Usage: java CoordinatorClient <city> <id number>"; - - private final CoordinatorClient client; - - private CoordinatorClientCLI(City city, short idNum) throws UnknownHostException, MalformedURLException { - client = new CoordinatorClient(city, idNum); - System.out.println("ID: "+client.id); - - commands.put("request", new Request()); - cmdDescriptions.add(new Description( - "request <resource ID> <duration>", - "Borrow a resource and reduce its duration.")); - - commands.put("find", new Find()); - cmdDescriptions.add(new Description( - "find <resource name>", - "List borrowed resources.")); - - commands.put("return", new Return()); - cmdDescriptions.add(new Description( - "return <resourceID>", - "Return a currently borrowed resource.")); - - commands.put("swap", new Swap()); - cmdDescriptions.add(new Description( - "swap <old resourceID> <new resourceID>", - "Return the old resource and borrow the new one.")); - - argDescriptions.add(new Description( - "<resource ID>", - "3-letter city code followed by 4-digit number.")); - argDescriptions.add(new Description( - "<resource name>", - "E.g., AMBULANCE.")); - argDescriptions.add(new Description( - "<duration>", - "A number representing a time period.")); - } - - public static void main(String[] cmdlineArgs) { - Args args = null; - try { - args = new Args(cmdlineArgs); - } catch (IllegalArgumentException e) { - System.err.println(e.getMessage()); - System.err.println(usage); - System.exit(1); - } - - try { - (new CoordinatorClientCLI(args.city, args.idNum)).run(); - } catch (UnknownHostException | MalformedURLException e) { - System.err.println(e.getMessage()); - System.exit(1); - } - } - - private class Request implements Command { - public void exec(String[] args) { - if (args.length < 2) { - System.out.println("invalid arguments for 'request'"); - } else { - requestResource(args[0], args[1]); - } - } - } - - private void requestResource(String resourceIDStr, String durationStr) { - ResourceID resourceID; - try { - resourceID = ResourceID.parse(resourceIDStr); - } catch (IllegalArgumentException e) { - System.out.println(e.getMessage()); - return; - } - - int duration; - try { - duration = Integer.parseInt(durationStr); - if (duration < 0) { - throw new IllegalArgumentException("invalid duration: "+durationStr); - } - } catch (Exception e) { - System.out.println(e.getMessage()); - return; - } - - try { - client.requestResource(resourceID, duration); - System.out.println("Successfully borrowed resource."); - } catch (ServerCommunicationError | NoSuchResourceException | AlreadyBorrowedException | InvalidDurationException e) { - System.out.println("Failed to borrow resource: "+e.getMessage()); - } - } - - private class Return implements Command { - public void exec(String[] args) { - if (args.length < 1) { - System.out.println("invalid arguments for 'return'"); - } else { - returnResource(args[0]); - } - } - } - - private void returnResource(String resourceIDStr) { - ResourceID resourceID; - try { - resourceID = ResourceID.parse(resourceIDStr); - } catch (IllegalArgumentException e) { - System.out.println("Invalid resource ID: "+e.getMessage()); - return; - } - - try { - client.returnResource(resourceID); - System.out.println("Successfully returned resource "+resourceIDStr); - } catch (ServerCommunicationError | NoSuchResourceException | NotBorrowedException e) { - System.out.println("Failed to return resource: "+e.getMessage()); - } - } - - private class Find implements Command { - public void exec(String[] args) { - if (args.length < 1) { - System.out.println("invalid arguments for 'find'"); - } else { - findResource(args[0]); - } - } - } - - private void findResource(String resourceNameStr) { - ResourceName resourceName; - try { - resourceName = ResourceName.parse(resourceNameStr); - } catch (Exception e) { - System.out.println("Invalid resource name: "+resourceNameStr); - return; - } - - Resource[] resources; - try { - resources = client.findResource(resourceName); - } catch (ServerCommunicationError e) { - System.out.println("Failed to find resource "+resourceName+": "+e.getMessage()); - return; - } - - System.out.println("Borrowed "+resourceNameStr+" resources:"); - for (Resource r : resources) { - String rid = r.id.toString(); - System.out.println("\t"+rid+" "+r.borrowDuration); - } - } - - private class Swap implements Command { - public void exec(String[] args) { - if (args.length < 2) { - System.out.println("invalid arguments for 'swap'"); - } else { - swapResource(args[0], args[1]); - } - } - } - - private void swapResource(String oldRIDStr, String newRIDStr) { - ResourceID oldRID; - ResourceID newRID; - try { - oldRID = ResourceID.parse(oldRIDStr); - newRID = ResourceID.parse(newRIDStr); - } catch (IllegalArgumentException e) { - System.out.println("Invalid resource id"); - return; - } - - try { - client.swapResource(oldRID, newRID); - System.out.println("Successfully swapped "+oldRID+" for "+newRID); - } catch (ServerCommunicationError | NoSuchResourceException e) { - System.out.println("Failed to swap resources: "+e.getMessage()); - } - } - - private static class Args { - private final City city; - private final short idNum; - - private Args(String[] args) throws IllegalArgumentException { - if (args.length < 1) { - throw new IllegalArgumentException("Missing argument 'city'"); - } - city = new City(args[0]); - - if (args.length < 2) { - throw new IllegalArgumentException("Missing argument 'id number'"); - } - try { - idNum = Short.parseShort(args[1]); - } catch (NumberFormatException e) { - throw new IllegalArgumentException("Bad value of 'id number'"); - } - } - } -} diff --git a/src/main/java/derms/replica/replica1/CoordinatorServer.java b/src/main/java/derms/replica/replica1/CoordinatorServer.java index ce1cc9e..0ff58de 100644 --- a/src/main/java/derms/replica/replica1/CoordinatorServer.java +++ b/src/main/java/derms/replica/replica1/CoordinatorServer.java @@ -9,10 +9,8 @@ import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; import java.util.logging.Logger; -import javax.jws.WebService; -@WebService(endpointInterface = "xyz.samanthony.derms.Coordinator") -public class CoordinatorServer implements Coordinator { +public class CoordinatorServer { public static final Duration timeout = Duration.ofSeconds(5); private City city; @@ -32,7 +30,6 @@ public class CoordinatorServer implements Coordinator { this(new City(), new Resources(), new Servers()); } - @Override public void requestResource(CoordinatorID cid, ResourceID rid, int duration) throws ServerCommunicationError, NoSuchResourceException, AlreadyBorrowedException, InvalidDurationException @@ -70,7 +67,6 @@ public class CoordinatorServer implements Coordinator { } } - @Override public Resource[] findResource(CoordinatorID cid, ResourceName rname) throws ServerCommunicationError { log.info("Find Resource "+rname+" from "+cid); FindResource.Request request = new FindResource.Request(cid, rname); @@ -106,7 +102,6 @@ public class CoordinatorServer implements Coordinator { return arr; } - @Override public void returnResource(CoordinatorID cid, ResourceID rid) throws ServerCommunicationError, NoSuchResourceException, NotBorrowedException { @@ -144,7 +139,6 @@ public class CoordinatorServer implements Coordinator { } } - @Override public void swapResource(CoordinatorID cid, ResourceID oldRID, ResourceID newRID) throws ServerCommunicationError, NoSuchResourceException { log.info(cid+": swap "+oldRID+", "+newRID); diff --git a/src/main/java/derms/replica/replica1/Responder.java b/src/main/java/derms/replica/replica1/Responder.java deleted file mode 100644 index 5fb7359..0000000 --- a/src/main/java/derms/replica/replica1/Responder.java +++ /dev/null @@ -1,16 +0,0 @@ -package derms.replica.replica1; - -import javax.jws.WebService; -import javax.jws.soap.SOAPBinding; - -@WebService -@SOAPBinding(style = SOAPBinding.Style.RPC) -public interface Responder { - void addResource(Resource r); - - void removeResource(ResourceID rid, int duration) - throws NoSuchResourceException; - - Resource[] listResourceAvailability(ResourceName rname) - throws ServerCommunicationError; -} diff --git a/src/main/java/derms/replica/replica1/ResponderClient.java b/src/main/java/derms/replica/replica1/ResponderClient.java deleted file mode 100644 index b21b67e..0000000 --- a/src/main/java/derms/replica/replica1/ResponderClient.java +++ /dev/null @@ -1,34 +0,0 @@ -package derms.replica.replica1; - -import java.net.MalformedURLException; -import java.net.UnknownHostException; -import java.util.HashMap; -import java.util.Map; - -public class ResponderClient extends Client<Responder, ResponderServer> { - public ResponderID id; - - public ResponderClient(ResponderID id) { - super(Responder.class, ResponderServer.class); - this.id = id; - } - - public ResponderClient(City city, short idNum) { - this(new ResponderID(city, idNum)); - } - - public void add(ResourceID rid, ResourceName name, int duration) throws UnknownHostException, MalformedURLException { - Responder server = connect(new City(rid.city)); - server.addResource(new Resource(rid, name, duration)); - } - - public void remove(ResourceID rid, int duration) throws UnknownHostException, MalformedURLException, NoSuchResourceException { - Responder server = connect(new City(rid.city)); - server.removeResource(rid, duration); - } - - public Resource[] listResources(ResourceName name) throws UnknownHostException, MalformedURLException, ServerCommunicationError { - Responder server = connect(id.city); - return server.listResourceAvailability(name); - } -} diff --git a/src/main/java/derms/replica/replica1/ResponderClientCLI.java b/src/main/java/derms/replica/replica1/ResponderClientCLI.java deleted file mode 100644 index 5bfbb11..0000000 --- a/src/main/java/derms/replica/replica1/ResponderClientCLI.java +++ /dev/null @@ -1,151 +0,0 @@ -package derms.replica.replica1; - -import java.net.MalformedURLException; -import java.net.UnknownHostException; - -public class ResponderClientCLI extends CLI { - public static final String usage = "Usage: java ResponderClientCLI <city> <id number>"; - - private final ResponderClient client; - - private ResponderClientCLI(City city, short idNum) { - client = new ResponderClient(city, idNum); - System.out.println("ID: "+client.id); - - commands.put("add", new Add()); - cmdDescriptions.add(new Description( - "add <resource ID> <resource name> <duration>", - "Add a 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 tresource is removed entirely.")); - - commands.put("list", new List()); - cmdDescriptions.add(new Description( - "list <resource name>", - "List available resources.")); - - argDescriptions.add(new Description( - "<resource ID>", - "3-letter city code followed by 4-digit number.")); - argDescriptions.add(new Description( - "<resource name>", - "E.g., AMBULANCE.")); - argDescriptions.add(new Description( - "<duration>", - "A number representing a time period.")); - } - - public static void main(String[] cmdlineArgs) { - Args args = null; - try { - args = new Args(cmdlineArgs); - } catch (IllegalArgumentException e) { - System.err.println(e); - System.err.println(usage); - System.exit(1); - } - - (new ResponderClientCLI(args.city, args.idNum)).run(); - } - - private class Add implements Command { - 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 resourceIDStr, String resourceNameStr, String durationStr) { - ResourceID rid = ResourceID.parse(resourceIDStr); - ResourceName name = ResourceName.parse(resourceNameStr); - int duration = Integer.parseInt(durationStr); - if (duration < 0) { - throw new NumberFormatException("duration less than 0"); - } - try { - client.add(rid, name, duration); - System.out.println("Successfully added resource to server."); - } catch (Exception e) { - System.out.println("Failed to add resource: "+e.getMessage()); - } - } - - private class Remove implements Command { - 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 resourceIDStr, String durationStr) { - try { - ResourceID resourceID = ResourceID.parse(resourceIDStr); - int duration = Integer.parseInt(durationStr); - client.remove(resourceID, duration); - System.out.println("Successfully removed resource from server."); - } catch (NumberFormatException e) { - System.out.println("invalid duration: "+durationStr); - } catch (IllegalArgumentException e) { - System.out.println(e.getMessage()); - } catch (Exception e) { - System.out.println("Failed to remove resource: "+e.getMessage()); - } - } - - private class List implements Command { - public void exec(String[] args) { - if (args.length < 1) { - System.out.println("invalid arguments for 'list'"); - } else { - list(args[0]); - } - } - } - - private void list(String resourceNameStr) { - try { - ResourceName name = ResourceName.parse(resourceNameStr); - Resource[] resources = client.listResources(name); - System.out.println("Available resources:"); - for (Resource resource : resources) { - System.out.println(resource.toString()); - } - } catch (IllegalArgumentException e) { - System.out.println("invalid resource name: " + resourceNameStr); - } catch (UnknownHostException | MalformedURLException e) { - System.err.println(e.getMessage()); - } catch (ServerCommunicationError e) { - System.err.println("Failed to retrieve resources from server: "+e.getMessage()); - } - } - - private static class Args { - private final City city; - private final short idNum; - - private Args(String[] args) throws IllegalArgumentException { - if (args.length < 1) { - throw new IllegalArgumentException("Missing argument 'city'"); - } - city = new City(args[0]); - - if (args.length < 2) { - throw new IllegalArgumentException("Missing argument 'id number'"); - } - try { - idNum = Short.parseShort(args[1]); - } catch (NumberFormatException e) { - throw new IllegalArgumentException("Bad value of 'id number'"); - } - } - } -} diff --git a/src/main/java/derms/replica/replica1/ResponderServer.java b/src/main/java/derms/replica/replica1/ResponderServer.java index d2406fb..e8000b2 100644 --- a/src/main/java/derms/replica/replica1/ResponderServer.java +++ b/src/main/java/derms/replica/replica1/ResponderServer.java @@ -10,10 +10,8 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; import java.util.logging.Logger; import java.util.NoSuchElementException; -import javax.jws.WebService; -@WebService(endpointInterface = "xyz.samanthony.derms.Responder") -public class ResponderServer implements Responder { +public class ResponderServer { public static final Duration timeout = Duration.ofSeconds(5); private City city; @@ -32,13 +30,11 @@ public class ResponderServer implements Responder { this(new City(), new Resources(), new Servers()); } - @Override public void addResource(Resource r) { resources.add(r); log.info("Added resource "+r+" - success"); } - @Override public void removeResource(ResourceID rid, int duration) throws NoSuchResourceException { log.info("Remove duration "+duration+" from "+rid); try { @@ -66,7 +62,6 @@ public class ResponderServer implements Responder { } } - @Override public Resource[] listResourceAvailability(ResourceName rname) throws ServerCommunicationError { log.info("Request for available "+rname); Collection<Resource> availableResources = ConcurrentHashMap.newKeySet(); diff --git a/src/main/java/derms/replica/replica1/StationServer.java b/src/main/java/derms/replica/replica1/StationServer.java index 3187ecb..cdb9cc1 100644 --- a/src/main/java/derms/replica/replica1/StationServer.java +++ b/src/main/java/derms/replica/replica1/StationServer.java @@ -7,12 +7,10 @@ import java.net.UnknownHostException; import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; import java.util.logging.Logger; -import javax.xml.ws.Endpoint; public class StationServer implements Runnable { public static final String usage = "Usage: java StationServer <city> <local address>"; public static final InetSocketAddress announceGroup = new InetSocketAddress("225.5.5.5", 5555); - public static final int publishPort = 8080; public City city; public InetAddress localAddr; @@ -101,14 +99,6 @@ public class StationServer implements Runnable { return; } - String url = "http://"+localAddr.getHostAddress()+":"+publishPort+"/"+Responder.class.getSimpleName(); - log.info("Publishing Responder to '"+url+"'..."); - Endpoint.publish(url, responderServer); - - url = "http://"+localAddr.getHostAddress()+":"+publishPort+"/"+Coordinator.class.getSimpleName(); - log.info("Publishing Coordinator to '"+url+"'..."); - Endpoint.publish(url, coordinatorServer); - try { pool.execute(new Announcer(announceGroup, localAddr, city)); } catch (IOException e) { |