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 org.jboss.netty.channel.ChannelEvent;
19  import org.jboss.netty.channel.ChannelFuture;
20  import org.jboss.netty.channel.ChannelFutureListener;
21  import org.jboss.netty.channel.ChannelHandlerContext;
22  import org.jboss.netty.channel.ChannelStateEvent;
23  import org.jboss.netty.channel.ExceptionEvent;
24  import org.jboss.netty.channel.MessageEvent;
25  import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
26  
27  import java.net.InetAddress;
28  import java.util.Date;
29  
30  /**
31   * Handles a server-side channel.
32   */
33  public class TelnetServerHandler extends SimpleChannelUpstreamHandler {
34  
35      @Override
36      public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
37          if (e instanceof ChannelStateEvent) {
38              System.err.println(e);
39          }
40          super.handleUpstream(ctx, e);
41      }
42  
43      @Override
44      public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
45          // Send greeting for a new connection.
46          e.getChannel().write("Welcome to " + InetAddress.getLocalHost().getHostName() + "!\r\n");
47          e.getChannel().write("It is " + new Date() + " now.\r\n");
48      }
49  
50      @Override
51      public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
52  
53          // Cast to a String first.
54          // We know it is a String because we put some codec in TelnetPipelineFactory.
55          String request = (String) e.getMessage();
56  
57          // Generate and write a response.
58          String response;
59          boolean close = false;
60          if (request.length() == 0) {
61              response = "Please type something.\r\n";
62          } else if ("bye".equals(request.toLowerCase())) {
63              response = "Have a good day!\r\n";
64              close = true;
65          } else {
66              response = "Did you say '" + request + "'?\r\n";
67          }
68  
69          // We do not need to write a ChannelBuffer here.
70          // We know the encoder inserted at TelnetPipelineFactory will do the conversion.
71          ChannelFuture future = e.getChannel().write(response);
72  
73          // Close the connection after sending 'Have a good day!'
74          // if the client has sent 'bye'.
75          if (close) {
76              future.addListener(ChannelFutureListener.CLOSE);
77          }
78      }
79  
80      @Override
81      public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
82          e.getCause().printStackTrace();
83          e.getChannel().close();
84      }
85  }