All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.tarantool.driver.handlers.TarantoolAuthenticationResponseHandler Maven / Gradle / Ivy

Go to download

Tarantool Cartridge driver for Tarantool versions 1.10+ based on Netty framework

There is a newer version: 0.14.0
Show newest version
package io.tarantool.driver.handlers;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.tarantool.driver.exceptions.TarantoolServerException;
import io.tarantool.driver.protocol.TarantoolErrorResult;
import io.tarantool.driver.protocol.TarantoolResponse;

import java.util.concurrent.CompletableFuture;

/**
 * Basic Tarantool server authentication response handler. Completes connection or makes exception out of Tarantool
 * server error
 *
 * @author Alexey Kuzin
 */
public class TarantoolAuthenticationResponseHandler extends SimpleChannelInboundHandler {

    private final CompletableFuture connectionFuture;

    public TarantoolAuthenticationResponseHandler(CompletableFuture connectionFuture) {
        super();
        this.connectionFuture = connectionFuture;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, TarantoolResponse tarantoolResponse) throws Exception {
        if (!connectionFuture.isDone()) {
            switch (tarantoolResponse.getResponseType()) {
                case IPROTO_NOT_OK:
                    TarantoolErrorResult errorResult = new TarantoolErrorResult(tarantoolResponse.getSyncId(),
                            tarantoolResponse.getResponseCode(), tarantoolResponse.getBody().getData());
                    connectionFuture.completeExceptionally(
                        new TarantoolServerException(errorResult.getErrorCode(), errorResult.getErrorMessage()));
                    break;
                case IPROTO_OK:
                    connectionFuture.complete(ctx.channel());
            }
        }
        ctx.pipeline().remove(this); // authorize once per channel
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        connectionFuture.completeExceptionally(cause);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy