List of usage examples for io.netty.channel AddressedEnvelope content
M content();
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 . jav a 2 s .c om } }
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; }//ww w . ja v a 2 s . co m 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();// w ww . j a v a2 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:org.apache.camel.component.netty4.DatagramPacketByteArrayCodecTest.java
License:Apache License
@Test public void testEncoder() { ByteBuf buf = Unpooled.buffer();//from w w w . j a v a 2s . co 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.NettyPayloadHelper.java
License:Apache License
public static void setIn(Exchange exchange, Object payload) { if (payload instanceof DefaultExchangeHolder) { DefaultExchangeHolder.unmarshal(exchange, (DefaultExchangeHolder) payload); } else if (payload instanceof AddressedEnvelope) { @SuppressWarnings("unchecked") AddressedEnvelope<Object, InetSocketAddress> dp = (AddressedEnvelope<Object, InetSocketAddress>) payload; // need to check if the content is ExchangeHolder if (dp.content() instanceof DefaultExchangeHolder) { DefaultExchangeHolder.unmarshal(exchange, (DefaultExchangeHolder) dp.content()); } else {//from w w w . j ava 2 s .c o m // need to take out the payload here exchange.getIn().setBody(dp.content()); } // setup the sender address here for sending the response message back exchange.setProperty(NettyConstants.NETTY_REMOTE_ADDRESS, dp.sender()); } else { // normal transfer using the body only exchange.getIn().setBody(payload); } }
From source file:org.apache.camel.component.netty4.NettyPayloadHelper.java
License:Apache License
public static void setOut(Exchange exchange, Object payload) { if (payload instanceof DefaultExchangeHolder) { DefaultExchangeHolder.unmarshal(exchange, (DefaultExchangeHolder) payload); } else if (payload instanceof AddressedEnvelope) { @SuppressWarnings("unchecked") AddressedEnvelope<Object, InetSocketAddress> dp = (AddressedEnvelope<Object, InetSocketAddress>) payload; // need to check if the content is ExchangeHolder if (dp.content() instanceof DefaultExchangeHolder) { DefaultExchangeHolder.unmarshal(exchange, (DefaultExchangeHolder) dp.content()); } else {/*from w ww .j av a 2 s . c om*/ // need to take out the payload here exchange.getOut().setBody(dp.content()); } // setup the sender address here for sending the response message back exchange.setProperty(NettyConstants.NETTY_REMOTE_ADDRESS, dp.sender()); } else { // normal transfer using the body only and preserve the headers exchange.getOut().setHeaders(exchange.getIn().getHeaders()); exchange.getOut().setBody(payload); } }
From source file:sas.systems.imflux.network.udp.UdpControlHandler.java
License:Apache License
/** * To be compatible to io.Netty version 5.0: * {@code channelRead0(ChannelHandlerContext, I)} will be renamed to {@code messageReceived(ChannelHandlerContext, I)} in 5.0. * /*from ww w . j av a 2s . c o m*/ * @param ctx the {@link ChannelHandlerContext} which this {@link SimpleChannelInboundHandler}/ * {@link UdpDataHandler} belongs to * @param msg the message to handle * @throws Exception is thrown if an error occurred */ //@Override protected void messageReceived(ChannelHandlerContext ctx, AddressedEnvelope<CompoundControlPacket, SocketAddress> msg) throws Exception { final CompoundControlPacket packet = msg.content(); final SocketAddress sender = msg.sender(); this.receiver.controlPacketReceived(sender, packet); }
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}. /*ww w . j a va 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 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); }
From source file:sas.systems.imflux.network.udp.UdpDataHandler.java
License:Apache License
/** * To be compatible to io.Netty version 5.0: * {@code channelRead0(ChannelHandlerContext, I)} will be renamed to {@code messageReceived(ChannelHandlerContext, I)} in 5.0. * //from w ww. jav a 2 s . c o m * @param ctx the {@link ChannelHandlerContext} which this {@link SimpleChannelInboundHandler}/ * {@link UdpDataHandler} belongs to * @param msg the message to handle * @throws Exception is thrown if an error occurred */ //@Override protected void messageReceived(ChannelHandlerContext ctx, AddressedEnvelope<DataPacket, SocketAddress> msg) throws Exception { final DataPacket packet = msg.content(); final SocketAddress sender = msg.sender(); this.receiver.dataPacketReceived(sender, packet); }
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 w w . 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 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); }