statistics/src/main/java/cz/xelfi/quoridor/statistics/resources/Statistics.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 14 Sep 2010 08:56:13 +0200
changeset 264 d60370059c3c
parent 223 7416b11ad09f
child 266 15fcdfc4cd4a
permissions -rw-r--r--
Changing headers to GPLv3
     1 /*
     2  * Quoridor server and related libraries
     3  * Copyright (C) 2009-2010 Martin
     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 package cz.xelfi.quoridor.statistics.resources;
    19 
    20 import com.sun.jersey.api.container.httpserver.HttpServerFactory;
    21 import com.sun.jersey.api.core.PackagesResourceConfig;
    22 import com.sun.jersey.api.core.ResourceConfig;
    23 import com.sun.jersey.spi.resource.Singleton;
    24 import com.sun.net.httpserver.HttpServer;
    25 import java.io.File;
    26 import java.io.IOException;
    27 import java.util.Comparator;
    28 import java.util.List;
    29 import java.util.Collections;
    30 import java.net.ServerSocket;
    31 import javax.ws.rs.Path;
    32 import cz.xelfi.quoridor.webidor.resources.Games;
    33 import cz.xelfi.quoridor.webidor.Game;
    34 
    35 /**
    36  *
    37  * @author Martin Rexa
    38  */
    39 @Path("/")
    40 @Singleton
    41 public class Statistics {
    42     static final Comparator<Game> OLDEST_FIRST = new OldestFirst();
    43     static Elo elo;
    44     static Openings openings;
    45 
    46     @Path("elo")
    47     public Elo getElo() {
    48         return elo;
    49     }
    50 
    51     @Path("openings")
    52     public Openings getOpenings() {
    53         return openings;
    54     }
    55 
    56     public static void main( String[] args ) throws IOException, InterruptedException
    57     {
    58         int port = 9444;
    59         // timeout between reprocessing games in miliseconds
    60         long timeout = 1000 * 60 * 60 * 24;
    61 //        long timeout = 1000 * 10;
    62 
    63         try {
    64             port = Integer.parseInt(args[0]);
    65         } catch (Exception ex) {
    66             // OK
    67         }
    68 
    69         if (System.getProperty("quoridor.dir") == null) {
    70             File home = new File(System.getProperty("user.home"));
    71             File quoridor = new File(home, ".quoridor");
    72             System.setProperty("quoridor.dir", quoridor.getPath());
    73         }
    74 
    75         long lastExec = System.currentTimeMillis();
    76         processGames();
    77         HttpServer s = start(port);
    78         System.out.println("Statistics started at port " + port);
    79         Object monitor = new Object();
    80         // Wait until begining of next period. If timeout is one day, then next
    81         // processing will be done after midnight
    82         synchronized (monitor) {
    83             monitor.wait(timeout - (lastExec % timeout));
    84         }
    85         for(;;){
    86             processGames();
    87             synchronized (monitor) {
    88                 monitor.wait(timeout);
    89             }
    90         }
    91     }
    92 
    93     public static HttpServer start(int port) throws IOException {
    94         if (port == -1) {
    95             ServerSocket ss = new ServerSocket(0);
    96             port =ss.getLocalPort();
    97             ss.close();
    98         }
    99         final String baseUri = "http://localhost:" + port + "/";
   100 
   101         if (System.getProperty("quoridor.dir") == null) {
   102             File home = new File(System.getProperty("user.home"));
   103             File quoridor = new File(home, ".quoridor");
   104             System.setProperty("quoridor.dir", quoridor.getPath());
   105         }
   106 
   107         ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.statistics");
   108         HttpServer server = HttpServerFactory.create(baseUri, rc);
   109         server.start();
   110         return server;
   111     }
   112 
   113     public static void processGames(){
   114         Elo eloTmp = new Elo();
   115         Openings openingsTmp = new Openings();
   116 
   117         final String prop = System.getProperty("quoridor.dir"); // NOI18N
   118         if (prop == null) {
   119             throw new IllegalStateException("quoridor.dir property must be specified"); // NOI18N
   120         }
   121         File path = new File(prop);
   122         path.mkdirs();
   123         File fGames = new File(path, "games");
   124         //fGames.mkdirs();
   125         Games games = new Games(fGames, null);
   126         List<Game> lGames = games.getGames();
   127         Collections.sort(lGames, OLDEST_FIRST);
   128 
   129         // process games
   130         for(Game g: lGames){
   131             if(g.getId().isFinished()){
   132                 eloTmp.processGame(g.getId());
   133                 openingsTmp.processGame(g);
   134             }
   135         }
   136 
   137         elo = eloTmp;
   138         openings = openingsTmp;
   139     }
   140 
   141     private static final class OldestFirst implements Comparator<Game> {
   142         public int compare(Game g1, Game g2) {
   143             if(g1.getId() == g2.getId())
   144                 return 0;
   145             long diff = g2.getId().getModified() - g1.getId().getModified();
   146             if (diff != 0) {
   147                 return diff < 0 ? 1 : -1;
   148             }
   149             return g1.getId().getId().compareTo(g2.getId().getId());
   150         }
   151     }
   152 
   153 }