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) 

Source Link

Document

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

Usage

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

License:Apache License

@Test
public void testDecoder() {
    ByteBuf buf = Unpooled.buffer();/*from www .  j ava  2s  . co m*/
    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:org.apache.camel.component.netty4.DatagramPacketByteArrayCodecTest.java

License:Apache License

@Test
public void testEncoder() {
    ByteBuf buf = Unpooled.buffer();/*from   w w  w  .j a va  2 s. c o  m*/
    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());
}

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

License:Apache License

/**
 * Writes the given body to Netty channel. Will <b>not</b >wait until the body has been written.
 *
 * @param log             logger to use//from  w  ww .  j  a  va2  s .  c om
 * @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);
    }
}

From source file:org.restcomm.media.network.netty.channel.AsyncNettyNetworkChannel.java

License:Open Source License

@Override
public void send(M message, SocketAddress remoteAddress, FutureCallback<Void> callback) {
    if (isBound()) {
        if (isConnected()) {
            callback.onFailure(//from  ww  w.  j  a va  2  s  .  com
                    new IllegalStateException("Channel is connected. Cannot send traffic to another peer."));
        } else {
            DefaultAddressedEnvelope<M, SocketAddress> envelope = new DefaultAddressedEnvelope<>(message,
                    remoteAddress);
            final ChannelFuture future = this.context.getChannel().writeAndFlush(envelope);
            future.addListener(new NettyNetworkChannelVoidCallbackListener(callback));
        }
    } else {
        callback.onFailure(new IllegalStateException("Channel is not bound."));
    }
}

From source file:sas.systems.imflux.session.rtp.AbstractRtpSession.java

License:Apache License

/**
 * Writes the packets information to the data channel
 * /* w  ww  .j a  v  a 2  s .c  o m*/
 * @param packet
 * @param destination
 */
protected void writeToData(DataPacket packet, SocketAddress destination) {
    final AddressedEnvelope<DataPacket, SocketAddress> envelope = new DefaultAddressedEnvelope<>(packet,
            destination);
    this.dataChannel.writeAndFlush(envelope);
}

From source file:sas.systems.imflux.session.rtp.AbstractRtpSession.java

License:Apache License

/**
 * Write the packets information to the control channel
 * //from   w  w  w  . j  a va  2s  . c  om
 * @param packet
 * @param destination
 */
protected void writeToControl(ControlPacket packet, SocketAddress destination) {
    // FIXME: does not work currently -> add new encoder for ControlPackets wrapped into Envelopes
    final AddressedEnvelope<ControlPacket, SocketAddress> envelope = new DefaultAddressedEnvelope<>(packet,
            destination);
    this.controlChannel.writeAndFlush(envelope);
}

From source file:sas.systems.imflux.session.rtp.AbstractRtpSession.java

License:Apache License

/**
 * Write the packets information to the control channel
 * //from  w  w w  .ja va2 s  . c  om
 * @param packet
 * @param destination
 */
protected void writeToControl(CompoundControlPacket packet, SocketAddress destination) {
    final AddressedEnvelope<CompoundControlPacket, SocketAddress> envelope = new DefaultAddressedEnvelope<>(
            packet, destination);
    this.controlChannel.writeAndFlush(envelope);
}