1 | /* |
---|
2 | * Jeti, a Java Jabber client, Copyright (C) 2003 E.S. de Boer |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation; either version 2 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program; if not, write to the Free Software |
---|
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | * |
---|
18 | * For questions, comments etc, |
---|
19 | * use the website at http://jeti.jabberstudio.org |
---|
20 | * or mail me at eric@jeti.tk |
---|
21 | */ |
---|
22 | |
---|
23 | package nu.fw.jeti.backend; |
---|
24 | |
---|
25 | import java.util.HashMap; |
---|
26 | import java.util.Map; |
---|
27 | import java.util.Timer; |
---|
28 | import java.util.TimerTask; |
---|
29 | |
---|
30 | import nu.fw.jeti.events.DiscoveryListener; |
---|
31 | import nu.fw.jeti.events.StatusChangeListener; |
---|
32 | import nu.fw.jeti.jabber.Backend; |
---|
33 | import nu.fw.jeti.jabber.JID; |
---|
34 | import nu.fw.jeti.jabber.elements.*; |
---|
35 | |
---|
36 | /** |
---|
37 | * @author E.S. de Boer |
---|
38 | */ |
---|
39 | //created on 6-8-2004 |
---|
40 | public class Discovery implements DiscoveryListener, StatusChangeListener |
---|
41 | { |
---|
42 | private Backend backend; |
---|
43 | private Map infoCache = new HashMap(); |
---|
44 | private Map itemCache = new HashMap(); |
---|
45 | private Map itemRequests = new HashMap(5); |
---|
46 | private Map infoRequests = new HashMap(10); |
---|
47 | private Map timeoutTimers = new HashMap(20); |
---|
48 | private Timer timer = new Timer(true); |
---|
49 | |
---|
50 | private int idCount; |
---|
51 | |
---|
52 | public Discovery(Backend backend) |
---|
53 | { |
---|
54 | this.backend = backend; |
---|
55 | } |
---|
56 | |
---|
57 | public void getItems(JID jid,DiscoveryListener listener) { |
---|
58 | getItems(jid, listener, true); |
---|
59 | } |
---|
60 | |
---|
61 | public void getItems(JID jid,DiscoveryListener listener, boolean useCache) |
---|
62 | { |
---|
63 | if (listener == null) listener = this; |
---|
64 | DiscoveryItem item = null; |
---|
65 | if (!useCache || null == (item = (DiscoveryItem)itemCache.get(jid))) |
---|
66 | {//first try disco |
---|
67 | String id = "Jeti_Disco_" + idCount++; |
---|
68 | InfoQuery iq = new InfoQuery(jid,"get",id,new IQDiscoItems()); |
---|
69 | timeout(iq); |
---|
70 | itemRequests.put(id,listener); |
---|
71 | backend.send(iq); |
---|
72 | } else { |
---|
73 | listener.discoveryItemResult(jid,item); |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | public void getInfo(JID jid,DiscoveryListener listener) |
---|
78 | { |
---|
79 | if (listener == null) listener = this; |
---|
80 | DiscoveryInfo info = (DiscoveryInfo)infoCache.get(jid); |
---|
81 | if(info == null) |
---|
82 | {//first try disco |
---|
83 | String id = "Jeti_Disco_" + idCount++; |
---|
84 | InfoQuery iq =new InfoQuery(jid,"get",id,new IQDiscoInfo()); |
---|
85 | timeout(iq); |
---|
86 | infoRequests.put(id,listener); |
---|
87 | backend.send(iq); |
---|
88 | } |
---|
89 | else listener.discoveryInfoResult(jid,info); |
---|
90 | } |
---|
91 | |
---|
92 | //no cache TODO add no browse if node query error |
---|
93 | public void getItems(JID jid,String node,DiscoveryListener listener) |
---|
94 | { |
---|
95 | if (listener == null) return;//no cache so empty listener is useless |
---|
96 | //DiscoveryItem item = (DiscoveryItem)itemCache.get(jid); |
---|
97 | //if(item == null) |
---|
98 | { |
---|
99 | String id = "Jeti_Disco_" + idCount++; |
---|
100 | InfoQuery iq = new InfoQuery(jid,"get",id,new IQDiscoItems(node)); |
---|
101 | timeout(iq); |
---|
102 | itemRequests.put(id,listener); |
---|
103 | backend.send(iq); |
---|
104 | } |
---|
105 | //else listener.discoveryItemResult(jid,item); |
---|
106 | } |
---|
107 | |
---|
108 | public void getInfo(JID jid,String node,DiscoveryListener listener) |
---|
109 | { |
---|
110 | if (listener == null) listener = this; |
---|
111 | DiscoveryInfo info = (DiscoveryInfo)infoCache.get(jid); |
---|
112 | if(info == null) |
---|
113 | {//first try disco |
---|
114 | String id = "Jeti_Disco_" + idCount++; |
---|
115 | InfoQuery iq =new InfoQuery(jid,"get",id,new IQDiscoInfo(node)); |
---|
116 | timeout(iq); |
---|
117 | infoRequests.put(id,listener); |
---|
118 | backend.send(iq); |
---|
119 | } |
---|
120 | else listener.discoveryInfoResult(jid,info); |
---|
121 | } |
---|
122 | |
---|
123 | public void discoveryInfoResult(JID jid,String id,IQDiscoInfo info) |
---|
124 | { |
---|
125 | TimerTask t =(TimerTask) timeoutTimers.remove(id); |
---|
126 | if(t!=null)t.cancel(); |
---|
127 | infoCache.put(jid,info); |
---|
128 | DiscoveryListener d = (DiscoveryListener)infoRequests.remove(id); |
---|
129 | if(d!=null) d.discoveryInfoResult(jid,info); |
---|
130 | } |
---|
131 | |
---|
132 | public void discoveryItemResult(JID jid,String id,IQDiscoItems item) |
---|
133 | { |
---|
134 | TimerTask t =(TimerTask) timeoutTimers.remove(id); |
---|
135 | if(t!=null)t.cancel(); |
---|
136 | itemCache.put(jid,item); |
---|
137 | DiscoveryListener d =(DiscoveryListener)itemRequests.remove(id); |
---|
138 | if(d!=null)d.discoveryItemResult(jid,item); |
---|
139 | } |
---|
140 | |
---|
141 | |
---|
142 | // public void browseResult(JID jid,String id,IQBrowse item) |
---|
143 | // { |
---|
144 | // TimerTask t =(TimerTask) timeoutTimers.remove(id); |
---|
145 | // if(t!=null)t.cancel(); |
---|
146 | // if(itemRequests.containsKey(id)) |
---|
147 | // { |
---|
148 | // itemCache.put(item.getJID(),item); |
---|
149 | // DiscoveryListener d =(DiscoveryListener)itemRequests.remove(id); |
---|
150 | // if(d!=null)d.discoveryItemResult(jid,item); |
---|
151 | // } |
---|
152 | // else if(infoRequests.containsKey(id)) |
---|
153 | // { |
---|
154 | // infoCache.put(item.getJID(),item); |
---|
155 | // DiscoveryListener d = (DiscoveryListener)infoRequests.remove(id); |
---|
156 | // if(d!=null)d.discoveryInfoResult(jid,item); |
---|
157 | // } |
---|
158 | // } |
---|
159 | |
---|
160 | public void discoError(String id,JID jid) |
---|
161 | {//no disco so try browse |
---|
162 | TimerTask t =(TimerTask) timeoutTimers.remove(id); |
---|
163 | if(t!=null)t.cancel(); |
---|
164 | // String newID = "Jeti_Browse_" + idCount++; |
---|
165 | // InfoQuery iq =new InfoQuery(jid,"get",newID,new IQBrowse()); |
---|
166 | // backend.send(iq); |
---|
167 | // timeout(iq); |
---|
168 | // Object o = infoRequests.remove(id); |
---|
169 | // if(o!=null)infoRequests.put(newID,o); |
---|
170 | // else |
---|
171 | // { |
---|
172 | // o=itemRequests.remove(id); |
---|
173 | // if(o!=null)itemRequests.put(newID,o); |
---|
174 | // } |
---|
175 | // } |
---|
176 | // |
---|
177 | // public void browseError(String id,JID jid) |
---|
178 | // { |
---|
179 | // TimerTask t =(TimerTask) timeoutTimers.remove(id); |
---|
180 | // if(t!=null)t.cancel(); |
---|
181 | Object o = infoRequests.remove(id); |
---|
182 | //change to disco |
---|
183 | if(o!=null) |
---|
184 | { |
---|
185 | DiscoveryInfo info = new IQBrowse(jid); |
---|
186 | if (infoCache.get(jid) == null) infoCache.put(jid,info);//if nothing in cache cache = empty browse (maby server browse did succeed) |
---|
187 | ((DiscoveryListener)o).discoveryInfoResult(jid,info); |
---|
188 | } |
---|
189 | else |
---|
190 | { |
---|
191 | o = itemRequests.remove(id); |
---|
192 | if(o!=null) |
---|
193 | { |
---|
194 | DiscoveryItem item = new IQBrowse(jid); |
---|
195 | if (itemCache.get(jid) == null) itemCache.put(jid,item);//if nothing in cache cache = empty browse (maby server browse did succeed) |
---|
196 | ((DiscoveryListener)o).discoveryItemResult(jid,item); |
---|
197 | } |
---|
198 | } |
---|
199 | } |
---|
200 | |
---|
201 | public void discoveryInfoResult(JID jid,DiscoveryInfo info){} |
---|
202 | |
---|
203 | public void discoveryItemResult(JID jid,DiscoveryItem item){} |
---|
204 | |
---|
205 | //times out a disco or browse request |
---|
206 | private void timeout(final InfoQuery query) |
---|
207 | { |
---|
208 | TimerTask t = new TimerTask() |
---|
209 | { |
---|
210 | public void run() |
---|
211 | { |
---|
212 | System.out.println("timeout " + query); |
---|
213 | // if(query.getIQExtension() instanceof IQBrowse) |
---|
214 | // {//browse |
---|
215 | // browseError(query.getID(),query.getTo()); |
---|
216 | // } |
---|
217 | // else |
---|
218 | {//disco |
---|
219 | discoError(query.getID(),query.getTo()); |
---|
220 | } |
---|
221 | } |
---|
222 | }; |
---|
223 | timer.schedule(t,60000); |
---|
224 | timeoutTimers.put(query.getID(),t); |
---|
225 | } |
---|
226 | |
---|
227 | //-------------------status events-------------\ |
---|
228 | public void connectionChanged(boolean online) |
---|
229 | {//clear cache when offline |
---|
230 | if (!online) |
---|
231 | { |
---|
232 | infoRequests = new HashMap(10); |
---|
233 | itemRequests = new HashMap(5); |
---|
234 | infoCache = new HashMap(); |
---|
235 | itemCache = new HashMap(); |
---|
236 | } |
---|
237 | } |
---|
238 | |
---|
239 | public void ownPresenceChanged(int show, String status){} |
---|
240 | |
---|
241 | public void exit(){} |
---|
242 | |
---|
243 | } |
---|
244 | /* |
---|
245 | * Overrides for emacs |
---|
246 | * Local variables: |
---|
247 | * tab-width: 4 |
---|
248 | * End: |
---|
249 | */ |
---|