View Javadoc

1   package twcsckernel.serverKernel.usr;
2   
3   import java.util.Set;
4   import java.util.Timer;
5   import java.util.TimerTask;
6   
7   import twcsckernel.serverKernel.impl.UserImpl;
8   
9   /***
10   * Klasa odpowiedzialna za wylogowanie użytkowników, którzy nie wykazywali
11   * aktywności dłużej niż przez zadany czas. Pełni funkcję user garbage
12   * collectora.
13   * 
14   * @author VMD Group
15   * 
16   */
17  public class UserGarbageCollector {
18  
19  	private long interval;
20  
21  	private Timer timer = null;
22  
23  	private Object timerMutex = new Object();
24  
25  	private Set<UserImpl> registeredUserSet = null;
26  
27  	private TimerTask gcTask = new TimerTask() {
28  
29  		@Override
30  		public void run() {
31  			synchronized (timerMutex) {
32  				if (timer == null)
33  					return;
34  				synchronized (registeredUserSet) {
35  					long newStamp = System.currentTimeMillis();
36  					//wykonywana jest kopia ze wzgledu na to, ze
37  					//user.kickUser() modyfikuje posrednio
38  					//registeredUserSet
39  					UserImpl[] tmpUsrCopy = registeredUserSet
40  							.toArray(new UserImpl[0]);
41  					long diff;
42  					for (UserImpl user : tmpUsrCopy) {
43  						diff = newStamp - user.getTimeStamp();
44  						if (diff > interval)
45  							user.kickUser();
46  					}
47  				}
48  			}
49  
50  		}
51  	};
52  
53  	/***
54  	 * Metoda ustawia zbiór zarejestrowanych użytkowników na którym będzie
55  	 * działać user garbage collector.
56  	 * 
57  	 * @param registeredUserSet
58  	 *            zbiór zarejestrowanych użytkowników
59  	 */
60  	public void setUserSet(Set<UserImpl> registeredUserSet) {
61  		synchronized (timerMutex) {
62  			boolean ifEnabled = timer != null;
63  			disable();
64  			this.registeredUserSet = registeredUserSet;
65  			if (ifEnabled)
66  				enable();
67  		}
68  
69  	}
70  
71  	/***
72  	 * Metoda wyłącza działanie garbage collectora.
73  	 */
74  	public void disable() {
75  		synchronized (timerMutex) {
76  			if (timer != null) {
77  				timer.cancel();
78  				timer = null;
79  			}
80  		}
81  	}
82  
83  	/***
84  	 * Metoda włącza działanie garbage collectora o ile zbiór użytkowników
85  	 * został ustawiony oraz zadany interwał czasowy jest większy od zera.
86  	 */
87  	public void enable() {
88  		synchronized (timerMutex) {
89  			if (registeredUserSet == null)
90  				return;
91  			if (interval == 0) {
92  				disable();
93  				return;
94  			}
95  			timer = new Timer(true);
96  			timer.schedule(gcTask, interval, interval);
97  		}
98  	}
99  
100 	/***
101 	 * Metoda ustawia interwał czasowy co który user garbage collector będzie
102 	 * przeglądał stan użytkowników.
103 	 * 
104 	 * @param interval -
105 	 *            interwał czasowy w milisekundach
106 	 */
107 	public void setInterval(long interval) {
108 		synchronized (timerMutex) {
109 			boolean ifEnabled = timer != null;
110 			disable();
111 			this.interval = interval;
112 			if (ifEnabled)
113 				enable();
114 		}
115 	}
116 
117 }