Search in sources :

Example 1 with FramedBufferInputStream

use of net.morimekta.providence.thrift.io.FramedBufferInputStream in project providence by morimekta.

the class NonblockingSocketClientHandler method handleReadResponses.

private void handleReadResponses(SocketChannel channel, PService service) {
    while (this.channel == channel && channel.isOpen()) {
        FramedBufferInputStream in = new FramedBufferInputStream(channel);
        try {
            in.nextFrame();
            PServiceCall reply = serializer.deserialize(in, service);
            if (reply.getType() == PServiceCallType.CALL || reply.getType() == PServiceCallType.ONEWAY) {
                throw new PApplicationException("Reply with invalid call type: " + reply.getType(), PApplicationExceptionType.INVALID_MESSAGE_TYPE);
            }
            CompletableFuture<PServiceCall> future = responseFutures.get(reply.getSequence());
            if (future == null) {
                // The item response timed out.
                LOGGER.debug("No future for sequence ID " + reply.getSequence());
                continue;
            }
            responseFutures.remove(reply.getSequence());
            future.complete(reply);
        } catch (Exception e) {
            if (!channel.isOpen()) {
                // If the channel is closed. Should not trigger on disconnected.
                break;
            }
            LOGGER.error("Exception in channel response reading", e);
        }
    }
    if (responseFutures.size() > 0) {
        LOGGER.warn("Channel closed with {} unfinished calls", responseFutures.size());
        responseFutures.forEach((s, f) -> f.completeExceptionally(new IOException("Channel closed")));
        responseFutures.clear();
    }
}
Also used : PServiceCall(net.morimekta.providence.PServiceCall) PApplicationException(net.morimekta.providence.PApplicationException) FramedBufferInputStream(net.morimekta.providence.thrift.io.FramedBufferInputStream) IOException(java.io.IOException) PApplicationException(net.morimekta.providence.PApplicationException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 PApplicationException (net.morimekta.providence.PApplicationException)1 PServiceCall (net.morimekta.providence.PServiceCall)1 FramedBufferInputStream (net.morimekta.providence.thrift.io.FramedBufferInputStream)1