KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sellwin > server > ChatReaper


1 package sellwin.server;
2
3 import java.io.*;
4 import java.net.*;
5 import java.util.*;
6 import sellwin.domain.*;
7
8 // SellWin http://sourceforge.net/projects/sellwincrm
9
//Contact support@open-app.com for commercial help with SellWin
10
//This software is provided "AS IS", without a warranty of any kind.
11

12 /**
13  * this class is a thread that runs all the time
14  * in the chat server to see if any online users
15  * have logged off or died and therefore need to
16  * be removed from the global online list of users
17  */

18 public class ChatReaper extends Thread JavaDoc {
19
20     /** the list of online users */
21     private TreeMap onlineUsers = null;
22
23     /**
24      * construct the thread, referring to
25      * the online users list
26      * @param onlineUsers the online users list reference
27      */

28     public ChatReaper(TreeMap onlineUsers) {
29         this.onlineUsers = onlineUsers;
30     }
31
32     /**
33      * this is the run() of this thread which executes all the
34      * time, waking up at intervals, does the online users check
35      * and goes back to sleep
36      */

37     public void run() {
38         try {
39             while(true) {
40                 sleep(9000);
41                 userCheck();
42             }
43         } catch (InterruptedException JavaDoc e) {
44             e.printStackTrace();
45         }
46     }
47
48     /**
49      * check the online list for dead users
50      */

51     private void userCheck() {
52         ChatConnInfo info = null;
53         Object JavaDoc[] keys = onlineUsers.keySet().toArray();
54
55         if (keys == null) return;
56
57         for (int i=0;i<onlineUsers.size();i++) {
58             info = (ChatConnInfo)onlineUsers.get(keys[i]);
59             System.out.println(info.getSender() + " connected since " + info.getConnectedSince());
60             if (info.stillConnected())
61                 System.out.println(info.getSender() + " not reaped yet");
62             else {
63                 System.out.println("reaping " + info.getSender());
64                 onlineUsers.remove(info.getSender());
65             }
66         }
67     }
68 }
69
Popular Tags