欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android XMPP通讯自定义Packet&Provider

程序员文章站 2024-03-06 10:58:25
摘要 在xmpp通信过程中,asmack中提供的packet组件是iq,message,presence三种: iq用于查询 message用于消息传递 presen...

摘要

在xmpp通信过程中,asmack中提供的packet组件是iq,message,presence三种: iq用于查询 message用于消息传递 presence用于状态交互 他们都是packet的子类,实质是用于将消息封装成响应的xml格式来进行数据交换,都有着良好的可扩展性。

简介

我们以开源项目androidpn为例:

androidpn (android push notification)是一个基于xmpp协议的java开源android push notification实现。它包含了完整的客户端和服务器端。

androidpn包括server端和client端,项目名称是androidpn-server和androidpn-client。

事实上,androidpn-server可以支持app运行于ios,uwp,windows,linux等平台,不仅限于android,因此,希望项目的名称改为xpn(xmpp push notification)似乎更加符合其实际场景,我们以后涉及到android push notification统称为xpn。

xnp目前状态

项目自2014年1月就已经停止更新了,此外,asmack项目也停止更新了,作者建议使用openfire官方的smack4.0,不过这样的话引入的jar会特别多,特别大。当然,我们下载到了asmack8.10.0比较新的稳定版本,可以完全用于学习和扩展。

项目相关下载站点

asmack-github.com - asmack项目地址

- asmack镜像地址

androidpn(xpn)-github.com - androidpn下载地址

一.关于packet数据包

packet是iq,message,presence的父类,用于实现消息组件类型。

消息语义学message

message是一种基本推送消息方法,它不要求响应。主要用于im、groupchat、alert和notification之类的应用中。
主要 属性如下:

    type属性,它主要有5种类型:
        normal:类似于email,主要特点是不要求响应;
        chat:类似于qq里的好友即时聊天,主要特点是实时通讯;
        groupchat:类似于聊天室里的群聊;
        headline:用于发送alert和notification;
        error:如果发送message出错,发现错误的实体会用这个类别来通知发送者出错了;

to属性:标识消息的接收方。

from属性:指发送方的名字或标示。为防止地址外泄,这个地址通常由发送者的server填写,而不是发送者。
载荷(payload):例如body,subject

<message to="lily@jabber.org/contact" 
 type="chat" > 
  <body> 你好,在忙吗</body> 
</message>

出席信息语义学presence

presence用来表明用户的状态,如:online、away、dnd(请勿打扰)等。当改变自己的状态时,就会在stream的上下文中插入一个presence元素,来表明自身的状态。要想接受presence消息,必须经过一个叫做presence subscription的授权过程。
属性:

type属性,非必须。有以下类别
    subscribe:订阅其他用户的状态
    probe:请求获取其他用户的状态
    unavailable:不可用,离线(offline)状态

to属性:标识消息的接收方。

from属性:指发送方的名字或标示。

载荷(payload):
    show:
    chat:聊天中
    away:暂时离开
    xa:extend away,长时间离开
    dnd:勿打扰
status:格式*,可阅读的文本。也叫做rich presence或者extended presence,常用来表示用户当前心情,活动,听的歌曲,看的视频,所在的聊天室,访问的网页,玩的游戏等等。
priority:范围-128~127。高优先级的resource能接受发送到bare jid的消息,低优先级的resource不能。优先级为
<presence from="">
  <show>xa</show>
  <status>down the rabbit hole!</status>
</presence>

iq语义学

一种请求/响应机制,从一个实体从发送请求,另外一个实体接受请求,并进行响应。例如,client在stream的上下文中插入一个元素,向server请求得到自己的好友列表,server返回一个,里面是请求的结果。
主要的属性是type。包括:
    get :获取当前域值。类似于http get方法。
    set :设置或替换get查询的值。类似于http put方法。
    result :说明成功的响应了先前的查询。类似于http状态码200。
    error: 查询和响应中出现的错误。
<iq from="" 
    id="rr82a1z7"
    to="" 
    type="get">
  <query xmlns="jabber:iq:roster"/>
</iq>

二.自定义packet

由于服务器和客户端使用的packet不同,但是他们交互的数据格式都是xml,因此,这个过程我们理解xml实现过程即可。

1.定义packet封装对象

由于asmack标签解析的限制,我们不能自定义解析,除非修改源码,这里出于简单,这里只能继承现有标签之一。

我么按照项目代码notificationiq为例,这里没有继承packet,而是继承了iq

import org.jivesoftware.smack.packet.iq;
/** 
 * this class represents a notifcatin iq packet.
 *
 * @author sehwan noh (devnoh@gmail.com)
 */
public class notificationiq extends iq {
  private string id;
  private string apikey;
  private string title;
  private string message;
  private string uri;
  public notificationiq() {
  }
  @override
  public string getchildelementxml() {
    stringbuilder buf = new stringbuilder();
    buf.append("<").append("notification").append(" xmlns=\"").append(
        "androidpn:iq:notification").append("\">");
    if (id != null) {
      buf.append("<id>").append(id).append("</id>");
    }
    buf.append("</").append("notification").append("> ");
    return buf.tostring();
  }
  public string getid() {
    return id;
  }
  public void setid(string id) {
    this.id = id;
  }
  public string getapikey() {
    return apikey;
  }
  public void setapikey(string apikey) {
    this.apikey = apikey;
  }
  public string gettitle() {
    return title;
  }
  public void settitle(string title) {
    this.title = title;
  }
  public string getmessage() {
    return message;
  }
  public void setmessage(string message) {
    this.message = message;
  }
  public string geturi() {
    return uri;
  }
  public void seturi(string url) {
    this.uri = url;
  }
}

其中,getchildelementxml()是iq的子类,用来拼接成<iq>下的直接点。

public abstract class iq extends packet {
  private type type = type.get;
  public iq() {
    super();
  }
  public iq(iq iq) {
    super(iq);
    type = iq.gettype();
  }
  /**
   * returns the type of the iq packet.
   *
   * @return the type of the iq packet.
   */
  public type gettype() {
    return type;
  }
  /**
   * sets the type of the iq packet.
   *
   * @param type the type of the iq packet.
   */
  public void settype(type type) {
    if (type == null) {
      this.type = type.get;
    }
    else {
      this.type = type;
    }
  }
  public string toxml() {
    stringbuilder buf = new stringbuilder();
    buf.append("<iq ");
    if (getpacketid() != null) {
      buf.append("id=\"" + getpacketid() + "\" ");
    }
    if (getto() != null) {
      buf.append("to=\"").append(stringutils.escapeforxml(getto())).append("\" ");
    }
    if (getfrom() != null) {
      buf.append("from=\"").append(stringutils.escapeforxml(getfrom())).append("\" ");
    }
    if (type == null) {
      buf.append("type=\"get\">");
    }
    else {
      buf.append("type=\"").append(gettype()).append("\">");
    }
    // add the query section if there is one.
    string queryxml = getchildelementxml();
    if (queryxml != null) {
      buf.append(queryxml);
    }
    // add the error sub-packet, if there is one.
    xmpperror error = geterror();
    if (error != null) {
      buf.append(error.toxml());
    }
    buf.append("</iq>");
    return buf.tostring();
  }
  /**
   * returns the sub-element xml section of the iq packet, or <tt>null</tt> if there
   * isn't one. packet extensions <b>must</b> be included, if any are defined.<p>
   *
   * extensions of this class must override this method.
   *
   * @return the child element section of the iq xml.
   */
  public abstract string getchildelementxml();
  /**
   * convenience method to create a new empty {@link type#result iq.type.result}
   * iq based on a {@link type#get iq.type.get} or {@link type#set iq.type.set}
   * iq. the new packet will be initialized with:<ul>
   *   <li>the sender set to the recipient of the originating iq.
   *   <li>the recipient set to the sender of the originating iq.
   *   <li>the type set to {@link type#result iq.type.result}.
   *   <li>the id set to the id of the originating iq.
   *   <li>no child element of the iq element.
   * </ul>
   *
   * @param iq the {@link type#get iq.type.get} or {@link type#set iq.type.set} iq packet.
   * @throws illegalargumentexception if the iq packet does not have a type of
   *   {@link type#get iq.type.get} or {@link type#set iq.type.set}.
   * @return a new {@link type#result iq.type.result} iq based on the originating iq.
   */
  public static iq createresultiq(final iq request) {
    if (!(request.gettype() == type.get || request.gettype() == type.set)) {
      throw new illegalargumentexception(
          "iq must be of type 'set' or 'get'. original iq: " + request.toxml());
    }
    final iq result = new iq() {
      public string getchildelementxml() {
        return null;
      }
    };
    result.settype(type.result);
    result.setpacketid(request.getpacketid());
    result.setfrom(request.getto());
    result.setto(request.getfrom());
    return result;
  }
  /**
   * convenience method to create a new {@link type#error iq.type.error} iq
   * based on a {@link type#get iq.type.get} or {@link type#set iq.type.set}
   * iq. the new packet will be initialized with:<ul>
   *   <li>the sender set to the recipient of the originating iq.
   *   <li>the recipient set to the sender of the originating iq.
   *   <li>the type set to {@link type#error iq.type.error}.
   *   <li>the id set to the id of the originating iq.
   *   <li>the child element contained in the associated originating iq.
   *   <li>the provided {@link xmpperror xmpperror}.
   * </ul>
   *
   * @param iq the {@link type#get iq.type.get} or {@link type#set iq.type.set} iq packet.
   * @param error the error to associate with the created iq packet.
   * @throws illegalargumentexception if the iq packet does not have a type of
   *   {@link type#get iq.type.get} or {@link type#set iq.type.set}.
   * @return a new {@link type#error iq.type.error} iq based on the originating iq.
   */
  public static iq createerrorresponse(final iq request, final xmpperror error) {
    if (!(request.gettype() == type.get || request.gettype() == type.set)) {
      throw new illegalargumentexception(
          "iq must be of type 'set' or 'get'. original iq: " + request.toxml());
    }
    final iq result = new iq() {
      public string getchildelementxml() {
        return request.getchildelementxml();
      }
    };
    result.settype(type.error);
    result.setpacketid(request.getpacketid());
    result.setfrom(request.getto());
    result.setto(request.getfrom());
    result.seterror(error);
    return result;
  }
  /**
   * a class to represent the type of the iq packet. the types are:
   *
   * <ul>
   *   <li>iq.type.get
   *   <li>iq.type.set
   *   <li>iq.type.result
   *   <li>iq.type.error
   * </ul>
   */
  public static class type {
    public static final type get = new type("get");
    public static final type set = new type("set");
    public static final type result = new type("result");
    public static final type error = new type("error");
    /**
     * converts a string into the corresponding types. valid string values
     * that can be converted to types are: "get", "set", "result", and "error".
     *
     * @param type the string value to covert.
     * @return the corresponding type.
     */
    public static type fromstring(string type) {
      if (type == null) {
        return null;
      }
      type = type.tolowercase();
      if (get.tostring().equals(type)) {
        return get;
      }
      else if (set.tostring().equals(type)) {
        return set;
      }
      else if (error.tostring().equals(type)) {
        return error;
      }
      else if (result.tostring().equals(type)) {
        return result;
      }
      else {
        return null;
      }
    }
    private string value;
    private type(string value) {
      this.value = value;
    }
    public string tostring() {
      return value;
    }
  }
}

最终可生成如下结构的数据

<iq from="">
 <nofitication xlns="">
<iq>

我们在项目中的使用很简单

xmppmanager.getconnection().sendpacket(<notificationiq>niq)

当然,上面只是实现了object->xml,接下来我们实现xml->data

2.实现iqprovider

先来看看iqprovider源码

public interface iqprovider {

  /**
   * parse the iq sub-document and create an iq instance. each iq must have a
   * single child element. at the beginning of the method call, the xml parser
   * will be positioned at the opening tag of the iq child element. at the end
   * of the method call, the parser <b>must</b> be positioned on the closing tag
   * of the child element.
   *
   * @param parser an xml parser.
   * @return a new iq instance.
   * @throws exception if an error occurs parsing the xml.
   */
  public iq parseiq(xmlpullparser parser) throws exception;
}

实现自定义的解析工具

public class notificationiqprovider implements iqprovider {
  public notificationiqprovider() {
  }
  @override
  public iq parseiq(xmlpullparser parser) throws exception {
    notificationiq notification = new notificationiq();
    for (boolean done = false; !done;) {
      int eventtype = parser.next();
      if (eventtype == 2) {
        if ("id".equals(parser.getname())) {
          notification.setid(parser.nexttext());
        }
        if ("apikey".equals(parser.getname())) {
          notification.setapikey(parser.nexttext());
        }
        if ("title".equals(parser.getname())) {
          notification.settitle(parser.nexttext());
        }
        if ("message".equals(parser.getname())) {
          notification.setmessage(parser.nexttext());
        }
        if ("uri".equals(parser.getname())) {
          notification.seturi(parser.nexttext());
        }
      } else if (eventtype == 3
          && "notification".equals(parser.getname())) {
        done = true;
      }
    }
    return notification;
  }
}

项目中使用方法

providermanager.getinstance().addiqprovider("notification",
              "androidpn:iq:notification",
              new notificationiqprovider());

在asmack中packetparserutils类中会进行如下调用

 object provider = providermanager.getinstance().getiqprovider(elementname, namespace);
          if (provider != null) {
            if (provider instanceof iqprovider) {
              iqpacket = ((iqprovider)provider).parseiq(parser);
            }
            else if (provider instanceof class) {
              iqpacket = (iq)packetparserutils.parsewithintrospection(elementname,
                  (class<?>)provider, parser);
            }
          }