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