1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.example.discard;
17
18 import org.jboss.netty.buffer.ChannelBuffer;
19 import org.jboss.netty.channel.ChannelEvent;
20 import org.jboss.netty.channel.ChannelHandlerContext;
21 import org.jboss.netty.channel.ChannelStateEvent;
22 import org.jboss.netty.channel.ExceptionEvent;
23 import org.jboss.netty.channel.MessageEvent;
24 import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
25
26
27
28
29 public class DiscardServerHandler extends SimpleChannelUpstreamHandler {
30
31 private long transferredBytes;
32
33 public long getTransferredBytes() {
34 return transferredBytes;
35 }
36
37 @Override
38 public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
39 if (e instanceof ChannelStateEvent) {
40 System.err.println(e);
41 }
42
43
44 super.handleUpstream(ctx, e);
45 }
46
47 @Override
48 public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
49
50 transferredBytes += ((ChannelBuffer) e.getMessage()).readableBytes();
51 }
52
53 @Override
54 public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
55
56 e.getCause().printStackTrace();
57 e.getChannel().close();
58 }
59 }