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 testcase;
21  
22  import org.apache.mina.core.buffer.IoBuffer;
23  import org.apache.mina.core.session.IoSession;
24  import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
25  import org.apache.mina.filter.codec.ProtocolDecoderOutput;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  import static testcase.MinaRegressionTest.MSG_SIZE;
29  import static testcase.MinaRegressionTest.OPEN;
30  
31  /**
32   * TODO: Document me !
33   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
34   */
35  public class MyRequestDecoder extends CumulativeProtocolDecoder {
36    private static final Logger logger = LoggerFactory.getLogger(MyRequestDecoder.class);
37  
38    @Override
39    protected boolean doDecode(final IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
40      if (!session.containsAttribute(OPEN)) {
41        logger.error("!decoding for closed session {}", session.getId());
42      }
43  
44      new Thread(new Runnable() {
45        public void run() {
46          try {
47              logger.debug( "Sleep for 500 ms for session {}", session.getId() );
48              Thread.sleep(500);
49              logger.debug( "Wake up now from a 500 ms sleep for session {}", session.getId() );
50          } catch (InterruptedException ignore) {}
51          session.close(true);
52        }
53      }).start();
54  
55      // sleep so that session.close(true) is already called when decoding continues
56      logger.debug( "Sleep for 1000 ms for session {}", session.getId() );
57      Thread.sleep(1000);
58      logger.debug( "Wake up now from a 1000 ms sleep for session {}", session.getId() );
59  
60      if (!session.containsAttribute(OPEN)) {
61        logger.error("!session {} closed before decoding completes!", session.getId());
62        int i = 0;
63        
64        try
65        {
66            int j = 2 / i;
67        } 
68        catch ( Exception e )
69        {
70            //e.printStackTrace();
71        }
72      }
73  
74      // no full message
75      if (in.remaining() < MSG_SIZE) return false;
76  
77      logger.info("Done decoding for session {}", session.getId());
78  
79      if (in.hasRemaining() && !session.isClosing() && session.isConnected()) {
80        IoBuffer tmp = IoBuffer.allocate(in.remaining());
81        tmp.put(in);
82        tmp.flip();
83        out.write(tmp);
84        return true;
85      }
86  
87      return false;
88    }
89  }