|
| 1 | +/* |
| 2 | + * Copyright (c) 2015-2017, Antonio Gabriel Muñoz Conejo <antoniogmc at gmail dot com> |
| 3 | + * Distributed under the terms of the MIT License |
| 4 | + */ |
| 5 | +package com.github.tonivade.resp; |
| 6 | + |
| 7 | +import static java.util.Objects.requireNonNull; |
| 8 | + |
| 9 | +import java.util.logging.Logger; |
| 10 | + |
| 11 | +import com.github.tonivade.resp.protocol.RedisDecoder; |
| 12 | +import com.github.tonivade.resp.protocol.RedisEncoder; |
| 13 | +import com.github.tonivade.resp.protocol.RedisToken; |
| 14 | + |
| 15 | +import io.netty.bootstrap.Bootstrap; |
| 16 | +import io.netty.buffer.PooledByteBufAllocator; |
| 17 | +import io.netty.channel.ChannelFuture; |
| 18 | +import io.netty.channel.ChannelHandlerContext; |
| 19 | +import io.netty.channel.ChannelOption; |
| 20 | +import io.netty.channel.EventLoopGroup; |
| 21 | +import io.netty.channel.nio.NioEventLoopGroup; |
| 22 | +import io.netty.channel.socket.SocketChannel; |
| 23 | +import io.netty.channel.socket.nio.NioSocketChannel; |
| 24 | +import io.netty.handler.codec.string.StringEncoder; |
| 25 | +import io.netty.util.CharsetUtil; |
| 26 | + |
| 27 | +public class RedisClient implements IRedis { |
| 28 | + |
| 29 | + private static final Logger LOGGER = Logger.getLogger(RedisClient.class.getName()); |
| 30 | + |
| 31 | + private static final String DELIMITER = "\r\n"; |
| 32 | + |
| 33 | + private static final int BUFFER_SIZE = 1024 * 1024; |
| 34 | + private static final int MAX_FRAME_SIZE = BUFFER_SIZE * 100; |
| 35 | + |
| 36 | + private final int port; |
| 37 | + private final String host; |
| 38 | + |
| 39 | + private EventLoopGroup workerGroup; |
| 40 | + private Bootstrap bootstrap; |
| 41 | + |
| 42 | + private ChannelFuture future; |
| 43 | + |
| 44 | + private ChannelHandlerContext context; |
| 45 | + private RedisInitializerHandler initHandler; |
| 46 | + private RedisConnectionHandler connectionHandler; |
| 47 | + |
| 48 | + private final IRedisCallback callback; |
| 49 | + |
| 50 | + public RedisClient(String host, int port, IRedisCallback callback) { |
| 51 | + this.host = requireNonNull(host); |
| 52 | + this.port = requireRange(port, 1024, 65535); |
| 53 | + this.callback = requireNonNull(callback); |
| 54 | + } |
| 55 | + |
| 56 | + public void start() { |
| 57 | + workerGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors()); |
| 58 | + initHandler = new RedisInitializerHandler(this); |
| 59 | + connectionHandler = new RedisConnectionHandler(this); |
| 60 | + |
| 61 | + bootstrap = new Bootstrap().group(workerGroup) |
| 62 | + .channel(NioSocketChannel.class) |
| 63 | + .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) |
| 64 | + .option(ChannelOption.SO_RCVBUF, BUFFER_SIZE) |
| 65 | + .option(ChannelOption.SO_SNDBUF, BUFFER_SIZE) |
| 66 | + .option(ChannelOption.SO_KEEPALIVE, true) |
| 67 | + .handler(initHandler); |
| 68 | + |
| 69 | + future = connect(); |
| 70 | + } |
| 71 | + |
| 72 | + public void stop() { |
| 73 | + try { |
| 74 | + if (future != null) { |
| 75 | + future.channel().close(); |
| 76 | + } |
| 77 | + } finally { |
| 78 | + workerGroup.shutdownGracefully(); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void channel(SocketChannel channel) { |
| 84 | + LOGGER.info(() -> "connected to server: " + host + ":" + port); |
| 85 | + channel.pipeline().addLast("redisEncoder", new RedisEncoder()); |
| 86 | + channel.pipeline().addLast("stringEncoder", new StringEncoder(CharsetUtil.UTF_8)); |
| 87 | + channel.pipeline().addLast("linDelimiter", new RedisDecoder(MAX_FRAME_SIZE)); |
| 88 | + channel.pipeline().addLast(connectionHandler); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void connected(ChannelHandlerContext ctx) { |
| 93 | + LOGGER.info(() -> "channel active"); |
| 94 | + this.context = ctx; |
| 95 | + callback.onConnect(); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void disconnected(ChannelHandlerContext ctx) { |
| 100 | + LOGGER.info(() -> "client disconected from server: " + host + ":" + port); |
| 101 | + if (this.context != null) { |
| 102 | + callback.onDisconnect(); |
| 103 | + this.context = null; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public void send(String message) { |
| 108 | + writeAndFlush(message + DELIMITER); |
| 109 | + } |
| 110 | + |
| 111 | + public void send(RedisToken message) { |
| 112 | + writeAndFlush(message); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void receive(ChannelHandlerContext ctx, RedisToken message) { |
| 117 | + callback.onMessage(message); |
| 118 | + } |
| 119 | + |
| 120 | + private ChannelFuture connect() { |
| 121 | + LOGGER.info(() -> "trying to connect"); |
| 122 | + ChannelFuture future = bootstrap.connect(host, port); |
| 123 | + future.syncUninterruptibly(); |
| 124 | + return future; |
| 125 | + } |
| 126 | + |
| 127 | + private void writeAndFlush(Object message) { |
| 128 | + if (context != null) { |
| 129 | + context.writeAndFlush(message); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private int requireRange(int value, int min, int max) { |
| 134 | + if (value <= min || value > max) { |
| 135 | + throw new IllegalArgumentException(min + " <= " + value + " < " + max); |
| 136 | + } |
| 137 | + return value; |
| 138 | + } |
| 139 | +} |
0 commit comments