statistics/src/main/java/cz/xelfi/quoridor/statistics/resources/Statistics.java
author Martin Rexa <martin.rexa@centrum.cz>
Wed, 13 Jan 2010 16:55:20 +0100
changeset 215 6744870122e0
parent 200 11c1287c7802
child 223 7416b11ad09f
permissions -rw-r--r--
processing games after midnight
jaroslav@178
     1
/*
jaroslav@178
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@178
     3
 *
jaroslav@178
     4
 * The contents of this file are subject to the terms of either the GNU
jaroslav@178
     5
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@178
     6
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@178
     7
 * "License"). You may not use this file except in compliance with the
jaroslav@178
     8
 * License. You can obtain a copy of the License at
jaroslav@178
     9
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@178
    10
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@178
    11
 * specific language governing permissions and limitations under the
jaroslav@178
    12
 * License.  When distributing the software, include this License Header
jaroslav@178
    13
 * Notice in each file and include the License file at
jaroslav@178
    14
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jaroslav@178
    15
 * particular file as subject to the "Classpath" exception as provided
jaroslav@178
    16
 * by Sun in the GPL Version 2 section of the License file that
jaroslav@178
    17
 * accompanied this code. If applicable, add the following below the
jaroslav@178
    18
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@178
    19
 * your own identifying information:
jaroslav@178
    20
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@178
    21
 *
jaroslav@178
    22
 * Contributor(s):
jaroslav@178
    23
 *
jaroslav@178
    24
 * Portions Copyrighted 2010 Martin Rexa
jaroslav@178
    25
 */
jaroslav@178
    26
jaroslav@178
    27
package cz.xelfi.quoridor.statistics.resources;
jaroslav@178
    28
jaroslav@178
    29
import com.sun.jersey.api.container.httpserver.HttpServerFactory;
jaroslav@178
    30
import com.sun.jersey.api.core.PackagesResourceConfig;
jaroslav@178
    31
import com.sun.jersey.api.core.ResourceConfig;
jaroslav@178
    32
import com.sun.jersey.spi.resource.Singleton;
jaroslav@178
    33
import com.sun.net.httpserver.HttpServer;
jaroslav@178
    34
import java.io.File;
jaroslav@178
    35
import java.io.IOException;
jaroslav@178
    36
import java.util.Comparator;
jaroslav@178
    37
import java.util.List;
jaroslav@178
    38
import java.util.Collections;
jaroslav@178
    39
import java.net.ServerSocket;
jaroslav@178
    40
import javax.ws.rs.Path;
jaroslav@178
    41
import cz.xelfi.quoridor.webidor.resources.Games;
jaroslav@178
    42
import cz.xelfi.quoridor.webidor.Game;
jaroslav@178
    43
jaroslav@178
    44
/**
jaroslav@178
    45
 *
jaroslav@178
    46
 * @author Martin Rexa
jaroslav@178
    47
 */
jaroslav@178
    48
@Path("/")
jaroslav@178
    49
@Singleton
jaroslav@178
    50
public class Statistics {
jaroslav@178
    51
    static final Comparator<Game> OLDEST_FIRST = new OldestFirst();
jaroslav@178
    52
    static Elo elo;
jaroslav@178
    53
    static Openings openings;
jaroslav@178
    54
jaroslav@178
    55
    @Path("elo")
jaroslav@178
    56
    public Elo getElo() {
jaroslav@178
    57
        return elo;
jaroslav@178
    58
    }
jaroslav@178
    59
jaroslav@178
    60
    @Path("openings")
jaroslav@178
    61
    public Openings getOpenings() {
jaroslav@178
    62
        return openings;
jaroslav@178
    63
    }
jaroslav@178
    64
jaroslav@178
    65
    public static void main( String[] args ) throws IOException, InterruptedException
jaroslav@178
    66
    {
jaroslav@178
    67
        int port = 9444;
jaroslav@178
    68
        // timeout between reprocessing games in miliseconds
martin@187
    69
        long timeout = 1000 * 60 * 60 * 24;
martin@187
    70
//        long timeout = 1000 * 10;
jaroslav@178
    71
jaroslav@178
    72
        try {
jaroslav@178
    73
            port = Integer.parseInt(args[0]);
jaroslav@178
    74
        } catch (Exception ex) {
jaroslav@178
    75
            // OK
jaroslav@178
    76
        }
jaroslav@178
    77
jaroslav@178
    78
        if (System.getProperty("quoridor.dir") == null) {
jaroslav@178
    79
            File home = new File(System.getProperty("user.home"));
jaroslav@178
    80
            File quoridor = new File(home, ".quoridor");
jaroslav@178
    81
            System.setProperty("quoridor.dir", quoridor.getPath());
jaroslav@178
    82
        }
jaroslav@178
    83
martin@215
    84
        long lastExec = System.currentTimeMillis();
jaroslav@178
    85
        processGames();
jaroslav@178
    86
        HttpServer s = start(port);
jaroslav@178
    87
        System.out.println("Statistics started at port " + port);
jaroslav@178
    88
        Object monitor = new Object();
martin@215
    89
        // Wait until begining of next period. If timeout is one day, then next
martin@215
    90
        // processing will be done after midnight
martin@215
    91
        synchronized (monitor) {
martin@215
    92
            monitor.wait(timeout - (lastExec % timeout));
martin@215
    93
        }
jaroslav@178
    94
        for(;;){
jaroslav@178
    95
            synchronized (monitor) {
jaroslav@178
    96
                monitor.wait(timeout);
jaroslav@178
    97
            }
jaroslav@178
    98
            processGames();
jaroslav@178
    99
        }
jaroslav@178
   100
    }
jaroslav@178
   101
jaroslav@178
   102
    public static HttpServer start(int port) throws IOException {
jaroslav@178
   103
        if (port == -1) {
jaroslav@178
   104
            ServerSocket ss = new ServerSocket(0);
jaroslav@178
   105
            port =ss.getLocalPort();
jaroslav@178
   106
            ss.close();
jaroslav@178
   107
        }
jaroslav@178
   108
        final String baseUri = "http://localhost:" + port + "/";
jaroslav@178
   109
jaroslav@178
   110
        if (System.getProperty("quoridor.dir") == null) {
jaroslav@178
   111
            File home = new File(System.getProperty("user.home"));
jaroslav@178
   112
            File quoridor = new File(home, ".quoridor");
jaroslav@178
   113
            System.setProperty("quoridor.dir", quoridor.getPath());
jaroslav@178
   114
        }
jaroslav@178
   115
jaroslav@178
   116
        ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.statistics");
jaroslav@178
   117
        HttpServer server = HttpServerFactory.create(baseUri, rc);
jaroslav@178
   118
        server.start();
jaroslav@178
   119
        return server;
jaroslav@178
   120
    }
jaroslav@178
   121
jaroslav@178
   122
    public static void processGames(){
jaroslav@178
   123
        Elo eloTmp = new Elo();
jaroslav@178
   124
        Openings openingsTmp = new Openings();
jaroslav@178
   125
jaroslav@178
   126
        final String prop = System.getProperty("quoridor.dir"); // NOI18N
jaroslav@178
   127
        if (prop == null) {
jaroslav@178
   128
            throw new IllegalStateException("quoridor.dir property must be specified"); // NOI18N
jaroslav@178
   129
        }
jaroslav@178
   130
        File path = new File(prop);
jaroslav@178
   131
        path.mkdirs();
jaroslav@178
   132
        File fGames = new File(path, "games");
jaroslav@178
   133
        //fGames.mkdirs();
jaroslav@178
   134
        Games games = new Games(fGames, null);
jaroslav@178
   135
        List<Game> lGames = games.getGames();
jaroslav@178
   136
        Collections.sort(lGames, OLDEST_FIRST);
jaroslav@178
   137
jaroslav@178
   138
        // process games
jaroslav@178
   139
        for(Game g: lGames){
jaroslav@178
   140
            if(g.getId().isFinished()){
martin@200
   141
                eloTmp.processGame(g.getId());
jaroslav@178
   142
                openingsTmp.processGame(g);
jaroslav@178
   143
            }
jaroslav@178
   144
        }
jaroslav@178
   145
jaroslav@178
   146
        elo = eloTmp;
jaroslav@178
   147
        openings = openingsTmp;
jaroslav@178
   148
    }
jaroslav@178
   149
jaroslav@178
   150
    private static final class OldestFirst implements Comparator<Game> {
jaroslav@178
   151
        public int compare(Game g1, Game g2) {
jaroslav@178
   152
            if(g1.getId() == g2.getId())
jaroslav@178
   153
                return 0;
jaroslav@178
   154
            long diff = g2.getId().getModified() - g1.getId().getModified();
jaroslav@178
   155
            if (diff != 0) {
martin@200
   156
                return diff < 0 ? 1 : -1;
jaroslav@178
   157
            }
jaroslav@178
   158
            return g1.getId().getId().compareTo(g2.getId().getId());
jaroslav@178
   159
        }
jaroslav@178
   160
    }
jaroslav@178
   161
jaroslav@178
   162
}