Example usage for io.netty.channel ChannelFuture cause

List of usage examples for io.netty.channel ChannelFuture cause

Introduction

In this page you can find the example usage for io.netty.channel ChannelFuture cause.

Prototype

Throwable cause();

Source Link

Document

Returns the cause of the failed I/O operation if the I/O operation has failed.

Usage

From source file:org.ballerinalang.net.http.nativeimpl.connection.CancelWebSocketUpgrade.java

License:Open Source License

@Override
public void execute(Context context, CallableUnitCallback callback) {
    try {/*from   w ww .  j a v  a2  s.c  om*/
        BMap<String, BValue> httpConnection = (BMap<String, BValue>) context.getRefArgument(0);
        int statusCode = (int) context.getIntArgument(0);
        String reason = context.getStringArgument(0);
        WebSocketHandshaker webSocketHandshaker = (WebSocketHandshaker) httpConnection
                .getNativeData(WebSocketConstants.WEBSOCKET_MESSAGE);
        if (webSocketHandshaker == null) {
            throw new BallerinaConnectorException("Not a WebSocket upgrade request. Cannot cancel the request");
        }
        ChannelFuture future = webSocketHandshaker.cancelHandshake(statusCode, reason);
        future.addListener((ChannelFutureListener) channelFuture -> {
            Throwable cause = future.cause();
            if (!future.isSuccess() && cause != null) {
                context.setReturnValues(HttpUtil.getError(context, cause));
            } else {
                context.setReturnValues();
            }
            if (channelFuture.channel().isOpen()) {
                channelFuture.channel().close();
            }
            callback.notifySuccess();
        });
    } catch (Exception e) {
        //Return this error.
        context.setReturnValues(HttpUtil.getError(context, e));
        callback.notifySuccess();
    }
}

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

License:Open Source License

/**
 * Provide asynchronous response to HTTP2 request.
 *
 * @param streamId StreamID/*w  ww . java 2  s. 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.betawares.jorre.handlers.client.ClientHeartbeatHandler.java

License:Open Source License

/**
 * Send a ping message to the server.//ww w  .  ja v  a 2 s.com
 * 
 * @throws CommunicationException thrown if there is an error sending the ping message
 */
public void pingServer() throws CommunicationException {
    ctx.writeAndFlush(PING).addListener((ChannelFutureListener) (ChannelFuture future) -> {
        if (!future.isSuccess()) {
            logger.error("Communication error", future.cause());
            throw new CommunicationException("Error pinging server", future.cause());
        }
    });
}

From source file:org.betawares.jorre.handlers.client.ClientMessageHandler.java

License:Open Source License

/**
 * Send a {@link ServerMessage} to the {@link Server}
 * //from   w w  w.ja  va  2s .  c o  m
 * @param message   the {@link ServerMessage} to send
 * @throws CommunicationException   thrown if there is an error while sending the message
 */
public void sendMessage(ServerMessage message) throws CommunicationException {
    if (!ctx.channel().isActive()) {
        logger.error("Connection is not active");
        throw new CommunicationException("No connection to server");
    }
    ctx.writeAndFlush(message).addListener((ChannelFutureListener) (ChannelFuture future) -> {
        if (future.isSuccess()) {
            message.setSentTS();
        } else {
            logger.error("Communication error", future.cause());
            throw new CommunicationException("Communication error", future.cause());
        }
    });
}

From source file:org.betawares.jorre.handlers.client.ClientMessageHandler.java

License:Open Source License

/**
 * Send a {@link ServerRequest} to the {@link Server} and return a {@link ResponseFuture}
 * /*from   w  w w . j  ava  2 s. c  om*/
 * @param request   the {@link ServerRequest} to send to the {@link Server}
 * @return  a {@link ResponseFuture} that will be notified when the response has been received
 * @throws CommunicationException   thrown if there is an error while sending the request
 */
public ResponseFuture sendRequest(ServerRequest request) throws CommunicationException {
    if (!ctx.channel().isActive()) {
        logger.error("Connection is not active");
        throw new CommunicationException("No connection to server");
    }

    ResponseFuture response = new ResponseFuture();
    ctx.writeAndFlush(request).addListener((ChannelFutureListener) (ChannelFuture future) -> {
        if (future.isSuccess()) {
            request.setSentTS();
            // add the ResponseFuture to the map so that we can later map the respsone to it
            responseMap.put(request.id(), response);
        } else {
            logger.error("Communication error", future.cause());
            response.completeExceptionally(future.cause());
        }
    });
    return response;
}

From source file:org.betawares.jorre.handlers.PingMessageHandler.java

License:Open Source License

@Override
protected void channelRead0(ChannelHandlerContext ctx, PingMessage msg) {
    //always respond to a ping with a pong
    ctx.writeAndFlush(PONG).addListener((ChannelFutureListener) (ChannelFuture future) -> {
        if (!future.isSuccess()) {
            logger.error("Communication error", future.cause());
        }/*  www .j  a  va  2  s .  co  m*/
    });
    logger.debug("Received ping.  Sending pong.");
}

From source file:org.betawares.jorre.handlers.server.ServerHeartbeatHandler.java

License:Open Source License

/**
 * Send a ping message to the client./*from   w  w  w. j a v  a  2 s .c  o m*/
 */
public void pingClient() {
    startTime = System.nanoTime();
    ctx.writeAndFlush(PING).addListener((ChannelFutureListener) (ChannelFuture future) -> {
        if (!future.isSuccess()) {
            logger.error("Communication error", future.cause());
        }
    });
}

From source file:org.betawares.jorre.messages.requests.ConnectClientRequest.java

License:Open Source License

private void respond(ConnectClientResponse response, ChannelHandlerContext ctx) {
    response.requestId(id);/*ww  w . j av  a2  s  .  c om*/
    if (!ctx.channel().isActive()) {
        logger.error("Connection is not active");
        return;
    }
    ctx.writeAndFlush(response).addListener((ChannelFutureListener) (ChannelFuture future) -> {
        if (!future.isSuccess()) {
            logger.error("Communication error", future.cause());
        }
    });
}

From source file:org.betawares.jorre.messages.requests.ServerRequest.java

License:Open Source License

/**
 * Send a response to the {@link Client}.
 * //from w ww . ja v a 2  s .  co  m
 * @param response  the response object to return of type {@link T}
 * @param ctx       the {@link ChannelHandlerContext} for the {@link Channel} to the {@link Client}
 * 
 * @throws CommunicationException thrown if an error occurs sending the response
 */
protected void respond(ClientResponse<C> response, ChannelHandlerContext ctx) throws CommunicationException {
    // set the reponse id to be the same as the request to that they can be matched
    response.requestId(id);
    if (!ctx.channel().isActive()) {
        logger.error("Connection is not active");
        throw new CommunicationException("No connection to client");
    }
    ctx.writeAndFlush(response).addListener((ChannelFutureListener) (ChannelFuture future) -> {
        if (future.isSuccess())
            response.setSentTS();
        else
            logger.error("Communication error", future.cause());
    });
}

From source file:org.betawares.jorre.Server.java

License:Open Source License

/**
 * Disconnect the specified client from the server.  
 * /*from  ww w.  j  ava  2 s .c o  m*/
 * Overriding classes should always call this super-class implementation.
 * 
 * @param channelId the {@link ChannelId} identifying the client
 * @param reason    the reason that the disconnect is occurring
 * @throws CommunicationException thrown if there is an error while disconnecting the client
 */
@Override
public void disconnectClient(ChannelId channelId, DisconnectReason reason) throws CommunicationException {
    Channel channel = clients.find(channelId);
    if (channel == null)
        throw new CommunicationException("Invalid sessionId during disconnect");
    channel.disconnect().addListener((ChannelFutureListener) (ChannelFuture future) -> {
        if (!future.isSuccess()) {
            logger.error("Communication error", future.cause());
            throw new CommunicationException("Communication error during disconnect", future.cause());
        }
    });
}