2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * The contents of this file are subject to the terms of either the GNU
5 * General Public License Version 2 only ("GPL") or the Common
6 * Development and Distribution License("CDDL") (collectively, the
7 * "License"). You may not use this file except in compliance with the
8 * License. You can obtain a copy of the License at
9 * http://www.netbeans.org/cddl-gplv2.html
10 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
11 * specific language governing permissions and limitations under the
12 * License. When distributing the software, include this License Header
13 * Notice in each file and include the License file at
14 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
15 * particular file as subject to the "Classpath" exception as provided
16 * by Sun in the GPL Version 2 section of the License file that
17 * accompanied this code. If applicable, add the following below the
18 * License Header, with the fields enclosed by brackets [] replaced by
19 * your own identifying information:
20 * "Portions Copyrighted [year] [name of copyright owner]"
24 * Portions Copyrighted 2010 Martin Rexa
27 package cz.xelfi.quoridor.statistics.resources;
29 import com.sun.jersey.api.container.httpserver.HttpServerFactory;
30 import com.sun.jersey.api.core.PackagesResourceConfig;
31 import com.sun.jersey.api.core.ResourceConfig;
32 import com.sun.jersey.spi.resource.Singleton;
33 import com.sun.net.httpserver.HttpServer;
35 import java.io.IOException;
36 import java.util.Comparator;
37 import java.util.List;
38 import java.util.Collections;
39 import java.net.ServerSocket;
40 import javax.ws.rs.Path;
41 import cz.xelfi.quoridor.webidor.resources.Games;
42 import cz.xelfi.quoridor.webidor.Game;
50 public class Statistics {
51 static final Comparator<Game> OLDEST_FIRST = new OldestFirst();
53 static Openings openings;
61 public Openings getOpenings() {
65 public static void main( String[] args ) throws IOException, InterruptedException
68 // timeout between reprocessing games in miliseconds
69 long timeout = 1000 * 60 * 60 * 24;
70 // long timeout = 1000 * 10;
73 port = Integer.parseInt(args[0]);
74 } catch (Exception ex) {
78 if (System.getProperty("quoridor.dir") == null) {
79 File home = new File(System.getProperty("user.home"));
80 File quoridor = new File(home, ".quoridor");
81 System.setProperty("quoridor.dir", quoridor.getPath());
84 long lastExec = System.currentTimeMillis();
86 HttpServer s = start(port);
87 System.out.println("Statistics started at port " + port);
88 Object monitor = new Object();
89 // Wait until begining of next period. If timeout is one day, then next
90 // processing will be done after midnight
91 synchronized (monitor) {
92 monitor.wait(timeout - (lastExec % timeout));
95 synchronized (monitor) {
96 monitor.wait(timeout);
102 public static HttpServer start(int port) throws IOException {
104 ServerSocket ss = new ServerSocket(0);
105 port =ss.getLocalPort();
108 final String baseUri = "http://localhost:" + port + "/";
110 if (System.getProperty("quoridor.dir") == null) {
111 File home = new File(System.getProperty("user.home"));
112 File quoridor = new File(home, ".quoridor");
113 System.setProperty("quoridor.dir", quoridor.getPath());
116 ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.statistics");
117 HttpServer server = HttpServerFactory.create(baseUri, rc);
122 public static void processGames(){
123 Elo eloTmp = new Elo();
124 Openings openingsTmp = new Openings();
126 final String prop = System.getProperty("quoridor.dir"); // NOI18N
128 throw new IllegalStateException("quoridor.dir property must be specified"); // NOI18N
130 File path = new File(prop);
132 File fGames = new File(path, "games");
134 Games games = new Games(fGames, null);
135 List<Game> lGames = games.getGames();
136 Collections.sort(lGames, OLDEST_FIRST);
140 if(g.getId().isFinished()){
141 eloTmp.processGame(g.getId());
142 openingsTmp.processGame(g);
147 openings = openingsTmp;
150 private static final class OldestFirst implements Comparator<Game> {
151 public int compare(Game g1, Game g2) {
152 if(g1.getId() == g2.getId())
154 long diff = g2.getId().getModified() - g1.getId().getModified();
156 return diff < 0 ? 1 : -1;
158 return g1.getId().getId().compareTo(g2.getId().getId());