您的位置:首页 > 编程语言 > PHP开发

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

2014-12-07 15:54 483 查看
接口 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<Object> messageQueue = new ConcurrentLinkedQueue<Object>();

public AbstractProtocolDecoderOutput() {
// Do nothing
}

public Queue<Object> 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<Object> messageQueue = getMessageQueue();

while (!messageQueue.isEmpty()) {
nextFilter.messageReceived(session, messageQueue.poll());
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: