Search in sources :

Example 11 with DatagramChannel

use of io.netty.channel.socket.DatagramChannel in project netty by netty.

the class NioDatagramChannelTest method testBindMultiple.

/**
     * Test try to reproduce issue #1335
     */
@Test
public void testBindMultiple() throws Exception {
    DefaultChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
    NioEventLoopGroup group = new NioEventLoopGroup();
    try {
        for (int i = 0; i < 100; i++) {
            Bootstrap udpBootstrap = new Bootstrap();
            udpBootstrap.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_BROADCAST, true).handler(new ChannelInboundHandlerAdapter() {

                @Override
                public void channelRead(ChannelHandlerContext ctx, Object msg) {
                    // Discard
                    ReferenceCountUtil.release(msg);
                }
            });
            DatagramChannel datagramChannel = (DatagramChannel) udpBootstrap.bind(new InetSocketAddress(0)).syncUninterruptibly().channel();
            channelGroup.add(datagramChannel);
        }
        Assert.assertEquals(100, channelGroup.size());
    } finally {
        channelGroup.close().sync();
        group.shutdownGracefully().sync();
    }
}
Also used : DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(io.netty.channel.socket.DatagramChannel) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Example 12 with DatagramChannel

use of io.netty.channel.socket.DatagramChannel 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 13 with DatagramChannel

use of io.netty.channel.socket.DatagramChannel 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)

Example 14 with DatagramChannel

use of io.netty.channel.socket.DatagramChannel in project reactor-netty by reactor.

the class UdpClient method newHandler.

@Override
public Mono<? extends NettyContext> newHandler(BiFunction<? super UdpInbound, ? super UdpOutbound, ? extends Publisher<Void>> handler) {
    final BiFunction<? super UdpInbound, ? super UdpOutbound, ? extends Publisher<Void>> targetHandler = null == handler ? ChannelOperations.noopHandler() : handler;
    return Mono.create(sink -> {
        Bootstrap b = options.get();
        SocketAddress adr = options.getAddress();
        if (adr == null) {
            sink.error(new NullPointerException("Provided UdpClientOptions do not " + "define any address to bind to "));
            return;
        }
        b.remoteAddress(adr);
        ContextHandler<DatagramChannel> c = doHandler(targetHandler, sink, adr);
        b.handler(c);
        c.setFuture(b.connect());
    });
}
Also used : DatagramChannel(io.netty.channel.socket.DatagramChannel) Bootstrap(io.netty.bootstrap.Bootstrap) SocketAddress(java.net.SocketAddress)

Example 15 with DatagramChannel

use of io.netty.channel.socket.DatagramChannel in project traccar by tananaev.

the class EskyProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    String sentence = (String) msg;
    Parser parser = new Parser(PATTERN, sentence);
    if (!parser.matches()) {
        return null;
    }
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
    if (deviceSession == null) {
        return null;
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    position.set(Position.KEY_SATELLITES, parser.nextInt());
    position.setValid(true);
    position.setTime(parser.nextDateTime());
    position.setLatitude(parser.nextDouble());
    position.setLongitude(parser.nextDouble());
    position.setSpeed(UnitsConverter.knotsFromMps(parser.nextDouble()));
    position.setCourse(parser.nextDouble());
    if (parser.hasNext(3)) {
        int input = parser.nextHexInt();
        position.set(Position.KEY_IGNITION, !BitUtil.check(input, 0));
        position.set(Position.PREFIX_IN + 1, !BitUtil.check(input, 1));
        position.set(Position.PREFIX_IN + 2, !BitUtil.check(input, 2));
        position.set(Position.KEY_EVENT, parser.nextInt());
        position.set(Position.KEY_ODOMETER, parser.nextInt());
    }
    position.set(Position.PREFIX_ADC + 1, parser.nextInt());
    position.set(Position.KEY_BATTERY, parser.nextInt() * 0.01);
    int index = sentence.lastIndexOf('+');
    if (index > 0 && channel instanceof DatagramChannel) {
        channel.writeAndFlush(new NetworkMessage("ACK," + sentence.substring(index + 1) + "#", remoteAddress));
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DatagramChannel(io.netty.channel.socket.DatagramChannel) NetworkMessage(org.traccar.NetworkMessage) Parser(org.traccar.helper.Parser)

Aggregations

DatagramChannel (io.netty.channel.socket.DatagramChannel)19 InetSocketAddress (java.net.InetSocketAddress)12 NioDatagramChannel (io.netty.channel.socket.nio.NioDatagramChannel)9 Bootstrap (io.netty.bootstrap.Bootstrap)8 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)8 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)6 EventLoopGroup (io.netty.channel.EventLoopGroup)6 DatagramPacket (io.netty.channel.socket.DatagramPacket)6 ByteBuf (io.netty.buffer.ByteBuf)5 UnknownHostException (java.net.UnknownHostException)4 NetworkMessage (org.traccar.NetworkMessage)4 List (java.util.List)3 DnsMessage (org.apache.directory.server.dns.messages.DnsMessage)3 QuestionRecord (org.apache.directory.server.dns.messages.QuestionRecord)3 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)2 ChannelPipeline (io.netty.channel.ChannelPipeline)2 EventLoop (io.netty.channel.EventLoop)2 FixedRecvByteBufAllocator (io.netty.channel.FixedRecvByteBufAllocator)2 DefaultChannelGroup (io.netty.channel.group.DefaultChannelGroup)2 MessageToMessageDecoder (io.netty.handler.codec.MessageToMessageDecoder)2