00001 00004 package org.jaebi.server.core; 00005 00006 import java.util.ArrayList; 00007 import java.util.Collection; 00008 import java.util.GregorianCalendar; 00009 import java.util.HashMap; 00010 import java.util.Iterator; 00011 00012 import org.jaebi.server.auth.Session; 00013 00046 public class TimeoutThreadedUserContainer 00047 extends UserContainerImpl implements Runnable { 00048 00058 class SessionLastAccessTimestamp { 00059 private final Session session; 00060 private long lastAccessTimestamp; 00061 00066 SessionLastAccessTimestamp ( Session newSession ) { 00067 session = newSession; 00068 update(); 00069 } 00070 00072 public synchronized void update() { 00073 lastAccessTimestamp = new GregorianCalendar().getTimeInMillis(); 00074 } 00075 00079 public Session getSession() { return session; } 00080 00084 public synchronized long getLastAccessTimestamp() { return lastAccessTimestamp; } 00085 } 00086 00091 private final HashMap timedSessions; 00092 00095 private final long timeOutCheckIntervall; 00097 private final long userTimeOut; 00099 private static final long sleepTime = 10000; // 10 sec 00100 00109 public TimeoutThreadedUserContainer(long userTimeOut, long timeOutCheckIntervall) { 00110 super(); 00111 00112 timedSessions = new HashMap(); 00113 this.userTimeOut = userTimeOut; 00114 this.timeOutCheckIntervall = timeOutCheckIntervall; 00115 new Thread(this).start(); 00116 } 00117 00126 public synchronized boolean addUser( User newUser ) { 00127 timedSessions.put( 00128 newUser.getNick(), 00129 new SessionLastAccessTimestamp( newUser.getSession() ) 00130 ); 00131 return super.addUser( newUser ); 00132 } 00133 00142 public synchronized boolean addUser( String nick, Session session, String type ) { 00143 timedSessions.put( 00144 nick, 00145 new SessionLastAccessTimestamp( session ) 00146 ); 00147 00148 return super.addUser( nick, session, type); 00149 } 00150 00156 public synchronized boolean removeUser( Session session ) { 00157 User user = super.getUser(session); 00158 if ( user != null ) 00159 timedSessions.remove( user.getNick() ); 00160 00161 return super.removeUser(session); 00162 } 00163 00169 public synchronized User getUser( Session session ) { 00170 User user = super.getUser(session); 00171 if ( user != null ) { 00172 SessionLastAccessTimestamp sessionLastAccess = 00173 (SessionLastAccessTimestamp)timedSessions.get( user.getNick() ); 00174 00175 sessionLastAccess.update(); 00176 } 00177 00178 return user; 00179 } 00180 00189 public void run() { 00190 // Recupero Set contenente tutte le SessionLastAccessTimestamp registrate 00191 Collection timedSessionsColl = timedSessions.values(); 00192 Iterator iter; 00193 SessionLastAccessTimestamp currSessionLastAccess; 00194 long actualTimestamp; 00195 // Collezione delle Sessioni degli User da eliminare 00196 ArrayList toDelete = new ArrayList(); 00197 00198 // Ciclo Indefinito: continua sino alla distruzione dell'Istanza 00199 while ( true ) { 00200 // Iteratore sugli oggetti SessionLastAccessTimestamp 00201 iter = timedSessionsColl.iterator(); 00202 // Timestamp attuale in millisecondi 00203 actualTimestamp = new GregorianCalendar().getTimeInMillis(); 00204 // Iteriamo sui SessionLastAccessTimestamp 00205 while ( iter.hasNext() ) { 00206 currSessionLastAccess = (SessionLastAccessTimestamp)iter.next(); 00207 // Se lo User e' inattivo da troppo tempo 00208 if ( 00209 ( actualTimestamp - 00210 currSessionLastAccess.getLastAccessTimestamp() ) 00211 >= this.userTimeOut 00212 ) { 00213 // Conservazione della Session da Eliminare 00214 toDelete.add( currSessionLastAccess.getSession() ); 00215 } 00216 } 00217 00218 // Iteratore sugli oggetti Session da Eliminare 00219 iter = toDelete.iterator(); 00220 while ( iter.hasNext() ) { 00221 // Rimozione User relativo alla Session "scaduta" 00222 removeUser( (Session)iter.next() ); 00223 } 00224 // Pulizia della lista di Session da Eliminare 00225 toDelete.clear(); 00226 // Suggerimento per la JVM di pulire la memoria 00227 System.gc(); 00228 00229 try { 00230 Thread.sleep(TimeoutThreadedUserContainer.sleepTime); 00231 } catch (InterruptedException e) { } 00232 } 00233 } 00234 00235 }