Example usage for io.netty.channel ChannelPromise isSuccess

List of usage examples for io.netty.channel ChannelPromise isSuccess

Introduction

In this page you can find the example usage for io.netty.channel ChannelPromise isSuccess.

Prototype

boolean isSuccess();

Source Link

Document

Returns true if and only if the I/O operation was completed successfully.

Usage

From source file:org.ballerinalang.test.util.http2.HTTP2ResponseHandler.java

License:Open Source License

/**
 * Provide asynchronous response to HTTP2 request.
 *
 * @param streamId StreamID//from w  w  w .j a  v  a  2s.  c  o  m
 * @return Response string
 */
public FullHttpResponse getResponse(int streamId) {

    FullHttpResponse message = streamIdResponseMap.get(streamId);
    if (message != null) {
        return message;
    } else {
        Entry<ChannelFuture, ChannelPromise> channelFutureChannelPromiseEntry = streamIdPromiseMap
                .get(streamId);
        if (channelFutureChannelPromiseEntry != null) {
            ChannelFuture writeFuture = channelFutureChannelPromiseEntry.getKey();
            if (!writeFuture.awaitUninterruptibly(TestConstant.HTTP2_RESPONSE_TIME_OUT,
                    TestConstant.HTTP2_RESPONSE_TIME_UNIT)) {
                streamIdPromiseMap.remove(streamId);
                throw new IllegalStateException("Timed out waiting to write for stream id " + streamId);
            }
            if (!writeFuture.isSuccess()) {
                streamIdPromiseMap.remove(streamId);
                throw new RuntimeException(writeFuture.cause());
            }
            ChannelPromise promise = channelFutureChannelPromiseEntry.getValue();
            if (!promise.awaitUninterruptibly(TestConstant.HTTP2_RESPONSE_TIME_OUT,
                    TestConstant.HTTP2_RESPONSE_TIME_UNIT)) {
                streamIdPromiseMap.remove(streamId);
                throw new IllegalStateException("Timed out waiting for response on stream id " + streamId);
            }
            if (!promise.isSuccess()) {
                streamIdPromiseMap.remove(streamId);
                throw new RuntimeException(promise.cause());
            }
        }
    }
    return streamIdResponseMap.get(streamId);
}

From source file:org.infinispan.rest.http2.HttpResponseHandler.java

License:Apache License

/**
 * Wait (sequentially) for a time duration for each anticipated response
 *
 * @param timeout Value of time to wait for each response
 * @param unit Units associated with {@code timeout}
 * @see HttpResponseHandler#put(int, io.netty.channel.ChannelFuture, io.netty.channel.ChannelPromise)
 *///from  w w w. j  a  v a 2s  .c  o m
public void awaitResponses(long timeout, TimeUnit unit) {
    Iterator<Entry<Integer, Entry<ChannelFuture, ChannelPromise>>> itr = streamidPromiseMap.entrySet()
            .iterator();
    while (itr.hasNext()) {
        Entry<Integer, Entry<ChannelFuture, ChannelPromise>> entry = itr.next();
        ChannelFuture writeFuture = entry.getValue().getKey();
        if (!writeFuture.awaitUninterruptibly(timeout, unit)) {
            throw new IllegalStateException("Timed out waiting to write for stream id " + entry.getKey());
        }
        if (!writeFuture.isSuccess()) {
            throw new RuntimeException(writeFuture.cause());
        }
        ChannelPromise promise = entry.getValue().getValue();
        if (!promise.awaitUninterruptibly(timeout, unit)) {
            throw new IllegalStateException("Timed out waiting for response on stream id " + entry.getKey());
        }
        if (!promise.isSuccess()) {
            throw new RuntimeException(promise.cause());
        }
        itr.remove();
    }
}

From source file:org.kaaproject.kaa.server.transports.tcp.transport.netty.KaaTcpEncoder.java

License:Apache License

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (!(msg instanceof MqttFrame)) {
        LOG.warn("Message is not a {}", MqttFrame.class.getSimpleName());
        super.write(ctx, msg, promise);
    } else {/*  w  w  w .  j a v  a2  s  .co  m*/
        MqttFrame frame = (MqttFrame) msg;
        byte[] data = frame.getFrame().array();
        if (LOG.isTraceEnabled()) {
            LOG.trace("Sending {} data for frame {}", Arrays.toString(data), frame);
        }
        if (LOG.isTraceEnabled()) {
            LOG.trace(
                    "Channel promise before writeAndFlush isSuccess [{}] isDone [{}]"
                            + " isCancelled [{}] for frame {}",
                    promise.isSuccess(), promise.isDone(), promise.isCancelled(), frame);
        }
        ChannelFuture future = ctx.writeAndFlush(data, promise);
        if (LOG.isTraceEnabled()) {
            LOG.trace(
                    "Returned future [{}] isSuccess [{}] isDone [{}] isCancelled [{}] cause [{}]"
                            + " for frame {}",
                    future, future.isSuccess(), future.isDone(), future.isCancelled(), future.cause(), frame);
            if (future.cause() != null) {
                LOG.trace("Write operation failed due to:", future.cause());
            }
        }
        if (frame.isNeedCloseConnection()) {
            future.addListener(ChannelFutureListener.CLOSE);
        }
    }
}

From source file:org.tinygroup.nettyremote.impl.ClientImpl.java

License:GNU General Public License

public void write(Object o) {
    if (o instanceof Event) {
        Event event = (Event) o;
        LOGGER.logMessage(LogLevel.DEBUG, "?:eventId:{},serviceId:{}", event.getEventId(),
                event.getServiceRequest().getServiceId());
    }//ww  w.  j  a  v a2  s .  co  m
    if (future == null || future.channel() == null) {
        throw new RuntimeException("");
    }
    ChannelFuture f = future.channel().writeAndFlush(o);
    if (f instanceof ChannelPromise) {
        ChannelPromise p = (ChannelPromise) f;
        try {
            p.await();
        } catch (InterruptedException e) {
            LOGGER.logMessage(LogLevel.WARN, "?");
        }
        if (p.isSuccess()) {
            LOGGER.logMessage(LogLevel.DEBUG, "??{}", p.isSuccess());
        } else {
            LOGGER.logMessage(LogLevel.WARN, "??false");
            throw new RuntimeException(p.cause());
        }

    }

}

From source file:org.wso2.carbon.transport.http.netty.util.client.http2.HTTP2ResponseHandler.java

License:Open Source License

/**
 * Provide asynchronous response to HTTP2 request
 *
 * @param streamId StreamID/*from  ww w.  ja v  a 2s .c  om*/
 * @return Response string
 */
public String getResponse(int streamId) {

    String message = streamIdResponseMap.get(streamId);
    if (message != null) {
        return message;
    } else {
        Entry<ChannelFuture, ChannelPromise> channelFutureChannelPromiseEntry = streamIdPromiseMap
                .get(streamId);
        if (channelFutureChannelPromiseEntry != null) {
            ChannelFuture writeFuture = channelFutureChannelPromiseEntry.getKey();
            if (!writeFuture.awaitUninterruptibly(TestUtil.HTTP2_RESPONSE_TIME_OUT,
                    TestUtil.HTTP2_RESPONSE_TIME_UNIT)) {
                streamIdPromiseMap.remove(streamId);
                throw new IllegalStateException("Timed out waiting to write for stream id " + streamId);
            }
            if (!writeFuture.isSuccess()) {
                streamIdPromiseMap.remove(streamId);
                throw new RuntimeException(writeFuture.cause());
            }
            ChannelPromise promise = channelFutureChannelPromiseEntry.getValue();
            if (!promise.awaitUninterruptibly(TestUtil.HTTP2_RESPONSE_TIME_OUT,
                    TestUtil.HTTP2_RESPONSE_TIME_UNIT)) {
                streamIdPromiseMap.remove(streamId);
                throw new IllegalStateException("Timed out waiting for response on stream id " + streamId);
            }
            if (!promise.isSuccess()) {
                streamIdPromiseMap.remove(streamId);
                throw new RuntimeException(promise.cause());
            }
        }
    }
    return streamIdResponseMap.get(streamId);
}

From source file:org.wso2.esb.integration.common.utils.clients.http2client.HttpResponseHandler.java

License:Open Source License

/**
 * Wait (sequentially) for a time duration for each anticipated response
 *
 * @param timeout Value of time to wait for each response
 * @param unit Units associated with {@code timeout}
 * @see HttpResponseHandler#put(int, io.netty.channel.ChannelFuture, io.netty.channel.ChannelPromise)
 *///from  w w  w  .j  a  v a  2 s.  co m
public void awaitResponses(long timeout, TimeUnit unit) {
    Iterator<Entry<Integer, Entry<ChannelFuture, ChannelPromise>>> itr = streamidPromiseMap.entrySet()
            .iterator();
    while (itr.hasNext()) {
        Entry<Integer, Entry<ChannelFuture, ChannelPromise>> entry = itr.next();
        ChannelFuture writeFuture = entry.getValue().getKey();
        if (!writeFuture.awaitUninterruptibly(timeout, unit)) {
            throw new IllegalStateException("Timed out waiting to write for stream id " + entry.getKey());
        }
        if (!writeFuture.isSuccess()) {
            throw new RuntimeException(writeFuture.cause());
        }
        ChannelPromise promise = entry.getValue().getValue();
        if (!promise.awaitUninterruptibly(timeout, unit)) {
            throw new IllegalStateException("Timed out waiting for response on stream id " + entry.getKey());
        }
        if (!promise.isSuccess()) {
            throw new RuntimeException(promise.cause());
        }
        log.debug("---Stream id: " + entry.getKey() + " received---");
        itr.remove();
    }
}

From source file:org.wso2.esb.integration.common.utils.clients.Http2Client.java

License:Open Source License

public Http2Response doGet(String url, Map<String, String> headers) {
    initChannel();//w  w w  . ja va  2  s  .c  om
    HttpScheme scheme = SSL ? HttpScheme.HTTPS : HttpScheme.HTTP;
    AsciiString hostName = new AsciiString(HOST + ':' + PORT);

    FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, GET, url);
    if (!headers.isEmpty()) {
        for (Map.Entry h : headers.entrySet()) {
            request.headers().add((CharSequence) h.getKey(), h.getValue());
        }
    }
    request.headers().add(HttpHeaderNames.HOST, hostName);
    request.headers().add(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text(), scheme.name());
    request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP);
    request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.DEFLATE);
    io.netty.channel.ChannelPromise p;
    int s = StreamId;
    if (responseHandler == null) {
        log.error("Response handler is null");
        return null;
    } else if (channel == null) {
        log.error("Channel is null");
        return null;
    } else {
        responseHandler.put(StreamId, channel.writeAndFlush(request), p = channel.newPromise());
        StreamId += 2;
        Http2Response response;
        try {
            while (!p.isSuccess()) {
                log.info("Waiting for response");
                Thread.sleep(20);
            }
            response = responseHandler.getResponse(s);
        } catch (InterruptedException e) {
            response = null;
            log.error(e.getStackTrace());
        }
        return response;
    }

}

From source file:org.wso2.esb.integration.common.utils.clients.Http2Client.java

License:Open Source License

public Http2Response doPost(String url, String data, Map<String, String> headers) {

    initChannel();//  w  ww  .  j  ava  2  s.c  o  m
    HttpScheme scheme = SSL ? HttpScheme.HTTPS : HttpScheme.HTTP;
    AsciiString hostName = new AsciiString(HOST + ':' + PORT);

    FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, POST, url,
            Unpooled.copiedBuffer(data.getBytes()));

    if (!headers.isEmpty()) {
        for (Map.Entry h : headers.entrySet()) {
            request.headers().add((CharSequence) h.getKey(), h.getValue());
        }
    }
    request.headers().add(HttpHeaderNames.HOST, hostName);
    request.headers().add(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text(), scheme.name());
    request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP);
    request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.DEFLATE);
    io.netty.channel.ChannelPromise p;
    int s = StreamId;
    responseHandler.put(StreamId, channel.writeAndFlush(request), p = channel.newPromise());
    StreamId += 2;
    Http2Response response;
    try {
        while (!p.isSuccess()) {
            log.info("Waiting for response");
            Thread.sleep(20);
        }
        response = responseHandler.getResponse(s);
    } catch (InterruptedException e) {
        response = null;
        log.error(e.getStackTrace());
    }
    return response;
}