001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package testcase;
021
022import org.apache.mina.core.buffer.IoBuffer;
023import org.apache.mina.core.session.IoSession;
024import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
025import org.apache.mina.filter.codec.ProtocolDecoderOutput;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028import static testcase.MinaRegressionTest.MSG_SIZE;
029import static testcase.MinaRegressionTest.OPEN;
030
031/**
032 * TODO: Document me !
033 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
034 */
035public class MyRequestDecoder extends CumulativeProtocolDecoder {
036    private static final Logger logger = LoggerFactory.getLogger(MyRequestDecoder.class);
037
038    @Override
039    protected boolean doDecode(final IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
040        if (!session.containsAttribute(OPEN)) {
041            logger.error("!decoding for closed session {}", session.getId());
042        }
043
044        new Thread(new Runnable() {
045            public void run() {
046                try {
047                    logger.debug("Sleep for 500 ms for session {}", session.getId());
048                    Thread.sleep(500);
049                    logger.debug("Wake up now from a 500 ms sleep for session {}", session.getId());
050                } catch (InterruptedException ignore) {
051                }
052                session.closeNow();
053            }
054        }).start();
055
056        // sleep so that session.closeNow() is already called when decoding continues
057        logger.debug("Sleep for 1000 ms for session {}", session.getId());
058        Thread.sleep(1000);
059        logger.debug("Wake up now from a 1000 ms sleep for session {}", session.getId());
060
061        if (!session.containsAttribute(OPEN)) {
062            logger.error("!session {} closed before decoding completes!", session.getId());
063            int i = 0;
064
065            try {
066                int j = 2 / i;
067            } catch (Exception e) {
068                //e.printStackTrace();
069            }
070        }
071
072        // no full message
073        if (in.remaining() < MSG_SIZE)
074            return false;
075
076        logger.info("Done decoding for session {}", session.getId());
077
078        if (in.hasRemaining() && !session.isClosing() && session.isConnected()) {
079            IoBuffer tmp = IoBuffer.allocate(in.remaining());
080            tmp.put(in);
081            tmp.flip();
082            out.write(tmp);
083            return true;
084        }
085
086        return false;
087    }
088}