/**
* An {@link IoFilter} which translates binary or protocol specific data into
* message object and vice versa using {@link ProtocolCodecFactory},
* {@link ProtocolEncoder}, or {@link ProtocolDecoder}.
*
* @author The Apache Directory Project ([email protected])
* @version $Rev$, $Date$
*/
public class ProtocolCodecFilter extends IoFilterAdapter
{
private static final Logger LOGGER = LoggerFactory.getLogger(org/apache/mina/filter/codec/ProtocolCodecFilter);
private static final Class EMPTY_PARAMS[] = new Class[0];
private static final IoBuffer EMPTY_BUFFER = IoBuffer.wrap(new byte[0]);
//编码器属性key
private static final AttributeKey ENCODER =
new AttributeKey(org/apache/mina/filter/codec/ProtocolCodecFilter, "encoder");
//编码器输出属性key
private static final AttributeKey ENCODER_OUT =
new AttributeKey(org/apache/mina/filter/codec/ProtocolCodecFilter, "encoderOut");
//解码器属性key
private static final AttributeKey DECODER =
new AttributeKey(org/apache/mina/filter/codec/ProtocolCodecFilter, "decoder");
//解码器输出属性key
private static final AttributeKey DECODER_OUT =
new AttributeKey(org/apache/mina/filter/codec/ProtocolCodecFilter, "decoderOut");
private final ProtocolCodecFactory factory;//协议编解码器工厂
}
/**
* Provides {@link ProtocolEncoder} and {@link ProtocolDecoder} which translates
* binary or protocol specific data into message object and vice versa.
* 协议编解码工厂ProtocolCodecFactory提供协议编码器和解码器,解码器二进制数据或
协议数据到消息对象;编码器反之。
* Please refer to
* [url=../../../../../xref-examples/org/apache/mina/examples/reverser/ReverseProtocolProvider.html]ReverserProtocolProvider
[/url]
* example.
*
* @author The Apache Directory Project ([email protected])
* @version $Rev$, $Date$
*/
public interface ProtocolCodecFactory {
/**
* Returns a new (or reusable) instance of {@link ProtocolEncoder} which
* encodes message objects into binary or protocol-specific data.
返回一个编码器实例,用于将消息对象编码成二进制或协议数据
*/
ProtocolEncoder getEncoder() throws Exception;
/**
* Returns a new (or reusable) instance of {@link ProtocolDecoder} which
* decodes binary or protocol-specific data into message objects.
返回一个解码器实例,用于将二进制或协议数据解码成消息对象
*/
ProtocolDecoder getDecoder() throws Exception;
}
/**
* Decodes binary or protocol-specific data into higher-level message objects.
* MINA invokes {@link #decode(IoSession, ByteBuffer, ProtocolDecoderOutput)}
* method with read data, and then the decoder implementation puts decoded
* messages into {@link ProtocolDecoderOutput} by calling
* {@link ProtocolDecoderOutput#write(Object)}.
*
* Please refer to
* [url=../../../../../xref-examples/org/apache/mina/examples/reverser/TextLineDecoder.html]TextLineDecoder
[/url]
* example.
*
* @author The Apache Directory Project ([email protected])
* @version $Rev$, $Date$
*/
public interface ProtocolDecoder {
/**
* Decodes binary or protocol-specific content into higher-level message objects.
* MINA invokes {@link #decode(IoSession, ByteBuffer, ProtocolDecoderOutput)}
* method with read data, and then the decoder implementation puts decoded
* messages into {@link ProtocolDecoderOutput}.
* 将二级制或协议内存解码成上层消息对象。mina在读取数据时,调用解码器的#decode,
将解码后的消息放到ProtocolDecoderOutput
* @throws Exception if the read data violated protocol specification
*/
void decode(IoSession session, ByteBuffer in, ProtocolDecoderOutput out)
throws Exception;
/**
* Invoked when the specified session 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 content-length header. Implement this
* method to process the remaining data that {@link #decode(IoSession, ByteBuffer, ProtocolDecoderOutput)}
* method didn't process completely.
* 到session关闭时,调用此方法。当处理没有精确长度协议时,特别有用,不如Http没有内容长度的回应。
实现此方法,主要是对于
* @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;
}
/**
* An abstract {@link ProtocolDecoder} implementation for those who don't need
* {@link ProtocolDecoder#finishDecode(IoSession, ProtocolDecoderOutput)} nor
* {@link ProtocolDecoder#dispose(IoSession)} method.
* 协议解码适配器主要是针对那些不需要#finishDecode和#dispose的解码器
* @author The Apache Directory Project ([email protected])
* @version $Rev$, $Date$
*/
public abstract class ProtocolDecoderAdapter implements ProtocolDecoder {
/**
* Override this method to deal with the closed connection.
* The default implementation does nothing.会话关闭
*/
public void finishDecode(IoSession session, ProtocolDecoderOutput out)
throws Exception {
}
/**
* Override this method to dispose all resources related with this decoder.
* The default implementation does nothing.释放资源
*/
public void dispose(IoSession session) throws Exception {
}
}
/**
* Callback for {@link ProtocolDecoder} to generate decoded messages.
* {@link ProtocolDecoder} must call {@link #write(Object)} for each decoded
* messages.
*
* @author The Apache Directory Project ([email protected])
* @version $Rev$, $Date$
*/
public interface ProtocolDecoderOutput {
/**
* Callback for {@link ProtocolDecoder} to generate decoded messages.
* {@link ProtocolDecoder} must call {@link #write(Object)} for each
* decoded messages.
* 用于解码器,解码消息后,回调write方法。
* @param message the decoded message
*/
void write(Object message);
/**
* Flushes all messages you wrote via {@link #write(Object)} to
* the next filter.刷新所有write方法写的消息
*/
void flush();
}
/**
* A {@link ProtocolDecoderOutput} based on queue.
*
* @author The Apache Directory Project ([email protected])
* @version $Rev$, $Date$
*
*/
public class SimpleProtocolDecoderOutput implements ProtocolDecoderOutput {
private final NextFilter nextFilter;//后继过滤器
private final IoSession session;//关联会话
private final Queue messageQueue = new Queue();//消息队列
//根据会话和后继过滤器构建简单协议解码输出
public SimpleProtocolDecoderOutput(IoSession session, NextFilter nextFilter) {
this.nextFilter = nextFilter;
this.session = session;
}
//写消息,直接将消息push到消息队列
public void write(Object message) {
messageQueue.push(message);
if (session instanceof BaseIoSession) {
((BaseIoSession) session).increaseReadMessages();
}
}
//将消息队列中的消息传给后继过滤器的messageReceived方法。
public void flush() {
while (!messageQueue.isEmpty()) {
nextFilter.messageReceived(session, messageQueue.pop());
}
}
}
private ProtocolDecoderOutput getDecoderOut(IoSession session,
NextFilter nextFilter) {
//从当前会话获取解码输出属性DECODER_OUT对一个的ProtocolDecoderOutput
ProtocolDecoderOutput out = (ProtocolDecoderOutput) session.getAttribute(DECODER_OUT);
if (out == null) {
//为null,则创建一个SimpleProtocolDecoderOutput,添加到会话中
out = new SimpleProtocolDecoderOutput(session, nextFilter);
session.setAttribute(DECODER_OUT, out);
}
return out;
}
private ProtocolEncoderOutputImpl getEncoderOut(IoSession session,
NextFilter nextFilter, WriteRequest writeRequest) {
//这个就是我们上一篇文章讲的
return new ProtocolEncoderOutputImpl(session, nextFilter, writeRequest);
}
ProtocolCodecFilter extends IoFilterAdapter
--ProtocolCodecFactory
--ProtocolEncoder
--ProtocolEncoderOutput(ProtocolEncoderOutputImpl)
--ProtocolDecoder
--ProtocolDecoderOutput(SimpleProtocolDecoderOutput)