1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/statistics/src/main/java/cz/xelfi/quoridor/statistics/resources/Statistics.java Thu Jan 07 22:34:17 2010 +0100
1.3 @@ -0,0 +1,156 @@
1.4 +/*
1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 + *
1.7 + * The contents of this file are subject to the terms of either the GNU
1.8 + * General Public License Version 2 only ("GPL") or the Common
1.9 + * Development and Distribution License("CDDL") (collectively, the
1.10 + * "License"). You may not use this file except in compliance with the
1.11 + * License. You can obtain a copy of the License at
1.12 + * http://www.netbeans.org/cddl-gplv2.html
1.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.14 + * specific language governing permissions and limitations under the
1.15 + * License. When distributing the software, include this License Header
1.16 + * Notice in each file and include the License file at
1.17 + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
1.18 + * particular file as subject to the "Classpath" exception as provided
1.19 + * by Sun in the GPL Version 2 section of the License file that
1.20 + * accompanied this code. If applicable, add the following below the
1.21 + * License Header, with the fields enclosed by brackets [] replaced by
1.22 + * your own identifying information:
1.23 + * "Portions Copyrighted [year] [name of copyright owner]"
1.24 + *
1.25 + * Contributor(s):
1.26 + *
1.27 + * Portions Copyrighted 2010 Martin Rexa
1.28 + */
1.29 +
1.30 +package cz.xelfi.quoridor.statistics.resources;
1.31 +
1.32 +import com.sun.jersey.api.container.httpserver.HttpServerFactory;
1.33 +import com.sun.jersey.api.core.PackagesResourceConfig;
1.34 +import com.sun.jersey.api.core.ResourceConfig;
1.35 +import com.sun.jersey.spi.resource.Singleton;
1.36 +import com.sun.net.httpserver.HttpServer;
1.37 +import java.io.File;
1.38 +import java.io.IOException;
1.39 +import java.util.Comparator;
1.40 +import java.util.List;
1.41 +import java.util.Collections;
1.42 +import java.net.ServerSocket;
1.43 +import javax.ws.rs.Path;
1.44 +import cz.xelfi.quoridor.webidor.resources.Games;
1.45 +import cz.xelfi.quoridor.webidor.Game;
1.46 +
1.47 +/**
1.48 + *
1.49 + * @author Martin Rexa
1.50 + */
1.51 +@Path("/")
1.52 +@Singleton
1.53 +public class Statistics {
1.54 + static final Comparator<Game> OLDEST_FIRST = new OldestFirst();
1.55 + static Elo elo;
1.56 + static Openings openings;
1.57 +
1.58 + @Path("elo")
1.59 + public Elo getElo() {
1.60 + return elo;
1.61 + }
1.62 +
1.63 + @Path("openings")
1.64 + public Openings getOpenings() {
1.65 + return openings;
1.66 + }
1.67 +
1.68 + public static void main( String[] args ) throws IOException, InterruptedException
1.69 + {
1.70 + int port = 9444;
1.71 + // timeout between reprocessing games in miliseconds
1.72 +// long timeout = 1000 * 60 * 60;
1.73 + long timeout = 1000 * 10;
1.74 +
1.75 + try {
1.76 + port = Integer.parseInt(args[0]);
1.77 + } catch (Exception ex) {
1.78 + // OK
1.79 + }
1.80 +
1.81 + if (System.getProperty("quoridor.dir") == null) {
1.82 + File home = new File(System.getProperty("user.home"));
1.83 + File quoridor = new File(home, ".quoridor");
1.84 + System.setProperty("quoridor.dir", quoridor.getPath());
1.85 + }
1.86 +
1.87 + processGames();
1.88 + HttpServer s = start(port);
1.89 + System.out.println("Statistics started at port " + port);
1.90 + Object monitor = new Object();
1.91 + for(;;){
1.92 + synchronized (monitor) {
1.93 + monitor.wait(timeout);
1.94 + }
1.95 + processGames();
1.96 + }
1.97 + }
1.98 +
1.99 + public static HttpServer start(int port) throws IOException {
1.100 + if (port == -1) {
1.101 + ServerSocket ss = new ServerSocket(0);
1.102 + port =ss.getLocalPort();
1.103 + ss.close();
1.104 + }
1.105 + final String baseUri = "http://localhost:" + port + "/";
1.106 +
1.107 + if (System.getProperty("quoridor.dir") == null) {
1.108 + File home = new File(System.getProperty("user.home"));
1.109 + File quoridor = new File(home, ".quoridor");
1.110 + System.setProperty("quoridor.dir", quoridor.getPath());
1.111 + }
1.112 +
1.113 + ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.statistics");
1.114 + HttpServer server = HttpServerFactory.create(baseUri, rc);
1.115 + server.start();
1.116 + return server;
1.117 + }
1.118 +
1.119 + public static void processGames(){
1.120 + Elo eloTmp = new Elo();
1.121 + Openings openingsTmp = new Openings();
1.122 +
1.123 + final String prop = System.getProperty("quoridor.dir"); // NOI18N
1.124 + if (prop == null) {
1.125 + throw new IllegalStateException("quoridor.dir property must be specified"); // NOI18N
1.126 + }
1.127 + File path = new File(prop);
1.128 + path.mkdirs();
1.129 + File fGames = new File(path, "games");
1.130 + //fGames.mkdirs();
1.131 + Games games = new Games(fGames, null);
1.132 + List<Game> lGames = games.getGames();
1.133 + Collections.sort(lGames, OLDEST_FIRST);
1.134 +
1.135 + // process games
1.136 + for(Game g: lGames){
1.137 + if(g.getId().isFinished()){
1.138 + eloTmp.processGame(g);
1.139 + openingsTmp.processGame(g);
1.140 + }
1.141 + }
1.142 +
1.143 + elo = eloTmp;
1.144 + openings = openingsTmp;
1.145 + }
1.146 +
1.147 + private static final class OldestFirst implements Comparator<Game> {
1.148 + public int compare(Game g1, Game g2) {
1.149 + if(g1.getId() == g2.getId())
1.150 + return 0;
1.151 + long diff = g2.getId().getModified() - g1.getId().getModified();
1.152 + if (diff != 0) {
1.153 + return diff < 0 ? -1 : 1;
1.154 + }
1.155 + return g1.getId().getId().compareTo(g2.getId().getId());
1.156 + }
1.157 + }
1.158 +
1.159 +}