View Javadoc

1   /*
2    * Copyright 2009 Red Hat, Inc.
3    *
4    * Red Hat licenses this file to you under the Apache License, version 2.0
5    * (the "License"); you may not use this file except in compliance with the
6    * License.  You may obtain a copy of the License at:
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.example.telnet;
17  
18  import java.net.InetAddress;
19  import java.util.Date;
20  import java.util.logging.Level;
21  import java.util.logging.Logger;
22  
23  import org.jboss.netty.channel.ChannelEvent;
24  import org.jboss.netty.channel.ChannelFuture;
25  import org.jboss.netty.channel.ChannelFutureListener;
26  import org.jboss.netty.channel.ChannelHandlerContext;
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  
32  /**
33   * Handles a server-side channel.
34   *
35   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
36   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
37   *
38   * @version $Rev: 2121 $, $Date: 2010-02-02 09:38:07 +0900 (Tue, 02 Feb 2010) $
39   */
40  public class TelnetServerHandler extends SimpleChannelUpstreamHandler {
41  
42      private static final Logger logger = Logger.getLogger(
43              TelnetServerHandler.class.getName());
44  
45      @Override
46      public void handleUpstream(
47              ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
48          if (e instanceof ChannelStateEvent) {
49              logger.info(e.toString());
50          }
51          super.handleUpstream(ctx, e);
52      }
53  
54      @Override
55      public void channelConnected(
56              ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
57          // Send greeting for a new connection.
58          e.getChannel().write(
59                  "Welcome to " + InetAddress.getLocalHost().getHostName() + "!\r\n");
60          e.getChannel().write("It is " + new Date() + " now.\r\n");
61      }
62  
63      @Override
64      public void messageReceived(
65              ChannelHandlerContext ctx, MessageEvent e) {
66  
67          // Cast to a String first.
68          // We know it is a String because we put some codec in TelnetPipelineFactory.
69          String request = (String) e.getMessage();
70  
71          // Generate and write a response.
72          String response;
73          boolean close = false;
74          if (request.length() == 0) {
75              response = "Please type something.\r\n";
76          } else if (request.toLowerCase().equals("bye")) {
77              response = "Have a good day!\r\n";
78              close = true;
79          } else {
80              response = "Did you say '" + request + "'?\r\n";
81          }
82  
83          // We do not need to write a ChannelBuffer here.
84          // We know the encoder inserted at TelnetPipelineFactory will do the conversion.
85          ChannelFuture future = e.getChannel().write(response);
86  
87          // Close the connection after sending 'Have a good day!'
88          // if the client has sent 'bye'.
89          if (close) {
90              future.addListener(ChannelFutureListener.CLOSE);
91          }
92      }
93  
94      @Override
95      public void exceptionCaught(
96              ChannelHandlerContext ctx, ExceptionEvent e) {
97          logger.log(
98                  Level.WARNING,
99                  "Unexpected exception from downstream.",
100                 e.getCause());
101         e.getChannel().close();
102     }
103 }