Source code: edu/ou/kmi/buddyspace/core/BSAgentsBean.java
1 package edu.ou.kmi.buddyspace.core;
2
3 /*
4 * BSAgentsBean.java
5 *
6 * Project: BuddySpace
7 * (C) Copyright Knowledge Media Institute 2002
8 *
9 *
10 * Created on 7 August 2002, 10:03
11 */
12
13 import java.util.*;
14 import org.jabber.jabberbeans.*;
15 import org.jabber.jabberbeans.Extension.*;
16
17 /**
18 * <code>BSAgentsBean</code> provides agents functionality handling.
19 * It relies on <code>BSInfoQueryBean</code>, which must be set after each
20 * reconnection.
21 *
22 * @author Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
23 */
24 public class BSAgentsBean implements PacketListener {
25 protected String servedID = null;
26 protected IQBean iqBean = null;
27 protected IQAgents agents = null;
28 protected final String name = "Agents";
29 protected Vector agentsListeners;
30
31 /**
32 * Constructor
33 */
34 public BSAgentsBean() {
35 //agents = new Hashtable();
36 agentsListeners = new Vector();
37 }
38
39 /**
40 * Constructor, which sets existing and connected <code>IQBean</code>.
41 * Then this is registered as listener for IQ packets.
42 */
43 public BSAgentsBean(IQBean iqBean) {
44 this();
45 setIQBean(iqBean);
46 }
47
48 /**
49 * Sets existing and connected <code>IQBean</code>.
50 * Then this is registered as listener for IQ packets.
51 */
52 public void setIQBean(IQBean iqBean) {
53 if (this.iqBean != null)
54 this.iqBean.delPacketListener(this);
55 this.iqBean = iqBean;
56 if (iqBean != null)
57 iqBean.addPacketListener(this);
58 servedID = null;
59 }
60
61 /**
62 * Returns currently used <code>IQBean</code>.
63 */
64 public IQBean getIQBean() {
65 return iqBean;
66 }
67
68 /**
69 * Frees all object bindings to allow object destroy
70 */
71 public void prepareToDestroy() {
72 if (iqBean != null)
73 iqBean.delPacketListener(this);
74 removeAllAgentsListeners();
75 iqBean = null;
76 }
77
78 /**
79 * Sends request for agents list.
80 */
81 public boolean getAgents() {
82
83 if (iqBean == null || iqBean.getConnection() == null) {
84 BSCore.logEvent(name, "error: not connected");
85 servedID = null;
86 return false;
87 }
88
89 // gets agents list
90 servedID = new String("GET_AGENTS_" + String.valueOf(BSCore.getNextID()));
91 InfoQueryBuilder iqBuilder = new InfoQueryBuilder();
92 IQAgentsBuilder iqAgentsBuilder = new IQAgentsBuilder();
93
94 try {
95 iqBuilder.addExtension(iqAgentsBuilder.build());
96 iqBuilder.setType("get");
97 iqBuilder.setIdentifier(servedID);
98 //iqBean.send((InfoQuery)iqBuilder.build());
99 iqBean.getConnection().send(iqBuilder.build());
100 } catch (InstantiationException e) {
101 BSCore.logEvent(name, "error: IQ builder failed");
102 servedID = null;
103 return false;
104 }
105
106 BSCore.logEvent(name, "getting agents list");
107 return true;
108 }
109
110 /**
111 * Returns <code>IQAgents</code> extension.
112 */
113 public IQAgents agents() {
114 return agents;
115 }
116
117 /**
118 * Returns <code>Enumeration</code> of agents.
119 */
120 public Enumeration agentsEnumeration() {
121 // reads the agents and stores into hashtable
122 Vector agentsVector = new Vector();
123 Enumeration agentsEnum = agents.agents();
124 while (agentsEnum.hasMoreElements()) {
125 Agent a = (Agent) agentsEnum.nextElement();
126 agentsVector.add(a);
127 }
128 return agentsVector.elements();
129 }
130
131 // *** packet handling ***
132
133 /**
134 * Invoked when a IQ packet is received.
135 */
136 public void receivedPacket(PacketEvent pe) {
137 if (!(pe.getPacket() instanceof InfoQuery)) {
138 BSCore.logEvent(name, "warning: nonIQ packet received");
139 return;
140 }
141
142 InfoQuery iq = (InfoQuery) pe.getPacket();
143 if (iq.getIdentifier() == null || !iq.getIdentifier().equals(servedID)) {
144 return;
145 }
146
147 if ((new String("result")).equals(iq.getType()))
148 handleResult(iq);
149 else if ((new String("error")).equals(iq.getType()))
150 handleError(iq);
151 else if ((new String("set")).equals(iq.getType()))
152 handleSet(iq);
153 }
154
155 /**
156 * Invoked when a IQ packet send failes.
157 */
158 public void sendFailed(PacketEvent pe) {
159 }
160
161 /**
162 * Invoked when a IQ packet is sent.
163 */
164 public void sentPacket(PacketEvent pe) {
165 }
166
167 // *** infoQuery handling ***
168
169 /**
170 * Handles <code>InfoQuery</code> packet, if it does contain an error.
171 * Before this is called it checks if that is response on the sent IQ
172 * agents packet.
173 */
174 protected void handleError(InfoQuery iq) {
175 BSCore.logEvent(name, "error " + iq.getErrorCode() + ": " + iq.getErrorText());
176 servedID = null;
177 fireAgentsError(iq);
178 }
179
180 /**
181 * Handles <code>InfoQuery</code> packet, if it does contain a result.
182 * Before this is called it checks if that is response on the sent IQ
183 * agents packet.
184 */
185 protected void handleResult(InfoQuery iq) {
186 Enumeration extensions = iq.Extensions();
187
188 // for all extensions
189 while (extensions != null && extensions.hasMoreElements()) {
190 Extension ext = (Extension) extensions.nextElement();
191 // if that is a agents extension
192 if (ext instanceof IQAgents) {
193 agents = (IQAgents) ext;
194 // fires that agents list arrived
195 BSCore.logEvent(name, "agents list received");
196 fireAgentsListReceived();
197 }
198 else {
199 BSCore.logEvent(name, "error: unexpected IQ extension");
200 servedID = null;
201 }
202 }
203 }
204
205 /**
206 * Handles <code>InfoQuery</code> packet, if it IQ-set.
207 * Before this is called it checks if that is response on the sent IQ
208 * agents packet.
209 */
210 protected void handleSet(InfoQuery iq) {
211 return;
212 }
213
214 // *** agents listeners ***
215
216 /**
217 * Adds <code>BSAgentsListener</code> to listeners notified when
218 * agents event occures.
219 *
220 * @see #removeAgentsListener
221 * @see #removeAllAgentsListeners
222 * @see #fireAgentsListReceived
223 */
224 public void addAgentsListener(BSAgentsListener listener) {
225 //assert listener != null : listener;
226 if (listener == null) return;
227 if (!agentsListeners.contains(listener)) {
228 agentsListeners.addElement(listener);
229 }
230 }
231
232 /**
233 * Removes <code>BSAgentsListener</code> to listeners notified when
234 * agents event occures.
235 *
236 * @see #addAgentsListener
237 * @see #removeAllAgentsListeners
238 * @see #fireAgentsListReceived
239 */
240 public void removeAgentsListener(BSAgentsListener listener) {
241 //assert listener != null : listener;
242 if (listener == null) return;
243 agentsListeners.removeElement(listener);
244 }
245
246 /**
247 * Removes all listeners notified when agents event occures.
248 * This can be used before to free dependencies and allow dispose of
249 * all objects.
250 *
251 * @see #addAgentsListener
252 * @see #removeAgentsListener
253 * @see #fireAgentsListReceived
254 */
255 public void removeAllAgentsListeners() {
256 agentsListeners.clear();
257 }
258
259 /**
260 * Notifies agents listeners when agents list arrives.
261 *
262 * @see #addAgentsListener
263 * @see #removeAgentsListener
264 * @see #removeAllAgentsListeners
265 */
266 protected void fireAgentsListReceived() {
267 for (Enumeration e = agentsListeners.elements(); e.hasMoreElements(); ) {
268 BSAgentsListener listener = (BSAgentsListener) e.nextElement();
269
270 listener.agentsListReceived();
271 }
272 }
273
274 /**
275 * Notifies agents listeners when error arrives.
276 *
277 * @see #addAgentsListener
278 * @see #removeAgentsListener
279 * @see #removeAllAgentsListeners
280 */
281 protected void fireAgentsError(InfoQuery iq) {
282 for (Enumeration e = agentsListeners.elements(); e.hasMoreElements(); ) {
283 BSAgentsListener listener = (BSAgentsListener) e.nextElement();
284
285 listener.agentsError(iq);
286 }
287 }
288
289 }