码迷,mamicode.com
首页 > 其他好文 > 详细

smack 监听不同packet机制

时间:2014-05-14 03:22:52      阅读:364      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   c   

之前做即时通讯,扒了smack源码来参考。说下其中解包后进行通知的机制。

Filter类:accept(Packet packet)函数,传入packet在此函数中进行对比判断,返回true 则通过此filter认证。实现PacketFilter接口即可。

bubuko.com,布布扣
public interface PacketFilter {

    /**
     * Tests whether or not the specified packet should pass the filter.
     *
     * @param packet the packet to test.
     * @return true if and only if <tt>packet</tt> passes the filter.
     */
    public boolean accept(Packet packet);
}
bubuko.com,布布扣

ListenerWrapper类:packetListener对应了自己在应用中定义的Listener(实现PacketListener接口),当通过对应的packetFilter之后,则进行回掉;

bubuko.com,布布扣
/**
     * A wrapper class to associate a packet filter with a listener.
     */
    protected static class ListenerWrapper {

        private PacketListener packetListener;
        private PacketFilter packetFilter;

        /**
         * Create a class which associates a packet filter with a listener.
         * 
         * @param packetListener the packet listener.
         * @param packetFilter the associated filter or null if it listen for all packets.
         */
        public ListenerWrapper(PacketListener packetListener, PacketFilter packetFilter) {
            this.packetListener = packetListener;
            this.packetFilter = packetFilter;
        }

        /**
         * Notify and process the packet listener if the filter matches the packet.
         * 
         * @param packet the packet which was sent or received.
         */
        public void notifyListener(Packet packet) {
            if (packetFilter == null || packetFilter.accept(packet)) {
                packetListener.processPacket(packet);
            }
        }
    }
bubuko.com,布布扣
bubuko.com,布布扣
public interface PacketListener {

    /**
     * Process the next packet sent to this packet listener.<p>
     *
     * A single thread is responsible for invoking all listeners, so
     * it‘s very important that implementations of this method not block
     * for any extended period of time.
     *
     * @param packet the packet to process.
     */
    public void processPacket(Packet packet);

}
bubuko.com,布布扣

 

接下来就是分包了,PacketReader中listenerExecutor.submit(new ListenerNotification(packet));在ListenerNotification线程里面进行判断,关键代码如下,写在run里面

bubuko.com,布布扣
for (ListenerWrapper listenerWrapper : connection.recvListeners.values()) {
                listenerWrapper.notifyListener(packet);
            }
bubuko.com,布布扣

通过迭代出ListenerWrapper调用notifyListener函数,查找通过验证的filter然后相对应是哪个listener就进行通知。

smack 监听不同packet机制,布布扣,bubuko.com

smack 监听不同packet机制

标签:style   blog   class   code   java   c   

原文地址:http://www.cnblogs.com/helloandroid/p/3721991.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!