怎么实现CumulativeProtocolDecoder类中的doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out)方法

解决方案 »

  1.   

    package www.leader.UITD.codec;import org.apache.mina.core.buffer.IoBuffer;
    import org.apache.mina.core.session.IoSession;
    import org.apache.mina.filter.codec.ProtocolDecoderOutput;
    import org.apache.mina.filter.codec.demux.MessageDecoder;
    import org.apache.mina.filter.codec.demux.MessageDecoderResult;public abstract class AbstractMessageDecoder implements MessageDecoder { private final byte type;
        private short DataLen;
        private int pack_len;
    public AbstractMessageDecoder(byte type) {
    this.type = type;
    } public MessageDecoderResult decodable(IoSession session, IoBuffer in) {
    pack_len = in.remaining();
    //System.out.println("抽象解码器:"+ in.remaining());
    if (pack_len < UITDConstants.HEADER_LEN ){
    return MessageDecoderResult.NEED_DATA;
    }else{
    if (in.getShort(0) != UITDConstants.START_CHAR){
    System.out.println("起始符错误!");
    return MessageDecoderResult.NOT_OK;
    // }else{
    // System.out.println("起始符匹配。");
    }
    DataLen = Short.reverseBytes(in.getShort(20));// 应用数据单元的长度
    if (DataLen > 1024){
    System.out.println("数据包超长!");
    return MessageDecoderResult.NOT_OK;
    }
    if (pack_len < UITDConstants.HEADER_LEN + DataLen +  UITDConstants.END_LEN){
    return MessageDecoderResult.NEED_DATA;
    }
    if (in.getShort(UITDConstants.HEADER_LEN + DataLen) != UITDConstants.END_CHAR){
    System.out.println("结束符错误!");
    return MessageDecoderResult.NOT_OK;
    }else{
    System.out.println("结束符匹配。");
    int i = UITDConstants.HEADER_LEN + DataLen + UITDConstants.END_LEN;
    System.out.println("date len = " + i);
    in.position(i);
    in.flip();
    System.out.println(in.getHexDump());
    }
    }
    if (type == in.get(22)){ // 消息类型匹配
    return MessageDecoderResult.OK;
    }
    return MessageDecoderResult.NOT_OK;

    }
     
    public short getDataLen() {
    return DataLen;
    } public void setDataLen(short dataLen) {
    DataLen = dataLen;
    } @Override
    public void finishDecode(IoSession session, ProtocolDecoderOutput out)
    throws Exception { }
    }
    package www.leader.UITD.codec;import org.apache.log4j.Logger;
    import org.apache.mina.core.buffer.IoBuffer;
    import org.apache.mina.core.session.IoSession;
    import org.apache.mina.filter.codec.ProtocolDecoderOutput;
    import org.apache.mina.filter.codec.demux.MessageDecoderResult;import www.leader.UITD.message.FireMsg;public class FireMsgDecoder extends AbstractMessageDecoder {
    @Override
    public MessageDecoderResult decode(IoSession session, IoBuffer in,
    ProtocolDecoderOutput out) throws Exception {
    System.out.println("解码器:收到火警消息");
    FireMsg m = new FireMsg();
    m.setData(in);
    out.write(m);
    in.position(in.limit());
            return MessageDecoderResult.OK;
    } static Logger log = Logger.getLogger(FireMsgDecoder.class);
    public FireMsgDecoder() {
    super(UITDConstants.FIRE_TYPE);
    }}