1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
35
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
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 }