statistics/src/main/java/cz/xelfi/quoridor/statistics/resources/Statistics.java
author Martin Rexa <martin.rexa@centrum.cz>
Sun, 01 May 2011 21:10:07 +0200
changeset 283 0ea04469967d
parent 266 15fcdfc4cd4a
permissions -rw-r--r--
Timeout and finish
     1 /**
     2  * Quoridor server and related libraries
     3  * Copyright (C) 2009-2010 Martin Rexa
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://www.gnu.org/licenses/.
    17  */
    18 
    19 package cz.xelfi.quoridor.statistics.resources;
    20 
    21 import com.sun.jersey.api.container.httpserver.HttpServerFactory;
    22 import com.sun.jersey.api.core.PackagesResourceConfig;
    23 import com.sun.jersey.api.core.ResourceConfig;
    24 import com.sun.jersey.spi.resource.Singleton;
    25 import com.sun.net.httpserver.HttpServer;
    26 import java.io.File;
    27 import java.io.IOException;
    28 import java.util.Comparator;
    29 import java.util.List;
    30 import java.util.Collections;
    31 import java.net.ServerSocket;
    32 import javax.ws.rs.Path;
    33 import cz.xelfi.quoridor.webidor.resources.Games;
    34 import cz.xelfi.quoridor.webidor.Game;
    35 
    36 /**
    37  *
    38  * @author Martin Rexa
    39  */
    40 @Path("/")
    41 @Singleton
    42 public class Statistics {
    43     static final Comparator<Game> OLDEST_FIRST = new OldestFirst();
    44     static Elo elo;
    45     static Openings openings;
    46 
    47     @Path("elo")
    48     public Elo getElo() {
    49         return elo;
    50     }
    51 
    52     @Path("openings")
    53     public Openings getOpenings() {
    54         return openings;
    55     }
    56 
    57     public static void main( String[] args ) throws IOException, InterruptedException
    58     {
    59         int port = 9444;
    60         // timeout between reprocessing games in miliseconds
    61         long timeout = 1000 * 60 * 60 * 24;
    62 //        long timeout = 1000 * 10;
    63 
    64         try {
    65             port = Integer.parseInt(args[0]);
    66         } catch (Exception ex) {
    67             // OK
    68         }
    69 
    70         if (System.getProperty("quoridor.dir") == null) {
    71             File home = new File(System.getProperty("user.home"));
    72             File quoridor = new File(home, ".quoridor");
    73             System.setProperty("quoridor.dir", quoridor.getPath());
    74         }
    75 
    76         long lastExec = System.currentTimeMillis();
    77         processGames();
    78         HttpServer s = start(port);
    79         System.out.println("Statistics started at port " + port);
    80         Object monitor = new Object();
    81 /*
    82          * Disabled until OutOfMemory is solved
    83         // Wait until begining of next period. If timeout is one day, then next
    84         // processing will be done after midnight
    85         synchronized (monitor) {
    86             monitor.wait(timeout - (lastExec % timeout));
    87         }
    88         for(;;){
    89             processGames();
    90             synchronized (monitor) {
    91                 monitor.wait(timeout);
    92             }
    93         }
    94  * 
    95  */
    96         synchronized (monitor) {
    97             monitor.wait(timeout);
    98         }
    99     }
   100 
   101     public static HttpServer start(int port) throws IOException {
   102         if (port == -1) {
   103             ServerSocket ss = new ServerSocket(0);
   104             port =ss.getLocalPort();
   105             ss.close();
   106         }
   107         final String baseUri = "http://localhost:" + port + "/";
   108 
   109         if (System.getProperty("quoridor.dir") == null) {
   110             File home = new File(System.getProperty("user.home"));
   111             File quoridor = new File(home, ".quoridor");
   112             System.setProperty("quoridor.dir", quoridor.getPath());
   113         }
   114 
   115         ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.statistics");
   116         HttpServer server = HttpServerFactory.create(baseUri, rc);
   117         server.start();
   118         return server;
   119     }
   120 
   121     public static void processGames(){
   122         Elo eloTmp = new Elo();
   123         Openings openingsTmp = new Openings();
   124 
   125         final String prop = System.getProperty("quoridor.dir"); // NOI18N
   126         if (prop == null) {
   127             throw new IllegalStateException("quoridor.dir property must be specified"); // NOI18N
   128         }
   129         File path = new File(prop);
   130         path.mkdirs();
   131         File fGames = new File(path, "games");
   132         //fGames.mkdirs();
   133         Games games = new Games(fGames, null);
   134         List<Game> lGames = games.getGames();
   135         Collections.sort(lGames, OLDEST_FIRST);
   136 
   137         // process games
   138         for(Game g: lGames){
   139             if(g.getId().isFinished()){
   140                 eloTmp.processGame(g.getId());
   141                 openingsTmp.processGame(g);
   142             }
   143         }
   144 
   145         elo = eloTmp;
   146         openings = openingsTmp;
   147     }
   148 
   149     private static final class OldestFirst implements Comparator<Game> {
   150         public int compare(Game g1, Game g2) {
   151             if(g1.getId() == g2.getId())
   152                 return 0;
   153             long diff = g2.getId().getModified() - g1.getId().getModified();
   154             if (diff != 0) {
   155                 return diff < 0 ? 1 : -1;
   156             }
   157             return g1.getId().getId().compareTo(g2.getId().getId());
   158         }
   159     }
   160 
   161 }