From 8bab0aae9e443c6abce66ccb635ecae7d50f4fcb Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Tue, 16 Oct 2012 15:00:32 -0700 Subject: [PATCH] Fix a bug in the SecureChat example where greeting is generated too soon --- .../securechat/SecureChatServerHandler.java | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/example/src/main/java/io/netty/example/securechat/SecureChatServerHandler.java b/example/src/main/java/io/netty/example/securechat/SecureChatServerHandler.java index a4a2b36357..5a0994c166 100644 --- a/example/src/main/java/io/netty/example/securechat/SecureChatServerHandler.java +++ b/example/src/main/java/io/netty/example/securechat/SecureChatServerHandler.java @@ -16,6 +16,8 @@ package io.netty.example.securechat; import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundMessageHandlerAdapter; import io.netty.channel.group.ChannelGroup; @@ -37,19 +39,23 @@ public class SecureChatServerHandler extends ChannelInboundMessageHandlerAdapter static final ChannelGroup channels = new DefaultChannelGroup(); @Override - public void channelActive(ChannelHandlerContext ctx) throws Exception { - // Once session is secured, send a greeting. - ctx.write( - "Welcome to " + InetAddress.getLocalHost().getHostName() + - " secure chat service!\n"); - ctx.write( - "Your session is protected by " + - ctx.pipeline().get(SslHandler.class).getEngine().getSession().getCipherSuite() + - " cipher suite.\n"); + public void channelActive(final ChannelHandlerContext ctx) throws Exception { + // Once session is secured, send a greeting and register the channel to the global channel + // list so the channel received the messages from others. + ctx.pipeline().get(SslHandler.class).handshake().addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) throws Exception { + ctx.write( + "Welcome to " + InetAddress.getLocalHost().getHostName() + + " secure chat service!\n"); + ctx.write( + "Your session is protected by " + + ctx.pipeline().get(SslHandler.class).getEngine().getSession().getCipherSuite() + + " cipher suite.\n"); - // Register the channel to the global channel list - // so the channel received the messages from others. - channels.add(ctx.channel()); + channels.add(ctx.channel()); + } + }); } @Override