在 (一)androidpn-server tomcat版源码解析之--项目启动这篇中,已经描述了整个推送服务器的启动过程,并且把握到了消息的入口即XmppIoHandler这个类,今天我将继续往下分析下面的核心代码,主要分为3大块,链接创建,消息的发送,链接关闭。
先贴一段XmppIoHandler的部分代码
/** * Invoked from an I/O processor thread when a new connection has been created. */ public void sessionCreated(IoSession session) throws Exception { log.debug("sessionCreated()..."); session.getConfig().setBothIdleTime(IDLE_TIME);; } /** * 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()..."); }
-
链接创建
androidpn-server的消息处理步奏为: sessionCreated->sessionOpened->Router->handle
对应的处理类为:XmppIoHandler- >StanzaHandler->PacketRouterIQAuthHandler.java->IQHandler.java
IQRegisterHandler.java
IQRosterHandler.java
PresenceUpdateHandler.java
androidpn是以xmpp为协议的,要看懂以下的处理步奏也就必须补充点xmpp的一个相关知识
xmpp的三种消息类型:
message:message是一种基本推送消息方法,它不要求响应。
presence:presence用来表明用户的状态,如:online、away、dnd(请勿打扰)等
iq:一种请求/响应机制,从一个实体从发送请求,另外一个实体接受请求,并进行响应。
XmppIoHandler.java-> sessionCreated:
public void sessionCreated(IoSession session) throws Exception { log.debug("sessionCreated()..."); session.getConfig().setBothIdleTime(IDLE_TIME);; }
client的请求第一次到达时候为session设置了一个idletime
XmppIoHandler.java-> sessionOpened
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)); }
初始化了xml解析器,并且创建了StanzaHandler实例,StanzaHandler这个类时后续处理的关键
StanzaHandler.java-> StanzaHandler
/** * Constructor. * * @param serverName the server name * @param connection the connection */ public StanzaHandler(String serverName, Connection connection) { this.serverName = serverName; this.connection = connection; this.router = new PacketRouter();//该构造中初始化了三个router实现,分别是 MessageRouter,PresenceRouter,IQRouter,分别处理xmpp的三种消息类型 notificationService = ServiceLocator.getNotificationService(); notificationManager = new NotificationManager(); }
XmppIoHandler.java-> messageReceived
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);//###这个方法中,程序会根据xmpp的xml头来判断消息类型,并且传递到对应的Router处理类 } catch (Exception e) { log.error( "Closing connection due to error while processing message: " + message, e); Connection connection = (Connection) session .getAttribute(CONNECTION); connection.close(); } }
因为当前做的需求是消息推送,而MessageRouter实用在IM这种对话场合,所以MessageRouter并没有实现处理PresenceRouter和IQRouter都有对应的功能实现。
IQRoute.java -> IQRouter
public IQRouter() { sessionManager = SessionManager.getInstance(); iqHandlers.add(new IQAuthHandler()); iqHandlers.add(new IQRegisterHandler()); iqHandlers.add(new IQRosterHandler()); notificationService = ServiceLocator.getNotificationService(); }
构造中又实现了3和handler,用户建立好链接后又会对用户进行IQAuthHandler(鉴权),IQRegisterHandler(解析用户携带参数,往数据库中插入用户信息,将链接保存在sessionManager中),IQRosterHandler(没进行什么操作)
-
消息的发送
以广播消息为例
SessionManager.java
/** * Returns a list that contains all authenticated client sessions. * * @return a list that contains all client sessions */ public Collection<ClientSession> getSessions() { return clientSessions.values(); }
获得链接池中所有链接
Session.java
/** * Delivers the packet to the associated connection. * * @param packet the packet to deliver */ public void deliver(Packet packet) { if (connection != null && !connection.isClosed()) { connection.deliver(packet); } }
Connection.java
/** * Delivers the packet to this connection (without checking the recipient). * * @param packet the packet to deliver */ 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);//###通过mina的isSession对象传递 } catch (Exception e) { log.debug("Connection: Error delivering packet" + "\n" + this.toString(), e); errorDelivering = true; } if (errorDelivering) { close(); } else { session.incrementServerPacketCount(); } } }
封装层次为isSession->Connection->Session
-
链接关闭
XmppIoHandler.java
/** * Invoked when a connection is closed. */ public void sessionClosed(IoSession session) throws Exception { log.debug("sessionClosed()..."); Connection connection = (Connection) session.getAttribute(CONNECTION); connection.close(); }
Connection.java
/** * Closes the session including associated socket connection, * notifing all listeners that the channel is shutting down. */ public void close() { boolean closedSuccessfully = false; synchronized (this) { if (!isClosed()) { try { deliverRawText("</stream:stream>", false); } catch (Exception e) { // Ignore } if (session != null) { session.setStatus(Session.STATUS_CLOSED); } ioSession.close(false); closed = true; closedSuccessfully = true; } } if (closedSuccessfully) { notifyCloseListeners(); } }
此处使用了观察者模式,首先传递了Xmpp的结束xml“</stream:stream>”,再关闭了mina最低层的ioSession,再通知观察者,也就是androidpn封装的ClientSession在内存中删除该会话。
原创文章,转载请声名出处 http://spjich.iteye.com/blog/2226149
相关推荐
- `androidpn-client-0.5.0.zip` 包含了AndroidPN的客户端源码,开发者可以深入研究其中的网络连接、消息解析和推送处理等逻辑。 - `androidpn-server-0.5.0-src.zip` 提供了服务端的源代码,有助于理解如何实现推...
AndroidPN项目提供了服务器端和客户端的源码,服务器端代码(androidpn-server)、示例应用(androidpn-demoapp)和客户端应用(androidpn-client)。如果你选择使用AndroidPN,需要注意项目导入时可能出现的构建...
在IT领域,尤其是在图形设计和跨平台应用开发中,字体的选择和使用是非常关键的。"Windows系统字体转unifont字体工具"是一个专门用于将Windows操作系统中的字体转换为unifont格式的工具。这个工具的主要目标是帮助用户解决在不同操作系统之间字体兼容性的问题,特别是对于那些需要支持大量字符集,如Unicode的项目。 Unifont是一种开放源代码的字体,包含了几乎所有的Unicode字符,因此在多语言环境和开源软件中特别受欢迎。它提供了一种统一的视觉体验,确保无论在哪种操作系统或设备上,都能准确显示各种语言的文字。然而,Windows系统默认的字体并不包含所有Unicode字符,这可能导致在某些情况下无法正确显示非标准字符。 转换过程涉及以下几个核心知识点: 1. **字体格式**:Windows系统中常见的字体格式有TrueType(.ttf)和OpenType(.otf),而unifont是一种特殊的Bitmap字体,通常以.gz ufnt或.ttf.gz形式存在。Bitmap字体将每个字符绘制为位图,适合低分辨率屏幕或嵌入式系统,但可能在高分辨率下显得模糊。
uClinux源代码中Make文件完全解析.pdf.rar
## 介绍 进入互联网新媒体时代,“股吧”作为一类专门针对上市公司的社交媒介,已经成为中小投资者分享投资经验和发表对公司运营意见的重要平台,股吧舆论作为投资者情绪的反映,直接影响股票的市场表现。 ## 一、上市公司股吧舆论数据的介绍 “股吧”作为新兴社交媒体代表,本身并不提供信息,仅提供多方交互平台,其将个体间的实时交流和回应形成公众关注和舆论;因此,股吧舆论数据可以帮助研究人员深入分析网络舆论与企业表现之间的关系,并为投资者提供情绪波动的参考依据。 本分享数据年份为2008年到2023年,数据来源于东方财富网股吧,涉及A股上市公司的讨论情况,涵盖了股吧发帖数量、阅读量、评论次数等多个维度。 ## 二、数据指标
【东海证券-2025研报】公司深度报告:AIOT次新品显著放量,产品矩阵拓展布局新市场.pdf
图书管理系统,系统包含两种角色:管理员、用户,系统分为前台和后台两大模块,主要功能如下。 前台: - 首页:展示系统的概览信息。 - 图书信息:展示图书的详细信息。 - 公告信息:展示图书馆相关的通知公告。 - 在线咨询:提供在线客服咨询服务。 - 个人中心:用户可以登录后进入个人中心 后台: 管理员角色: - 个人中心:管理员可以管理个人信息,修改密码等。 - 用户管理:管理员可以对用户的信息进行增删改查等操作。 - 图书分类管理:管理员可以管理图书分类信息,添加、修改、删除分类名称及其描述。 - 图书信息管理:管理员可以管理图书的基本信息。 - 系统管理:管理员可以管理系统的一些通用配置。 二、项目技术 编程语言:Java 数据库:MySQL 项目管理工具:Maven 前端技术:Vue 后端技术:SpringBoot 三、运行环境 操作系统:Windows、macOS都可以 JDK版本:JDK1.8以上都可以 开发工具:IDEA、Ecplise、Myecplise都可以 数据库: MySQL5.7以上都可以 Maven:任意版本都可以
企业数字化转型IT信息化战略.pdf
LB1Q-PHP+MySql_1个通用条件工资成绩等通用查询系统手机加强版版(Utf-8)_2024最终版
基于Python卷积神经网络人脸识别驾驶员疲劳检测与预警系统+源码+文档(毕业设计)本资源中的源码都是经过本地编译过可运行的,评审分达到95分以上,适合正在准备毕业设计或者大作业的学生和实战人员,可作为毕业设计、大作业,资源项目的难度比较适中,内容都是经过助教老师审定过的能够满足学习、使用需求,如果有需要的话可以放心下载使用。 基于Python卷积神经网络人脸识别驾驶员疲劳检测与预警系统+源码+文档(毕业设计)基于Python卷积神经网络人脸识别驾驶员疲劳检测与预警系统+源码+文档(毕业设计)基于Python卷积神经网络人脸识别驾驶员疲劳检测与预警系统+源码+文档(毕业设计)基于Python卷积神经网络人脸识别驾驶员疲劳检测与预警系统+源码+文档(毕业设计)基于Python卷积神经网络人脸识别驾驶员疲劳检测与预警系统+源码+文档(毕业设计)基于Python卷积神经网络人脸识别驾驶员疲劳检测与预警系统+源码+文档(毕业设计)基于Python卷积神经网络人脸识别驾驶员疲劳检测与预警系统+源码+文档(毕业设计)基于Python卷积神经网络人脸识别驾驶员疲劳检测与预警系统+源码+文档(毕业设
矢量边界,行政区域边界,精确到乡镇街道,可直接导入arcgis使用
平芯微PW4584应用电路pcb,ad格式
民宿管理系统,系统包含两种角色:管理员、用户,系统分为前台和后台两大模块,主要功能如下。 前台功能: 1. 首页:展示民宿的相关信息和推荐房源。 2. 房间信息:用户可以查看房间的详细信息。 3. 论坛:用户可以在论坛上进行讨论和交流。 4. 公告信息:展示民宿的公告信息,包括优惠活动、重要通知等。 5. 个人中心:用户可以管理个人信息、订单记录、收藏房源等。 后台功能: 用户: 1. 个人中心:管理个人信息。 2. 房间信息管理:管理房间信息,包括添加、编辑、删除房间信息。 3. 论坛管理:管理论坛帖子,包括查看、删除、置顶等操作。 4. 公告信息管理:管理公告信息,包括添加、编辑、删除公告等操作。 管理员: 1. 个人中心:管理个人信息。 2. 管理员管理:管理其他管理员账号,包括添加、编辑、删除管理员等操作。 3. 基础数据管理:管理基础数据,包括地区信息、设施信息等。 4. 房间信息管理:管理房间信息,包括添加、编辑、删除房间信息。 5. 论坛管理:对论坛帖子进行管理,包括查看、删除、置顶等操作。 6. 公告信息管理:管理公告信息,包括添加、编辑、删除公告等操作。
30012第9章MCS-51单片机IO接口技术20140413.ppt
【最新版】 JESD22-A104F.01 2023.rar
船舶维保管理系统,系统包含四种角色:管理员、船家、维保人员、维保公司,系统分为前台和后台两大模块,主要功能如下。 船家: - 个人中心:管理个人信息。 - 公告管理:查看和发布系统公告。 - 船舶管理:管理自己的船舶信息。 - 维保公司管理:选择和管理维保公司。 - 维保计划管理:制定和管理船舶的维保计划。 - 故障上报管理:上报船舶的故障情况。 维保公司: - 个人中心:管理个人信息。 - 公告管理:查看和发布系统公告。 - 船舶管理:管理负责的船舶信息。 - 维保人员管理:管理维保人员的信息。 - 维保计划管理:制定和管理船舶的维保计划。 - 故障上报管理:接收和处理船舶的故障上报。 - 维修成本管理:记录和统计维修过程中的成本。 维保人员: - 个人中心:管理个人信息。 - 公告管理:查看系统公告。 - 船舶管理:管理负责的船舶信息。 - 维保计划管理:查看和执行船舶的维保计划。 - 故障上报管理:上报船舶的故障情况。 - 维修成本管理:记录和统计维修过程中的成本。
LW3T-PHP+TXT_3个通用条件工资成绩等通用查询系统电脑网页版版(Utf-8)_2024最终版
【东兴证券-2025研报】食品饮料行业:市场预期逐渐改变,关注短期估值修复行情.pdf
毕业就业信息管理系统,系统包含三种角色:管理员、公司、用户,系统分为前台和后台两大模块,主要功能如下。 前台: 首页:展示毕业就业信息管理平台的相关内容。 公司:浏览和搜索招聘公司的信息。 简历:学生上传和管理自己的简历,进行简历投递。 公告信息:查看和发布最新的就业相关公告信息。 职位招聘:浏览和搜索最新的职位招聘信息。 个人中心:管理个人信息和简历。 后台(管理员): 个人中心:管理个人信息。 管理员管理:管理其他管理员账号的信息。 基础数据管理:管理系统中的基础数据, 公司管理:管理招聘公司的信息, 简历管理:管理学生上传的简历信息, 就业统计管理:统计毕业生的就业情况。 公告信息管理:发布和管理最新的就业相关公告信息。 简历投递管理:管理简历投递情况, 学生管理:管理学生账号信息, 职位招聘管理:管理职位招聘信息, 轮播图信息:管理系统的轮播图信息, 用户: 个人中心:管理个人信息和简历。 简历管理:上传和管理个人的简历信息。 就业统计管理:查看和更新个人的就业情况。 公告信息管理:查看最新的就业相关公告信息。 简历投递管理:查看个人简历投递情况和审核进度。 职位招聘
君の850云端女孩全面屏.apk