Search in sources :

Example 41 with DatagramPacket

use of io.netty.channel.socket.DatagramPacket in project ribbon by Netflix.

the class HelloUdpServerExternalResource method start.

public void start() {
    int port;
    try {
        port = choosePort();
    } catch (SocketException e) {
        throw new RuntimeException("Error choosing point", e);
    }
    server = RxNetty.createUdpServer(port, new ConnectionHandler<DatagramPacket, DatagramPacket>() {

        @Override
        public Observable<Void> handle(final ObservableConnection<DatagramPacket, DatagramPacket> newConnection) {
            return newConnection.getInput().flatMap(new Func1<DatagramPacket, Observable<Void>>() {

                @Override
                public Observable<Void> call(final DatagramPacket received) {
                    return Observable.interval(timeout, TimeUnit.MILLISECONDS).take(1).flatMap(new Func1<Long, Observable<Void>>() {

                        @Override
                        public Observable<Void> call(Long aLong) {
                            InetSocketAddress sender = received.sender();
                            LOG.info("Received datagram. Sender: " + sender);
                            ByteBuf data = newConnection.getChannel().alloc().buffer(WELCOME_MSG_BYTES.length);
                            data.writeBytes(WELCOME_MSG_BYTES);
                            return newConnection.writeAndFlush(new DatagramPacket(data, sender));
                        }
                    });
                }
            });
        }
    });
    server.start();
    LOG.info("UDP hello server started at port: " + port);
}
Also used : SocketException(java.net.SocketException) ObservableConnection(io.reactivex.netty.channel.ObservableConnection) InetSocketAddress(java.net.InetSocketAddress) ByteBuf(io.netty.buffer.ByteBuf) Observable(rx.Observable) ConnectionHandler(io.reactivex.netty.channel.ConnectionHandler) DatagramPacket(io.netty.channel.socket.DatagramPacket) Func1(rx.functions.Func1)

Example 42 with DatagramPacket

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

the class DatagramPacketEncoderTest method testEncode.

@Test
public void testEncode() {
    InetSocketAddress recipient = SocketUtils.socketAddress("127.0.0.1", 10000);
    InetSocketAddress sender = SocketUtils.socketAddress("127.0.0.1", 20000);
    assertTrue(channel.writeOutbound(new DefaultAddressedEnvelope<String, InetSocketAddress>("netty", recipient, sender)));
    DatagramPacket packet = channel.readOutbound();
    try {
        assertEquals("netty", packet.content().toString(CharsetUtil.UTF_8));
        assertEquals(recipient, packet.recipient());
        assertEquals(sender, packet.sender());
    } finally {
        packet.release();
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DatagramPacket(io.netty.channel.socket.DatagramPacket) DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope) Test(org.junit.Test)

Example 43 with DatagramPacket

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

the class DnsQueryTest method writeQueryTest.

@Test
public void writeQueryTest() throws Exception {
    InetSocketAddress addr = SocketUtils.socketAddress("8.8.8.8", 53);
    EmbeddedChannel embedder = new EmbeddedChannel(new DatagramDnsQueryEncoder());
    List<DnsQuery> queries = new ArrayList<DnsQuery>(5);
    queries.add(new DatagramDnsQuery(null, addr, 1).setRecord(DnsSection.QUESTION, new DefaultDnsQuestion("1.0.0.127.in-addr.arpa", DnsRecordType.PTR)));
    queries.add(new DatagramDnsQuery(null, addr, 1).setRecord(DnsSection.QUESTION, new DefaultDnsQuestion("www.example.com", DnsRecordType.A)));
    queries.add(new DatagramDnsQuery(null, addr, 1).setRecord(DnsSection.QUESTION, new DefaultDnsQuestion("example.com", DnsRecordType.AAAA)));
    queries.add(new DatagramDnsQuery(null, addr, 1).setRecord(DnsSection.QUESTION, new DefaultDnsQuestion("example.com", DnsRecordType.MX)));
    queries.add(new DatagramDnsQuery(null, addr, 1).setRecord(DnsSection.QUESTION, new DefaultDnsQuestion("example.com", DnsRecordType.CNAME)));
    for (DnsQuery query : queries) {
        assertThat(query.count(DnsSection.QUESTION), is(1));
        assertThat(query.count(DnsSection.ANSWER), is(0));
        assertThat(query.count(DnsSection.AUTHORITY), is(0));
        assertThat(query.count(DnsSection.ADDITIONAL), is(0));
        embedder.writeOutbound(query);
        DatagramPacket packet = embedder.readOutbound();
        Assert.assertTrue(packet.content().isReadable());
        packet.release();
        Assert.assertNull(embedder.readOutbound());
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DatagramPacket(io.netty.channel.socket.DatagramPacket) ArrayList(java.util.ArrayList) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.Test)

Example 44 with DatagramPacket

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

the class DnsResponseTest method readMalormedResponseTest.

@Test
public void readMalormedResponseTest() throws Exception {
    EmbeddedChannel embedder = new EmbeddedChannel(new DatagramDnsResponseDecoder());
    ByteBuf packet = embedder.alloc().buffer(512).writeBytes(malformedLoopPacket);
    exception.expect(CorruptedFrameException.class);
    embedder.writeInbound(new DatagramPacket(packet, null, new InetSocketAddress(0)));
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DatagramPacket(io.netty.channel.socket.DatagramPacket) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 45 with DatagramPacket

use of io.netty.channel.socket.DatagramPacket in project camel by apache.

the class DatagramPacketEncoder method encode.

@Override
protected void encode(ChannelHandlerContext ctx, AddressedEnvelope<Object, InetSocketAddress> msg, List<Object> out) throws Exception {
    if (msg.content() instanceof ByteBuf) {
        ByteBuf payload = (ByteBuf) msg.content();
        // Just wrap the message as DatagramPacket, need to make sure the message content is ByteBuf
        DatagramPacket dp = new DatagramPacket(payload.retain(), msg.recipient());
        out.add(dp);
    }
}
Also used : DatagramPacket(io.netty.channel.socket.DatagramPacket) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

DatagramPacket (io.netty.channel.socket.DatagramPacket)85 InetSocketAddress (java.net.InetSocketAddress)48 ByteBuf (io.netty.buffer.ByteBuf)45 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)16 Test (org.junit.Test)15 Channel (io.netty.channel.Channel)9 DatagramChannel (io.netty.channel.socket.DatagramChannel)9 Test (org.junit.jupiter.api.Test)9 ChannelFuture (io.netty.channel.ChannelFuture)8 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 Bootstrap (io.netty.bootstrap.Bootstrap)7 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)7 NioDatagramChannel (io.netty.channel.socket.nio.NioDatagramChannel)6 InetAddress (java.net.InetAddress)6 ByteBuffer (java.nio.ByteBuffer)6 DefaultAddressedEnvelope (io.netty.channel.DefaultAddressedEnvelope)5 SimpleChannelInboundHandler (io.netty.channel.SimpleChannelInboundHandler)5 SocketAddress (java.net.SocketAddress)5 SocketException (java.net.SocketException)5