gloox代码分析 - 注册模块(摘抄)

来源:互联网 发布:电子数据交换流程 编辑:程序博客网 时间:2024/06/06 04:23

#jabber协议中如何注册一个用户?
首先要与服务器建立一个连接, 在完成TLS握手之后就可以进行注册了,为什么不需要SASL握手呢?因为SASL握手只针对已经注册的用户在登陆服务器的时候使用.(修改密码和删除用户的时候需要SASL握手)
下面以openfire作为服务器,注册一个用户的过程如下:
(假设已经完成了TLS握手)
1. ( C->S )
<stream:stream
    to='ziz-wrks-tfsxp1'
    xmlns='jabber:client'
    xmlns:stream='http://etherx.jabber.org/streams'
    xml:lang='en'
    version='1.0'>
TLS握手结束后, 发送新的'hello'消息

2. ( S->C )
<stream:stream
    xmlns:stream='http://etherx.jabber.org/streams'
    xmlns='jabber:client'
    from='ziz-wrks-tfsxp1'
    id='b691538a'
    xml:lang='en' version='1.0'/>
Server回应Client的hello消息.

3. ( S->C )
<stream:features>
<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>
    <mechanism>DIGEST-MD5</mechanism>
    <mechanism>PLAIN</mechanism>
    <mechanism>ANONYMOUS</mechanism>
    <mechanism>CRAM-MD5</mechanism>
</mechanisms>
<compression xmlns='http://jabber.org/features/compress'>
    <method>zlib</method>
</compression>
<auth xmlns='http://jabber.org/features/iq-auth'/>
<register xmlns='http://jabber.org/features/iq-register'/>
</stream:features>
服务器支持的features(可以register)

4. ( C->S )
<iq type='get' id='uid1'>
<query xmlns='jabber:iq:register'/>
</iq>
客户端请求注册


5. ( S->C )
<iq type='result' id='uid1'>
<query xmlns='jabber:iq:register'>
    <username/>
    <password/>
    <email/>
    <name/>
    <x xmlns='jabber:x:data' type='form'>
      <title>XMPP Client Registration</title>
      <instructions>Please provide the following information</instructions>
      <field var='FORM_TYPE' type='hidden'>
        <value>jabber:iq:register</value>
      </field>
      <field label='Username' var='username' type='text-single'>
        <required/>
      </field>
      <field label='Full name' var='name' type='text-single'/>
      <field label='Email' var='email' type='text-single'/>
      <field label='Password' var='password' type='text-private'>
        <required/>
      </field>
    </x>
</query>
</iq>
服务器返回注册需要的fields
(其中元素x里面的数据是扩展性的数据表但,暂时忽略)

6. ( C->S )
<iq id='uid2' type='set'>
<query xmlns='jabber:iq:register'>
    <username>user3</username>
    <password>user@123</password>
    <name/>
    <email/>
</query>
</iq>
用户提交注册信息.

7. ( S->C )
<iq type='result' id='uid2' to='ziz-wrks-tfsxp1/b691538a'/>
服务器返回注册结果


#如何更改用户密码
更改用户密码必须在完成与服务器的链接之后才能进行,也就是完成与服务器的TLS,SASl握手之后
再进行.

1. ( C->S )
<iq type='set' id='uid3'>
<query xmlns='jabber:iq:register'>
    <username>user23</username>
    <password>123456</password>
</query>
</iq>
发送更改密码的请求

2. ( S->C )
<iq type='result' id='uid3' to='user23@ziz-wrks-tfsxp1/2f21fd1f'/>
返回处理结果(操作成功)


#如何注销一个用户
注销一个用户必须在完成与服务器的链接之后才能进行,也就是完成与服务器的TLS,SASl握手之后
再进行.
1. ( C->S )
<iq type='set' id='uid3' from='testuser@ziz-wrks-tfsxp1/3a418274'>
   <query xmlns='jabber:iq:register'><remove/></query>
</iq>
发送注销用户的请求

2. ( S->S )
<iq type='result' id='uid3' to='testuser@ziz-wrks-tfsxp1/3a418274'/>
返回处理结果(注销成功)

总结:
注册用户和更改或者注销用户的一个区别就是注册用户不需要经过SASL握手,而更改和注销用户需要.
也就是需要对用户身份进行验证.

gloox的注册模块
registration.h
registration.cpp
registrationhandler.h
就是对上述协议的封装,方便创建register stanza,并把收到的消息分发到相应的handler方法.

gloox的client是如何处理xml流的呢?
1. 连接到服务器后,使客户端进入"receive mode",可以循环的接收数据.
2. 数据流 [接收到的数据 handleReceiveData() ] -> [解析xml数据流,解析成不同的tag, parser()] -> [ 触发handleTag(), 针对不同的tag分发到不同的handlers做不同的处理 ]
- stream:stream - ...
- stream:error - ...
- Iq           - IqHandler
- Presence - presenceHandler
- Message - messageHandler

Iq消息的处理机制,以及trackID机制:
实质上registration是一个Iqhandler, 它实现的是handleIqID( Stanza *stanza, int context )接口,而不是handleIq( Stanza *stanza )接口,其中context实质上为iqHandler具体实现中的子操作类型,在registration中分别为 fetchRegisterFiled, createAccount, changePassword, removeAccount.

对一个Iq Stanza的处理方式可以通过一般的IqHandler - xmlnamespace filer或者IqHandler - TrackID机制来处理.

下面代码展示了如何注册/修改密码/删除用户(服务器是openfire):

#include <iostream>
#include "registration.h" // gloox headers
#include "client.h"
#include "connectionlistener.h"
#pragma comment( lib, "gloox.lib" )
using namespace gloox;

class RegisterImpl : public RegistrationHandler, ConnectionListener{
public:
    void run();

    //implement RegistrationHandler
    void handleRegistrationFields( const JID& from, int fields, std::string instructions );
    void handleAlreadyRegistered( const JID& from );
    void handleRegistrationResult( const JID& from, RegistrationResult regResult );
    void handleDataForm( const JID& from, const DataForm &form );
    void handleOOB( const JID& from, const OOB& oob );

    //implement ConnectionListener
    void onConnect();
    void onDisconnect( ConnectionError e );
    void onResourceBindError( ResourceBindError error );
    void onSessionCreateError( SessionCreateError error );
    bool onTLSConnect( const CertInfo& info );
    void onStreamEvent( StreamEvent event );

private:
    Registration* register;
    Client* client;
};

RegisterImpl::run()
{
    client = new Client("servername");
    client_->disableRoster();
    client->registerConnectionListener(this);
    register = new Registration( client );
    register->registerRegistrationHandler( this );

    //进行注册的时候这两行代码需要注释掉的
    //当进行修改密码和删除用户时需要进行登陆验证的
    // client->setUsername("name");
    // client->setPassword("pwd");

    client->connect();

    delete register;
    delete client;
}

void RegisterImpl::handleRegistrationFields( const JID& from, int fields, std::string instructions )
{
    // 注册新的用户
    RegistrationFields vals;
    vals.username = "newusername";
    vals.password = "password";
    register->createAccount( fields, vals );
}

void RegisterImpl::handleAlreadyRegistered( const JID& from )
{
    std::cout<<"impl# the count already exists."<<std::endl;
}

void RegisterImpl::handleRegistrationResult( const JID& from, RegistrationResult regResult ) {
    if( regResult == RegistrationSuccess ) {
        std::cout<<"impl# operation success."<<std::endl;
    } else {
        std::cout<<"impl# operation failed."<<std::endl;
    }

    client->disconnect();
}

void RegisterImpl::handleDataForm( const JID& from, const DataForm &form ) {
    //TODO:
}

void RegisterImpl::handleOOB( const JID& from, const OOB& oob ) {
    //TODO:
}

void RegisterImpl::onConnect() {
    std::cout<<"impl# connect to server success."<<std::endl;

    // register
    // this will invoke handleRegistrationResult() to createAccount
    // Note: doesn't need SASL handshake
    register->fetchRegistrationFields();

    //  change password
    // Note: need SASL handshake, in gloox, set the username & password in clientbase, will
    //       run the SASL handshake.
    // register->changePassword( client_->username(), NEWPASSWORD );

    // delete account
    // Note: need SASL handshake,
    // register->removeAccount();
}

void RegisterImpl::onDisconnect( ConnectionError e ) {
    std::cout<<"impl# disconnected."<<std::endl;
}

void RegisterImpl::onResourceBindError( ResourceBindError error ) {
    //TODO:
}

void RegisterImpl::onSessionCreateError( SessionCreateError error ) {
    //TODO:
}

bool RegisterImpl::onTLSConnect( const CertInfo& info ) {
    std::cout<<"impl# tls connect to server success."<<std::endl;
    return true;
}

void RegisterImpl::onStreamEvent( StreamEvent event ) {
    //TODO:
}

void RegisterImpl::handleLog( LogLevel level, LogArea area, const std::string& message ) {
    std::cout<<"impl-log# "<<message<<std::endl;
}

int main( int argc, char* argv[] )
{
   RegisterImpl impl;
   impl.run();
   return 0;
}

原创粉丝点击