Example usage for io.netty.channel AddressedEnvelope recipient

List of usage examples for io.netty.channel AddressedEnvelope recipient

Introduction

In this page you can find the example usage for io.netty.channel AddressedEnvelope recipient.

Prototype

A recipient();

Source Link

Document

Returns the address of the recipient of this message.

Usage

From source file:org.apache.camel.component.netty4.codec.DatagramPacketByteArrayDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, AddressedEnvelope<Object, InetSocketAddress> msg,
        List<Object> out) throws Exception {
    if (msg.content() instanceof ByteBuf) {
        delegateDecoder.decode(ctx, (ByteBuf) msg.content(), out);
        byte[] content = (byte[]) out.remove(out.size() - 1);
        AddressedEnvelope<Object, InetSocketAddress> addressedEnvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>(
                content, msg.recipient(), msg.sender());
        out.add(addressedEnvelop);/* w ww  . j a v a  2s . c o m*/
    }
}

From source file:org.apache.camel.component.netty4.codec.DatagramPacketByteArrayEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, AddressedEnvelope<Object, InetSocketAddress> msg,
        List<Object> out) throws Exception {
    if (msg.content() instanceof byte[]) {
        delegateEncoder.encode(ctx, (byte[]) msg.content(), out);
        ByteBuf buf = (ByteBuf) out.remove(out.size() - 1);
        AddressedEnvelope<Object, InetSocketAddress> addressedEnvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>(
                buf.retain(), msg.recipient(), msg.sender());
        out.add(addressedEnvelop);/*from   w w w.j  a v  a  2  s  .  co m*/
    }
}

From source file:org.apache.camel.component.netty4.codec.DatagramPacketDelimiterDecoder.java

License:Apache License

@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();
        Object result = delegateDecoder.decode(ctx, payload);
        AddressedEnvelope<Object, InetSocketAddress> addressEvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>(
                result, msg.recipient(), msg.sender());
        out.add(addressEvelop);//ww  w .  j a  v  a  2 s. c o  m
    }

}

From source file:org.apache.camel.component.netty4.codec.DatagramPacketEncoder.java

License:Apache License

@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);/*from  w  w  w.ja va  2 s  .com*/
    }
}

From source file:org.apache.camel.component.netty4.codec.DatagramPacketObjectDecoder.java

License:Apache License

@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();
        Object result = delegateDecoder.decode(ctx, payload);
        AddressedEnvelope<Object, InetSocketAddress> addressedEnvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>(
                result, msg.recipient(), msg.sender());
        out.add(addressedEnvelop);//from   ww w  .jav a2s  . c o m
    }
}

From source file:org.apache.camel.component.netty4.codec.DatagramPacketObjectEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, AddressedEnvelope<Object, InetSocketAddress> msg,
        List<Object> out) throws Exception {
    if (msg.content() instanceof Serializable) {
        Serializable payload = (Serializable) msg.content();
        ByteBuf buf = ctx.alloc().heapBuffer();
        delegateObjectEncoder.encode(ctx, payload, buf);
        AddressedEnvelope<Object, InetSocketAddress> addressedEnvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>(
                buf.retain(), msg.recipient(), msg.sender());
        out.add(addressedEnvelop);/*from w  w  w.j  a  v a 2s. c o  m*/
    }

}

From source file:org.apache.camel.component.netty4.codec.DatagramPacketStringDecoder.java

License:Apache License

@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);/* w  w  w .j  a va2 s  . com*/
    }
}

From source file:org.apache.camel.component.netty4.codec.DatagramPacketStringEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, AddressedEnvelope<Object, InetSocketAddress> msg,
        List<Object> out) throws Exception {
    if (msg.content() instanceof CharSequence) {
        CharSequence payload = (CharSequence) msg.content();
        if (payload.length() == 0) {
            return;
        }/*from  www. j  a v  a  2s . c  om*/
        AddressedEnvelope<Object, InetSocketAddress> addressedEnvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>(
                ByteBufUtil.encodeString(ctx.alloc(), CharBuffer.wrap(payload), charset), msg.recipient(),
                msg.sender());
        out.add(addressedEnvelop);
    }
}

From source file:org.apache.camel.component.netty4.DatagramPacketByteArrayCodecTest.java

License:Apache License

@Test
public void testDecoder() {
    ByteBuf buf = Unpooled.buffer();//from  w  w w  .j av a 2 s.  com
    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());
}

From source file:sas.systems.imflux.network.udp.UdpControlPacketEncoder.java

License:Apache License

/**
 * Encodes a {@link CompoundControlPacket} wrapped into an {@link AddressedEnvelope} to a {@link ByteBuf} also wrapped
 * into an {@link AddressedEnvelope}. /*www  . j  av  a2  s .  c  o  m*/
 * 
 * @param ctx The context of the ChannelHandler
 * @param message the message which should be encoded
 * @param out a list where all messages are written to
 */
@Override
protected void encode(ChannelHandlerContext ctx, AddressedEnvelope<CompoundControlPacket, SocketAddress> msg,
        List<Object> out) throws Exception {
    // encode CompountControlPacket here and forward destination (recipient) of the packet
    final CompoundControlPacket compoundControlPacket = msg.content();
    final List<ControlPacket> packets = compoundControlPacket.getControlPackets();
    ByteBuf compoundBuffer = Unpooled.EMPTY_BUFFER;
    if (packets.isEmpty()) {
        final ByteBuf[] buffers = new ByteBuf[packets.size()];
        for (int i = 0; i < buffers.length; i++) {
            buffers[i] = packets.get(i).encode();
        }
        compoundBuffer = Unpooled.wrappedBuffer(buffers);
    }

    AddressedEnvelope<ByteBuf, SocketAddress> newMsg = new DefaultAddressedEnvelope<ByteBuf, SocketAddress>(
            compoundBuffer, msg.recipient(), ctx.channel().localAddress());
    out.add(newMsg);
}