Example usage for io.netty.channel DefaultAddressedEnvelope DefaultAddressedEnvelope

List of usage examples for io.netty.channel DefaultAddressedEnvelope DefaultAddressedEnvelope

Introduction

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

Prototype

public DefaultAddressedEnvelope(M message, A recipient, A sender) 

Source Link

Document

Creates a new instance with the specified message , recipient address, and sender address.

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);/*from  w w w . j av a  2s  .  com*/
    }
}

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);//  w  ww.java 2  s . com
    }
}

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

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, DatagramPacket msg, List<Object> out) throws Exception {
    // decode the DatagramPackage to AddressedEnvelope
    DefaultAddressedEnvelope<Object, InetSocketAddress> addressEvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>(
            msg.content().retain(), msg.recipient(), msg.sender());
    out.add(addressEvelop);/*  w  w w.  j a  v  a2  s  .  c  o 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);/* w  w w.  jav a 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   w ww. ja v a 2s  . 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);/*  w  w  w  .jav  a  2  s  .  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);//from w  w  w .j a va2 s . c  o  m
    }
}

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;
        }/* w  w w  . j  a v a2  s.  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:sas.systems.imflux.network.udp.UdpControlPacketDecoder.java

License:Apache License

/**
 * Decodes a {@link DatagramPacket} to a {@link CompoundControlPacket} wrapped into an {@link AddressedEnvelope}.
 * //from  w  ww.  j a  v  a  2 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 decode(ChannelHandlerContext ctx, DatagramPacket msg, List<Object> out) throws Exception {
    final ByteBuf content = msg.content();
    final SocketAddress sender = msg.sender();
    final SocketAddress recipient = msg.recipient();

    if ((content.readableBytes() % 4) != 0) {
        LOG.debug("Invalid RTCP packet received: total length should be multiple of 4 but is {}",
                content.readableBytes());
        return;
    }

    // Usually 2 packets per UDP frame...
    final List<ControlPacket> controlPacketList = new ArrayList<>(2);

    // While there's data to read, keep on decoding.
    while (content.readableBytes() > 0) {
        try {
            // prevent adding null
            final ControlPacket packet = ControlPacket.decode(content);
            if (packet != null) {
                controlPacketList.add(packet);
            }
        } catch (Exception e1) {
            LOG.debug("Exception caught while decoding RTCP packet.", e1);
            break;
        }
    }

    if (!controlPacketList.isEmpty()) {
        // Only forward to next ChannelHandler when there were more than one valid decoded packets.
        // TODO shouldn't the whole compound packet be discarded when one of them has errors?!
        final AddressedEnvelope<CompoundControlPacket, SocketAddress> newMsg = new DefaultAddressedEnvelope<>(
                new CompoundControlPacket(controlPacketList), recipient, sender);
        out.add(newMsg);
    }
}

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