`
longgangbai
  • 浏览: 7325494 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

androidpn的学习研究(六)Androidpn-server的Mina编码和解码解析过程

阅读更多

          在许多网络应用中可能针对传输的数据进行加密操作,接收到数据之后进行解码操作。

在mina中提供许多加密和解密的解析方式:

1.带一定前缀的字符串的解析方式。

2.序列化对象的字符串解析方式。

3.分隔符方式的字符串解析方式。

 

 

在mina中提供相关的filterchain支持相关的操作。

Mina的源代码如下:

package org.apache.mina.filter.codec;

import org.apache.mina.core.session.IoSession;

/**
 * Provides {@link ProtocolEncoder} and {@link ProtocolDecoder} which translates
 * binary or protocol specific data into message object and vice versa.
 * <p>
 * Please refer to
 * <a href="../../../../../xref-examples/org/apache/mina/examples/reverser/ReverseProtocolProvider.html"><code>ReverserProtocolProvider</code></a>
 * example.
 *
 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
 */
public interface ProtocolCodecFactory {
    /**
     * Returns a new (or reusable) instance of {@link ProtocolEncoder} which
     * encodes message objects into binary or protocol-specific data.
     */
    ProtocolEncoder getEncoder(IoSession session) throws Exception;

    /**
     * Returns a new (or reusable) instance of {@link ProtocolDecoder} which
     * decodes binary or protocol-specific data into message objects.
     */
    ProtocolDecoder getDecoder(IoSession session) throws Exception;
}

 

 

加密处理类:

package org.apache.mina.filter.codec;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;

/**
 * Encodes higher-level message objects into binary or protocol-specific data.
 * MINA invokes {@link #encode(IoSession, Object, ProtocolEncoderOutput)}
 * method with message which is popped from the session write queue, and then
 * the encoder implementation puts encoded messages (typically {@link IoBuffer}s)
 * into {@link ProtocolEncoderOutput} by calling {@link ProtocolEncoderOutput#write(Object)}.
 * <p>
 * Please refer to
 * <a href="../../../../../xref-examples/org/apache/mina/examples/reverser/TextLineEncoder.html"><code>TextLineEncoder</code></a>
 * example.
 *
 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
 * 
 * @see ProtocolEncoderException
 */
public interface ProtocolEncoder {

    /**
     * Encodes higher-level message objects into binary or protocol-specific data.
     * MINA invokes {@link #encode(IoSession, Object, ProtocolEncoderOutput)}
     * method with message which is popped from the session write queue, and then
     * the encoder implementation puts encoded messages (typically {@link IoBuffer}s)
     * into {@link ProtocolEncoderOutput}.
     *
     * @throws Exception if the message violated protocol specification
     */
    void encode(IoSession session, Object message, ProtocolEncoderOutput out)
            throws Exception;

    /**
     * Releases all resources related with this encoder.
     *
     * @throws Exception if failed to dispose all resources
     */
    void dispose(IoSession session) throws Exception;
}

 

解密类如下:

package org.apache.mina.filter.codec;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;

/**
 * Decodes binary or protocol-specific data into higher-level message objects.
 * MINA invokes {@link #decode(IoSession, IoBuffer, ProtocolDecoderOutput)}
 * method with read data, and then the decoder implementation puts decoded
 * messages into {@link ProtocolDecoderOutput} by calling
 * {@link ProtocolDecoderOutput#write(Object)}.
 * <p>
 * Please refer to
 * <a href="../../../../../xref-examples/org/apache/mina/examples/reverser/TextLineDecoder.html"><code>TextLineDecoder</code></a>
 * example.
 *
 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
 * 
 * @see ProtocolDecoderException
 */
public interface ProtocolDecoder {
    /**
     * Decodes binary or protocol-specific content into higher-level message objects.
     * MINA invokes {@link #decode(IoSession, IoBuffer, ProtocolDecoderOutput)}
     * method with read data, and then the decoder implementation puts decoded
     * messages into {@link ProtocolDecoderOutput}.
     *
     * @throws Exception if the read data violated protocol specification
     */
    void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out)
            throws Exception;

    /**
     * Invoked when the specified <tt>session</tt> is closed.  This method is useful
     * when you deal with the protocol which doesn't specify the length of a message
     * such as HTTP response without <tt>content-length</tt> header. Implement this
     * method to process the remaining data that {@link #decode(IoSession, IoBuffer, ProtocolDecoderOutput)}
     * method didn't process completely.
     *
     * @throws Exception if the read data violated protocol specification
     */
    void finishDecode(IoSession session, ProtocolDecoderOutput out)
            throws Exception;

    /**
     * Releases all resources related with this decoder.
     *
     * @throws Exception if failed to dispose all resources
     */
    void dispose(IoSession session) throws Exception;
}

 

在androidpn中的没有采取任何加密方式,但是提供相关的类org.androidpn.server.xmpp.codec,如果需要可以针对传输的数据进行加密和解密工作。

package org.androidpn.server.xmpp.codec;

import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolEncoder;

/** 
 * Factory class that specifies the encode and decoder to use for parsing XMPP stanzas.
 *
 * @author Sehwan Noh (devnoh@gmail.com)
 */
public class XmppCodecFactory implements ProtocolCodecFactory {

    private final XmppEncoder encoder;

    private final XmppDecoder decoder;

    /**
     * Constructor.
     */
    public XmppCodecFactory() {
        encoder = new XmppEncoder();
        decoder = new XmppDecoder();
    }

    /**
     * Returns a new (or reusable) instance of ProtocolEncoder.
     */
    public ProtocolEncoder getEncoder(IoSession session) throws Exception {
        return encoder;
    }

    /**
     * Returns a new (or reusable) instance of ProtocolDecoder.
     */
    public ProtocolDecoder getDecoder(IoSession session) throws Exception {
        return decoder;
    }

}

 

androidpn的解密类:

package org.androidpn.server.xmpp.codec;

import org.androidpn.server.xmpp.net.XmppIoHandler;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
import org.jivesoftware.openfire.nio.XMLLightweightParser;

/** 
 * Decoder class that parses ByteBuffers and generates XML stanzas.
 *
 * @author Sehwan Noh (devnoh@gmail.com)
 */
public class XmppDecoder extends CumulativeProtocolDecoder {

    // private final Log log = LogFactory.getLog(XmppDecoder.class);

    @Override
    public boolean doDecode(IoSession session, IoBuffer in,
            ProtocolDecoderOutput out) throws Exception {
        // log.debug("doDecode(...)...");

        XMLLightweightParser parser = (XMLLightweightParser) session
                .getAttribute(XmppIoHandler.XML_PARSER);
        parser.read(in);

        if (parser.areThereMsgs()) {
            for (String stanza : parser.getMsgs()) {
                out.write(stanza);
            }
        }
        return !in.hasRemaining();
    }

}

 

 

androidpn的加密类:

package org.androidpn.server.xmpp.codec;

import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoder;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;

/** 
 *  Encoder class that does nothing (to the already encoded data). 
 *
 * @author Sehwan Noh (devnoh@gmail.com)
 */
public class XmppEncoder implements ProtocolEncoder {

    // private final Log log = LogFactory.getLog(XmppEncoder.class);

    public void encode(IoSession session, Object message,
            ProtocolEncoderOutput out) throws Exception {
        // log.debug("encode()...");
    }

    public void dispose(IoSession session) throws Exception {
        // log.debug("dispose()...");
    }

}

   最终将编码解析器转换为过滤器使用,具体配置如下:

					<bean class="org.apache.mina.filter.codec.ProtocolCodecFilter">
						<constructor-arg>
							<bean class="org.androidpn.server.xmpp.codec.XmppCodecFactory" />
						</constructor-arg>
					</bean>

 

分享到:
评论

相关推荐

    androidpn-client-0.5.0.zip和androidpn-server-0.5.0-bin.zip

    首先, 我们需要下载androidpn-client-0.5.0.zip和androidpn-server-0.5.0-bin.zip。 下载地址:http://sourceforge.net/projects/Androidpn/ 解压两个包,Eclipse导入client,配置好目标平台,打开raw/...

    androidpn-client-0.5.01.rar和androidpn-server-0.5.0-bin.zip可在真机上运行

    androidpn-server-0.5.0-bin.zip解压后,打卡bin目录下run.bat运行,之后在浏览器中输入http://127.0.0.1:7070/ 将androidpn-client-0.5.0解压后导入Eclipse,修改/raw/androidpn.properties中的xmppHost=xxx.xxx.x...

    androidpn-client-0.5.0和androidpn-server-0.5.0-bin

    本文将围绕"androidpn-client-0.5.0"和"androidpn-server-0.5.0-bin"这两个核心组件,深入探讨AndroidPN的工作原理、实现方式以及其在实际应用中的价值。 首先,我们来看客户端组件"androidpn-client-0.5.0"。它是...

    androidpn-server-0.5.0-bin、androidpn-client-0.5.0、androidpn-demoapp-0.5.0

    这个项目包括三个主要组件:服务器端(androidpn-server)、客户端库(androidpn-client)以及一个演示应用(androidpn-demoapp)。我们来逐一解析这些组件及其相关知识点。 1. **AndroidPN-Server-0.5.0-bin**: 这...

    androidpn-client-0.5.0 AND androidpn-server-0.5.0

    使用Apndroid Push Notification 实现android信息推送,AndroidPn项目是使用XMPP协议实现信息推送的一个开源项目。内涵服务端和客户端源码

    androidpn-bin-server-0.5.0

    "androidpn-bin-server-0.5.0"是该项目的一个特定版本,主要用于实现服务端功能。在这个版本中,我们可以找到核心的服务器组件,用于处理与Android客户端的通信,发送实时通知。 在AndroidPN中,服务端扮演着至关...

    androidpn-server-app-master

    "androidpn-server-app-master" 是一个基于Android的推送通知服务(Push Notification)的项目,它采用了Tomcat作为服务器端的应用程序容器。这个项目的核心目标是为Android设备提供实时的消息推送功能,使得应用...

    androidpn-server-1.0.0.zip-jre7

    该程序包修改至开源项目androidpn-server-0.0.5,携带jre7(此为windows版本,如要运行在linix,需更换为linix jre7),无需java配置运行环境,实现离线推送功能,支持推送消息有效期设置,服务端能够自主判断消息...

    androidpn-tomcat-server端

    总之,"androidpn-tomcat-server端"为开发者提供了一个自定义推送服务的基础框架,涉及到了Java Web开发、服务器配置、网络编程等多个领域的知识,对于深入理解移动应用的后台服务有着重要的学习价值。

    androidpn-0.5.0 server+demoapp+client+成功配置方法

    内含:配置方法一份、androidpn-server-0.5.0-bin.zip、androidpn-demoapp-0.5.0-bin.zip、androidpn-client-0.5.0-bin.zip。三个压缩包都是从官网下载http://sourceforge.net/projects/androidpn/,未做任何修改。...

    androidpn-server-0.6.0.jar

    androidpn-server-0.6.0.jar

    androidpn(客户端和服务器端)

    androidpn-client.zip和androidpn-server-0.5.0-bin.zip, 解压两个包,Eclipse导入client,配置好目标平台,打开raw/androidpn.properties文件, apiKey=1234567890 xmppHost=10.0.2.2 xmppPort=5222 如果是...

    androidpn-client-0.5.0

    "AndroidPN-client-0.5.0"是一个针对Android平台的Push Notification服务的客户端库,主要功能是为了实现设备与服务器之间的即时通信。Push Notification服务在移动应用开发中扮演着重要角色,它允许服务器向已安装...

    xmpp-androidPn server and client

    在这个“xmpp-androidPn server and client”的项目中,我们关注的是XMPP在Android平台上的应用,特别是在实现推送通知(Push Notification)方面的功能。 服务端组件: 1. androidpn-server-0.5.0-bin.zip:这个...

    androidpn-client-0.5.0.zip

    总的来说,"androidpn-client-0.5.0.zip"为Android开发者提供了一个实现Push Notification功能的工具,通过这个压缩包,开发者可以学习、定制并集成PN服务到他们的应用程序中,从而提高应用的互动性和用户体验。

    androidpn_tomcat整合(发布直接使用)

    本文将深入探讨如何将AndroidPN-server与Tomcat应用服务器进行整合,以便在MyEclipse环境中直接运行和管理。这将帮助开发者更方便地实现远程通知功能,提高应用程序的交互性和用户体验。 首先,我们需要理解...

    androidpn-client-Demo.zip_DEMO_androidpn-server_asmack _xmpp _推送

    基于XMPP协议、使用Androidpn服务器,Asmack编程的即时通讯IM客户端源代码;主要功能实现向客户端推送消息!

    androidpn-client-0.5.0.rar_androidpn-server_消息推送an

    这是一个消息推送客户端,安装在自己的手机上,可以实现与andriodpn-sever的连接,接受推送的消息

    androidpn0.5.0

    androidpn0.5.0,包括androidpn-server-0.5.0-bin.zip、androidpn-demoapp-0.5.0.zip、androidpn-client-0.5.0.zip

Global site tag (gtag.js) - Google Analytics