KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > client > notify > NotifyData


1 package rero.client.notify;
2
3 import java.util.*;
4
5 import rero.client.*;
6 import rero.util.*;
7
8 import rero.ircfw.*;
9 import rero.ircfw.interfaces.*;
10
11 import rero.config.*;
12
13 //
14
// Documented bug - if there is no notify reply before the next one is sent and then both come one of them
15
// will be echo'd. Its not really worth working around this problem.
16
//
17
public class NotifyData extends Feature implements ChatListener, TimerListener, ClientStateListener
18 {
19    protected HashMap users = new HashMap();
20    protected Set signedon = new HashSet();
21    protected Lag lag = new Lag();
22
23    protected int isChecking = 0;
24
25    public void reset()
26    {
27       signedon = new HashSet();
28    }
29
30    public void propertyChanged(String JavaDoc value, String JavaDoc parameter)
31    {
32       hashUsers();
33    }
34
35    public void hashUsers()
36    {
37       HashMap newUsers = new HashMap();
38
39       Iterator i = ClientState.getClientState().getStringList("notify.users").getList().iterator();
40       while (i.hasNext())
41       {
42          String JavaDoc temp = (String JavaDoc)i.next();
43          if (users.containsKey(temp))
44          {
45             newUsers.put(temp, users.get(temp));
46          }
47          else
48          {
49             newUsers.put(temp, createNotifyUser(temp));
50          }
51       }
52
53       users = newUsers;
54    }
55
56    public void addUser(String JavaDoc nickname) // *permanently adds user to notify list*
57
{
58       StringList temp = ClientState.getClientState().getStringList("notify.users");
59       temp.add(nickname);
60       temp.save();
61       ClientState.getClientState().sync();
62    }
63
64    public void removeUser(String JavaDoc nickname)
65    {
66       StringList temp = ClientState.getClientState().getStringList("notify.users");
67       temp.remove(nickname);
68       temp.save();
69       ClientState.getClientState().sync();
70    }
71
72    public Set getSignedOnUsers()
73    {
74       Set rv = new HashSet();
75
76       Iterator i = users.values().iterator();
77       while (i.hasNext())
78       {
79          NotifyUser temp = (NotifyUser)i.next();
80          if (temp.isSignedOn())
81          {
82             rv.add(temp);
83          }
84       }
85  
86       return rv;
87    }
88
89    public Set getNotifyUsers()
90    {
91       return new HashSet(users.values());
92    }
93
94    public Set getSignedOffUsers()
95    {
96       Set rv = new HashSet();
97
98       Iterator i = users.values().iterator();
99       while (i.hasNext())
100       {
101          NotifyUser temp = (NotifyUser)i.next();
102          if (!temp.isSignedOn())
103          {
104             rv.add(temp);
105          }
106       }
107  
108       return rv;
109    }
110
111    public void init()
112    {
113       hashUsers();
114
115       getCapabilities().addChatListener(this);
116       getCapabilities().getTimer().addTimer(this, 60 * 1000); // check the notify list every 60 seconds.
117

118       ClientState.getClientState().addClientStateListener("notify.users", this);
119    }
120
121    public void cleanup()
122    {
123       getCapabilities().getTimer().stopTimer(this);
124    }
125
126    public void storeDataStructures(WeakHashMap data)
127    {
128       data.put("lag", lag);
129       data.put("notify", this);
130    }
131
132    public NotifyUser createNotifyUser(String JavaDoc nickname)
133    {
134       NotifyUser temp = new NotifyUser(nickname);
135       temp.installCapabilities(getCapabilities());
136
137       return temp;
138    }
139
140    public NotifyUser getUserInfo(String JavaDoc nickname)
141    {
142       return (NotifyUser)users.get(nickname);
143    }
144
145    public void checkNotify()
146    {
147       if (getCapabilities().isConnected())
148       {
149          isChecking++;
150
151          StringBuffer JavaDoc temp = new StringBuffer JavaDoc("ISON :");
152          Iterator i = users.keySet().iterator();
153          while (i.hasNext())
154          {
155             temp.append((String JavaDoc)i.next());
156             temp.append(" ");
157          }
158
159          lag.checkLag();
160          getCapabilities().sendln(temp.toString());
161       }
162    }
163
164    public void timerExecute()
165    {
166       checkNotify();
167    }
168
169    public int fireChatEvent (HashMap eventDescription)
170    {
171       if (eventDescription.get("$event").equals("376"))
172       {
173           // We are connected, check the notify
174
//
175
checkNotify();
176           return EVENT_DONE;
177       }
178
179       lag.setLag();
180
181       if (eventDescription.get("$event").equals("303"))
182       {
183          String JavaDoc parms = (String JavaDoc)eventDescription.get("$parms");
184
185          TokenizedString temp = new TokenizedString(parms);
186          temp.tokenize(" ");
187
188          Set newbatch = new HashSet();
189
190          for (int x = 0; x < temp.getTotalTokens(); x++)
191          {
192             NotifyUser user = getUserInfo(temp.getToken(x));
193
194             if (user != null && !user.isSignedOn())
195             {
196                user.signOn();
197             }
198             newbatch.add(user);
199          }
200
201          signedon.removeAll(newbatch);
202
203          Iterator i = signedon.iterator();
204          while (i.hasNext())
205          {
206             NotifyUser user = (NotifyUser)i.next();
207             if (user != null && user.isSignedOn() && users.containsKey(user.toString()))
208             {
209                user.signOff();
210             }
211          }
212
213          signedon = newbatch;
214       }
215
216       isChecking--;
217
218       return EVENT_HALT;
219    }
220
221    public boolean isChatEvent(String JavaDoc eventId, HashMap eventDescription)
222    {
223       // :lug.mtu.edu 303 ^butang :shrunk mutilator `butane
224
return (isChecking > 0 && (eventId.equals("303") || eventId.equals("461"))) || eventId.equals("376");
225    }
226 }
227
Popular Tags