androidpn的学习研究(八)androidpn 中业务类XmppIoHandler实现分析
在androidpn中主要采用Mina进行网络通讯,其中Mina中IoHandler用来处理主要的业务逻辑。
Mina 中源代码如下:
- package org.apache.mina.core.service;
- import java.io.IOException;
- import org.apache.mina.core.session.IdleStatus;
- import org.apache.mina.core.session.IoSession;
- /**
- * Handles all I/O events fired by MINA.
- *
- * @author <a href="http://mina.apache.org">Apache MINA Project</a>
- *
- * @see IoHandlerAdapter
- */
- public interface IoHandler {
- /**
- * Invoked from an I/O processor thread when a new connection has been created.
- * Because this method is supposed to be called from the same thread that
- * handles I/O of multiple sessions, please implement this method to perform
- * tasks that consumes minimal amount of time such as socket parameter
- * and user-defined session attribute initialization.
- */
- void sessionCreated(IoSession session) throws Exception;
- /**
- * Invoked when a connection has been opened. This method is invoked after
- * {@link #sessionCreated(IoSession)}. The biggest difference from
- * {@link #sessionCreated(IoSession)} is that it's invoked from other thread
- * than an I/O processor thread once thread model is configured properly.
- */
- void sessionOpened(IoSession session) throws Exception;
- /**
- * Invoked when a connection is closed.
- */
- void sessionClosed(IoSession session) throws Exception;
- /**
- * Invoked with the related {@link IdleStatus} when a connection becomes idle.
- * This method is not invoked if the transport type is UDP; it's a known bug,
- * and will be fixed in 2.0.
- */
- void sessionIdle(IoSession session, IdleStatus status) throws Exception;
- /**
- * Invoked when any exception is thrown by user {@link IoHandler}
- * implementation or by MINA. If <code>cause</code> is an instance of
- * {@link IOException}, MINA will close the connection automatically.
- */
- void exceptionCaught(IoSession session, Throwable cause) throws Exception;
- /**
- * Invoked when a message is received.
- */
- void messageReceived(IoSession session, Object message) throws Exception;
- /**
- * Invoked when a message written by {@link IoSession#write(Object)} is
- * sent out.
- */
- void messageSent(IoSession session, Object message) throws Exception;
- }
Mina中IoHandler可供处理的事件回调
sessionCreate(IoSession)
IoSession对象被创建时的回调,一般用于进行会话初始化操作。注意:与sessionOpened(IoSession)不同,IoSession对象的创建并不意味着对应底层TCP连接的建立,而仅仅代表字面意思:一个IoSession对象被创建出来了。
sessionOpened(IoSession)
IoSession对象被打开时回调。在TCP中,该事件是在TCP连接建立时触发,一般可用于发起连接建立的握手、认证等操作。
sessionIdle(IoSession,IdleStatus)
IoSession对象超时时回调。当一个IoSession对象在指定的超时时常内没有读写事件发生,就会触发该事件,一般可用于通知服务器断开长时间闲置的连接等处理。具体的超时设置可由 IoService.setWriteIdleTime(int) ,IoService.setReadIdleTime(int) ,IoService.setBothIdleTime(int)设置。
messageReceived(IoSession,Object)
当接收到IoSession对Client发送的数据时回调。
messageSent(IoSession,Object)
当发送给IoSession对Client的数据发送成功时回调。
exceptionCaught(IoSession,Throwable)
当会话过程中出现异常时回调,通常用于错误处理。
session.write(Object)方法是一个异步方法,对该方法的调用并不会阻塞,而是向Mina投递一个异步的写操作,并返回一个可用于对已投递异步写操作进行控制的WriteFuture对象。例如:调用WriteFuture的await()或awaitUninterruptibly(),可由同步等待该异步操作的完成。
在I/O处理器中实现业务逻辑的时候,对于简单的情况,一般只需要在messageReceived中对传入的消息进行处理。如果需要写回数据到对等体,用IoSession.write()即可。
另外的情况,client和server的通信协议比较复杂,client是有状态变迁的,这时可用Mina提供的状态机实现,可使用IO处理器的实现更加简单。
androidpn中XmppIoHandler源代码:
- package org.androidpn.server.xmpp.net;
- import java.util.Map;
- import java.util.concurrent.ConcurrentHashMap;
- import org.androidpn.server.xmpp.XmppServer;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.apache.mina.core.service.IoHandler;
- import org.apache.mina.core.session.IdleStatus;
- import org.apache.mina.core.session.IoSession;
- import org.dom4j.io.XMPPPacketReader;
- import org.jivesoftware.openfire.net.MXParser;
- import org.jivesoftware.openfire.nio.XMLLightweightParser;
- import org.xmlpull.v1.XmlPullParserException;
- import org.xmlpull.v1.XmlPullParserFactory;
- /**
- * This class is to create new sessions, destroy sessions and deliver
- * received XML stanzas to the StanzaHandler.
- *
- * @author Sehwan Noh (devnoh@gmail.com)
- */
- public class XmppIoHandler implements IoHandler {
- private static final Log log = LogFactory.getLog(XmppIoHandler.class);
- public static final String XML_PARSER = "XML_PARSER";
- private static final String CONNECTION = "CONNECTION";
- private static final String STANZA_HANDLER = "STANZA_HANDLER";
- private String serverName;
- private static Map<Integer, XMPPPacketReader> parsers = new ConcurrentHashMap<Integer, XMPPPacketReader>();
- private static XmlPullParserFactory factory = null;
- static {
- try {
- factory = XmlPullParserFactory.newInstance(
- MXParser.class.getName(), null);
- factory.setNamespaceAware(true);
- } catch (XmlPullParserException e) {
- log.error("Error creating a parser factory", e);
- }
- }
- /**
- * Constructor. Set the server name from server instance.
- */
- protected XmppIoHandler() {
- serverName = XmppServer.getInstance().getServerName();
- }
- /**
- * Invoked from an I/O processor thread when a new connection has been created.
- */
- public void sessionCreated(IoSession session) throws Exception {
- log.debug("sessionCreated()...");
- }
- /**
- * Invoked when a connection has been opened.
- */
- public void sessionOpened(IoSession session) throws Exception {
- log.debug("sessionOpened()...");
- log.debug("remoteAddress=" + session.getRemoteAddress());
- // Create a new XML parser
- XMLLightweightParser parser = new XMLLightweightParser("UTF-8");
- session.setAttribute(XML_PARSER, parser);
- // Create a new connection
- Connection connection = new Connection(session);
- session.setAttribute(CONNECTION, connection);
- session.setAttribute(STANZA_HANDLER, new StanzaHandler(serverName,
- connection));
- }
- /**
- * Invoked when a connection is closed.
- */
- public void sessionClosed(IoSession session) throws Exception {
- log.debug("sessionClosed()...");
- Connection connection = (Connection) session.getAttribute(CONNECTION);
- connection.close();
- }
- /**
- * Invoked with the related IdleStatus when a connection becomes idle.
- */
- public void sessionIdle(IoSession session, IdleStatus status)
- throws Exception {
- log.debug("sessionIdle()...");
- Connection connection = (Connection) session.getAttribute(CONNECTION);
- if (log.isDebugEnabled()) {
- log.debug("Closing connection that has been idle: " + connection);
- }
- connection.close();
- }
- /**
- * Invoked when any exception is thrown.
- */
- public void exceptionCaught(IoSession session, Throwable cause)
- throws Exception {
- log.debug("exceptionCaught()...");
- log.error(cause);
- }
- /**
- * Invoked when a message is received.
- */
- public void messageReceived(IoSession session, Object message)
- throws Exception {
- log.debug("messageReceived()...");
- log.debug("RCVD: " + message);
- // Get the stanza handler
- StanzaHandler handler = (StanzaHandler) session
- .getAttribute(STANZA_HANDLER);
- // Get the XMPP packet parser
- int hashCode = Thread.currentThread().hashCode();
- XMPPPacketReader parser = parsers.get(hashCode);
- if (parser == null) {
- parser = new XMPPPacketReader();
- parser.setXPPFactory(factory);
- parsers.put(hashCode, parser);
- }
- // The stanza handler processes the message
- try {
- handler.process((String) message, parser);
- } catch (Exception e) {
- log.error(
- "Closing connection due to error while processing message: "
- + message, e);
- Connection connection = (Connection) session
- .getAttribute(CONNECTION);
- connection.close();
- }
- }
- /**
- * Invoked when a message written by IoSession.write(Object) is sent out.
- */
- public void messageSent(IoSession session, Object message) throws Exception {
- log.debug("messageSent()...");
- }
- }
XmppIoHandler在加载的时候创建相关的xml解析工厂。
sessionOpened:在连接打开时候创建相关的xml的解析器和Handler处理器。
sessionClosed:关闭相关的连接。
sessionIdle:关闭相关的连接。
messageReceived:获取相关的xml解析器和handler处理器处理相关的消息。
<!-- Baidu Button BEGIN -->
androidpn的学习研究(八)androidpn 中业务类XmppIoHandler实现分析相关推荐
- BT源代码学习心得(八):跟踪服务器(Tracker)的代码分析(用户请求的实际处理) - 转贴自 wolfenstein (NeverSayNever)
BT源代码学习心得(八):跟踪服务器(Tracker)的代码分析(用户请求的实际处理) author: wolfenstein 通过上一次的分析,我们已经知道了Tracker采用http协议和客户端通 ...
- C++语言学习(十四)——C++类成员函数调用分析
C++语言学习(十四)--C++类成员函数调用分析 一.C++成员函数 1.C++成员函数的编译 C++中的函数在编译时会根据命名空间.类.参数签名等信息进行重新命名,形成新的函数名.函数重命名的过程 ...
- androidpn的学习研究(二)androidpn-server服务端启动过程的理解分析
在Androidpn的底层主要采用的mina和openfire两大框架,其中mina主要为底层数据传输的Socket框架.下面简单说明mina的框架. Apache Mina Server 是一个网络 ...
- androidpn的学习研究(七)Androidpn-server的添加其他xmpp相关的协议(如查看好友列表等)...
曾经有一个同学,在网上问我,如果想androidpn添加额外的xmpp协议的方法在怎么加呢?我当时很迷惑,后来经过一翻仔细研究androidpn发现,其实每一种处理xmpp协议方法,必须有一个Hand ...
- androidpn的学习研究(五)androidpn-client 常见BUG解决方法
原文地址:http://phonepush.sinaapp.com/forum.php?mod=viewthread&tid=6&extra=page%3D1 最近有需要做手机推送方面 ...
- androidpn的学习研究(六)Androidpn-server的Mina编码和解码解析过程
在许多网络应用中可能针对传输的数据进行加密操作,接收到数据之后进行解码操作. 在mina中提供许多加密和解密的解析方式: 1.带一定前缀的字符串的解析方式. 2.序列化对象的字符串解析方式. 3.分隔 ...
- TS学习(八) :TS中的类
TS中类的书写 以前在js中书写类是这样的,然后我们在加上TS的类型检查你会发现报错了 class User {constructor(name:string,age:number) {this.na ...
- python学习随笔:python中的类
python中的类 # 以Student 为类的名称(类名)有一个或多个单词组成,每个单词的首字母大写,其余小写 class Student:native_pace = '成都' # 直接写在类里的变 ...
- androidpn的学习研究(四)androidpn-client客户端几个类说明
在androidpn的客户端几个重要的类: ServiceManager:管理消息服务和加载相关的配置. ConnectivityReceiver:处理网络状态的广播. NotificationRec ...
最新文章
- java jdk 1.8 配置_Java开发环境jdk 1.8安装配置方法(Win7 64位系统/windows server 2008)...
- android Adapter剖析理解
- Centos 6.4 搭建LANMP一键安装版
- sql 精读(一)标准 SQL 中的分析函数概念
- html5 js选择器,使用HTML5的JS选择器操作页面中的元素
- 桌面在计算机领域常用来指,桌面在计算机领域常用来指什么
- Java 8新特性探究(二)深入解析默认方法
- python重命名窗口_为《Python实现批量重命名》程序加一个GUI
- 20条.net编码习惯 【转】
- PTA: 6-5 删除单链表偶数节点 (20 分)
- html怎么添加视频旋转,拍摄的视频如何旋转 三种方法教你旋转视频
- 2020 JUSTCTF F@k3 0ff1c@l REVERSE WP
- VMware 苹果虚拟机 Xcode真机调试失败 设备不信任该机器
- JS实现:纵向表格,且可在当前行下方添加一行
- 学生学籍管理系统数据流图
- 制作 .Img 镜像文件
- 相机照片大小设置_我应该为运动照片使用哪些相机设置?
- zabbix随堂笔记
- Magisk模块开发指南
- dnf剑魂buff等级上限_DNF5.8新版buff换装提升整理以及装备选择,防止弯路