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);
}
}
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();
}
}
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);
}
}
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());
}
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());
}
Aggregations