1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
/*
* Copyright (c) 2010-2014, Isode Limited, London, England.
* All rights reserved.
*/
/*
* Copyright (c) 2010, Remko Tronçon.
* All rights reserved.
*/
package com.isode.stroke.queries;
import java.util.Vector;
import com.isode.stroke.elements.ErrorPayload;
import com.isode.stroke.elements.IQ;
import com.isode.stroke.signals.Slot1;
import com.isode.stroke.jid.JID;
/**
* This class is responsible for routing all IQ stanzas to the handlers. It's
* needed because, unlike Message and Presence, there are strict rules about
* replying to IQ stanzas, replies need to be tracked, handled by the
* responsible bit of code, replied to, etc. and when it's an outgoing IQ, it
* needs to be tracked such that when the reply comes in, the callback is
* called.
*/
public class IQRouter {
private final Vector<IQHandler> handlers_ = new Vector<IQHandler>();
private final IQChannel channel_;
private JID jid_ = new JID();
public IQRouter(IQChannel channel) {
channel_ = channel;
channel_.onIQReceived.connect(new Slot1<IQ>() {
public void call(IQ p1) {
handleIQ(p1);
}
});
}
public void addHandler(IQHandler handler) {
synchronized (handlers_) {
handlers_.add(handler);
}
}
public void removeHandler(IQHandler handler) {
synchronized (handlers_) {
handlers_.remove(handler);
}
}
public void sendIQ(IQ iq) {
channel_.sendIQ(iq);
}
public String getNewIQID() {
return channel_.getNewIQID();
}
public boolean isAvailable() {
return channel_.isAvailable();
}
private void handleIQ(IQ iq) {
boolean handled = false;
synchronized (handlers_) {
for (IQHandler handler : handlers_) {
handled |= handler.handleIQ(iq);
if (handled) {
break;
}
}
}
if (!handled && (iq.getType().equals(IQ.Type.Get) || iq.getType().equals(IQ.Type.Set))) {
sendIQ(IQ.createError(iq.getFrom(), iq.getID(), ErrorPayload.Condition.FeatureNotImplemented, ErrorPayload.Type.Cancel));
}
}
/**
* Sets the JID of this IQ router.
*
* This JID is used by requests to check whether incoming results
* are addressed correctly
* @param jid the JID
*/
public void setJID(final JID jid) {
jid_ = jid;
}
public JID getJID() {
return jid_;
}
}
|