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  
41      public static AtomicInteger received = new AtomicInteger(0);
42  
43      public static AtomicInteger closed = new AtomicInteger(0);
44  
45      private final Object LOCK;
46  
47      public MyIoHandler(Object lock) {
48          LOCK = lock;
49      }
50  
51      @Override
52      public void exceptionCaught(IoSession session, Throwable cause) {
53          if (!(cause instanceof IOException)) {
54              logger.error("Exception: ", cause);
55          } else {
56              logger.info("I/O error: " + cause.getMessage());
57          }
58          session.closeNow();
59      }
60  
61      @Override
62      public void sessionOpened(IoSession session) throws Exception {
63          logger.info("Session " + session.getId() + " is opened");
64          session.resumeRead();
65      }
66  
67      @Override
68      public void sessionCreated(IoSession session) throws Exception {
69          logger.info("Creation of session " + session.getId());
70          session.setAttribute(OPEN);
71          session.suspendRead();
72      }
73  
74      @Override
75      public void sessionClosed(IoSession session) throws Exception {
76          session.removeAttribute(OPEN);
77          logger.info("{}> Session closed", session.getId());
78          final int clsd = closed.incrementAndGet();
79  
80          if (clsd == MSG_COUNT) {
81              synchronized (LOCK) {
82                  LOCK.notifyAll();
83              }
84          }
85  
86          int i = 0;
87  
88          try {
89              int j = 2 / i;
90          } catch (Exception e) {
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.closeNow();
108     }
109 }