AndroidPN服务器源码简要分析

本文简要分析AndroidPN项目的源码,重点探讨Mina网络库在服务器和客户端中的应用,以及Spring MVC如何与Mina结合。文章提及服务器端的TimeServerHandler和自定义编解码,客户端的实现待续。此外,还介绍了服务器与客户端的数据交互过程,以及项目结构和用户在线状态的处理流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AndroidPN
https://github.com/dannytiehui/androidpn

郭霖 Android消息推送教学视频
http://www.imooc.com/learn/223
http://www.imooc.com/learn/358

Mina
官网 http://mina.apache.org/mina-project/index.html
资源
http://my.oschina.net/ielts0909/blog/92716?p=1#comments
http://my.oschina.net/makemyownlife/blog/122738

XMPP
官网 http://xmpp.org/
资源 http://www.cnblogs.com/hanyonglu/archive/2012/03/04/2378956.html

Mina


百度百科
ApacheMINA是一个网络应用程序框架,用来帮助用户简单地开发高性能和高可扩展性的网络应用程序。它提供了一个通过Java NIO在不同的传输例如TCP/IP和UDP/IP上抽象的事件驱动的异步API。


服务器

public class MinaTimeServer
{
   
    public static void main( String[] args ) throws IOException
    {
        IoAcceptor acceptor = new NioSocketAcceptor();
        //日志过滤器
        acceptor.getFilterChain().addLast( "logger", new LoggingFilter() );
        //ProtocolCodecFilter该过滤器将二进制或者协议特定的数据转换为消息对象
        //TextLineCodecFactory处理文本信息(编解码处理)
        acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter(new 
        TextLineCodecFactory( Charset.forName( "UTF-8" ))));
        //设置逻辑处理器(服务于客户端的请求)
        acceptor.setHandler(  new TimeServerHandler() );
        acceptor.getSessionConfig().setReadBufferSize( 2048 );
        //设置空闲时间
        acceptor.getSessionConfig().setIdleTime( IdleStatus.BOTH_IDLE, 10 );
    }
}

TimeServerHandler

public class TimeServerHandler extends IoHandlerAdapter
{
   
    @Override
    public void exceptionCaught( IoSession session, Throwable cause ) throws 
    Exception
    {
        cause.printStackTrace();
    }

    @Override
    public void messageReceived( IoSession session, Object message ) throws Exception
    {
  //服务器接收请求
        String str = message.toString();
        if( str.trim().equalsIgnoreCase("quit") ) {
            session.close();
            return;
        }

        Date date = new Date();
        session.write( date.toString() );
        System.out.println("Message written...");
    }

    @Override
    public void sessionIdle( IoSession session, IdleStatus status ) throws Exception
    {
  //服务器进入空闲状态
        System.out.println( "IDLE " + session.getIdleCount( status ));
    }

    sessionOpen()...//服务器与客户端连接开启
    sessionCreated()...//服务器与客户端连接创建
    sessionClose()...//服务器与客户端连接关闭
    messageSent()...//服务器发送消息,消息发送事件触发,当消息即响应发送(调用IoSession.write())
}

自定义编解码
http://www.himigame.com/apache-mina/839.html
http://blog.youkuaiyun.com/a600423444/article/details/6671035

(以下代码出处 郭霖消息推送视频)
XmppDecoder

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 {
       int startPosition = in.position();
       while(in.hasRemaing()){
            byte b = in.get();
            if(b=='\n'){
                int currentPosition = in.position();
                int limit = in.limit();
                in.position(startPosition);
                in.limit(currentPosition);
                IoBuffer buf= in.slince();
                byte[] dest = new byte[buf.limit()];
                buf.get(dest);
                String str = new String(dest);
                out.write(str);
                in.position(currentPosition);
                in.limit(limit);
            }
        }
    }

}

XmppEncoder

public class XmppEncoder implements ProtocolEncoder {
   

    public void encode(IoSession session, Object message,
            ProtocolEncoderOutput out) throws Exception {
        // log.debug("encode()...");
        String s =null;
        if(message instanceof String){
            s = (String)message;
        }
        if(s!=null){
            CharsetEncoder charsetEncoder =(CharsetEncoder)
            session.getAttribute("encoder");
            if(charsetEncoder==null){
            charsetEncoder  = Charset.defaultCharset().newEncoder();
            session.setAttribute("encoder",charsetEncoder);
        }
        IoBuffer ioBuffer = IoBuffer.allocate(s.length);
        ioBuffer.setAutoExpanded(true);
        ioBuffer.putString(s,charsetEncoder);
        ioBuffer.flip();
        out.write(ioBuffer);
    }

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

}

XmppFactory

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值