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 java.util.logging.Level;
19 import java.util.logging.Logger;
20
21 import org.jboss.netty.buffer.ChannelBuffer;
22 import org.jboss.netty.channel.ChannelEvent;
23 import org.jboss.netty.channel.ChannelHandlerContext;
24 import org.jboss.netty.channel.ChannelStateEvent;
25 import org.jboss.netty.channel.ExceptionEvent;
26 import org.jboss.netty.channel.MessageEvent;
27 import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
28
29
30
31
32 public class DiscardServerHandler extends SimpleChannelUpstreamHandler {
33
34 private static final Logger logger = Logger.getLogger(
35 DiscardServerHandler.class.getName());
36
37 private long transferredBytes;
38
39 public long getTransferredBytes() {
40 return transferredBytes;
41 }
42
43 @Override
44 public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
45 if (e instanceof ChannelStateEvent) {
46 logger.info(e.toString());
47 }
48
49
50 super.handleUpstream(ctx, e);
51 }
52
53 @Override
54 public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
55
56 transferredBytes += ((ChannelBuffer) e.getMessage()).readableBytes();
57 }
58
59 @Override
60 public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
61
62 logger.log(
63 Level.WARNING,
64 "Unexpected exception from downstream.",
65 e.getCause());
66 e.getChannel().close();
67 }
68 }