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.buffer.ChannelBuffers;
20 import org.jboss.netty.channel.Channel;
21 import org.jboss.netty.channel.ChannelEvent;
22 import org.jboss.netty.channel.ChannelHandlerContext;
23 import org.jboss.netty.channel.ChannelState;
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 import org.jboss.netty.channel.WriteCompletionEvent;
29
30
31
32
33 public class DiscardClientHandler extends SimpleChannelUpstreamHandler {
34
35 private long transferredBytes;
36 private final byte[] content;
37
38 public DiscardClientHandler() {
39 content = new byte[DiscardClient.SIZE];
40 }
41
42 public long getTransferredBytes() {
43 return transferredBytes;
44 }
45
46 @Override
47 public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
48 if (e instanceof ChannelStateEvent) {
49 if (((ChannelStateEvent) e).getState() != ChannelState.INTEREST_OPS) {
50 System.err.println(e);
51 }
52 }
53
54
55 super.handleUpstream(ctx, e);
56 }
57
58 @Override
59 public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
60
61 generateTraffic(e);
62 }
63
64 @Override
65 public void channelInterestChanged(ChannelHandlerContext ctx, ChannelStateEvent e) {
66
67 generateTraffic(e);
68 }
69
70 @Override
71 public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
72
73 }
74
75 @Override
76 public void writeComplete(ChannelHandlerContext ctx, WriteCompletionEvent e) {
77 transferredBytes += e.getWrittenAmount();
78 }
79
80 @Override
81 public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
82
83 e.getCause().printStackTrace();
84 e.getChannel().close();
85 }
86
87 private void generateTraffic(ChannelStateEvent e) {
88
89
90
91
92 Channel channel = e.getChannel();
93 while (channel.isWritable()) {
94 ChannelBuffer m = nextMessage();
95 if (m == null) {
96 break;
97 }
98 channel.write(m);
99 }
100 }
101
102 private ChannelBuffer nextMessage() {
103 return ChannelBuffers.wrappedBuffer(content);
104 }
105 }