Skip to content

Instantly share code, notes, and snippets.

@Vladislav-Kisliy
Created July 17, 2016 10:04
Show Gist options
  • Save Vladislav-Kisliy/8bdc164cfd3bb74451e7ba86bdf77218 to your computer and use it in GitHub Desktop.
Save Vladislav-Kisliy/8bdc164cfd3bb74451e7ba86bdf77218 to your computer and use it in GitHub Desktop.
StO: Create one to many TCP-proxy
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class MultiplierClientHandler extends ChannelInboundHandlerAdapter {
private final ByteBuf message;
public MultiplierClientHandler(ByteBuf buf) {
message = buf;
}
@Override
public void channelActive(ChannelHandlerContext ctx) {
ctx.writeAndFlush(message);
ctx.close();
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
}
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
public final class MultiplierServer {
static final int PORT = Integer.parseInt(System.getProperty("port", "8007"));
public static void main(String[] args) throws Exception {
// Configure the server.
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(new LoggingHandler(LogLevel.INFO));
p.addLast(new MultiplierServerHandler());
}
});
// Start the server.
ChannelFuture f = b.bind(PORT).sync();
// Wait until the server socket is closed.
f.channel().closeFuture().sync();
} finally {
// Shut down all event loops to terminate all threads.
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
import com.karlsoft.wrapper.config.HostInfo;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MultiplierServerHandler extends ChannelInboundHandlerAdapter {
private static final Logger LOG = Logger.getLogger(MultiplierServerHandler.class.getName());
private final List<HostInfo> HOSTS = new LinkedList<>();
private ByteBuf buf;
public MultiplierServerHandler() {
HOSTS.add(new HostInfo("127.0.0.1", 10000));
HOSTS.add(new HostInfo("127.0.0.1", 20000));
HOSTS.add(new HostInfo("127.0.0.1", 30000));
HOSTS.add(new HostInfo("127.0.0.1", 40000));
HOSTS.add(new HostInfo("127.0.0.1", 50000));
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
buf = (ByteBuf) msg;
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws InterruptedException {
ctx.flush();
ctx.close();
for (HostInfo connectionInfo : HOSTS) {
// Configure the client.
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
//p.addLast(new LoggingHandler(LogLevel.INFO));
p.addLast(new MultiplierClientHandler(buf.copy()));
}
});
// Start the client.
ChannelFuture f = b.connect(connectionInfo.getHost(), connectionInfo.getPort());
f.addListener((ChannelFutureListener) (ChannelFuture future) -> {
if (future.isSuccess()) {
// connection complete start to read first data
LOG.log(Level.INFO, "Connected to {0}:{1} successfully.",
new Object[]{connectionInfo.getHost(), connectionInfo.getPort().toString()});
} else {
// Close the connection if the connection attempt has failed.
LOG.log(Level.WARNING, "Connection problem to {0}:{1}.",
new Object[]{connectionInfo.getHost(), connectionInfo.getPort().toString()});
}
});
// Wait until the connection is closed.
f.channel().closeFuture().sync();
} finally {
// Shut down the event loop to terminate all threads.
group.shutdownGracefully();
}
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
// Close the connection when an exception is raised.
cause.printStackTrace();
ctx.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment