1 Rename package name "rendzvous" to "rendezvous" 2 Add bytes stream rendezvous example
This commit is contained in:
parent
ce87b627be
commit
f008ac8d47
@ -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
|
@ -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;
|
@ -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;
|
@ -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;
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
* <p/>
|
||||
* 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.
|
||||
* <p/>
|
||||
*/
|
||||
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<UdtChannel>() {
|
||||
@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();
|
||||
}
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -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
|
||||
* <p/>
|
||||
* 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.
|
||||
* <p/>
|
||||
*/
|
||||
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();
|
||||
}
|
||||
}
|
@ -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
|
||||
* <p/>
|
||||
* 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.
|
||||
* <p/>
|
||||
*/
|
||||
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();
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user