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 io.netty.example.securechat;
17  
18  import io.netty.channel.Channel;
19  import io.netty.channel.ChannelHandlerContext;
20  import io.netty.channel.SimpleChannelInboundHandler;
21  import io.netty.channel.group.ChannelGroup;
22  import io.netty.channel.group.DefaultChannelGroup;
23  import io.netty.handler.ssl.SslHandler;
24  import io.netty.util.concurrent.Future;
25  import io.netty.util.concurrent.GenericFutureListener;
26  import io.netty.util.concurrent.GlobalEventExecutor;
27  
28  import java.net.InetAddress;
29  
30  /**
31   * Handles a server-side channel.
32   */
33  public class SecureChatServerHandler extends SimpleChannelInboundHandler<String> {
34  
35      static final ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
36  
37      @Override
38      public void channelActive(final ChannelHandlerContext ctx) {
39          // Once session is secured, send a greeting and register the channel to the global channel
40          // list so the channel received the messages from others.
41          ctx.pipeline().get(SslHandler.class).handshakeFuture().addListener(
42                  new GenericFutureListener<Future<Channel>>() {
43                      @Override
44                      public void operationComplete(Future<Channel> future) throws Exception {
45                          ctx.writeAndFlush(
46                                  "Welcome to " + InetAddress.getLocalHost().getHostName() + " secure chat service!\n");
47                          ctx.writeAndFlush(
48                                  "Your session is protected by " +
49                                          ctx.pipeline().get(SslHandler.class).engine().getSession().getCipherSuite() +
50                                          " cipher suite.\n");
51  
52                          channels.add(ctx.channel());
53                      }
54          });
55      }
56  
57      @Override
58      public void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {
59          // Send the received message to all channels but the current one.
60          for (Channel c: channels) {
61              if (c != ctx.channel()) {
62                  c.writeAndFlush("[" + ctx.channel().remoteAddress() + "] " + msg + '\n');
63              } else {
64                  c.writeAndFlush("[you] " + msg + '\n');
65              }
66          }
67  
68          // Close the connection if the client has sent 'bye'.
69          if ("bye".equals(msg.toLowerCase())) {
70              ctx.close();
71          }
72      }
73  
74      @Override
75      public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
76          cause.printStackTrace();
77          ctx.close();
78      }
79  }