freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java
changeset 121 95dfb04fcee1
parent 120 6bf820453a5f
child 122 e0ecef0c421b
     1.1 --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Sat Oct 10 17:15:35 2009 +0200
     1.2 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Sun Oct 11 14:20:19 2009 +0200
     1.3 @@ -36,6 +36,7 @@
     1.4  import com.sun.net.httpserver.HttpServer;
     1.5  import cz.xelfi.quoridor.Board;
     1.6  import cz.xelfi.quoridor.IllegalPositionException;
     1.7 +import cz.xelfi.quoridor.webidor.resources.Quoridor;
     1.8  import java.awt.Image;
     1.9  import java.io.File;
    1.10  import java.io.IOException;
    1.11 @@ -306,13 +307,14 @@
    1.12      //
    1.13  
    1.14      public static void main(String[] args) throws Exception {
    1.15 -        int port = 9998;
    1.16 +        int port = 9333;
    1.17          if (args.length > 1) {
    1.18              port = Integer.parseInt(args[0]);
    1.19          }
    1.20 +        String remoteAPI = args.length > 2 ? args[1] : null;
    1.21  
    1.22  
    1.23 -        Callable<Void> r = startServers(port);
    1.24 +        Callable<Void> r = startServers(port, remoteAPI);
    1.25  
    1.26          if (args.length < 2 || !args[1].equals("--kill")) {
    1.27              System.out.println("Hit enter to stop it...");
    1.28 @@ -326,22 +328,23 @@
    1.29          System.exit(0);
    1.30      }
    1.31  
    1.32 -    static Callable<Void> startServers(int port) throws Exception {
    1.33 +    static Callable<Void> startServers(int port, String remoteAPI) throws Exception {
    1.34 +        Client client = new Client();
    1.35  
    1.36 -        if (System.getProperty("quoridor.dir") == null) {
    1.37 -            File home = new File(System.getProperty("user.home"));
    1.38 -            File quoridor = new File(home, ".quoridor");
    1.39 -            System.setProperty("quoridor.dir", quoridor.getPath());
    1.40 +        final HttpServer apiServer;
    1.41 +        if (remoteAPI == null) {
    1.42 +            int localAPIPort = port - 1;
    1.43 +            apiServer = Quoridor.start(localAPIPort);
    1.44 +            base = client.resource(new URI("http://localhost:" + localAPIPort + "/api/"));
    1.45 +        } else {
    1.46 +            base = client.resource(new URI(remoteAPI));
    1.47 +            apiServer = null;
    1.48          }
    1.49  
    1.50          ResourceConfig rc = new PackagesResourceConfig(
    1.51 -            "cz.xelfi.quoridor.webidor",
    1.52              "cz.xelfi.quoridor.freemarkerdor"
    1.53          );
    1.54  
    1.55 -        Client client = new Client();
    1.56 -        base = client.resource(new URI("http://localhost:" + port + "/api/"));
    1.57 -
    1.58          final String baseUri = "http://localhost:" + port + "/";
    1.59          final HttpServer server = HttpServerFactory.create(baseUri, rc);
    1.60          server.start();
    1.61 @@ -349,6 +352,9 @@
    1.62  
    1.63          return new Callable<Void>() {
    1.64              public Void call() throws Exception {
    1.65 +                if (apiServer != null) {
    1.66 +                    apiServer.stop(0);
    1.67 +                }
    1.68                  server.stop(0);
    1.69                  return null;
    1.70              }