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:sas.systems.imflux.network.udp.UdpDataPacketEncoder.java

License:Apache License

/**
 * Encodes a {@link DataPacket} wrapped into an {@link AddressedEnvelope} in a {@link ByteBuf} also wrapped into an 
 * {@link AddressedEnvelope}. If the {@link DataPacket}'s content is not empty it is added, otherwise an empty ByteBuf 
 * is added to the AddressedEnvelope./*from  w ww .j a v a  2s.  c  om*/
 * 
 * @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<DataPacket, SocketAddress> msg,
        List<Object> out) throws Exception {
    // encode CompountControlPacket here and forward destination (recipient) of the packet
    final DataPacket dataPacket = msg.content();
    final SocketAddress recipient = msg.recipient();
    final SocketAddress sender = ctx.channel().localAddress();

    final ByteBuf buffer;
    if (dataPacket.getDataSize() == 0) {
        buffer = Unpooled.EMPTY_BUFFER;
    } else {
        buffer = dataPacket.encode();
    }

    final AddressedEnvelope<ByteBuf, SocketAddress> newMsg = new DefaultAddressedEnvelope<ByteBuf, SocketAddress>(
            buffer, recipient, sender);
    out.add(newMsg);
}