io.trane.ndbc.postgres.netty4.ChannelSupplier Maven / Gradle / Ivy
package io.trane.ndbc.postgres.netty4;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.List;
import java.util.function.Supplier;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.handler.flow.FlowControlHandler;
import io.trane.future.Future;
import io.trane.future.Promise;
import io.trane.ndbc.postgres.proto.marshaller.Marshaller;
import io.trane.ndbc.postgres.proto.unmarshaller.Unmarshaller;
import io.trane.ndbc.proto.ClientMessage;
final class ChannelSupplier implements Supplier> {
private final Marshaller encoder;
private final Unmarshaller decoder;
private final EventLoopGroup eventLoopGroup;
private final String host;
private final int port;
private final Charset charset;
public ChannelSupplier(final Charset charset, final Marshaller encoder,
final Unmarshaller decoder,
final EventLoopGroup eventLoopGroup, final String host, final int port) {
super();
this.charset = charset;
this.encoder = encoder;
this.decoder = decoder;
this.eventLoopGroup = eventLoopGroup;
this.host = host;
this.port = port;
}
@Override
public final Future get() {
final NettyChannel channel = new NettyChannel();
return bootstrap(channel).map(v -> channel);
}
private class MessageDecoder extends ByteToMessageDecoder {
boolean firstMessage = true;
@Override
protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List