diff --git a/handshake/pom.xml b/handshake/pom.xml
index 842d2dc..57a6a9f 100644
--- a/handshake/pom.xml
+++ b/handshake/pom.xml
@@ -32,16 +32,6 @@
UTF-8
-
-
- repository.jboss.org
- http://repository.jboss.org/nexus/content/groups/public/
-
- false
-
-
-
-
junit
@@ -50,9 +40,9 @@
test
- org.jboss.netty
- netty
- 3.2.3.Final
+ io.netty
+ netty-all
+ 4.0.10.Final
compile
diff --git a/handshake/src/main/java/com/biasedbit/nettytutorials/handshake/client/Client.java b/handshake/src/main/java/com/biasedbit/nettytutorials/handshake/client/Client.java
index 5ec4a69..b871756 100644
--- a/handshake/src/main/java/com/biasedbit/nettytutorials/handshake/client/Client.java
+++ b/handshake/src/main/java/com/biasedbit/nettytutorials/handshake/client/Client.java
@@ -2,22 +2,18 @@
import com.biasedbit.nettytutorials.handshake.common.ByteCounter;
import com.biasedbit.nettytutorials.handshake.common.MessageCounter;
-import org.jboss.netty.bootstrap.ClientBootstrap;
-import org.jboss.netty.channel.Channel;
-import org.jboss.netty.channel.ChannelFactory;
-import org.jboss.netty.channel.ChannelFuture;
-import org.jboss.netty.channel.ChannelPipeline;
-import org.jboss.netty.channel.ChannelPipelineFactory;
-import org.jboss.netty.channel.Channels;
-import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
-import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder;
-import org.jboss.netty.handler.codec.frame.Delimiters;
-import org.jboss.netty.handler.codec.string.StringDecoder;
-import org.jboss.netty.handler.codec.string.StringEncoder;
+import io.netty.bootstrap.Bootstrap;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelInitializer;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.nio.NioSocketChannel;
+import io.netty.handler.codec.DelimiterBasedFrameDecoder;
+import io.netty.handler.codec.Delimiters;
+import io.netty.handler.codec.string.StringDecoder;
+import io.netty.handler.codec.string.StringEncoder;
import java.net.InetSocketAddress;
-import java.util.concurrent.Executor;
-import java.util.concurrent.Executors;
/**
* @author Bruno de Carvalho
@@ -29,7 +25,7 @@ public class Client {
private final String id;
private final String serverId;
private final ClientListener listener;
- private ClientBootstrap bootstrap;
+ private Bootstrap bootstrap;
private Channel connector;
// constructors -----------------------------------------------------------
@@ -44,26 +40,27 @@ public Client(String id, String serverId, ClientListener listener) {
public boolean start() {
// Standard netty bootstrapping stuff.
- Executor bossPool = Executors.newCachedThreadPool();
- Executor workerPool = Executors.newCachedThreadPool();
- ChannelFactory factory =
- new NioClientSocketChannelFactory(bossPool, workerPool);
- this.bootstrap = new ClientBootstrap(factory);
+ NioEventLoopGroup workerGroup = new NioEventLoopGroup();
+ this.bootstrap = new Bootstrap()
+ .channel(NioSocketChannel.class)
+ .group(workerGroup);
// Declared outside to fit under 80 char limit
final DelimiterBasedFrameDecoder frameDecoder =
new DelimiterBasedFrameDecoder(Integer.MAX_VALUE,
Delimiters.lineDelimiter());
- this.bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
- public ChannelPipeline getPipeline() throws Exception {
+ this.bootstrap.handler(new ChannelInitializer() {
+
+ @Override
+ protected void initChannel(Channel ch) throws Exception {
ByteCounter byteCounter =
new ByteCounter("--- CLIENT-COUNTER :: ");
MessageCounter messageCounter =
new MessageCounter("--- CLIENT-MSGCOUNTER :: ");
ClientHandshakeHandler handshakeHandler =
new ClientHandshakeHandler(id, serverId, 5000);
-
- return Channels.pipeline(byteCounter,
+
+ ch.pipeline().addLast(byteCounter,
frameDecoder,
new StringDecoder(),
new StringEncoder(),
@@ -78,29 +75,29 @@ public ChannelPipeline getPipeline() throws Exception {
if (!future.awaitUninterruptibly().isSuccess()) {
System.out.println("--- CLIENT - Failed to connect to server at " +
"localhost:12345.");
- this.bootstrap.releaseExternalResources();
+ workerGroup.shutdownGracefully();
return false;
}
- this.connector = future.getChannel();
- return this.connector.isConnected();
+ this.connector = future.channel();
+ return this.connector.isActive();
}
public void stop() {
if (this.connector != null) {
this.connector.close().awaitUninterruptibly();
}
- this.bootstrap.releaseExternalResources();
+ this.bootstrap.group().shutdownGracefully();
System.out.println("--- CLIENT - Stopped.");
}
public boolean sendMessage(String message) {
- if (this.connector.isConnected()) {
+ if (this.connector.isActive()) {
// Append \n if it's not present, because of the frame delimiter
if (!message.endsWith("\n")) {
- this.connector.write(message + '\n');
+ this.connector.writeAndFlush(message + '\n');
} else {
- this.connector.write(message);
+ this.connector.writeAndFlush(message);
}
return true;
}
diff --git a/handshake/src/main/java/com/biasedbit/nettytutorials/handshake/client/ClientHandler.java b/handshake/src/main/java/com/biasedbit/nettytutorials/handshake/client/ClientHandler.java
index a84426a..3e3ad2b 100644
--- a/handshake/src/main/java/com/biasedbit/nettytutorials/handshake/client/ClientHandler.java
+++ b/handshake/src/main/java/com/biasedbit/nettytutorials/handshake/client/ClientHandler.java
@@ -1,18 +1,15 @@
package com.biasedbit.nettytutorials.handshake.client;
import com.biasedbit.nettytutorials.handshake.common.HandshakeEvent;
-import org.jboss.netty.channel.ChannelEvent;
-import org.jboss.netty.channel.ChannelHandlerContext;
-import org.jboss.netty.channel.ChannelStateEvent;
-import org.jboss.netty.channel.MessageEvent;
-import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelInboundHandlerAdapter;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author Bruno de Carvalho
*/
-public class ClientHandler extends SimpleChannelUpstreamHandler {
+public class ClientHandler extends ChannelInboundHandlerAdapter {
// internal vars ----------------------------------------------------------
@@ -29,7 +26,7 @@ public ClientHandler(ClientListener listener) {
// SimpleChannelUpstreamHandler -------------------------------------------
@Override
- public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e)
+ public void userEventTriggered(ChannelHandlerContext ctx, Object e)
throws Exception {
if (e instanceof HandshakeEvent) {
if (((HandshakeEvent) e).isSuccessful()) {
@@ -41,22 +38,21 @@ public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e)
return;
}
- super.handleUpstream(ctx, e);
+ super.userEventTriggered(ctx, e);
}
@Override
- public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
+ public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
this.counter.incrementAndGet();
- this.listener.messageReceived(e.getMessage().toString());
+ this.listener.messageReceived(msg.toString());
}
@Override
- public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)
- throws Exception {
- super.channelClosed(ctx, e);
+ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
+ super.channelInactive(ctx);
out("--- CLIENT-HANDLER :: Channel closed, received " +
- this.counter.get() + " messages: " + e.getChannel());
+ this.counter.get() + " messages: " + ctx.channel());
}
// private static helpers -------------------------------------------------
diff --git a/handshake/src/main/java/com/biasedbit/nettytutorials/handshake/client/ClientHandshakeHandler.java b/handshake/src/main/java/com/biasedbit/nettytutorials/handshake/client/ClientHandshakeHandler.java
index 6cddb66..6d10bff 100644
--- a/handshake/src/main/java/com/biasedbit/nettytutorials/handshake/client/ClientHandshakeHandler.java
+++ b/handshake/src/main/java/com/biasedbit/nettytutorials/handshake/client/ClientHandshakeHandler.java
@@ -2,16 +2,12 @@
import com.biasedbit.nettytutorials.handshake.common.Challenge;
import com.biasedbit.nettytutorials.handshake.common.HandshakeEvent;
-import org.jboss.netty.channel.Channel;
-import org.jboss.netty.channel.ChannelFuture;
-import org.jboss.netty.channel.ChannelFutureListener;
-import org.jboss.netty.channel.ChannelHandlerContext;
-import org.jboss.netty.channel.ChannelStateEvent;
-import org.jboss.netty.channel.Channels;
-import org.jboss.netty.channel.DownstreamMessageEvent;
-import org.jboss.netty.channel.ExceptionEvent;
-import org.jboss.netty.channel.MessageEvent;
-import org.jboss.netty.channel.SimpleChannelHandler;
+import io.netty.channel.ChannelDuplexHandler;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelFutureListener;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelPromise;
+import java.nio.channels.Channels;
import java.util.ArrayDeque;
import java.util.Queue;
@@ -22,7 +18,7 @@
/**
* @author Bruno de Carvalho
*/
-public class ClientHandshakeHandler extends SimpleChannelHandler {
+public class ClientHandshakeHandler extends ChannelDuplexHandler {
// internal vars ----------------------------------------------------------
@@ -32,7 +28,7 @@ public class ClientHandshakeHandler extends SimpleChannelHandler {
private final AtomicBoolean handshakeComplete;
private final AtomicBoolean handshakeFailed;
private final CountDownLatch latch = new CountDownLatch(1);
- private final Queue messages = new ArrayDeque();
+ private final Queue