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.netty5.example.securechat;
17  
18  import io.netty5.channel.Channel;
19  import io.netty5.channel.ChannelHandlerContext;
20  import io.netty5.channel.SimpleChannelInboundHandler;
21  import io.netty5.channel.group.ChannelGroup;
22  import io.netty5.channel.group.DefaultChannelGroup;
23  import io.netty5.handler.ssl.SslHandshakeCompletionEvent;
24  import io.netty5.util.concurrent.GlobalEventExecutor;
25  
26  import java.net.InetAddress;
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 channelInboundEvent(ChannelHandlerContext ctx, Object evt) throws Exception {
37          if (evt instanceof SslHandshakeCompletionEvent) {
38              SslHandshakeCompletionEvent completionEvent = (SslHandshakeCompletionEvent) evt;
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              if (completionEvent.isSuccess()) {
42                  ctx.writeAndFlush(
43                          "Welcome to " + InetAddress.getLocalHost().getHostName() + " secure chat service!\n");
44                  ctx.writeAndFlush(
45                          "Your session is protected by " +
46                                  completionEvent.session().getCipherSuite() +
47                                  " cipher suite.\n");
48                  channels.add(ctx.channel());
49              } else {
50                  ctx.close();
51              }
52          }
53          super.channelInboundEvent(ctx, evt);
54      }
55  
56      @Override
57      public void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {
58          // Send the received message to all channels but the current one.
59          for (Channel c: channels) {
60              if (c != ctx.channel()) {
61                  c.writeAndFlush("[" + ctx.channel().remoteAddress() + "] " + msg + '\n');
62              } else {
63                  c.writeAndFlush("[you] " + msg + '\n');
64              }
65          }
66  
67          // Close the connection if the client has sent 'bye'.
68          if ("bye".equals(msg.toLowerCase())) {
69              ctx.close();
70          }
71      }
72  
73      @Override
74      public void channelExceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
75          cause.printStackTrace();
76          ctx.close();
77      }
78  }