1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.mina.examples.sumup.codec;
20
21 import org.apache.mina.common.ByteBuffer;
22 import org.apache.mina.examples.sumup.message.AbstractMessage;
23 import org.apache.mina.protocol.ProtocolDecoderOutput;
24 import org.apache.mina.protocol.ProtocolSession;
25 import org.apache.mina.protocol.ProtocolViolationException;
26 import org.apache.mina.protocol.codec.MessageDecoder;
27 import org.apache.mina.protocol.codec.MessageDecoderResult;
28
29 /***
30 * A {@link MessageDecoder} that decodes message header and forwards
31 * the decoding of body to a subclass.
32 *
33 * @author The Apache Directory Project
34 * @version $Rev: 264677 $, $Date: 2005-08-30 11:44:35 +0900 $
35 */
36 public abstract class AbstractMessageDecoder implements MessageDecoder
37 {
38 private final int type;
39
40 private int sequence;
41 private boolean readHeader;
42
43 protected AbstractMessageDecoder( int type )
44 {
45 this.type = type;
46 }
47
48 public MessageDecoderResult decodable( ProtocolSession session, ByteBuffer in )
49 {
50
51 if( in.remaining() < Constants.HEADER_LEN )
52 {
53 return MessageDecoderResult.NEED_DATA;
54 }
55
56
57 if( type == in.getShort() )
58 {
59 return MessageDecoderResult.OK;
60 }
61
62
63 return MessageDecoderResult.NOT_OK;
64 }
65
66 public MessageDecoderResult decode( ProtocolSession session, ByteBuffer in, ProtocolDecoderOutput out ) throws ProtocolViolationException
67 {
68
69 if( !readHeader )
70 {
71 in.getShort();
72 sequence = in.getInt();
73 readHeader = true;
74 }
75
76
77 AbstractMessage m = decodeBody( session, in );
78
79 if( m == null )
80 {
81 return MessageDecoderResult.NEED_DATA;
82 }
83 else
84 {
85 readHeader = false;
86 }
87 m.setSequence( sequence );
88 out.write( m );
89
90 return MessageDecoderResult.OK;
91 }
92
93 /***
94 * @return <tt>null</tt> if the whole body is not read yet
95 */
96 protected abstract AbstractMessage decodeBody( ProtocolSession session, ByteBuffer in );
97 }