diff --git a/example/src/main/java/io/netty/example/udt/echo/rendevous/Config.java b/example/src/main/java/io/netty/example/udt/echo/rendezvous/Config.java similarity index 95% rename from example/src/main/java/io/netty/example/udt/echo/rendevous/Config.java rename to example/src/main/java/io/netty/example/udt/echo/rendezvous/Config.java index 4e48674cab..3343796954 100644 --- a/example/src/main/java/io/netty/example/udt/echo/rendevous/Config.java +++ b/example/src/main/java/io/netty/example/udt/echo/rendezvous/Config.java @@ -13,7 +13,7 @@ * License for the specific language governing permissions and limitations * under the License. */ -package io.netty.example.udt.echo.rendevous; +package io.netty.example.udt.echo.rendezvous; /** * Peer to Peer Config diff --git a/example/src/main/java/io/netty/example/udt/echo/rendevous/MsgEchoPeerBase.java b/example/src/main/java/io/netty/example/udt/echo/rendezvous/MsgEchoPeerBase.java similarity index 98% rename from example/src/main/java/io/netty/example/udt/echo/rendevous/MsgEchoPeerBase.java rename to example/src/main/java/io/netty/example/udt/echo/rendezvous/MsgEchoPeerBase.java index 604775007f..8121e0ace8 100644 --- a/example/src/main/java/io/netty/example/udt/echo/rendevous/MsgEchoPeerBase.java +++ b/example/src/main/java/io/netty/example/udt/echo/rendezvous/MsgEchoPeerBase.java @@ -13,7 +13,7 @@ * License for the specific language governing permissions and limitations * under the License. */ -package io.netty.example.udt.echo.rendevous; +package io.netty.example.udt.echo.rendezvous; import io.netty.bootstrap.Bootstrap; import io.netty.channel.ChannelFuture; diff --git a/example/src/main/java/io/netty/example/udt/echo/rendevous/MsgEchoPeerHandler.java b/example/src/main/java/io/netty/example/udt/echo/rendezvous/MsgEchoPeerHandler.java similarity index 98% rename from example/src/main/java/io/netty/example/udt/echo/rendevous/MsgEchoPeerHandler.java rename to example/src/main/java/io/netty/example/udt/echo/rendezvous/MsgEchoPeerHandler.java index b3a9ba222e..a552495308 100644 --- a/example/src/main/java/io/netty/example/udt/echo/rendevous/MsgEchoPeerHandler.java +++ b/example/src/main/java/io/netty/example/udt/echo/rendezvous/MsgEchoPeerHandler.java @@ -13,7 +13,7 @@ * License for the specific language governing permissions and limitations * under the License. */ -package io.netty.example.udt.echo.rendevous; +package io.netty.example.udt.echo.rendezvous; import com.yammer.metrics.Metrics; import com.yammer.metrics.core.Meter; diff --git a/example/src/main/java/io/netty/example/udt/echo/rendevous/MsgEchoPeerOne.java b/example/src/main/java/io/netty/example/udt/echo/rendezvous/MsgEchoPeerOne.java similarity index 97% rename from example/src/main/java/io/netty/example/udt/echo/rendevous/MsgEchoPeerOne.java rename to example/src/main/java/io/netty/example/udt/echo/rendezvous/MsgEchoPeerOne.java index 5b652b66a8..6fb12781c0 100644 --- a/example/src/main/java/io/netty/example/udt/echo/rendevous/MsgEchoPeerOne.java +++ b/example/src/main/java/io/netty/example/udt/echo/rendezvous/MsgEchoPeerOne.java @@ -13,7 +13,7 @@ * License for the specific language governing permissions and limitations * under the License. */ -package io.netty.example.udt.echo.rendevous; +package io.netty.example.udt.echo.rendezvous; import java.net.InetSocketAddress; import java.util.logging.Logger; diff --git a/example/src/main/java/io/netty/example/udt/echo/rendevous/MsgEchoPeerTwo.java b/example/src/main/java/io/netty/example/udt/echo/rendezvous/MsgEchoPeerTwo.java similarity index 97% rename from example/src/main/java/io/netty/example/udt/echo/rendevous/MsgEchoPeerTwo.java rename to example/src/main/java/io/netty/example/udt/echo/rendezvous/MsgEchoPeerTwo.java index 779739f58d..394d065232 100644 --- a/example/src/main/java/io/netty/example/udt/echo/rendevous/MsgEchoPeerTwo.java +++ b/example/src/main/java/io/netty/example/udt/echo/rendezvous/MsgEchoPeerTwo.java @@ -13,7 +13,7 @@ * License for the specific language governing permissions and limitations * under the License. */ -package io.netty.example.udt.echo.rendevous; +package io.netty.example.udt.echo.rendezvous; import io.netty.example.udt.util.UtilConsoleReporter; diff --git a/example/src/main/java/io/netty/example/udt/echo/rendevous/package-info.java b/example/src/main/java/io/netty/example/udt/echo/rendezvous/package-info.java similarity index 93% rename from example/src/main/java/io/netty/example/udt/echo/rendevous/package-info.java rename to example/src/main/java/io/netty/example/udt/echo/rendezvous/package-info.java index 72fbb38e07..405d85e452 100644 --- a/example/src/main/java/io/netty/example/udt/echo/rendevous/package-info.java +++ b/example/src/main/java/io/netty/example/udt/echo/rendezvous/package-info.java @@ -17,5 +17,5 @@ /** * Examples show how to use UDT Message Rendezvous. */ -package io.netty.example.udt.echo.rendevous; +package io.netty.example.udt.echo.rendezvous; diff --git a/example/src/main/java/io/netty/example/udt/echo/rendezvousBytes/ByteEchoPeerBase.java b/example/src/main/java/io/netty/example/udt/echo/rendezvousBytes/ByteEchoPeerBase.java new file mode 100644 index 0000000000..448e28069e --- /dev/null +++ b/example/src/main/java/io/netty/example/udt/echo/rendezvousBytes/ByteEchoPeerBase.java @@ -0,0 +1,73 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.example.udt.echo.rendezvousBytes; + +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.udt.UdtChannel; +import io.netty.channel.udt.nio.NioUdtProvider; +import io.netty.example.udt.util.UtilThreadFactory; +import io.netty.handler.logging.LogLevel; +import io.netty.handler.logging.LoggingHandler; + +import java.net.SocketAddress; +import java.util.concurrent.ThreadFactory; + +/** + * UDT Byte Stream Peer + *

+ * Sends one message when a connection is open and echoes back any received data + * to the server. Simply put, the echo client initiates the ping-pong traffic + * between the echo client and server by sending the first message to the + * server. + *

+ */ +public class ByteEchoPeerBase { + protected final int messageSize; + protected SocketAddress myAddress; + protected SocketAddress peerAddress; + + public ByteEchoPeerBase(int messageSize, SocketAddress myAddress, SocketAddress peerAddress) { + this.messageSize = messageSize; + this.myAddress = myAddress; + this.peerAddress = peerAddress; + } + + public void run() throws Exception { + final Bootstrap bootstrap = new Bootstrap(); + final ThreadFactory connectFactory = new UtilThreadFactory("rendezvous"); + final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, + connectFactory, NioUdtProvider.BYTE_PROVIDER); + try { + bootstrap.group(connectGroup) + .channelFactory(NioUdtProvider.BYTE_RENDEZVOUS) + .handler(new ChannelInitializer() { + @Override + protected void initChannel(UdtChannel ch) throws Exception { + ch.pipeline().addLast( + new LoggingHandler(LogLevel.INFO), + new ByteEchoPeerHandler(messageSize)); + } + }); + final ChannelFuture future = bootstrap.connect(peerAddress, myAddress).sync(); + future.channel().closeFuture().sync(); + } finally { + bootstrap.shutdown(); + } + } +} diff --git a/example/src/main/java/io/netty/example/udt/echo/rendezvousBytes/ByteEchoPeerHandler.java b/example/src/main/java/io/netty/example/udt/echo/rendezvousBytes/ByteEchoPeerHandler.java new file mode 100644 index 0000000000..9452aa5648 --- /dev/null +++ b/example/src/main/java/io/netty/example/udt/echo/rendezvousBytes/ByteEchoPeerHandler.java @@ -0,0 +1,76 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.example.udt.echo.rendezvousBytes; + +import com.yammer.metrics.Metrics; +import com.yammer.metrics.core.Meter; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelHandlerUtil; +import io.netty.channel.ChannelInboundByteHandlerAdapter; +import io.netty.channel.ChannelOption; +import io.netty.channel.udt.nio.NioUdtProvider; + +import java.util.concurrent.TimeUnit; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Handler implementation for the echo client. It initiates the ping-pong + * traffic between the echo client and server by sending the first message to + * the server on activation. + */ +public class ByteEchoPeerHandler extends ChannelInboundByteHandlerAdapter { + private static final Logger log = Logger.getLogger(ByteEchoPeerHandler.class.getName()); + private final ByteBuf message; + + final Meter meter = Metrics.newMeter(ByteEchoPeerHandler.class, "rate", + "bytes", TimeUnit.SECONDS); + + public ByteEchoPeerHandler(final int messageSize) { + message = Unpooled.buffer(messageSize); + for (int i = 0; i < message.capacity(); i++) { + message.writeByte((byte) i); + } + } + + @Override + public ByteBuf newInboundBuffer(ChannelHandlerContext ctx) throws Exception { + return ChannelHandlerUtil.allocate(ctx, + ctx.channel().config().getOption(ChannelOption.SO_RCVBUF)); + } + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + log.info("ECHO active " + NioUdtProvider.socketUDT(ctx.channel()).toStringOptions()); + ctx.write(message); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + log.log(Level.WARNING, "close the connection when an exception is raised", cause); + ctx.close(); + } + + @Override + protected void inboundBufferUpdated(ChannelHandlerContext ctx, ByteBuf in) throws Exception { + meter.mark(in.readableBytes()); + final ByteBuf out = ctx.nextOutboundByteBuffer(); + out.writeBytes(in); + ctx.flush(); + } +} diff --git a/example/src/main/java/io/netty/example/udt/echo/rendezvousBytes/ByteEchoPeerOne.java b/example/src/main/java/io/netty/example/udt/echo/rendezvousBytes/ByteEchoPeerOne.java new file mode 100644 index 0000000000..a8cc43a8a4 --- /dev/null +++ b/example/src/main/java/io/netty/example/udt/echo/rendezvousBytes/ByteEchoPeerOne.java @@ -0,0 +1,49 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.example.udt.echo.rendezvousBytes; + +import io.netty.example.udt.echo.rendezvous.Config; + +import java.net.InetSocketAddress; +import java.net.SocketAddress; +import java.util.logging.Logger; + +/** + * UDT Byte Stream Peer + *

+ * Sends one message when a connection is open and echoes back any received data + * to the server. Simply put, the echo client initiates the ping-pong traffic + * between the echo client and server by sending the first message to the + * server. + *

+ */ +public class ByteEchoPeerOne extends ByteEchoPeerBase { + private static final Logger log = Logger.getLogger(ByteEchoPeerOne.class.getName()); + + public ByteEchoPeerOne(int messageSize, SocketAddress myAddress, SocketAddress peerAddress) { + super(messageSize, myAddress, peerAddress); + } + + public static void main(String[] args) throws Exception { + log.info("init"); + final int messageSize = 64 * 1024; + final InetSocketAddress myAddress = new InetSocketAddress( + Config.hostOne, Config.portOne); + final InetSocketAddress peerAddress = new InetSocketAddress( + Config.hostTwo, Config.portTwo); + new ByteEchoPeerOne(messageSize, myAddress, peerAddress).run(); + } +} diff --git a/example/src/main/java/io/netty/example/udt/echo/rendezvousBytes/ByteEchoPeerTwo.java b/example/src/main/java/io/netty/example/udt/echo/rendezvousBytes/ByteEchoPeerTwo.java new file mode 100644 index 0000000000..a92796e18a --- /dev/null +++ b/example/src/main/java/io/netty/example/udt/echo/rendezvousBytes/ByteEchoPeerTwo.java @@ -0,0 +1,54 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.example.udt.echo.rendezvousBytes; + +import io.netty.example.udt.echo.rendezvous.Config; +import io.netty.example.udt.util.UtilConsoleReporter; + +import java.net.InetSocketAddress; +import java.net.SocketAddress; +import java.util.concurrent.TimeUnit; +import java.util.logging.Logger; + +/** + * UDT Byte Stream Peer + *

+ * Sends one message when a connection is open and echoes back any received data + * to the server. Simply put, the echo client initiates the ping-pong traffic + * between the echo client and server by sending the first message to the + * server. + *

+ */ +public class ByteEchoPeerTwo extends ByteEchoPeerBase { + private static final Logger log = Logger.getLogger(ByteEchoPeerTwo.class.getName()); + + public ByteEchoPeerTwo(int messageSize, SocketAddress myAddress, SocketAddress peerAddress) { + super(messageSize, myAddress, peerAddress); + } + + public static void main(String[] args) throws Exception { + log.info("init"); + // peer two is reporting metrics + UtilConsoleReporter.enable(3, TimeUnit.SECONDS); + final int messageSize = 64 * 1024; + final InetSocketAddress myAddress = new InetSocketAddress( + Config.hostTwo, Config.portTwo); + final InetSocketAddress peerAddress = new InetSocketAddress( + Config.hostOne, Config.portOne); + + new ByteEchoPeerTwo(messageSize, myAddress, peerAddress).run(); + } +} diff --git a/example/src/main/java/io/netty/example/udt/echo/rendezvousBytes/package-info.java b/example/src/main/java/io/netty/example/udt/echo/rendezvousBytes/package-info.java new file mode 100644 index 0000000000..530345eb9d --- /dev/null +++ b/example/src/main/java/io/netty/example/udt/echo/rendezvousBytes/package-info.java @@ -0,0 +1,21 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +/** + * Examples show how to use UDT Byte Streams Rendezvous. + */ +package io.netty.example.udt.echo.rendezvousBytes; +