1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.example.telnet;
17
18 import java.util.logging.Level;
19 import java.util.logging.Logger;
20
21 import org.jboss.netty.channel.ChannelEvent;
22 import org.jboss.netty.channel.ChannelHandlerContext;
23 import org.jboss.netty.channel.ChannelStateEvent;
24 import org.jboss.netty.channel.ExceptionEvent;
25 import org.jboss.netty.channel.MessageEvent;
26 import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
27
28
29
30
31
32
33
34
35
36 public class TelnetClientHandler extends SimpleChannelUpstreamHandler {
37
38 private static final Logger logger = Logger.getLogger(
39 TelnetClientHandler.class.getName());
40
41 @Override
42 public void handleUpstream(
43 ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
44 if (e instanceof ChannelStateEvent) {
45 logger.info(e.toString());
46 }
47 super.handleUpstream(ctx, e);
48 }
49
50 @Override
51 public void messageReceived(
52 ChannelHandlerContext ctx, MessageEvent e) {
53
54 System.err.println(e.getMessage());
55 }
56
57 @Override
58 public void exceptionCaught(
59 ChannelHandlerContext ctx, ExceptionEvent e) {
60 logger.log(
61 Level.WARNING,
62 "Unexpected exception from downstream.",
63 e.getCause());
64 e.getChannel().close();
65 }
66 }