MINA源码分析---协议解码输出接口ProtocolDecoderOutput及其实现


接口 ProtocolDecoderOutput

/*
 */
package org.apache.mina.filter.codec;

import org.apache.mina.core.filterchain.IoFilter.NextFilter;
import org.apache.mina.core.session.IoSession;

/**
 * Callback for {@link ProtocolDecoder} to generate decoded messages.
 * 协议解码器ProtocolDecoder的回调接口来产生解码后的消息对象
 * {@link ProtocolDecoder} must call {@link #write(Object)} for each decoded
 * messages.
 * 协议解码器ProtocolDecoder必须为每一个解码后的消息对象调用此接口的write(Object)方法
 * 将消息输出
 */
public interface ProtocolDecoderOutput {
    /**
     * Callback for {@link ProtocolDecoder} to generate decoded messages.
     * {@link ProtocolDecoder} must call {@link #write(Object)} for each
     * decoded messages.
     *
     * @param message the decoded message
     */
    void write(Object message);

    /**
     * Flushes all messages you wrote via {@link #write(Object)} to
     * the next filter.通过write(Object)方法将所有消息立即写到下一个过滤器中
     */
    void flush(NextFilter nextFilter, IoSession session);
}

接口  ProtocolDecoderOutput的一个实现

package org.apache.mina.filter.codec;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * A {@link ProtocolDecoderOutput} based on queue.
 * 一个基于ConcurrentLinkedQueue并发队列的协议解码输出器
 */
public abstract class AbstractProtocolDecoderOutput implements ProtocolDecoderOutput {
    private final Queue messageQueue = new ConcurrentLinkedQueue();

    public AbstractProtocolDecoderOutput() {
        // Do nothing
    }

    public Queue getMessageQueue() {
        return messageQueue;
    }

    public void write(Object message) {
        if (message == null) {
            throw new IllegalArgumentException("message");
        }

        messageQueue.add(message);
    }
}
 
  


ProtocolCodecFilter过滤器中提供一个实现,主要是实现了flush方法
    //协议解码器输出的一个实现
    private static class ProtocolDecoderOutputImpl extends
            AbstractProtocolDecoderOutput {
        public ProtocolDecoderOutputImpl() {
            // Do nothing
        }

        public void flush(NextFilter nextFilter, IoSession session) {
            Queue messageQueue = getMessageQueue();
            
            while (!messageQueue.isEmpty()) {
                nextFilter.messageReceived(session, messageQueue.poll());
            }
        }
    } 
  

你可能感兴趣的:(mina框架)