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

io.tarantool.driver.core.TarantoolChannelInitializer 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.core;

import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.ssl.SslContext;
import io.tarantool.driver.TarantoolVersionHolder;
import io.tarantool.driver.api.TarantoolClientConfig;
import io.tarantool.driver.auth.ChapSha1TarantoolAuthenticator;
import io.tarantool.driver.auth.SimpleTarantoolCredentials;
import io.tarantool.driver.codecs.MessagePackFrameDecoder;
import io.tarantool.driver.codecs.MessagePackFrameEncoder;
import io.tarantool.driver.exceptions.TarantoolClientException;
import io.tarantool.driver.handlers.TarantoolAuthenticationHandler;
import io.tarantool.driver.handlers.TarantoolAuthenticationResponseHandler;
import io.tarantool.driver.handlers.TarantoolRequestHandler;
import io.tarantool.driver.handlers.TarantoolResponseHandler;
import io.tarantool.driver.mappers.factories.DefaultMessagePackMapperFactory;

import java.util.concurrent.CompletableFuture;

/**
 * The main channel pipeline initializer.
 * 

* - Adds authentication handler which accepts the Tarantool server greeting and sets up the pipeline when channel * is connect to the server; * - Sets up the necessary handlers and codecs. * * @author Alexey Kuzin */ public class TarantoolChannelInitializer extends ChannelInitializer { private final TarantoolClientConfig config; private final TarantoolVersionHolder versionHolder; private final CompletableFuture connectionFuture; private final RequestFutureManager futureManager; public TarantoolChannelInitializer( TarantoolClientConfig config, RequestFutureManager futureManager, TarantoolVersionHolder versionHolder, CompletableFuture connectionFuture) { this.config = config; this.versionHolder = versionHolder; this.connectionFuture = connectionFuture; this.futureManager = futureManager; } @Override protected void initChannel(SocketChannel socketChannel) { final ChannelPipeline pipeline = socketChannel.pipeline(); if (config.isSecure()) { wrapForSecure(socketChannel, pipeline); } // greeting and authentication (will be removed after successful authentication) pipeline.addLast("TarantoolAuthenticationHandler", new TarantoolAuthenticationHandler<>( connectionFuture, versionHolder, (SimpleTarantoolCredentials) config.getCredentials(), new ChapSha1TarantoolAuthenticator())) // frame encoder and decoder .addLast("MessagePackFrameDecoder", new MessagePackFrameDecoder()) .addLast("MessagePackFrameEncoder", new MessagePackFrameEncoder( DefaultMessagePackMapperFactory.getInstance().defaultComplexTypesMapper())) // outbound .addLast("TarantoolRequestHandler", new TarantoolRequestHandler(futureManager)) // inbound auth response handler .addLast("TarantoolAuthenticationResponseHandler", new TarantoolAuthenticationResponseHandler( connectionFuture)) // inbound .addLast("TarantoolResponseHandler", new TarantoolResponseHandler(futureManager)); } private void wrapForSecure(SocketChannel socketChannel, ChannelPipeline pipeline) { final SslContext sslContext = config.getSslContext(); if (sslContext == null) { throw new TarantoolClientException("Ssl context must not be null!"); } pipeline.addLast(sslContext.newHandler(socketChannel.alloc())); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy