in special cases where the client did not follow the protocol, the
previous version would have a memory leak where ancient game states
weren't cleaned up.
attached are 2 patch files that should fix this issue.
--- Game.java Sun Aug 27 00:20:03 2006
+++ Game.java Sun Aug 27 00:20:03 2006
@@ -10,0 +11 @@
+ private long timestamp;
@@ -11,0 +13,4 @@
+ public Game(){
+ timestamp = System.currentTimeMillis();
+ }
+
@@ -63,0 +69,3 @@
+
+ public long getTimestamp(){
+ return timestamp;
@@ -64,0 +73 @@
+}
--- GameManager.java Sun Aug 27 00:46:57 2006
+++ GameManager.java Sun Aug 27 00:46:57 2006
@@ -9,0 +10,5 @@
+
+ Map<String, Game> openGames, startedGames;
+
+ Cleaner openGamesCleaner, startedGamesCleaner;
+
@@ -10,0 +16,7 @@
+ openGames = new HashMap();
+ startedGames = new HashMap();
+
+ openGamesCleaner = new Cleaner(openGames);
+ startedGamesCleaner = new Cleaner(startedGames);
+ openGamesCleaner.start();
+ startedGamesCleaner.start();
@@ -11,0 +24 @@
+
@@ -17,3 +29,0 @@
-
- Map<String, Game> open_games = new HashMap(), started_games = new
HashMap();
-
@@ -21 +31 @@
- open_games.put(game.getHost()+game.getPort(), game);
+ openGames.put(game.getHost()+game.getPort(), game);
@@ -26 +36 @@
- Game game = open_games.get(key);
+ Game game = openGames.get(key);
@@ -28,2 +38,2 @@
- started_games.put(key, game);
- open_games.remove(key);
+ startedGames.put(key, game);
+ openGames.remove(key);
@@ -34 +44 @@
- started_games.remove(key);
+ startedGames.remove(key);
@@ -39 +49 @@
- return open_games.containsKey(key);
+ return openGames.containsKey(key);
@@ -44 +54 @@
- return started_games.containsKey(key);
+ return startedGames.containsKey(key);
@@ -48,3 +58,3 @@
- ArrayList<Game> allGames = new
ArrayList(open_games.size()+started_games.size());
- allGames.addAll(open_games.values());
- allGames.addAll(started_games.values());
+ ArrayList<Game> allGames = new
ArrayList(openGames.size()+startedGames.size());
+ allGames.addAll(openGames.values());
+ allGames.addAll(startedGames.values());
@@ -61,0 +72,8 @@
+
+ class Cleaner extends Thread{
+ Map<String, Game> subject;
+
+ Cleaner(Map<String, Game> subject){
+ super("Cleaner");
+ this.subject = subject;
+ this.setDaemon(true);
@@ -62,0 +81,30 @@
+
+ public void run(){
+ System.out.println("starting cleaner thread.");
+ try {
+ long hour = 1000*60*60;
+ while(!this.isInterrupted()){
+ this.sleep(6*hour);
+
+ clean();
+ }
+ }catch(InterruptedException e){
+ ; // do nothing.
+ }finally{
+ System.out.println("stopping cleaner thread.");
+ }
+ }
+
+ void clean(){
+ Iterator<Game> itr = subject.values().iterator();
+ long now = System.currentTimeMillis();
+ long _24_hours_ago = now - (1000*60*60*24);
+
+ while(itr.hasNext()){
+ if(itr.next().getTimestamp() < _24_hours_ago){
+ itr.remove();
+ }
+ }
+ }
+ }
+}
_______________________________________________
Warzone-dev mailing list
[email protected]
https://mail.gna.org/listinfo/warzone-dev