Example usage for io.netty.buffer ByteBuf readableBytes

List of usage examples for io.netty.buffer ByteBuf readableBytes

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf readableBytes.

Prototype

public abstract int readableBytes();

Source Link

Document

Returns the number of readable bytes which is equal to (this.writerIndex - this.readerIndex) .

Usage

From source file:com.hazelcast.simulator.protocol.handler.ConnectionHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object obj) throws Exception {
    if (!(obj instanceof ByteBuf)) {
        return;/*www. j av  a  2  s . c  o m*/
    }

    ByteBuf buf = (ByteBuf) obj;
    if (buf.readableBytes() < MINIMUM_BYTE_BUFFER_SIZE) {
        return;
    }

    if (!isSimulatorMessage(buf) && !isResponse(buf)) {
        LOGGER.warn(format("Invalid connection from %s (no magic bytes found)", ctx.channel().remoteAddress()));
        ctx.close();
        return;
    }

    // the connection is valid so we remove this handler and forward the buffer to the pipeline
    LOGGER.info(format("Valid connection from %s (magic bytes found)", ctx.channel().remoteAddress()));
    isConnectionValid.countDown();
    ctx.pipeline().remove(this);
    ctx.fireChannelRead(obj);

    ctx.channel().closeFuture().addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            connectionListener.disconnected(future.channel());
        }
    });

    connectionListener.connected(ctx.channel());
}

From source file:com.hazelcast.simulator.protocol.handler.ConnectionValidationHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object obj) throws Exception {
    if (!(obj instanceof ByteBuf)) {
        return;//from   w  w w.j  a v  a2  s  . co m
    }

    ByteBuf buf = (ByteBuf) obj;
    if (buf.readableBytes() < MINIMUM_BYTE_BUFFER_SIZE) {
        return;
    }

    if (!isSimulatorMessage(buf) && !isResponse(buf)) {
        LOGGER.warn(format("Invalid connection from %s (no magic bytes found)", ctx.channel().remoteAddress()));
        ctx.close();
        return;
    }

    // the connection is valid so we remove this handler and forward the buffer to the pipeline
    LOGGER.info(format("Valid connection from %s (magic bytes found)", ctx.channel().remoteAddress()));
    ctx.pipeline().remove(this);
    ctx.fireChannelRead(obj);
}

From source file:com.hazelcast.simulator.protocol.handler.SimulatorProtocolDecoder.java

License:Open Source License

private void decodeSimulatorMessage(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) {
    long messageId = SimulatorMessageCodec.getMessageId(buffer);
    AddressLevel dstAddressLevel = AddressLevel
            .fromInt(SimulatorMessageCodec.getDestinationAddressLevel(buffer));
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace(format("[%d] %s %s received a message for addressLevel %s", messageId, addressLevel,
                localAddress, dstAddressLevel));
    }//  w ww . jav a2  s.  c  o m

    if (dstAddressLevel == addressLevel) {
        SimulatorMessage message = SimulatorMessageCodec.decodeSimulatorMessage(buffer);
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace(format("[%d] %s %s will consume %s", messageId, addressLevel, localAddress, message));
        }
        out.add(message);
    } else {
        int addressIndex = SimulatorMessageCodec.getChildAddressIndex(buffer, addressLevelValue);
        ctx.attr(forwardAddressIndex).set(addressIndex);

        out.add(buffer.duplicate());
        buffer.readerIndex(buffer.readableBytes());
        buffer.retain();
    }
}

From source file:com.hazelcast.simulator.protocol.handler.SimulatorProtocolDecoder.java

License:Open Source License

private void decodeResponse(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) {
    long messageId = ResponseCodec.getMessageId(buffer);
    AddressLevel dstAddressLevel = AddressLevel.fromInt(ResponseCodec.getDestinationAddressLevel(buffer));
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace(format("[%d] %s %s received a response for addressLevel %s", messageId, addressLevel,
                localAddress, dstAddressLevel));
    }/*from  www .j  a va 2 s. c  om*/

    if (dstAddressLevel == addressLevel || dstAddressLevel.isParentAddressLevel(addressLevel)) {
        Response response = ResponseCodec.decodeResponse(buffer);
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace(format("[%d] %s %s received %s", response.getMessageId(), addressLevel, localAddress,
                    response));
        }
        if (workerJvmManager != null) {
            workerJvmManager.updateLastSeenTimestamp(response);
        }

        out.add(response);
    } else {
        int addressIndex = ResponseCodec.getChildAddressIndex(buffer, addressLevelValue);
        ctx.attr(forwardAddressIndex).set(addressIndex);

        out.add(buffer.duplicate());
        buffer.readerIndex(buffer.readableBytes());
        buffer.retain();
    }
}

From source file:com.heelenyc.im.coder.MessageEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, Message msg, ByteBuf sendBuf) throws Exception {
    if (msg == null || msg.getHeader() == null)
        throw new Exception("The encode message is null");

    sendBuf.writeInt((msg.getHeader().getCrcCode()));
    sendBuf.writeInt((msg.getHeader().getLength())); // ?
    sendBuf.writeLong((msg.getHeader().getSessionID()));
    sendBuf.writeByte((msg.getHeader().getType()));
    sendBuf.writeByte((msg.getHeader().getPriority()));
    sendBuf.writeInt((msg.getHeader().getAttachment().size()));
    String key = null;/* w  ww . j  ava  2 s  . co m*/
    byte[] keyArray = null;
    Object value = null;
    for (Map.Entry<String, Object> param : msg.getHeader().getAttachment().entrySet()) {
        // write key
        key = param.getKey();
        keyArray = key.getBytes(Constans.ATTACHMENT_KEY_CHARACTSET);
        sendBuf.writeInt(keyArray.length);
        sendBuf.writeBytes(keyArray);
        // write value
        value = param.getValue();
        encoder.encode(value, sendBuf);
    }
    key = null;
    keyArray = null;
    value = null;
    if (msg.getBody() != null) {
        encoder.encode(msg.getBody(), sendBuf);
    } else
        sendBuf.writeInt(0);
    int totalSize = sendBuf.readableBytes() - Constans.MESSAGE_LENGTH_FIELD_OFFSET
            - Constans.MESSAGE_LENGTH_FIELD_LENGTH;
    // fix head length field
    msg.getHeader().setLength(totalSize);
    // ?
    sendBuf.setInt(Constans.MESSAGE_LENGTH_FIELD_OFFSET, totalSize);
    logger.debug("encode :" + msg);
}

From source file:com.heliosapm.streams.chronicle.MessageQueue.java

License:Apache License

/**
 * {@inheritDoc}//ww w .  j  ava2  s .  com
 * @see java.lang.Runnable#run()
 */
@Override
public void run() {
    final ExcerptTailer tailer = queue.createTailer();
    final ByteBufMarshallable smm = new ByteBufMarshallable(compression);
    startLatch.countDown();
    while (keepRunning.get()) {
        try {
            long processed = 0L;
            long reads = 0L;
            int listenerProcessed = 0;
            final long startTime = System.currentTimeMillis();
            while (tailer.readBytes(smm)) {
                chronicleReads.inc();
                reads++;
                final ByteBuf sm = smm.getAndNullByteBuf();
                log.debug("MessageQueue Read Buffer, size: {} bytes", sm.readableBytes());
                if (sm != null) {
                    listenerProcessed += listener.onMetric(sm);
                    //                  sm.release();
                    processed++;
                    if (processed == stopCheckCount) {
                        processed = 0;
                        if (!keepRunning.get())
                            break;
                    }
                }
            }
            if (reads == 0) {
                Jvm.pause(idlePauseTime);
            } else {
                final long elapsedTime = System.currentTimeMillis() - startTime;
                log.info("Processed [{}] in [{}] ms.", listenerProcessed, elapsedTime);
            }
            reads = 0;
        } catch (Exception ex) {
            if (ex instanceof InterruptedException) {
                if (keepRunning.get()) {
                    if (Thread.interrupted())
                        Thread.interrupted();
                }
                log.info("Reader Thread [{}] shutting down", Thread.currentThread());
            } else {
                log.warn("Unexpected exception in reader thread", ex);
            }
        }
    }
}

From source file:com.heliosapm.streams.forwarder.HttpJsonOutboundHandler.java

License:Apache License

/**
 * {@inheritDoc}//from   ww  w. ja  va 2  s .c om
 * @see io.netty.handler.codec.MessageToMessageEncoder#encode(io.netty.channel.ChannelHandlerContext, java.lang.Object, java.util.List)
 */
@Override
protected void encode(final ChannelHandlerContext ctx, final ConsumerRecords<String, StreamedMetricValue> msg,
        final List<Object> out) throws Exception {
    final StreamedMetricValue[] smvs = StreamSupport.stream(msg.spliterator(), true)
            .map(new Function<ConsumerRecord<String, StreamedMetricValue>, StreamedMetricValue>() {
                @Override
                public StreamedMetricValue apply(ConsumerRecord<String, StreamedMetricValue> t) {
                    return t.value();
                }

            }).toArray(s -> new StreamedMetricValue[s]);
    final int size = smvs.length;
    final ByteBuf buff = buffManager.buffer(size * 200);

    JSONOps.serializeAndGzip(smvs, buff);
    final int sz = buff.readableBytes();
    final HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, postUri,
            buff);
    request.headers().set(HttpHeaderNames.HOST, host);
    request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    request.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
    request.headers().set(HttpHeaderNames.CONTENT_ENCODING, HttpHeaderValues.GZIP);
    request.headers().set(HttpHeaderNames.CONTENT_LENGTH, buff.readableBytes());
    out.add(request);

    ctx.executor().execute(new Runnable() {
        public void run() {
            final NonBlockingHashSet<String> hosts = new NonBlockingHashSet<String>();
            StreamSupport.stream(msg.spliterator(), true)
                    .map(new Function<ConsumerRecord<String, StreamedMetricValue>, String>() {
                        @Override
                        public String apply(ConsumerRecord<String, StreamedMetricValue> t) {
                            return t.value().getTags().get("host");
                        }
                    }).forEach(h -> hosts.add(h));
            log.info("Hosts:{}, Size: {}", hosts, sz);
        }
    });
}

From source file:com.heliosapm.streams.json.JSONOps.java

License:Apache License

/**
 * Parses the passed channel buffer into a JsonNode
 * @param buff The buffer to parse//from   w w w  . ja  v  a  2  s. c om
 * @param nullIfNoContent If true, returns null if no content is available to parse
 * @return the parsed JsonNode
 */
public static JsonNode parseToNode(final ByteBuf buff, final boolean nullIfNoContent) {
    if (buff == null || buff.readableBytes() < 1) {
        if (nullIfNoContent)
            return null;
        throw new IllegalArgumentException("Incoming data was null");
    }
    final InputStream is = new ByteBufInputStream(buff);
    try {
        return parseToNode(is);
    } catch (Exception e) {
        if (nullIfNoContent)
            return null;
        throw new JSONException(e);
    } finally {
        try {
            is.close();
        } catch (Exception x) {
            /* No Op */}
    }
}

From source file:com.heliosapm.streams.metrichub.HubManager.java

License:Apache License

public RequestCompletion evaluate(final QueryContext queryContext, final RequestBuilder requestBuilder,
        final String expression) {
    try {//from w  w  w.  j a  va2s. c  o  m
        final JsonGenerator jg = requestBuilder.renderHeader();
        final ChannelPool pool = channelPool();
        final Channel channel = pool.acquire().get();
        final CompletableFuture<RequestCompletion> completionFuture = new CompletableFuture<RequestCompletion>();

        evaluate(queryContext, expression).flush().consume(lmt -> {
            final RequestCompletion rc = new RequestCompletion(queryContext.getTimeout(), pool);
            if (channel.pipeline().get("completion") != null) {
                try {
                    channel.pipeline().remove("completion");
                } catch (Exception ex) {
                }
            }
            channel.pipeline().addLast("completion", rc);
            completionFuture.complete(rc);
            final ByteBuf bb = requestBuilder.merge(jg, lmt);
            if (log.isDebugEnabled())
                log.debug("CREQUEST:\n{}", bb.toString(UTF8));
            final HttpRequest httpRequest = buildHttpRequest(bb);
            log.info("Flushed request. Size: {} bytes", bb.readableBytes());
            channel.writeAndFlush(httpRequest);
        });
        return completionFuture.get();
    } catch (Exception ex) {
        throw new RuntimeException("eval error", ex);
    }
}

From source file:com.heliosapm.streams.metrichub.HubManager.java

License:Apache License

protected ByteBuf updateJsonRequest(final List<TSMeta> tsMetas, final ByteBuf header) {
    try {/*from  www . j  a  va  2s.  com*/
        final ByteBuf request = BufferManager.getInstance().buffer(header.readableBytes() + 1024);
        request.writeBytes(header);
        header.resetReaderIndex();
        request.writerIndex(request.writerIndex() - REQUEST_CLOSER.length());
        for (TSMeta ts : tsMetas) {
            request.writeCharSequence(new StringBuilder("\"").append(ts.getTSUID()).append("\","), UTF8);
        }
        request.writerIndex(request.writerIndex() - 1);
        request.writeCharSequence(REQUEST_CLOSER, UTF8);
        return request;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}