Example usage for io.netty.channel.socket DatagramPacket content

List of usage examples for io.netty.channel.socket DatagramPacket content

Introduction

In this page you can find the example usage for io.netty.channel.socket DatagramPacket content.

Prototype

ByteBuf content();

Source Link

Document

Return the data which is held by this ByteBufHolder .

Usage

From source file:groovyx.gpars.remote.netty.discovery.DiscoveryRequestDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, DatagramPacket msg, List<Object> out) throws Exception {
    String actorUrl = msg.content().toString(CharsetUtil.UTF_8);
    DiscoveryRequest request = new DiscoveryRequest(actorUrl);
    DiscoveryRequestWithSender requestWithSender = new DiscoveryRequestWithSender(request, msg.sender());
    out.add(requestWithSender);//from   ww w  .j  a  v a  2s.  c o  m
}

From source file:groovyx.gpars.remote.netty.discovery.DiscoveryResponseDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, DatagramPacket msg, List<Object> out) throws Exception {
    ByteBuf buf = msg.content();
    int serverPort = buf.readInt();
    String actorUrl = buf.toString(CharsetUtil.UTF_8);

    InetSocketAddress sender = msg.sender();
    InetSocketAddress serverSocketAddress = new InetSocketAddress(sender.getAddress(), serverPort);

    DiscoveryResponse response = new DiscoveryResponse(actorUrl, serverSocketAddress);
    out.add(response);/*from w  w w  .j  ava2 s.  c  o  m*/
}

From source file:hms.webrtc.udp.proxy.remote.RemoteConfiguraitonClient.java

License:Apache License

public static void main(String[] args) {
    EventLoopGroup group = new NioEventLoopGroup();
    try {/*w w  w . j av  a 2  s.  c  o m*/
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioDatagramChannel.class)
                .handler(new SimpleChannelInboundHandler<DatagramPacket>() {
                    @Override
                    protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg)
                            throws Exception {
                        if ("SUCCESS".equals(msg.content().toString(CharsetUtil.UTF_8))) {
                            System.out.println("Sending configuration is success.");
                        }
                        ctx.close();
                    }
                });

        ch = b.bind(34001).sync().channel();

        ch.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("78737383:40000:40001", CharsetUtil.UTF_8),
                new InetSocketAddress("127.0.0.1", PORT))).sync();

        if (!ch.closeFuture().await(5000)) {
            Assert.fail("Rtp communication timeout");
        } else {
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        group.shutdownGracefully();
    }
}

From source file:hms.webrtc.udp.proxy.RtpPartyAHandler.java

License:Apache License

@Override
public void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
    System.out.println(msg.content());
    ctx.close();/*from  w  ww  . j a  v  a  2  s. co  m*/
}

From source file:io.advantageous.conekt.datagram.impl.DatagramServerHandler.java

License:Open Source License

@Override
protected Object safeObject(Object msg, ByteBufAllocator allocator) throws Exception {
    if (msg instanceof DatagramPacket) {
        DatagramPacket packet = (DatagramPacket) msg;
        ByteBuf content = packet.content();
        if (content.isDirect()) {
            content = safeBuffer(content, allocator);
        }/*  w w  w .  j a va2s .c  o  m*/
        return new DatagramPacketImpl(packet.sender(), Buffer.buffer(content));
    }
    return msg;
}

From source file:io.advantageous.conekt.dns.impl.netty.DnsResponseDecoder.java

License:Open Source License

/**
 * Decodes a response from a {@link io.netty.channel.socket.DatagramPacket} containing a
 * {@link io.netty.buffer.ByteBuf} with a DNS packet. Responses are sent from a DNS server
 * to a client in response to a query. This method writes the decoded
 * response to the specified {@link java.util.List} to be handled by a specialized
 * message handler.//from   w ww.  ja va 2  s  .c o m
 *
 * @param ctx    the {@link io.netty.channel.ChannelHandlerContext} this
 *               {@link DnsResponseDecoder} belongs to
 * @param packet the message being decoded, a {@link io.netty.channel.socket.DatagramPacket} containing
 *               a DNS packet
 * @param out    the {@link java.util.List} to which decoded messages should be added
 * @throws Exception
 */
@Override
protected void decode(ChannelHandlerContext ctx, DatagramPacket packet, List<Object> out) throws Exception {
    out.add(decodeResponse(packet.content(), ctx.alloc()).retain());
}

From source file:io.aos.netty5.qotm.QuoteOfTheMomentClientHandler.java

License:Apache License

@Override
public void messageReceived(ChannelHandlerContext ctx, DatagramPacket msg) {
    String response = msg.content().toString(CharsetUtil.UTF_8);
    if (response.startsWith("QOTM: ")) {
        System.out.println("Quote of the Moment: " + response.substring(6));
        ctx.close();/*from   w  w w  .  j a v  a2 s  . c o m*/
    }
}

From source file:io.aos.netty5.qotm.QuoteOfTheMomentServerHandler.java

License:Apache License

@Override
public void messageReceived(ChannelHandlerContext ctx, DatagramPacket packet) {
    System.err.println(packet);/*from  ww w  .j a  va2  s .c  o  m*/
    if ("QOTM?".equals(packet.content().toString(CharsetUtil.UTF_8))) {
        ctx.write(new DatagramPacket(Unpooled.copiedBuffer("QOTM: " + nextQuote(), CharsetUtil.UTF_8),
                packet.sender()));
    }
}

From source file:io.atomix.cluster.messaging.impl.NettyBroadcastService.java

License:Apache License

private CompletableFuture<Void> bootstrapClient() {
    Bootstrap clientBootstrap = new Bootstrap().group(group)
            .channelFactory(() -> new NioDatagramChannel(InternetProtocolFamily.IPv4))
            .handler(new SimpleChannelInboundHandler<DatagramPacket>() {
                @Override/*from   w w  w .  ja  v a 2 s.c om*/
                protected void channelRead0(ChannelHandlerContext context, DatagramPacket packet)
                        throws Exception {
                    byte[] payload = new byte[packet.content().readInt()];
                    packet.content().readBytes(payload);
                    Message message = SERIALIZER.decode(payload);
                    Set<Consumer<byte[]>> listeners = NettyBroadcastService.this.listeners
                            .get(message.subject());
                    if (listeners != null) {
                        for (Consumer<byte[]> listener : listeners) {
                            listener.accept(message.payload());
                        }
                    }
                }
            }).option(ChannelOption.IP_MULTICAST_IF, iface).option(ChannelOption.SO_REUSEADDR, true)
            .localAddress(localAddress.getPort());

    CompletableFuture<Void> future = new CompletableFuture<>();
    clientBootstrap.bind().addListener((ChannelFutureListener) f -> {
        if (f.isSuccess()) {
            clientChannel = (DatagramChannel) f.channel();
            log.info("{} joining multicast group {} on port {}", localAddress.getHostName(),
                    groupAddress.getHostName(), groupAddress.getPort());
            clientChannel.joinGroup(groupAddress, iface).addListener(f2 -> {
                if (f2.isSuccess()) {
                    log.info("{} successfully joined multicast group {} on port {}", localAddress.getHostName(),
                            groupAddress.getHostName(), groupAddress.getPort());
                    future.complete(null);
                } else {
                    log.info("{} failed to join group {} on port {}", localAddress.getHostName(),
                            groupAddress.getHostName(), groupAddress.getPort());
                    future.completeExceptionally(f2.cause());
                }
            });
        } else {
            future.completeExceptionally(f.cause());
        }
    });
    return future;
}

From source file:io.haze.transport.udp.decoder.InOrderReliableUDPDecoder.java

License:Apache License

/**
 * Handle an incoming {@link DatagramPacket}.
 *
 * @param client   The transport client.
 * @param packet   The datagram packet.//from  w w w . j  a va2 s.  c  o  m
 * @param messages The output messages.
 *
 * @throws Exception If an error has occurred.
 */
@Override
public void handlePacket(UDPTransportClient client, DatagramPacket packet, List<Object> messages)
        throws Exception {
    messages.add(new MessageContext(client, new BufferMessage(packet.content().retain())));
}