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.session.IoSession;
24 import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
25 import org.apache.mina.filter.codec.ProtocolDecoderOutput;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import static testcase.MinaRegressionTest.MSG_SIZE;
29 import static testcase.MinaRegressionTest.OPEN;
30
31
32
33
34
35 public class MyRequestDecoder extends CumulativeProtocolDecoder {
36 private static final Logger logger = LoggerFactory.getLogger(MyRequestDecoder.class);
37
38 @Override
39 protected boolean doDecode(final IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
40 if (!session.containsAttribute(OPEN)) {
41 logger.error("!decoding for closed session {}", session.getId());
42 }
43
44 new Thread(new Runnable() {
45 public void run() {
46 try {
47 logger.debug("Sleep for 500 ms for session {}", session.getId());
48 Thread.sleep(500);
49 logger.debug("Wake up now from a 500 ms sleep for session {}", session.getId());
50 } catch (InterruptedException ignore) {
51 }
52 session.closeNow();
53 }
54 }).start();
55
56
57 logger.debug("Sleep for 1000 ms for session {}", session.getId());
58 Thread.sleep(1000);
59 logger.debug("Wake up now from a 1000 ms sleep for session {}", session.getId());
60
61 if (!session.containsAttribute(OPEN)) {
62 logger.error("!session {} closed before decoding completes!", session.getId());
63 int i = 0;
64
65 try {
66 int j = 2 / i;
67 } catch (Exception e) {
68
69 }
70 }
71
72
73 if (in.remaining() < MSG_SIZE)
74 return false;
75
76 logger.info("Done decoding for session {}", session.getId());
77
78 if (in.hasRemaining() && !session.isClosing() && session.isConnected()) {
79 IoBuffer tmp = IoBuffer.allocate(in.remaining());
80 tmp.put(in);
81 tmp.flip();
82 out.write(tmp);
83 return true;
84 }
85
86 return false;
87 }
88 }