diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2024-11-23 13:51:12 -0500 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2024-11-23 13:51:12 -0500 |
| commit | 3c62b863509131e78c18ed13c6b83e4fc508848f (patch) | |
| tree | cf246e91da283edf704af936b9cef7eb8e09a6f8 /src/main/java/derms/replica/replica1/StationServer.java | |
| parent | e3b72053e8b04f2df013da0d7d49fe33927461a9 (diff) | |
| download | soen423-3c62b863509131e78c18ed13c6b83e4fc508848f.zip | |
import replica code from assignment
Diffstat (limited to 'src/main/java/derms/replica/replica1/StationServer.java')
| -rw-r--r-- | src/main/java/derms/replica/replica1/StationServer.java | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/src/main/java/derms/replica/replica1/StationServer.java b/src/main/java/derms/replica/replica1/StationServer.java new file mode 100644 index 0000000..3187ecb --- /dev/null +++ b/src/main/java/derms/replica/replica1/StationServer.java @@ -0,0 +1,146 @@ +package derms.replica.replica1; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +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; + public Resources resources; + public Servers servers; + private Logger log; + private ResponderServer responderServer; + private CoordinatorServer coordinatorServer; + + public StationServer(City city, InetAddress localAddr) throws IOException { + this.city = city; + this.localAddr = localAddr; + this.resources = new Resources(); + this.servers = new Servers(); + this.log = DermsLogger.getLogger(getClass()); + + try { + this.responderServer = new ResponderServer(city, resources, servers); + } catch (IOException e) { + throw new IOException("Failed to create ResponderServer: "+e.getMessage()); + } + log.info("Created ResponderServer"); + + try { + this.coordinatorServer = new CoordinatorServer(city, resources, servers); + } catch (IOException e) { + throw new IOException("Failed to create CoordinatorServer: "+e.getMessage()); + } + log.info("Created CoordinatorServer"); + } + + public static void main(String cmdlineArgs[]) { + Args args = null; + try { + args = new Args(cmdlineArgs); + } catch (IllegalArgumentException e) { + System.err.println(e.getMessage()); + System.out.println(usage); + System.exit(1); + } + + try { + (new StationServer(args.city, args.localAddr)).run(); + } catch (Exception e) { + System.err.println(e.getMessage()); + System.exit(1); + } + } + + @Override + public void run() { + log.info("Running"); + log.config("Local address is "+localAddr.toString()); + + ExecutorService pool = Executors.newCachedThreadPool(); + + try { + pool.execute(new ResourceAvailability.Server(localAddr, resources)); + } catch (IOException e) { + String msg = "Failed to start ResourceAvailability Server: "+e.getMessage(); + log.severe(msg); + return; + } + try { + pool.execute(new RequestResource.Server(localAddr, resources)); + } catch (IOException e) { + log.severe("Failed to start RequestResource Server: "+e.getMessage()); + return; + } + try { + pool.execute(new FindResource.Server(localAddr, resources)); + } catch (IOException e) { + log.severe("Failed to start FindResource Server: "+e.getMessage()); + return; + } + try { + pool.execute(new ReturnResource.Server(localAddr, resources)); + } catch (IOException e) { + log.severe("Failed to start ReturnResource Server: "+e.getMessage()); + return; + } + try { + pool.execute(new SwapResource.Server(localAddr, resources, servers)); + } catch (IOException e) { + log.severe("Failed to start SwapResource Server: "+e.getMessage()); + 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) { + log.severe("Failed to start Announcer: "+e.getMessage()); + return; + } + try { + pool.execute(new AnnounceListener(announceGroup, localAddr, servers)); + } catch (IOException e) { + log.severe("Failed to start AnnounceListener: "+e.getMessage()); + return; + } + } + + private static class Args { + private City city; + private InetAddress localAddr; + + 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 'local host'"); + } + try { + localAddr = InetAddress.getByName(args[1]); + } catch (UnknownHostException | SecurityException e) { + throw new IllegalArgumentException("Bad value of 'local host': "+e.getMessage()); + } + } + } +}
\ No newline at end of file |