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