1.5.4 IQ 节
<iq>节表示的是Info/Query(信息与查询),它为XMPP通信提供请求与响应机制。它与HTTP
协议的基本工作原理非常相似,允许获取和设置查询,与HTTP 的GET 和POST 动作类似。
每个<iq>节都必须有一个响应,而且前面曾经提到过,该节的必需的id 属性将用来把响应
与导致该响应的请求关联起来。<iq>节有四种,通过该节的type 属性区分。有两种<iq>节请求
(get 和set)和两种响应(result 和error)。在本书中,这些节通常被缩写为IQ-get、IQ-set、IQ-result
和IQ-error。
每一个IQ-get 或IQ-set 节均必须接收响应的IQ-result 或IQ-error 节。下面的示例给出了一
些常见的<iq>节以及它们可能的响应。注意,与<message>和<presence>节(它们定义了子元素)
不同,<iq>节通常只包含与它们功能相关的扩展元素。此外,每一对<iq>节必须匹配id 属性。
<iq from='jane@longbourn.lit/garden'
type='get'
id='roster1'>
<query xmlns='jabber:iq:roster'/>
</iq>
<iq to='jane@longbourn.lit/garden'
type='error'
id='roster1'>
<query xmlns='jabber:iq:roster'/>
<error type='cancel'>
<feature-not-implemented xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
</error>
</iq>
Jane 向她的服务器发送了一个格式错误的花名册请求。服务器使用一个错误提示节作为响
应。后面我们将详细讨论错误提示节。
<iq from='jane@longbourn.lit/garden'
type='get'
id='roster2'>
<query xmlns='jabber:iq:roster'/>
</iq>
<iq to='jane@longbourn.lit/garden'
type='result'
id='roster2'>
<query xmlns='jabber:iq:roster'>
<item jid='elizabeth@longbourn.lit' name='Elizabeth'/>
<item jid='bingley@netherfield.lit' name='Bingley'/>
</query>
</iq>
在重新发送正确的请求之后,服务器将Jane 的简短花名册响应给她。可以看到Elizabeth 和
Bingley 均在Jane 的联系人列表中。
第1 章了解 XMPP 协议
15
<iq from='jane@longbourn.lit/garden'
type='set'
id='roster3'>
<query xmlns='jabber:iq:roster'>
<item jid='darcy@pemberley.lit' name='Mr. Darcy'/>
</query>
</iq>
<iq to='jane@longbourn.lit/garden'
type='result'
id='roster3'/>
Jane 试图将Darcy 添加到自己的花名册中,服务器用一个空白IQ-result 节来指出添加成功。
如果应答节只是成功确认,那么IQ-result 节通常是空白的。
在任何需要结果数据或者需要简单确认的场合中,<iq>节都非常有用。大多数XMPP 扩展
协议混合使用<iq>和<message>节来实现它们的功能。<iq>节用于类似于配置和状态变化这样的
信息,而<message>节则用于常规通信。在某些场合中,<iq>节也用于通信,这是因为节确认机
制可实现限速功能。
NotificationManager类中的IQ创建方法
/**
* Creates a new notification IQ and returns it.
*/
private IQ createNotificationIQ(String apiKey, String title,
String message, String uri) {
Random random = new Random();
String id = Integer.toHexString(random.nextInt());
// String id = String.valueOf(System.currentTimeMillis());
Element notification = DocumentHelper.createElement(QName.get(
"notification", NOTIFICATION_NAMESPACE));
notification.addElement("id").setText(id);
notification.addElement("apiKey").setText(apiKey);
notification.addElement("title").setText(title);
notification.addElement("message").setText(message);
notification.addElement("uri").setText(uri);
IQ iq = new IQ();
iq.setType(IQ.Type.set);
iq.setChildElement(notification);
return iq;
}
tinder-1.2.1.jar包中的 IQ类中的方法
public void setChildElement(Element childElement)
{
for (Iterator i = this.element.elementIterator(); i.hasNext(); ) {
this.element.remove((Element)i.next());
}
this.element.add(childElement);
}
public IQ()
{
this.element = docFactory.createDocument().addElement("iq");
String id = String.valueOf(random.nextInt(1000) + "-" + sequence++);
setType(Type.get);
setID(id);
}
/**
* Broadcasts a newly created notification message to all connected users.
*
* @param apiKey the API key
* @param title the title
* @param message the message details
* @param uri the uri
*/
public void sendBroadcast(String apiKey, String title, String message,
String uri) {
log.debug("sendBroadcast()...");
IQ notificationIQ = createNotificationIQ(apiKey, title, message, uri);
for (ClientSession session : sessionManager.getSessions()) {
if (session.getPresence().isAvailable()) {
notificationIQ.setTo(session.getAddress());
session.deliver(notificationIQ);
}
}
}
IQ是继承Packet其中
@NotThreadSafe
public abstract class Packet
{
private static final Logger Log = LoggerFactory.getLogger(Packet.class);
protected static final DocumentFactory docFactory = DocumentFactory.getInstance();
protected Element element;
protected JID toJID;
protected JID fromJID;
public void setTo(JID to)
{
this.toJID = to;
if (to == null) {
this.element.addAttribute("to", null);
}
else
this.element.addAttribute("to", to.toString());
}
可以看出JID就是要广播的地址 ,xmpp协议中的地址。
Connection类中的 使用Apache mina管理
public void deliver(Packet packet) {
log.debug("SENT: " + packet.toXML());
if (!isClosed()) {
IoBuffer buffer = IoBuffer.allocate(4096);
buffer.setAutoExpand(true);
boolean errorDelivering = false;
try {
XMLWriter xmlSerializer = new XMLWriter(new IoBufferWriter(
buffer, (CharsetEncoder) encoder.get()),
new OutputFormat());
xmlSerializer.write(packet.getElement());
xmlSerializer.flush();
buffer.flip();
ioSession.write(buffer);
} catch (Exception e) {
log.debug("Connection: Error delivering packet" + "\n"
+ this.toString(), e);
errorDelivering = true;
}
if (errorDelivering) {
close();
} else {
session.incrementServerPacketCount();
}
}
}