Search in sources :

Example 31 with SimpleChannelInboundHandler

use of org.apache.flink.shaded.netty4.io.netty.channel.SimpleChannelInboundHandler in project atomix by atomix.

the class NettyBroadcastService method bootstrapServer.

private CompletableFuture<Void> bootstrapServer() {
    Bootstrap serverBootstrap = new Bootstrap().group(group).channelFactory(() -> new NioDatagramChannel(InternetProtocolFamily.IPv4)).handler(new SimpleChannelInboundHandler<Object>() {

        @Override
        public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
        // Nothing will be sent.
        }
    }).option(ChannelOption.IP_MULTICAST_IF, iface).option(ChannelOption.SO_REUSEADDR, true);
    CompletableFuture<Void> future = new CompletableFuture<>();
    serverBootstrap.bind(localAddress).addListener((ChannelFutureListener) f -> {
        if (f.isSuccess()) {
            serverChannel = f.channel();
            future.complete(null);
        } else {
            future.completeExceptionally(f.cause());
        }
    });
    return future;
}
Also used : Address(io.atomix.utils.net.Address) ChannelOption(io.netty.channel.ChannelOption) AtomixRuntimeException(io.atomix.utils.AtomixRuntimeException) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) SocketException(java.net.SocketException) Threads.namedThreads(io.atomix.utils.concurrent.Threads.namedThreads) Namespaces(io.atomix.utils.serializer.Namespaces) DatagramChannel(io.netty.channel.socket.DatagramChannel) ByteBuf(io.netty.buffer.ByteBuf) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Map(java.util.Map) DatagramPacket(io.netty.channel.socket.DatagramPacket) BroadcastService(io.atomix.cluster.messaging.BroadcastService) InternetProtocolFamily(io.netty.channel.socket.InternetProtocolFamily) ManagedBroadcastService(io.atomix.cluster.messaging.ManagedBroadcastService) EventLoopGroup(io.netty.channel.EventLoopGroup) Logger(org.slf4j.Logger) NetworkInterface(java.net.NetworkInterface) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Set(java.util.Set) Maps(com.google.common.collect.Maps) InetSocketAddress(java.net.InetSocketAddress) Sets(com.google.common.collect.Sets) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Channel(io.netty.channel.Channel) Consumer(java.util.function.Consumer) Bootstrap(io.netty.bootstrap.Bootstrap) Namespace(io.atomix.utils.serializer.Namespace) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) Serializer(io.atomix.utils.serializer.Serializer) CompletableFuture(java.util.concurrent.CompletableFuture) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) AtomixRuntimeException(io.atomix.utils.AtomixRuntimeException) SocketException(java.net.SocketException)

Example 32 with SimpleChannelInboundHandler

use of org.apache.flink.shaded.netty4.io.netty.channel.SimpleChannelInboundHandler in project atomix by atomix.

the class NettyBroadcastService method bootstrapClient.

private CompletableFuture<Void> bootstrapClient() {
    Bootstrap clientBootstrap = new Bootstrap().group(group).channelFactory(() -> new NioDatagramChannel(InternetProtocolFamily.IPv4)).handler(new SimpleChannelInboundHandler<DatagramPacket>() {

        @Override
        protected void channelRead0(ChannelHandlerContext context, DatagramPacket packet) throws Exception {
            byte[] payload = new byte[packet.content().readInt()];
            packet.content().readBytes(payload);
            Message message = SERIALIZER.decode(payload);
            Set<Consumer<byte[]>> listeners = NettyBroadcastService.this.listeners.get(message.subject());
            if (listeners != null) {
                for (Consumer<byte[]> listener : listeners) {
                    listener.accept(message.payload());
                }
            }
        }
    }).option(ChannelOption.IP_MULTICAST_IF, iface).option(ChannelOption.SO_REUSEADDR, true).localAddress(localAddress.getPort());
    CompletableFuture<Void> future = new CompletableFuture<>();
    clientBootstrap.bind().addListener((ChannelFutureListener) f -> {
        if (f.isSuccess()) {
            clientChannel = (DatagramChannel) f.channel();
            log.info("{} joining multicast group {} on port {}", localAddress.getHostName(), groupAddress.getHostName(), groupAddress.getPort());
            clientChannel.joinGroup(groupAddress, iface).addListener(f2 -> {
                if (f2.isSuccess()) {
                    log.info("{} successfully joined multicast group {} on port {}", localAddress.getHostName(), groupAddress.getHostName(), groupAddress.getPort());
                    future.complete(null);
                } else {
                    log.info("{} failed to join group {} on port {}", localAddress.getHostName(), groupAddress.getHostName(), groupAddress.getPort());
                    future.completeExceptionally(f2.cause());
                }
            });
        } else {
            future.completeExceptionally(f.cause());
        }
    });
    return future;
}
Also used : SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) Address(io.atomix.utils.net.Address) ChannelOption(io.netty.channel.ChannelOption) AtomixRuntimeException(io.atomix.utils.AtomixRuntimeException) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) SocketException(java.net.SocketException) Threads.namedThreads(io.atomix.utils.concurrent.Threads.namedThreads) Namespaces(io.atomix.utils.serializer.Namespaces) DatagramChannel(io.netty.channel.socket.DatagramChannel) ByteBuf(io.netty.buffer.ByteBuf) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Map(java.util.Map) DatagramPacket(io.netty.channel.socket.DatagramPacket) BroadcastService(io.atomix.cluster.messaging.BroadcastService) InternetProtocolFamily(io.netty.channel.socket.InternetProtocolFamily) ManagedBroadcastService(io.atomix.cluster.messaging.ManagedBroadcastService) EventLoopGroup(io.netty.channel.EventLoopGroup) Logger(org.slf4j.Logger) NetworkInterface(java.net.NetworkInterface) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Set(java.util.Set) Maps(com.google.common.collect.Maps) InetSocketAddress(java.net.InetSocketAddress) Sets(com.google.common.collect.Sets) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Channel(io.netty.channel.Channel) Consumer(java.util.function.Consumer) Bootstrap(io.netty.bootstrap.Bootstrap) Namespace(io.atomix.utils.serializer.Namespace) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) Serializer(io.atomix.utils.serializer.Serializer) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) DatagramChannel(io.netty.channel.socket.DatagramChannel) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CompletableFuture(java.util.concurrent.CompletableFuture) Consumer(java.util.function.Consumer) DatagramPacket(io.netty.channel.socket.DatagramPacket) Bootstrap(io.netty.bootstrap.Bootstrap)

Example 33 with SimpleChannelInboundHandler

use of org.apache.flink.shaded.netty4.io.netty.channel.SimpleChannelInboundHandler in project atomix by atomix.

the class NettyUnicastService method bootstrap.

private CompletableFuture<Void> bootstrap() {
    Bootstrap serverBootstrap = new Bootstrap().group(group).channel(NioDatagramChannel.class).handler(new SimpleChannelInboundHandler<DatagramPacket>() {

        @Override
        protected void channelRead0(ChannelHandlerContext context, DatagramPacket packet) throws Exception {
            byte[] payload = new byte[packet.content().readInt()];
            packet.content().readBytes(payload);
            Message message = SERIALIZER.decode(payload);
            Map<BiConsumer<Address, byte[]>, Executor> listeners = NettyUnicastService.this.listeners.get(message.subject());
            if (listeners != null) {
                listeners.forEach((consumer, executor) -> executor.execute(() -> consumer.accept(message.source(), message.payload())));
            }
        }
    }).option(ChannelOption.RCVBUF_ALLOCATOR, new DefaultMaxBytesRecvByteBufAllocator()).option(ChannelOption.SO_BROADCAST, true).option(ChannelOption.SO_REUSEADDR, true);
    return bind(serverBootstrap);
}
Also used : SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) Executor(java.util.concurrent.Executor) DatagramPacket(io.netty.channel.socket.DatagramPacket) Bootstrap(io.netty.bootstrap.Bootstrap) DefaultMaxBytesRecvByteBufAllocator(io.netty.channel.DefaultMaxBytesRecvByteBufAllocator) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) BiConsumer(java.util.function.BiConsumer)

Aggregations

ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)33 SimpleChannelInboundHandler (io.netty.channel.SimpleChannelInboundHandler)33 Channel (io.netty.channel.Channel)26 Bootstrap (io.netty.bootstrap.Bootstrap)22 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)21 SocketChannel (io.netty.channel.socket.SocketChannel)16 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)16 EventLoopGroup (io.netty.channel.EventLoopGroup)15 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)11 ByteBuf (io.netty.buffer.ByteBuf)11 ChannelPipeline (io.netty.channel.ChannelPipeline)11 ChannelFuture (io.netty.channel.ChannelFuture)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)8 InetSocketAddress (java.net.InetSocketAddress)8 ChannelFutureListener (io.netty.channel.ChannelFutureListener)7 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)6 HttpRequest (io.netty.handler.codec.http.HttpRequest)6 IOException (java.io.IOException)6 DatagramPacket (io.netty.channel.socket.DatagramPacket)5