View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.filter.codec.statemachine;
21  
22  import java.util.Queue;
23  import java.util.concurrent.ConcurrentLinkedQueue;
24  
25  import org.apache.mina.core.buffer.IoBuffer;
26  import org.apache.mina.core.session.IoSession;
27  import org.apache.mina.filter.codec.ProtocolDecoder;
28  import org.apache.mina.filter.codec.ProtocolDecoderOutput;
29  
30  /**
31   * {@link ProtocolDecoder} which uses a {@link DecodingState} to decode data.
32   * Use a {@link DecodingStateMachine} as {@link DecodingState} to create
33   * a state machine which can decode your protocol.
34   * <p>
35   * NOTE: This is a stateful decoder. You should create one instance per session.
36   * </p>
37   * 
38   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
39   */
40  public class DecodingStateProtocolDecoder implements ProtocolDecoder {
41      private final DecodingState state;
42  
43      private final Queue<IoBuffer> undecodedBuffers = new ConcurrentLinkedQueue<>();
44  
45      private IoSession session;
46  
47      /**
48       * Creates a new instance using the specified {@link DecodingState} 
49       * instance.
50       * 
51       * @param state the {@link DecodingState}.
52       * @throws IllegalArgumentException if the specified state is <code>null</code>.
53       */
54      public DecodingStateProtocolDecoder(DecodingState state) {
55          if (state == null) {
56              throw new IllegalArgumentException("state");
57          }
58          this.state = state;
59      }
60  
61      /**
62       * {@inheritDoc}
63       */
64      @Override
65      public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
66          if (this.session == null) {
67              this.session = session;
68          } else if (this.session != session) {
69              throw new IllegalStateException(getClass().getSimpleName() + " is a stateful decoder.  "
70                      + "You have to create one per session.");
71          }
72  
73          undecodedBuffers.offer(in);
74          
75          for (;;) {
76              IoBuffer b = undecodedBuffers.peek();
77              if (b == null) {
78                  break;
79              }
80  
81              int oldRemaining = b.remaining();
82              state.decode(b, out);
83              int newRemaining = b.remaining();
84              
85              if (newRemaining != 0) {
86                  if (oldRemaining == newRemaining) {
87                      throw new IllegalStateException(DecodingState.class.getSimpleName() + " must "
88                              + "consume at least one byte per decode().");
89                  }
90              } else {
91                  undecodedBuffers.poll();
92              }
93          }
94      }
95  
96      /**
97       * {@inheritDoc}
98       */
99      @Override
100     public void finishDecode(IoSession session, ProtocolDecoderOutput out) throws Exception {
101         state.finishDecode(out);
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public void dispose(IoSession session) throws Exception {
109         // Do nothing
110     }
111 }