Search in sources :

Example 6 with DefaultAddressedEnvelope

use of io.netty.channel.DefaultAddressedEnvelope in project camel by apache.

the class DatagramPacketStringDecoder method decode.

@Override
protected void decode(ChannelHandlerContext ctx, AddressedEnvelope<Object, InetSocketAddress> msg, List<Object> out) throws Exception {
    if (msg.content() instanceof ByteBuf) {
        ByteBuf payload = (ByteBuf) msg.content();
        AddressedEnvelope<Object, InetSocketAddress> addressedEnvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>(payload.toString(charset), msg.recipient(), msg.sender());
        out.add(addressedEnvelop);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ByteBuf(io.netty.buffer.ByteBuf) DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope)

Example 7 with DefaultAddressedEnvelope

use of io.netty.channel.DefaultAddressedEnvelope 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 8 with DefaultAddressedEnvelope

use of io.netty.channel.DefaultAddressedEnvelope in project camel by apache.

the class NettyHelper method writeBodyAsync.

/**
     * Writes the given body to Netty channel. Will <b>not</b >wait until the body has been written.
     *
     * @param log             logger to use
     * @param channel         the Netty channel
     * @param remoteAddress   the remote address when using UDP
     * @param body            the body to write (send)
     * @param exchange        the exchange
     * @param listener        listener with work to be executed when the operation is complete
     */
public static void writeBodyAsync(Logger log, Channel channel, SocketAddress remoteAddress, Object body, Exchange exchange, ChannelFutureListener listener) {
    ChannelFuture future;
    if (remoteAddress != null) {
        if (log.isDebugEnabled()) {
            log.debug("Channel: {} remote address: {} writing body: {}", new Object[] { channel, remoteAddress, body });
        }
        // Need to create AddressedEnvelope to setup the address information here
        DefaultAddressedEnvelope<Object, InetSocketAddress> ae = new DefaultAddressedEnvelope<Object, InetSocketAddress>(body, (InetSocketAddress) remoteAddress);
        future = channel.writeAndFlush(ae);
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Channel: {} writing body: {}", new Object[] { channel, body });
        }
        // In netty4 we need to call channel flush to send out the message 
        future = channel.writeAndFlush(body);
    }
    if (listener != null) {
        future.addListener(listener);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope)

Example 9 with DefaultAddressedEnvelope

use of io.netty.channel.DefaultAddressedEnvelope in project camel by apache.

the class DatagramPacketByteArrayCodecTest method testDecoder.

@Test
public void testDecoder() {
    ByteBuf buf = Unpooled.buffer();
    buf.writeBytes(VALUE.getBytes());
    ByteBuf input = buf.duplicate();
    AddressedEnvelope<Object, InetSocketAddress> addressedEnvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>(input, new InetSocketAddress(8888));
    EmbeddedChannel channel = new EmbeddedChannel(ChannelHandlerFactories.newByteArrayDecoder("udp").newChannelHandler());
    Assert.assertTrue(channel.writeInbound(addressedEnvelop));
    Assert.assertTrue(channel.finish());
    AddressedEnvelope<Object, InetSocketAddress> result = (AddressedEnvelope) channel.readInbound();
    Assert.assertEquals(result.recipient().getPort(), addressedEnvelop.recipient().getPort());
    Assert.assertTrue(result.content() instanceof byte[]);
    Assert.assertEquals(VALUE, new String((byte[]) result.content()));
    Assert.assertNull(channel.readInbound());
}
Also used : DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope) AddressedEnvelope(io.netty.channel.AddressedEnvelope) InetSocketAddress(java.net.InetSocketAddress) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope) Test(org.junit.Test)

Example 10 with DefaultAddressedEnvelope

use of io.netty.channel.DefaultAddressedEnvelope in project camel by apache.

the class DatagramPacketByteArrayCodecTest method testEncoder.

@Test
public void testEncoder() {
    ByteBuf buf = Unpooled.buffer();
    buf.writeBytes(VALUE.getBytes());
    AddressedEnvelope<Object, InetSocketAddress> addressedEnvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>(VALUE.getBytes(), new InetSocketAddress(8888));
    EmbeddedChannel channel = new EmbeddedChannel(ChannelHandlerFactories.newByteArrayEncoder("udp").newChannelHandler());
    Assert.assertTrue(channel.writeOutbound(addressedEnvelop));
    Assert.assertTrue(channel.finish());
    AddressedEnvelope output = (AddressedEnvelope) channel.readOutbound();
    Assert.assertTrue(output.content() instanceof ByteBuf);
    ByteBuf resultContent = (ByteBuf) output.content();
    Assert.assertEquals(VALUE, new String(resultContent.array()));
    Assert.assertNull(channel.readOutbound());
}
Also used : DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope) AddressedEnvelope(io.netty.channel.AddressedEnvelope) InetSocketAddress(java.net.InetSocketAddress) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope) Test(org.junit.Test)

Aggregations

DefaultAddressedEnvelope (io.netty.channel.DefaultAddressedEnvelope)12 InetSocketAddress (java.net.InetSocketAddress)12 ByteBuf (io.netty.buffer.ByteBuf)9 AddressedEnvelope (io.netty.channel.AddressedEnvelope)4 Test (org.junit.Test)4 DatagramPacket (io.netty.channel.socket.DatagramPacket)3 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2 SocketAddress (java.net.SocketAddress)2 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)1 ChannelFuture (io.netty.channel.ChannelFuture)1 DatagramSocketAddress (io.netty.channel.unix.DatagramSocketAddress)1 Serializable (java.io.Serializable)1