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.service.IoHandlerAdapter;
24  import org.apache.mina.core.session.IoSession;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  import static testcase.MinaRegressionTest.MSG_COUNT;
28  import static testcase.MinaRegressionTest.OPEN;
29  
30  import java.io.IOException;
31  import java.util.concurrent.atomic.AtomicInteger;
32  
33  /**
34   * TODO: Document me !
35   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
36   *
37   */
38  public class MyIoHandler extends IoHandlerAdapter {
39    private static final Logger logger = LoggerFactory.getLogger(MyIoHandler.class);
40    public static AtomicInteger received = new AtomicInteger(0);
41    public static AtomicInteger closed = new AtomicInteger(0);
42    private final Object LOCK;
43  
44    public MyIoHandler(Object lock) {
45      LOCK = lock;
46    }
47  
48    @Override
49    public void exceptionCaught(IoSession session, Throwable cause) {
50      if (!(cause instanceof IOException)) {
51        logger.error("Exception: ", cause);
52      } else {
53        logger.info("I/O error: " + cause.getMessage());
54      }
55      session.close(true);
56    }
57  
58    @Override
59    public void sessionOpened(IoSession session) throws Exception {
60        logger.info( "Session " + session.getId() + " is opened" );
61      session.resumeRead();
62    }
63  
64    @Override
65    public void sessionCreated(IoSession session) throws Exception {
66        logger.info( "Creation of session " + session.getId() );
67      session.setAttribute(OPEN);
68      session.suspendRead();
69    }
70  
71    @Override
72    public void sessionClosed(IoSession session) throws Exception {
73      session.removeAttribute(OPEN);
74      logger.info("{}> Session closed", session.getId());
75      final int clsd = closed.incrementAndGet();
76      
77      if (clsd == MSG_COUNT) {
78        synchronized (LOCK) {
79          LOCK.notifyAll();
80        }
81      }
82      
83      int i = 0;
84      
85      try
86      {
87          int j = 2 / i;
88      } 
89      catch ( Exception e )
90      {
91          //e.printStackTrace();
92      }
93    }
94  
95    @Override
96    public void messageReceived(IoSession session, Object message) throws Exception {
97      IoBuffer msg = (IoBuffer) message;
98      logger.info("MESSAGE: " + msg.remaining() + " on session " + session.getId() );
99      final int rec = received.incrementAndGet();
100     
101     if (rec == MSG_COUNT) {
102       synchronized (LOCK) {
103         LOCK.notifyAll();
104       }
105     }
106     
107     session.close(true);
108   }
109 }