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@283
|
81 |
/*
|
martin@283
|
82 |
* Disabled until OutOfMemory is solved
|
martin@215
|
83 |
// Wait until begining of next period. If timeout is one day, then next
|
martin@215
|
84 |
// processing will be done after midnight
|
martin@215
|
85 |
synchronized (monitor) {
|
martin@215
|
86 |
monitor.wait(timeout - (lastExec % timeout));
|
martin@215
|
87 |
}
|
jaroslav@178
|
88 |
for(;;){
|
martin@223
|
89 |
processGames();
|
jaroslav@178
|
90 |
synchronized (monitor) {
|
jaroslav@178
|
91 |
monitor.wait(timeout);
|
jaroslav@178
|
92 |
}
|
jaroslav@178
|
93 |
}
|
martin@283
|
94 |
*
|
martin@283
|
95 |
*/
|
martin@283
|
96 |
synchronized (monitor) {
|
martin@283
|
97 |
monitor.wait(timeout);
|
martin@283
|
98 |
}
|
jaroslav@178
|
99 |
}
|
jaroslav@178
|
100 |
|
jaroslav@178
|
101 |
public static HttpServer start(int port) throws IOException {
|
jaroslav@178
|
102 |
if (port == -1) {
|
jaroslav@178
|
103 |
ServerSocket ss = new ServerSocket(0);
|
jaroslav@178
|
104 |
port =ss.getLocalPort();
|
jaroslav@178
|
105 |
ss.close();
|
jaroslav@178
|
106 |
}
|
jaroslav@178
|
107 |
final String baseUri = "http://localhost:" + port + "/";
|
jaroslav@178
|
108 |
|
jaroslav@178
|
109 |
if (System.getProperty("quoridor.dir") == null) {
|
jaroslav@178
|
110 |
File home = new File(System.getProperty("user.home"));
|
jaroslav@178
|
111 |
File quoridor = new File(home, ".quoridor");
|
jaroslav@178
|
112 |
System.setProperty("quoridor.dir", quoridor.getPath());
|
jaroslav@178
|
113 |
}
|
jaroslav@178
|
114 |
|
jaroslav@178
|
115 |
ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.statistics");
|
jaroslav@178
|
116 |
HttpServer server = HttpServerFactory.create(baseUri, rc);
|
jaroslav@178
|
117 |
server.start();
|
jaroslav@178
|
118 |
return server;
|
jaroslav@178
|
119 |
}
|
jaroslav@178
|
120 |
|
jaroslav@178
|
121 |
public static void processGames(){
|
jaroslav@178
|
122 |
Elo eloTmp = new Elo();
|
jaroslav@178
|
123 |
Openings openingsTmp = new Openings();
|
jaroslav@178
|
124 |
|
jaroslav@178
|
125 |
final String prop = System.getProperty("quoridor.dir"); // NOI18N
|
jaroslav@178
|
126 |
if (prop == null) {
|
jaroslav@178
|
127 |
throw new IllegalStateException("quoridor.dir property must be specified"); // NOI18N
|
jaroslav@178
|
128 |
}
|
jaroslav@178
|
129 |
File path = new File(prop);
|
jaroslav@178
|
130 |
path.mkdirs();
|
jaroslav@178
|
131 |
File fGames = new File(path, "games");
|
jaroslav@178
|
132 |
//fGames.mkdirs();
|
jaroslav@178
|
133 |
Games games = new Games(fGames, null);
|
jaroslav@178
|
134 |
List<Game> lGames = games.getGames();
|
jaroslav@178
|
135 |
Collections.sort(lGames, OLDEST_FIRST);
|
jaroslav@178
|
136 |
|
jaroslav@178
|
137 |
// process games
|
jaroslav@178
|
138 |
for(Game g: lGames){
|
jaroslav@178
|
139 |
if(g.getId().isFinished()){
|
martin@200
|
140 |
eloTmp.processGame(g.getId());
|
jaroslav@178
|
141 |
openingsTmp.processGame(g);
|
jaroslav@178
|
142 |
}
|
jaroslav@178
|
143 |
}
|
jaroslav@178
|
144 |
|
jaroslav@178
|
145 |
elo = eloTmp;
|
jaroslav@178
|
146 |
openings = openingsTmp;
|
jaroslav@178
|
147 |
}
|
jaroslav@178
|
148 |
|
jaroslav@178
|
149 |
private static final class OldestFirst implements Comparator<Game> {
|
jaroslav@178
|
150 |
public int compare(Game g1, Game g2) {
|
jaroslav@178
|
151 |
if(g1.getId() == g2.getId())
|
jaroslav@178
|
152 |
return 0;
|
jaroslav@178
|
153 |
long diff = g2.getId().getModified() - g1.getId().getModified();
|
jaroslav@178
|
154 |
if (diff != 0) {
|
martin@200
|
155 |
return diff < 0 ? 1 : -1;
|
jaroslav@178
|
156 |
}
|
jaroslav@178
|
157 |
return g1.getId().getId().compareTo(g2.getId().getId());
|
jaroslav@178
|
158 |
}
|
jaroslav@178
|
159 |
}
|
jaroslav@178
|
160 |
|
jaroslav@178
|
161 |
}
|