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:org.codice.alliance.video.stream.mpegts.netty.RawUdpDataToMTSPacketDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, DatagramPacket msg, List<Object> outputList) throws Exception {

    notNull(ctx, "ctx must be non-null");
    notNull(msg, "msg must be non-null");
    notNull(outputList, "outputList must be non-null");

    LOCK.lock();//from  w w w .  j av a  2s  .co m
    try {
        checkSecuritySubject(msg);

        byteBuf.writeBytes(msg.content());

        skipToSyncByte();

        while (byteBuf.readableBytes() >= TS_PACKET_SIZE) {
            parseMpegTsPacket(outputList);
        }

        byteBuf.discardReadBytes();
    } finally {
        LOCK.unlock();
    }

}

From source file:org.graylog2.gelfclient.encoder.GelfMessageUdpEncoderTest.java

License:Apache License

@Test
public void testEncode() throws Exception {
    InetSocketAddress remoteAddress = new InetSocketAddress("127.0.0.1", 12201);
    EmbeddedChannel channel = new EmbeddedChannel(new GelfMessageUdpEncoder(remoteAddress));

    // Test writing.
    assertTrue(channel.writeOutbound(Unpooled.wrappedBuffer("test".getBytes())));
    assertTrue(channel.finish());/*from   w  w  w  .  j  a  v a2s.c o m*/

    // Test reading.
    DatagramPacket datagramPacket = (DatagramPacket) channel.readOutbound();

    byte[] bytes = new byte[datagramPacket.content().readableBytes()];

    datagramPacket.content().getBytes(0, bytes);

    assertEquals(remoteAddress, datagramPacket.recipient());
    assertEquals("test", new String(bytes));
}

From source file:org.graylog2.inputs.transports.netty.DatagramPacketHandler.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, DatagramPacket msg, List<Object> out) throws Exception {
    final ByteBuf content = msg.content();
    out.add(ReferenceCountUtil.retain(content));
}

From source file:org.hawkular.metrics.clients.ptrans.collectd.packet.CollectdPacketDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext context, DatagramPacket packet, List<Object> out) throws Exception {
    long start = System.currentTimeMillis();
    ByteBuf content = packet.content();
    List<Part> parts = new ArrayList<>(100);
    for (;;) {/* w  ww  .ja va2s . co  m*/
        if (!hasReadableBytes(content, 4)) {
            break;
        }
        short partTypeId = content.readShort();
        PartType partType = PartType.findById(partTypeId);
        int partLength = content.readUnsignedShort();
        int valueLength = partLength - 4;
        if (!hasReadableBytes(content, valueLength)) {
            break;
        }
        if (partType == null) {
            content.skipBytes(valueLength);
            continue;
        }
        Part part;
        switch (partType) {
        case HOST:
        case PLUGIN:
        case PLUGIN_INSTANCE:
        case TYPE:
        case INSTANCE:
            part = new StringPart(partType, readStringPartContent(content, valueLength));
            break;
        case TIME:
        case TIME_HIGH_RESOLUTION:
        case INTERVAL:
        case INTERVAL_HIGH_RESOLUTION:
            part = new NumericPart(partType, readNumericPartContent(content));
            break;
        case VALUES:
            part = new ValuePart(partType, readValuePartContent(content, valueLength));
            break;
        default:
            part = null;
            content.skipBytes(valueLength);
        }
        //noinspection ConstantConditions
        if (part != null) {
            logger.trace("Decoded part: {}", part);
            parts.add(part);
        }
    }

    if (logger.isTraceEnabled()) {
        long stop = System.currentTimeMillis();
        logger.trace("Decoded datagram in {} ms", stop - start);
    }

    if (parts.size() > 0) {
        CollectdPacket collectdPacket = new CollectdPacket(parts);
        out.add(collectdPacket);
    } else {
        logger.debug("No parts decoded, no CollectdPacket output");
    }
}

From source file:org.hawkular.metrics.clients.ptrans.ganglia.UdpGangliaDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, DatagramPacket in, List<Object> out) throws Exception {
    ByteBuf msg = in.content();
    if (msg.readableBytes() < 5) {
        msg.clear();//from www.j a v a 2  s . c om
        ctx.close();
        return;
    }

    short magic = msg.getUnsignedByte(3);
    if (msg.getByte(0) == 0 && msg.getByte(1) == 0 && msg.getByte(2) == 0 && magic == 134) {

        // We have an UnsafeSuperDuperBuffer, so we need to "manually" pull the bytes from it.
        byte[] bytes = new byte[msg.readableBytes()];
        msg.readBytes(bytes);

        XdrBufferDecodingStream stream = new XdrBufferDecodingStream(bytes);
        stream.beginDecoding();
        stream.xdrDecodeInt(); // Packet id , should be 134 as in above magic => type of value
        String host = stream.xdrDecodeString();
        String metricName = stream.xdrDecodeString();
        stream.xdrDecodeInt();
        String format = stream.xdrDecodeString(); // e.g. .0f for a number
        String value;
        if (format.endsWith("f")) {
            value = String.valueOf(stream.xdrDecodeFloat());
        } else {
            value = stream.xdrDecodeString();
        }
        stream.endDecoding();

        try {
            String path = host + "." + metricName;
            Double val = Double.parseDouble(value);

            SingleMetric metric = new SingleMetric(path, System.currentTimeMillis(), val);
            out.add(metric);
        } catch (Exception e) {
            e.printStackTrace();
            msg.clear();
            ctx.close();

        }
    }

}

From source file:org.hawkular.metrics.clients.ptrans.statsd.StatsdDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, DatagramPacket msg, List<Object> out) throws Exception {
    ByteBuf buf = msg.content();

    if (buf.readableBytes() < 3) {
        // Not enough data - nothing to do.
        buf.clear();/*from w  w  w.j a  va  2 s .c  om*/
        ctx.close();
        return;
    }

    String packet = buf.toString(CharsetUtil.UTF_8).trim();
    if (!packet.contains(":")) {
        buf.clear();
        ctx.close();
        return;
    }

    String name = packet.substring(0, packet.indexOf(":"));
    String remainder = packet.substring(packet.indexOf(":") + 1);
    String valString;
    String type;
    if (remainder.contains("|")) {
        valString = remainder.substring(0, remainder.indexOf("|"));
        type = remainder.substring(remainder.indexOf("|") + 1);
    } else {
        valString = remainder;
        type = "";
    }
    Double value = Double.valueOf(valString);
    SingleMetric singleMetric = new SingleMetric(name, System.currentTimeMillis(), value,
            MetricType.from(type));
    out.add(singleMetric);
}

From source file:org.hawkular.metrics.clients.ptrans.syslog.UdpSyslogEventDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, DatagramPacket msg, List<Object> out) throws Exception {

    ByteBuf buf = msg.content();

    DecoderUtil.decodeTheBuffer(buf, out);
}

From source file:org.kaazing.messaging.driver.transport.netty.udp.NettySendingTransport.java

License:Apache License

@Override
public void submit(DriverMessage driverMessage) {
    DatagramPacket nettyMessage = tlNettyMessage.get();
    nettyMessage.content().retain();

    //TODO(JAF): Avoid making a new byte array with each send
    byte[] bytesToSend = new byte[driverMessage.getBufferLength()];
    driverMessage.getBuffer().getBytes(driverMessage.getBufferOffset(), bytesToSend);
    nettyMessage.content().setBytes(0, bytesToSend);
    nettyMessage.content().writerIndex(bytesToSend.length);

    ChannelFuture future = sendingChannel.writeAndFlush(nettyMessage);
    try {/*w w w .jav  a 2  s .  com*/
        if (future.sync().await(1000) == false) {
            LOGGER.debug("Timed out sending message");
        }

    } catch (InterruptedException e) {
        LOGGER.debug("Interrupted while sending message");
    }
}

From source file:org.lanternpowered.server.network.query.QueryHandler.java

License:MIT License

@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
    final ByteBuf buf = msg.content();
    if (buf.readableBytes() < 7) {
        return;/*w w w .  j av  a  2  s. c om*/
    }

    final int magic = buf.readUnsignedShort();
    final byte type = buf.readByte();
    final int sessionId = buf.readInt();

    if (magic != 0xFEFD) {
        return;
    }

    if (type == ACTION_HANDSHAKE) {
        handleHandshake(ctx, msg, sessionId);
    } else if (type == ACTION_STATS) {
        if (buf.readableBytes() < 4) {
            return;
        }
        final int token = buf.readInt();
        if (this.queryServer.verifyChallengeToken(msg.sender(), token)) {
            if (buf.readableBytes() == 4) {
                this.handleFullStats(ctx, msg, sessionId);
            } else {
                this.handleBasicStats(ctx, msg, sessionId);
            }
        }
    }
}

From source file:org.loggo.server.DgramHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
    String msg = packet.content().toString(UTF_8);
    logger.channelRead0(ctx, msg);//from  www.  ja  v  a2 s .c  om
}