use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelInitializer in project wildfly-camel by wildfly-extras.
the class LumberjackComponentTest method sendMessages.
private List<Integer> sendMessages(int port, SSLContextParameters sslContextParameters) throws InterruptedException {
NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup();
try {
// This list will hold the acknowledgment response sequence numbers
List<Integer> responses = new ArrayList<>();
// This initializer configures the SSL and an acknowledgment recorder
ChannelInitializer<Channel> initializer = new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
if (sslContextParameters != null) {
SSLEngine sslEngine = sslContextParameters.createSSLContext(null).createSSLEngine();
sslEngine.setUseClientMode(true);
pipeline.addLast(new SslHandler(sslEngine));
}
// Add the response recorder
pipeline.addLast(new SimpleChannelInboundHandler<ByteBuf>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
Assert.assertEquals(msg.readUnsignedByte(), (short) '2');
Assert.assertEquals(msg.readUnsignedByte(), (short) 'A');
synchronized (responses) {
responses.add(msg.readInt());
}
}
});
}
};
// Connect to the server
Channel channel = new Bootstrap().group(eventLoopGroup).channel(NioSocketChannel.class).handler(initializer).connect("127.0.0.1", port).sync().channel();
// Send the 2 window frames
TimeUnit.MILLISECONDS.sleep(100);
channel.writeAndFlush(readSample("lumberjack/window10"));
TimeUnit.MILLISECONDS.sleep(100);
channel.writeAndFlush(readSample("lumberjack/window15"));
TimeUnit.MILLISECONDS.sleep(100);
channel.close();
synchronized (responses) {
return responses;
}
} finally {
eventLoopGroup.shutdownGracefully();
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelInitializer in project herddb by diennea.
the class NettyChannelAcceptor method start.
public void start() throws Exception {
if (ssl) {
if (sslCertFile == null) {
LOGGER.log(Level.SEVERE, "start SSL with self-signed auto-generated certificate");
if (sslCiphers != null) {
LOGGER.log(Level.SEVERE, "required sslCiphers " + sslCiphers);
}
SelfSignedCertificate ssc = new SelfSignedCertificate();
try {
sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).ciphers(sslCiphers).build();
} finally {
ssc.delete();
}
} else {
LOGGER.log(Level.SEVERE, "start SSL with certificate " + sslCertFile.getAbsolutePath() + " chain file " + sslCertChainFile.getAbsolutePath());
if (sslCiphers != null) {
LOGGER.log(Level.SEVERE, "required sslCiphers " + sslCiphers);
}
sslCtx = SslContextBuilder.forServer(sslCertChainFile, sslCertFile, sslCertPassword).ciphers(sslCiphers).build();
}
}
if (callbackThreads == 0) {
callbackExecutor = Executors.newCachedThreadPool();
} else {
callbackExecutor = Executors.newFixedThreadPool(callbackThreads, new ThreadFactory() {
private final AtomicLong count = new AtomicLong();
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "herddb-callbacks-" + count.incrementAndGet());
}
});
}
InetSocketAddress address = new InetSocketAddress(host, port);
LOGGER.log(Level.SEVERE, "Starting HerdDB network server at {0}:{1}", new Object[] { host, port + "" });
ChannelInitializer<io.netty.channel.Channel> channelInitialized = new ChannelInitializer<io.netty.channel.Channel>() {
@Override
public void initChannel(io.netty.channel.Channel ch) throws Exception {
NettyChannel session = new NettyChannel("unnamed", ch, callbackExecutor);
if (acceptor != null) {
acceptor.createConnection(session);
}
// Add SSL handler first to encrypt and decrypt everything.
if (ssl) {
ch.pipeline().addLast(sslCtx.newHandler(ch.alloc()));
}
ch.pipeline().addLast("lengthprepender", new LengthFieldPrepender(4));
ch.pipeline().addLast("lengthbaseddecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
//
ch.pipeline().addLast("messageencoder", new DataMessageEncoder());
ch.pipeline().addLast("messagedecoder", new DataMessageDecoder());
ch.pipeline().addLast(new InboundMessageHandler(session));
}
};
if (enableRealNetwork) {
if (NetworkUtils.isEnableEpoolNative()) {
bossGroup = new EpollEventLoopGroup(workerThreads);
workerGroup = new EpollEventLoopGroup(workerThreads);
LOGGER.log(Level.INFO, "Using netty-native-epoll network type");
} else {
bossGroup = new NioEventLoopGroup(workerThreads);
workerGroup = new NioEventLoopGroup(workerThreads);
LOGGER.log(Level.INFO, "Using nio network type");
}
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup).channel(NetworkUtils.isEnableEpoolNative() ? EpollServerSocketChannel.class : NioServerSocketChannel.class).childHandler(channelInitialized).option(ChannelOption.SO_BACKLOG, 128);
ChannelFuture f = b.bind(address).sync();
this.channel = f.channel();
}
if (enableJVMNetwork) {
localBossGroup = new DefaultEventLoopGroup(workerThreads);
localWorkerGroup = new DefaultEventLoopGroup(workerThreads);
ServerBootstrap b_local = new ServerBootstrap();
b_local.group(localBossGroup, localWorkerGroup).channel(LocalServerChannel.class).childHandler(channelInitialized);
String hostAddress = NetworkUtils.getAddress(address);
LocalServerRegistry.registerLocalServer(hostAddress, port, ssl);
ChannelFuture local_f = b_local.bind(new LocalAddress(hostAddress + ":" + port + ":" + ssl)).sync();
this.local_channel = local_f.channel();
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelInitializer in project moco by dreamhead.
the class MocoSocketServer method channelInitializer.
@Override
public ChannelInitializer<SocketChannel> channelInitializer() {
return new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(final SocketChannel ch) {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("aggregator", new MocoAggregator());
pipeline.addLast("handler", new MocoSocketHandler(serverSetting));
}
};
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelInitializer in project chuidiang-ejemplos by chuidiang.
the class MulticastServer method start.
public void start() throws InterruptedException, SocketException, UnknownHostException {
NetworkInterface ni = NetworkInterface.getByInetAddress(InetAddress.getByName("127.0.0.1"));
InetSocketAddress groupAddress = new InetSocketAddress("239.255.27.1", 1234);
EventLoopGroup group = new NioEventLoopGroup();
Bootstrap b = new Bootstrap();
b.group(group).channelFactory(new ChannelFactory<NioDatagramChannel>() {
@Override
public NioDatagramChannel newChannel() {
return new NioDatagramChannel(InternetProtocolFamily.IPv4);
}
}).handler(new ChannelInitializer<NioDatagramChannel>() {
@Override
protected void initChannel(NioDatagramChannel ch) throws Exception {
ch.pipeline().addLast(new ServerHandler());
}
}).option(ChannelOption.SO_REUSEADDR, true);
NioDatagramChannel ch = (NioDatagramChannel) b.bind(groupAddress.getPort()).sync().channel();
ch.joinGroup(groupAddress, ni).sync();
ch.closeFuture().await();
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelInitializer in project chuidiang-ejemplos by chuidiang.
the class MulticastClient method start.
public void start() throws InterruptedException, SocketException, UnknownHostException {
EventLoopGroup group = new NioEventLoopGroup();
Bootstrap b = new Bootstrap();
b.group(group).channelFactory(new ChannelFactory<NioDatagramChannel>() {
@Override
public NioDatagramChannel newChannel() {
return new NioDatagramChannel(InternetProtocolFamily.IPv4);
}
}).handler(new ChannelInitializer<NioDatagramChannel>() {
@Override
protected void initChannel(NioDatagramChannel ch) throws Exception {
ch.pipeline().addLast(myHandler);
}
}).option(ChannelOption.SO_REUSEADDR, true);
NioDatagramChannel ch = (NioDatagramChannel) b.bind(1234).sync().channel();
new Thread() {
public void run() {
while (true) {
try {
Thread.sleep(1000);
myHandler.sendMessage("Hola desde netty");
System.out.println("Envio Hola");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}.start();
ch.closeFuture().await();
}
Aggregations