Search in sources :

Example 1 with MessageToMessageDecoder

use of io.netty.handler.codec.MessageToMessageDecoder in project opennms by OpenNMS.

the class UdpListener method start.

public void start() throws InterruptedException {
    bossGroup = new NioEventLoopGroup();
    final Bootstrap b = new Bootstrap().group(bossGroup).channel(NioDatagramChannel.class).option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.SO_RCVBUF, Integer.MAX_VALUE).option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(maxPacketSize)).handler(new ChannelInitializer<DatagramChannel>() {

        @Override
        protected void initChannel(DatagramChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            p.addLast(new MessageToMessageDecoder<DatagramPacket>() {

                @Override
                protected void decode(ChannelHandlerContext ctx, DatagramPacket packet, List<Object> out) throws Exception {
                    // Wrap the contents of the packet in a ByteBuffer, referencing
                    // the underlying byte array if possible
                    final ByteBuffer buffer = wrapContentsWithNioByteBuffer(packet);
                    // Build the message to dispatch via the Sink API
                    final TelemetryMessage msg = new TelemetryMessage(packet.sender(), buffer);
                    // Dispatch and retain a reference to the packet
                    // in the case that we are sharing the underlying byte array
                    final CompletableFuture<TelemetryMessage> future = dispatcher.send(msg);
                    packet.retain();
                    future.whenComplete((res, ex) -> packet.release());
                }
            });
        }
    });
    future = b.bind(host, port).await();
}
Also used : DatagramChannel(io.netty.channel.socket.DatagramChannel) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuffer(java.nio.ByteBuffer) ChannelPipeline(io.netty.channel.ChannelPipeline) TelemetryMessage(org.opennms.netmgt.telemetry.listeners.api.TelemetryMessage) MessageToMessageDecoder(io.netty.handler.codec.MessageToMessageDecoder) DatagramPacket(io.netty.channel.socket.DatagramPacket) FixedRecvByteBufAllocator(io.netty.channel.FixedRecvByteBufAllocator) Bootstrap(io.netty.bootstrap.Bootstrap) List(java.util.List) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 2 with MessageToMessageDecoder

use of io.netty.handler.codec.MessageToMessageDecoder in project opennms by OpenNMS.

the class UdpListener method start.

public void start() throws InterruptedException {
    bossGroup = new NioEventLoopGroup();
    final Bootstrap b = new Bootstrap().group(bossGroup).channel(NioDatagramChannel.class).option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.SO_RCVBUF, Integer.MAX_VALUE).handler(new ChannelInitializer<DatagramChannel>() {

        @Override
        protected void initChannel(DatagramChannel ch) throws Exception {
            ch.pipeline().addLast(new LoggingHandler());
            ch.pipeline().addLast(new MessageToMessageDecoder<DatagramPacket>() {

                @Override
                protected void decode(ChannelHandlerContext ctx, DatagramPacket msg, List<Object> out) throws Exception {
                    msg.retain();
                    out.add(msg.content());
                }
            });
            ch.pipeline().addLast(new XmlFrameDecoder(2147483647));
            ch.pipeline().addLast(new XmlEventProcessor(eventIpcManager));
        }
    });
    future = b.bind(config.getUDPIpAddress(), config.getUDPPort()).await();
}
Also used : LoggingHandler(io.netty.handler.logging.LoggingHandler) 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) XmlFrameDecoder(io.netty.handler.codec.xml.XmlFrameDecoder) MessageToMessageDecoder(io.netty.handler.codec.MessageToMessageDecoder) DatagramPacket(io.netty.channel.socket.DatagramPacket) Bootstrap(io.netty.bootstrap.Bootstrap) List(java.util.List) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

Bootstrap (io.netty.bootstrap.Bootstrap)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)2 DatagramChannel (io.netty.channel.socket.DatagramChannel)2 DatagramPacket (io.netty.channel.socket.DatagramPacket)2 NioDatagramChannel (io.netty.channel.socket.nio.NioDatagramChannel)2 MessageToMessageDecoder (io.netty.handler.codec.MessageToMessageDecoder)2 List (java.util.List)2 ChannelPipeline (io.netty.channel.ChannelPipeline)1 FixedRecvByteBufAllocator (io.netty.channel.FixedRecvByteBufAllocator)1 XmlFrameDecoder (io.netty.handler.codec.xml.XmlFrameDecoder)1 LoggingHandler (io.netty.handler.logging.LoggingHandler)1 ByteBuffer (java.nio.ByteBuffer)1 TelemetryMessage (org.opennms.netmgt.telemetry.listeners.api.TelemetryMessage)1