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:com.mpush.netty.codec.PacketDecoder.java

License:Apache License

public static Packet decodeFrame(DatagramPacket frame) {
    ByteBuf in = frame.content();
    int readableBytes = in.readableBytes();
    int bodyLength = in.readInt();
    if (readableBytes < (bodyLength + Packet.HEADER_LEN)) {
        return null;
    }/*from  w  ww .jav  a 2  s  . c  o  m*/

    return decodePacket(new UDPPacket(in.readByte(), frame.sender()), in, bodyLength);
}

From source file:com.netflix.client.netty.udp.UdpClientTest.java

License:Apache License

@Test
public void testUdpClientWithoutTimeout() throws Exception {
    int port = choosePort();
    UdpServer<DatagramPacket, DatagramPacket> server = new HelloUdpServer(port, 0).createServer();
    server.start();//  www  . j  a  v  a  2  s . c  o  m
    BaseLoadBalancer lb = new BaseLoadBalancer();
    lb.setServersList(Lists.newArrayList(new Server("localhost", port)));
    RxClient<DatagramPacket, DatagramPacket> client = RibbonTransport.newUdpClient(lb,
            DefaultClientConfigImpl.getClientConfigWithDefaultValues());
    try {
        String response = client.connect().flatMap(
                new Func1<ObservableConnection<DatagramPacket, DatagramPacket>, Observable<DatagramPacket>>() {
                    @Override
                    public Observable<DatagramPacket> call(
                            ObservableConnection<DatagramPacket, DatagramPacket> connection) {
                        connection.writeStringAndFlush("Is there anybody out there?");
                        return connection.getInput();
                    }
                }).take(1).map(new Func1<DatagramPacket, String>() {
                    @Override
                    public String call(DatagramPacket datagramPacket) {
                        return datagramPacket.content().toString(Charset.defaultCharset());
                    }
                }).toBlocking().first();
        assertEquals(HelloUdpServer.WELCOME_MSG, response);
    } finally {
        server.shutdown();
    }
}

From source file:com.netflix.client.netty.udp.UdpClientTest.java

License:Apache License

@Test
public void testUdpClientTimeout() throws Exception {
    int port = choosePort();
    UdpServer<DatagramPacket, DatagramPacket> server = new HelloUdpServer(port, 5000).createServer();
    server.start();/*from  ww w.  j ava2  s .  c om*/
    BaseLoadBalancer lb = new BaseLoadBalancer();
    Server myServer = new Server("localhost", port);
    lb.setServersList(Lists.newArrayList(myServer));
    MyUDPClient client = new MyUDPClient(lb, DefaultClientConfigImpl.getClientConfigWithDefaultValues());
    try {
        String response = client.submit("Is there anybody out there?").map(new Func1<DatagramPacket, String>() {
            @Override
            public String call(DatagramPacket datagramPacket) {
                return datagramPacket.content().toString(Charset.defaultCharset());
            }
        }).toBlocking().first();
        fail("Exception expected");
    } catch (Exception e) {
        assertTrue(e.getCause() instanceof TimeoutException);
        assertEquals(1,
                client.getLoadBalancerContext().getServerStats(myServer).getSuccessiveConnectionFailureCount());
    } finally {
        server.shutdown();
    }
}

From source file:com.spotify.ffwd.netty.DatagramPacketToByteBuf.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, DatagramPacket packet, List<Object> out) throws Exception {
    final ByteBuf buf = packet.content();
    buf.retain();/*  ww w .  j a  v a 2s.  c  o  m*/
    out.add(buf);
}

From source file:com.spotify.ffwd.riemann.RiemannDatagramDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, DatagramPacket packet, List<Object> out) throws Exception {
    out.add(serializer.parse0(packet.content()));
}

From source file:com.spotify.heroic.consumer.collectd.CollectdChannelHandler.java

License:Apache License

@Override
protected void channelRead0(final ChannelHandlerContext ctx, final DatagramPacket msg) throws Exception {
    final Iterator<CollectdSample> samples = CollectdParser.parse(msg.content());

    while (samples.hasNext()) {
        final CollectdSample s = samples.next();

        final Set<Map.Entry<String, String>> base = ImmutableMap
                .of("host", s.getHost(), "plugin", s.getPlugin()).entrySet();

        final List<Ingestion.Request> ingestions;

        if (hostProcessor.isPresent()) {
            final Map<String, Object> parts = hostProcessor.get().parse(s.getHost());

            final Set<Map.Entry<String, String>> tags = ImmutableSet.copyOf(
                    Iterables.transform(parts.entrySet(), e -> Pair.of(e.getKey(), e.getValue().toString())));

            ingestions = types.convert(s, Iterables.concat(base, tags));
        } else {//  w w  w. ja  v  a2  s .  c  o  m
            ingestions = types.convert(s, base);
        }

        final List<AsyncFuture<Ingestion>> futures = new ArrayList<>();

        for (final Ingestion.Request w : ingestions) {
            futures.add(ingestion.write(w));
        }

        async.collectAndDiscard(futures);
    }
}

From source file:com.streamsets.pipeline.lib.parser.udp.DatagramParser.java

License:Apache License

@Override
public Record parse() throws IOException, DataParserException {
    if (!parsed) {
        // fresh data. parse and cache records
        byte[] bytes = ByteStreams.toByteArray(is);
        udpMessage = UDP_MESSAGE_DESERIALIZER.deserialize(bytes);
        DatagramPacket datagram = udpMessage.getDatagram();
        validateMessageType(udpMessage.getType());
        queue = new LinkedList<>();
        try {// www  .j ava  2  s.c o m
            queue.addAll(datagramParser.parse(datagram.content(), datagram.recipient(), datagram.sender()));
        } catch (OnRecordErrorException e) {
            throw new DataParserException(e.getErrorCode(), e.getMessage());
        }
        parsed = true;
    }

    if (queue.isEmpty()) {
        // no more records to return, return null
        return null;
    }

    Record record = queue.poll();
    // set received time in record header
    record.getHeader().setAttribute(RECEIVED, String.valueOf(udpMessage.getReceived()));
    return record;
}

From source file:com.streamsets.pipeline.lib.udp.UDPMessageSerializer.java

License:Apache License

public byte[] serialize(UDPMessage message) throws IOException {
    baos.reset();//from   ww  w  .  j  a v  a 2s  . c om
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeInt(UDPConstants.UDP_MESSAGE_VERSION);
    oos.writeInt(message.getType());
    oos.writeLong(message.getReceived());
    DatagramPacket datagram = message.getDatagram();
    oos.writeUTF(datagram.sender().getAddress().getHostAddress());
    oos.writeInt(datagram.sender().getPort());
    oos.writeUTF(datagram.recipient().getAddress().getHostAddress());
    oos.writeInt(datagram.recipient().getPort());
    if (datagram.content().readableBytes() > MAX_UDP_PACKAGE_SIZE) {
        throw new IOException(
                Utils.format("Message size '{}' exceeds maximum size '{}'", baos.size(), MAX_UDP_PACKAGE_SIZE));
    }
    oos.writeInt(datagram.content().readableBytes());
    datagram.content().readBytes(oos, datagram.content().readableBytes());
    oos.close();
    byte[] buffer = new byte[baos.size()];
    System.arraycopy(baos.getInternalBuffer(), 0, buffer, 0, baos.size());
    return buffer;
}

From source file:com.streamsets.pipeline.stage.origin.udp.MultithreadedUDPSource.java

License:Apache License

@Override
public void produce(Map<String, String> offsets, int maxBatchSize) throws StageException {
    Utils.checkNotNull(udpServer, "UDP server is null");

    final int finalMaxBatchSize = Math.min(configs.batchSize, maxBatchSize);

    try {/*from   w  w w .j a  va2  s  .  co m*/
        ExecutorCompletionService<Future> completionService = new ExecutorCompletionService<>(executorService);

        List<Future> allFutures = new LinkedList<>();
        IntStream.range(0, numWorkerThreads).forEach(threadNumber -> {
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    BatchContext batchContext = null;
                    long remainingTime = configs.maxWaitTime;
                    while (!getContext().isStopped()) {
                        if (batchContext == null) {
                            batchContext = getContext().startBatch();
                        }

                        final long startingRecordCount = recordCount;
                        try {
                            long start = System.currentTimeMillis();
                            //ParseResult result = incomingQueue.poll(remainingTime, TimeUnit.MILLISECONDS);

                            final DatagramPacket packet = handler.getPacketQueue().poll(remainingTime,
                                    TimeUnit.MILLISECONDS);
                            List<Record> records = null;
                            if (packet != null) {
                                if (LOG.isTraceEnabled()) {
                                    LOG.trace("Took packet; new size: {}", handler.getPacketQueue().size());
                                }

                                try {
                                    records = parser.parse(packet.content(), packet.recipient(),
                                            packet.sender());
                                } catch (OnRecordErrorException ex) {
                                    getContext().reportError(ex.getErrorCode(), ex.getParams());
                                } catch (Exception e) {
                                    getContext().reportError(e);
                                    continue;
                                } finally {
                                    packet.release();
                                }
                            }
                            long elapsedTime = System.currentTimeMillis() - start;
                            if (elapsedTime > 0) {
                                remainingTime -= elapsedTime;
                            }

                            if (records != null) {
                                if (IS_TRACE_ENABLED) {
                                    LOG.trace("Found {} records", records.size());
                                }
                                for (Record record : records) {
                                    if (IS_TRACE_ENABLED) {
                                        LOG.trace("Processed {} records", (recordCount - startingRecordCount));
                                    }

                                    batchContext.getBatchMaker().addRecord(record);

                                    if (++recordCount % finalMaxBatchSize == 0) {
                                        getContext().processBatch(batchContext);
                                        batchContext = getContext().startBatch();
                                    }
                                }
                            }

                            if (remainingTime <= 0) {
                                remainingTime = configs.maxWaitTime;
                                getContext().processBatch(batchContext);
                                batchContext = getContext().startBatch();
                            }
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                        }
                    }
                }
            };
            allFutures.add(completionService.submit(runnable, null));
        });

        while (!getContext().isStopped()) {
            ThreadUtil.sleep(101);
        }

        for (Future future : allFutures) {
            try {
                future.get();
            } catch (ExecutionException e) {
                LOG.error(
                        "ExecutionException when attempting to wait for all UDP runnables to complete, after context was"
                                + " stopped: {}",
                        e.getMessage(), e);
            } catch (InterruptedException e) {
                LOG.error(
                        "InterruptedException when attempting to wait for all UDP runnables to complete, after context "
                                + "was stopped: {}",
                        e.getMessage(), e);
                Thread.currentThread().interrupt();
            }
        }
    } finally {
        if (!executorService.isShutdown()) {
            executorService.shutdown();
        }
    }

}

From source file:com.streamsets.pipeline.stage.origin.udp.QueuingUDPConsumer.java

License:Apache License

public void process(DatagramPacket packet) throws Exception {
    long total = totalPackets.incrementAndGet();
    boolean droppedPacket = false;
    ParseResult result;//from w w  w .j a  v a  2  s.  c  o  m
    try {
        List<Record> records = parser.parse(packet.content(), packet.recipient(), packet.sender());
        result = new ParseResult(records);
    } catch (OnRecordErrorException ex) {
        result = new ParseResult(ex);
    }
    if (!queue.offer(result)) {
        droppedPacket = true;
        long dropped = droppedPackets.incrementAndGet();
        if (dropped % 1000 == 0) {
            LOG.info("Could not add packet to queue, dropped {} of {} packets", dropped, total);
        }
    }
    if (!droppedPacket && total % 1000 == 0) {
        LOG.info("Consumed {} total packets", total);
    }
}