Protocol implementation
This commit is contained in:
106
src/main/java/redis/clients/jedis/Client.java
Normal file
106
src/main/java/redis/clients/jedis/Client.java
Normal file
@@ -0,0 +1,106 @@
|
||||
package redis.clients.jedis;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import redis.clients.jedis.Protocol;
|
||||
|
||||
public class Client {
|
||||
private String host;
|
||||
private int port = Protocol.DEFAULT_PORT;
|
||||
private Socket socket;
|
||||
private boolean connected = false;
|
||||
private Protocol protocol = new Protocol();
|
||||
private OutputStream outputStream;
|
||||
private InputStream inputStream;
|
||||
|
||||
public Client(String host) {
|
||||
super();
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public Client(String host, int port) {
|
||||
super();
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public Client() {
|
||||
}
|
||||
|
||||
public String ping() {
|
||||
String command = protocol.buildCommand("PING");
|
||||
try {
|
||||
outputStream.write(command.getBytes());
|
||||
return protocol.getSingleLineReply(inputStream);
|
||||
} catch (IOException e) {
|
||||
// TODO Not sure what to do here
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void connect() throws UnknownHostException, IOException {
|
||||
if (!connected) {
|
||||
socket = new Socket(host, port);
|
||||
connected = socket.isConnected();
|
||||
outputStream = socket.getOutputStream();
|
||||
inputStream = socket.getInputStream();
|
||||
}
|
||||
}
|
||||
|
||||
public void disconnect() throws IOException {
|
||||
if (connected) {
|
||||
inputStream.close();
|
||||
outputStream.close();
|
||||
if (!socket.isClosed()) {
|
||||
socket.close();
|
||||
}
|
||||
connected = false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
return connected;
|
||||
}
|
||||
|
||||
public String set(String key, String value) {
|
||||
String command = protocol.buildCommand("SET", key, value);
|
||||
try {
|
||||
outputStream.write(command.getBytes());
|
||||
return protocol.getSingleLineReply(inputStream);
|
||||
} catch (IOException e) {
|
||||
// TODO Not sure what to do here
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public String get(String key) {
|
||||
String command = protocol.buildCommand("GET", key);
|
||||
try {
|
||||
outputStream.write(command.getBytes());
|
||||
return protocol.getBulkReply(inputStream);
|
||||
} catch (IOException e) {
|
||||
// TODO Not sure what to do here
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
116
src/main/java/redis/clients/jedis/Protocol.java
Normal file
116
src/main/java/redis/clients/jedis/Protocol.java
Normal file
@@ -0,0 +1,116 @@
|
||||
package redis.clients.jedis;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Protocol {
|
||||
public static final String DOLLAR = "$";
|
||||
public static final String ASTERISK = "*";
|
||||
public static final String PLUS = "+";
|
||||
public static final String COLON = ":";
|
||||
public static final String COMMAND_DELIMITER = "\r\n";
|
||||
public static final int DEFAULT_PORT = 6379;
|
||||
|
||||
public static final byte DOLLAR_BYTE = DOLLAR.getBytes()[0];
|
||||
public static final byte ASTERISK_BYTE = ASTERISK.getBytes()[0];
|
||||
public static final byte PLUS_BYTE = PLUS.getBytes()[0];
|
||||
public static final byte COLON_BYTE = COLON.getBytes()[0];
|
||||
|
||||
public String buildCommand(String name, String... args) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append(ASTERISK).append(args.length + 1).append(
|
||||
COMMAND_DELIMITER);
|
||||
builder.append(DOLLAR).append(name.length()).append(COMMAND_DELIMITER);
|
||||
builder.append(name).append(COMMAND_DELIMITER);
|
||||
for (String arg : args) {
|
||||
builder.append(DOLLAR).append(arg.length()).append(
|
||||
COMMAND_DELIMITER).append(arg).append(COMMAND_DELIMITER);
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public String getBulkReply(InputStream is) {
|
||||
String ret = null;
|
||||
try {
|
||||
if ((byte) is.read() == DOLLAR_BYTE) {
|
||||
int len = Integer.parseInt(readLine(is));
|
||||
if (len == -1) {
|
||||
return null;
|
||||
}
|
||||
byte[] read = new byte[len];
|
||||
is.read(read);
|
||||
// read 2 more bytes for the command delimiter
|
||||
is.read();
|
||||
is.read();
|
||||
|
||||
ret = new String(read);
|
||||
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// TODO Not sure that I should return null
|
||||
return null;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
private String readLine(InputStream is) throws IOException {
|
||||
byte b;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
while ((b = (byte) is.read()) != -1) {
|
||||
if (b == '\r') {
|
||||
b = (byte) is.read();
|
||||
if (b == '\n') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
sb.append((char) b);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public String getSingleLineReply(InputStream is) {
|
||||
String ret = null;
|
||||
try {
|
||||
if ((byte) is.read() == PLUS_BYTE) {
|
||||
ret = readLine(is);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// TODO Not sure that I should return null
|
||||
return null;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public int getIntegerReply(InputStream is) {
|
||||
int ret = 0;
|
||||
try {
|
||||
if ((byte) is.read() == COLON_BYTE) {
|
||||
String num = readLine(is);
|
||||
ret = Integer.parseInt(num);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// TODO Not sure that I should return 0
|
||||
return 0;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public List<String> getMultiBulkReply(InputStream is) {
|
||||
List<String> ret = new ArrayList<String>();
|
||||
try {
|
||||
if ((byte) is.read() == ASTERISK_BYTE) {
|
||||
int num = Integer.parseInt(readLine(is));
|
||||
for (int i = 0; i < num; i++) {
|
||||
ret.add(getBulkReply(is));
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// TODO Not sure that I should return null
|
||||
return null;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user