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.buffer.ChannelBuffers;
23 import org.jboss.netty.channel.Channel;
24 import org.jboss.netty.channel.ChannelEvent;
25 import org.jboss.netty.channel.ChannelHandlerContext;
26 import org.jboss.netty.channel.ChannelState;
27 import org.jboss.netty.channel.ChannelStateEvent;
28 import org.jboss.netty.channel.ExceptionEvent;
29 import org.jboss.netty.channel.MessageEvent;
30 import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
31 import org.jboss.netty.channel.WriteCompletionEvent;
32
33
34
35
36 public class DiscardClientHandler extends SimpleChannelUpstreamHandler {
37
38 private static final Logger logger = Logger.getLogger(
39 DiscardClientHandler.class.getName());
40
41 private long transferredBytes;
42 private final byte[] content;
43
44 public DiscardClientHandler(int messageSize) {
45 if (messageSize <= 0) {
46 throw new IllegalArgumentException(
47 "messageSize: " + messageSize);
48 }
49 content = new byte[messageSize];
50 }
51
52 public long getTransferredBytes() {
53 return transferredBytes;
54 }
55
56 @Override
57 public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
58 if (e instanceof ChannelStateEvent) {
59 if (((ChannelStateEvent) e).getState() != ChannelState.INTEREST_OPS) {
60 logger.info(e.toString());
61 }
62 }
63
64
65 super.handleUpstream(ctx, e);
66 }
67
68 @Override
69 public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
70
71 generateTraffic(e);
72 }
73
74 @Override
75 public void channelInterestChanged(ChannelHandlerContext ctx, ChannelStateEvent e) {
76
77 generateTraffic(e);
78 }
79
80 @Override
81 public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
82
83 }
84
85 @Override
86 public void writeComplete(ChannelHandlerContext ctx, WriteCompletionEvent e) {
87 transferredBytes += e.getWrittenAmount();
88 }
89
90 @Override
91 public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
92
93 logger.log(
94 Level.WARNING,
95 "Unexpected exception from downstream.",
96 e.getCause());
97 e.getChannel().close();
98 }
99
100 private void generateTraffic(ChannelStateEvent e) {
101
102
103
104
105 Channel channel = e.getChannel();
106 while (channel.isWritable()) {
107 ChannelBuffer m = nextMessage();
108 if (m == null) {
109 break;
110 }
111 channel.write(m);
112 }
113 }
114
115 private ChannelBuffer nextMessage() {
116 return ChannelBuffers.wrappedBuffer(content);
117 }
118 }