这部分的作用是客户端可以存放任意格式的私有xml数据到服务器上,并且在想要查看的时候取回!
1.协议 - XEP-0049 - http://xmpp.org/extensions/xep-0049.html
2. jabber client可以保存任意的xml数据到服务器,通过一个<iq type="set">的请求.该请求需要包含一个名称空间
为jabber:iq:private的<query/>子元素. <query/>的子元素就是我们需要保存的私有的xml的根节点,必须有一个私有的名称空间.
3. 通过一个<iq type="get">的请求来获取保存在服务器上的私有数据.该请求需要包含一个名称空间为jabber:iq:private
的<query/>子元素.<query>的子节点是包含一个命名空间的根节点.
4. 客户端保存私有信息到服务器.
存储下列xml到服务器:
<exodus xmlns="exodus:prefs">
<defaultnick>Hamlet</defaultnick>
</exodus>
(C->S)
<iq type="set" id="xxx1">
<query xmlns="jabber:iq:private">
<exodus xmlns="exodus:prefs">
<defaultnick>Hamlet</defaultnick>
</exodus>
</query>
</iq>
(S->C)
<iq type="result"
from="hamlet@shakespeare.lit/denmark"
to="hamlet@shakespeare.lit/denmark"
id="xxx1"/>
5. 客户端获得私有信息从服务器
(C->S)
<iq type="get" id="xxx2">
<query xmlns="jabber:iq:private">
<exodus xmlns="exodus:prefs"/>
</query>
</iq>
(S->C)
<iq type="result"
from="hamlet@shakespeare.lit/denmark"
to="hamlet@shakespeare.lit/denmark"
id="xxx2"/>
<query xmlns="jabber:iq:private">
<exodus xmlns="exodus:prefs">
<defaultnick>Hamlet</defaultnick>
</exodus>
</query>
</iq>
6. 服务器段的存储方式: 如果采用DB的话,实质上exodus + exodus:prefs为key,也就是私有的根节点名字和
私有的根节点的命名空间一起作为key.
下面看一段事例代码(服务器为openfire):
1#include <iostream>
2#include "client.h"
3#include "connectionlistener.h"
4#include "discohandler.h"
5#include "disco.h"
6#include "privatexml.h"
7
8#pragma comment( lib, "gloox.lib" )
9using namespace gloox;
10
11#define SERVERNAME "ziz-wrks-tfsxp1"
12#define HEADERTO "ziz-wrks-tfsxp1"
13
14#define USERNAME "user123"
15#define PASSWORD "user@123"
16
17// APIs of PrivateXML
18// requestXML - get the data stored in serve side
19// storeXML - send the store data request to the server
20
21// scenario1 - store data
22// scenario2 - get data
23//
24// how to run the testcases?
25// Note: run scenario1 first, then comment scenario1, run scenario2
26class PrivateXMLImpl : public PrivateXMLHandler, ConnectionListener {
27public:
28 void run();
29
30 //implement PrivateXMLHandler
31 void handlePrivateXML( const std::string& tag, Tag *xml );
32 void handlePrivateXMLResult( const std::string& uid, PrivateXMLResult pxResult );
33
34 //implement ConnectionListener
35 void onConnect();
36 void onDisconnect( ConnectionError e );
37 void onResourceBindError( ResourceBindError error );
38 void onSessionCreateError( SessionCreateError error );
39 bool onTLSConnect( const CertInfo& info );
40 void onStreamEvent( StreamEvent event );
41
42private:
43 Client* client_;
44 PrivateXML* privateXmlHandler_;
45};
46
47void PrivateXMLImpl::run() {
48 client_ = new Client(SERVERNAME);
49 client_->setHeaderTo(HEADERTO);
50 client_->registerConnectionListener(this);
51 client_->setUsername(USERNAME);
52 client_->setPassword(PASSWORD);
53
54 privateXmlHandler_ = new PrivateXML( client_ );
55
56 client_->connect();
57
58 delete privateXmlHandler_;
59 delete client_;
60}
61
62 // this function can receive the data of PrivateXML::requestXML().
63void PrivateXMLImpl::handlePrivateXML( const std::string& tag, Tag *xml ) {
64 std::cout<<"impl# get the data."<<std::endl;
65 std::cout<<"impl# name: "<<tag<<std::endl;
66 std::cout<<"impl# xml: "<<xml->xml()<<std::endl;
67}
68
69// the function can be called of PrivateXML::storeXML().
70void PrivateXMLImpl::handlePrivateXMLResult( const std::string& uid, PrivateXMLResult pxResult ) {
71 if( pxResult == PxmlStoreOk ) {
72 std::cout<<"impl# operation success."<<std::endl;
73 } else {
74 std::cout<<"impl# operation failed."<<std::endl;
75 }
76}
77
78void PrivateXMLImpl::onConnect() {
79 std::cout<<"impl# connect to server success."<<std::endl;
80
81 // scenario1 - store the privateXml to server
82 Tag* privatexml = new Tag( "privatedata" );
83 privatexml->addAttribute( "xmlns","privatedata-namespace" );
84 Tag* child = new Tag( "child-test", "cdata-test" );
85 privatexml->addChild( child );
86 privateXmlHandler_->storeXML( privatexml, this );
87
88
89 // scenario2 - get the privateXml from the server side
90 // privateXmlHandler_->requestXML( "privatedata", "privatedata-namespace", this );
91
92}
93
94void PrivateXMLImpl::onDisconnect( ConnectionError e ) {
95 std::cout<<"impl# disconnected."<<std::endl;
96}
97
98void PrivateXMLImpl::onResourceBindError( ResourceBindError error ) {
99 //TODO:
100}
101
102void PrivateXMLImpl::onSessionCreateError( SessionCreateError error ) {
103 //TODO:
104}
105
106bool PrivateXMLImpl::onTLSConnect( const CertInfo& info ) {
107 std::cout<<"impl# tls connect to server success."<<std::endl;
108 return true;
109}
110
111void PrivateXMLImpl::onStreamEvent( StreamEvent event ) {
112 //TODO:
113}
114
115int main( int argc, char* argv[] ) {
116 PrivateXMLImpl impl;
117 impl.run();
118 return 0;
119}