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.service.IoHandlerAdapter;
024import org.apache.mina.core.session.IoSession;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027import static testcase.MinaRegressionTest.MSG_COUNT;
028import static testcase.MinaRegressionTest.OPEN;
029
030import java.io.IOException;
031import java.util.concurrent.atomic.AtomicInteger;
032
033/**
034 * TODO: Document me !
035 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
036 *
037 */
038public class MyIoHandler extends IoHandlerAdapter {
039    private static final Logger logger = LoggerFactory.getLogger(MyIoHandler.class);
040
041    public static AtomicInteger received = new AtomicInteger(0);
042
043    public static AtomicInteger closed = new AtomicInteger(0);
044
045    private final Object LOCK;
046
047    public MyIoHandler(Object lock) {
048        LOCK = lock;
049    }
050
051    @Override
052    public void exceptionCaught(IoSession session, Throwable cause) {
053        if (!(cause instanceof IOException)) {
054            logger.error("Exception: ", cause);
055        } else {
056            logger.info("I/O error: " + cause.getMessage());
057        }
058        session.closeNow();
059    }
060
061    @Override
062    public void sessionOpened(IoSession session) throws Exception {
063        logger.info("Session " + session.getId() + " is opened");
064        session.resumeRead();
065    }
066
067    @Override
068    public void sessionCreated(IoSession session) throws Exception {
069        logger.info("Creation of session " + session.getId());
070        session.setAttribute(OPEN);
071        session.suspendRead();
072    }
073
074    @Override
075    public void sessionClosed(IoSession session) throws Exception {
076        session.removeAttribute(OPEN);
077        logger.info("{}> Session closed", session.getId());
078        final int clsd = closed.incrementAndGet();
079
080        if (clsd == MSG_COUNT) {
081            synchronized (LOCK) {
082                LOCK.notifyAll();
083            }
084        }
085
086        int i = 0;
087
088        try {
089            int j = 2 / i;
090        } catch (Exception e) {
091            //e.printStackTrace();
092        }
093    }
094
095    @Override
096    public void messageReceived(IoSession session, Object message) throws Exception {
097        IoBuffer msg = (IoBuffer) message;
098        logger.info("MESSAGE: " + msg.remaining() + " on session " + session.getId());
099        final int rec = received.incrementAndGet();
100
101        if (rec == MSG_COUNT) {
102            synchronized (LOCK) {
103                LOCK.notifyAll();
104            }
105        }
106
107        session.closeNow();
108    }
109}