}
return version;
}
public static <T extends RedisClientBase> Promise<T> connect(String hostname, int port, final T redisClient, final ExecutorService executor) {
final ClientBootstrap cb = new ClientBootstrap(new NioClientSocketChannelFactory(executor, executor));
final Queue<Promise> queue = new LinkedTransferQueue<>();
final SimpleChannelUpstreamHandler handler = new SimpleChannelUpstreamHandler() {
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
Object message = e.getMessage();
if (queue.isEmpty()) {
if (message instanceof MultiBulkReply) {
redisClient.handleMessage(message);
} else {
// Need some way to notify
}
} else {
Promise poll = queue.poll();
if (message instanceof ErrorReply) {
poll.setException(new RedisException(((ErrorReply) message).data()));
} else {
poll.set(message);
}
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
if (queue.isEmpty()) {
// Needed for pub/sub?
} else {
Promise poll = queue.poll();
poll.setException(e.getCause());
}
}
};
final RedisEncoder encoder = new RedisEncoder();
final RedisDecoder decoder = new RedisDecoder();
cb.setPipelineFactory(new ChannelPipelineFactory() {
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("redisEncoder", encoder);
pipeline.addLast("redisDecoder", decoder);
pipeline.addLast("result", handler);
return pipeline;
}
});
final Promise<T> redisClientBasePromise = new Promise<>();
cb.connect(new InetSocketAddress(hostname, port)).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture channelFuture) throws Exception {
if (channelFuture.isSuccess()) {
redisClient.init(channelFuture.getChannel(), queue, executor);
redisClient.execute(BulkReply.class, new Command("INFO")).onSuccess(new Block<BulkReply>() {