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                  }
52                  session.close(true);
53              }
54          }).start();
55  
56          // sleep so that session.close(true) is already called when decoding continues
57          logger.debug("Sleep for 1000 ms for session {}", session.getId());
58          Thread.sleep(1000);
59          logger.debug("Wake up now from a 1000 ms sleep for session {}", session.getId());
60  
61          if (!session.containsAttribute(OPEN)) {
62              logger.error("!session {} closed before decoding completes!", session.getId());
63              int i = 0;
64  
65              try {
66                  int j = 2 / i;
67              } catch (Exception e) {
68                  //e.printStackTrace();
69              }
70          }
71  
72          // no full message
73          if (in.remaining() < MSG_SIZE)
74              return false;
75  
76          logger.info("Done decoding for session {}", session.getId());
77  
78          if (in.hasRemaining() && !session.isClosing() && session.isConnected()) {
79              IoBuffer tmp = IoBuffer.allocate(in.remaining());
80              tmp.put(in);
81              tmp.flip();
82              out.write(tmp);
83              return true;
84          }
85  
86          return false;
87      }
88  }