View Javadoc

    1   /*
    2    * Copyright 2012 The Netty Project
    3    *
    4    * The Netty Project licenses this file to you under the Apache License,
    5    * version 2.0 (the "License"); you may not use this file except in compliance
    6    * with the 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  public class TelnetServerHandler extends SimpleChannelUpstreamHandler {
    36
    37      private static final Logger logger = Logger.getLogger(
    38              TelnetServerHandler.class.getName());
    39
    40      @Override
    41      public void handleUpstream(
    42              ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
    43          if (e instanceof ChannelStateEvent) {
    44              logger.info(e.toString());
    45          }
    46          super.handleUpstream(ctx, e);
    47      }
    48
    49      @Override
    50      public void channelConnected(
    51              ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
    52          // Send greeting for a new connection.
    53          e.getChannel().write(
    54                  "Welcome to " + InetAddress.getLocalHost().getHostName() + "!\r\n");
    55          e.getChannel().write("It is " + new Date() + " now.\r\n");
    56      }
    57
    58      @Override
    59      public void messageReceived(
    60              ChannelHandlerContext ctx, MessageEvent e) {
    61
    62          // Cast to a String first.
    63          // We know it is a String because we put some codec in TelnetPipelineFactory.
    64          String request = (String) e.getMessage();
    65
    66          // Generate and write a response.
    67          String response;
    68          boolean close = false;
    69          if (request.length() == 0) {
    70              response = "Please type something.\r\n";
    71          } else if (request.toLowerCase().equals("bye")) {
    72              response = "Have a good day!\r\n";
    73              close = true;
    74          } else {
    75              response = "Did you say '" + request + "'?\r\n";
    76          }
    77
    78          // We do not need to write a ChannelBuffer here.
    79          // We know the encoder inserted at TelnetPipelineFactory will do the conversion.
    80          ChannelFuture future = e.getChannel().write(response);
    81
    82          // Close the connection after sending 'Have a good day!'
    83          // if the client has sent 'bye'.
    84          if (close) {
    85              future.addListener(ChannelFutureListener.CLOSE);
    86          }
    87      }
    88
    89      @Override
    90      public void exceptionCaught(
    91              ChannelHandlerContext ctx, ExceptionEvent e) {
    92          logger.log(
    93                  Level.WARNING,
    94                  "Unexpected exception from downstream.",
    95                  e.getCause());
    96          e.getChannel().close();
    97      }
    98  }