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
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.close(true);
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
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 }