Small refactoring to make command definitions even smaller
This commit is contained in:
@@ -7,19 +7,24 @@ import java.net.Socket;
|
|||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
|
||||||
public class Client {
|
public class Client {
|
||||||
protected String host;
|
private String host;
|
||||||
protected int port = Protocol.DEFAULT_PORT;
|
private int port = Protocol.DEFAULT_PORT;
|
||||||
protected Socket socket;
|
private Socket socket;
|
||||||
protected boolean connected = false;
|
private boolean connected = false;
|
||||||
protected Protocol protocol = new Protocol();
|
private Protocol protocol = new Protocol();
|
||||||
protected OutputStream outputStream;
|
private OutputStream outputStream;
|
||||||
protected InputStream inputStream;
|
private InputStream inputStream;
|
||||||
|
|
||||||
public Client(String host) {
|
public Client(String host) {
|
||||||
super();
|
super();
|
||||||
this.host = host;
|
this.host = host;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected Client sendCommand(String name, String... args) {
|
||||||
|
protocol.sendCommand(outputStream, name, args);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public Client(String host, int port) {
|
public Client(String host, int port) {
|
||||||
super();
|
super();
|
||||||
this.host = host;
|
this.host = host;
|
||||||
@@ -69,4 +74,12 @@ public class Client {
|
|||||||
return connected;
|
return connected;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected String getSingleLineReply() {
|
||||||
|
return protocol.getSingleLineReply(inputStream);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getBulkReply() {
|
||||||
|
return protocol.getBulkReply(inputStream);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,49 +1,21 @@
|
|||||||
package redis.clients.jedis;
|
package redis.clients.jedis;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class Jedis extends Client {
|
public class Jedis extends Client {
|
||||||
public Jedis(String host) {
|
public Jedis(String host) {
|
||||||
super(host);
|
super(host);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String ping() {
|
public String ping() {
|
||||||
// TODO: I want to be able to do the followin
|
return sendCommand("PING").getSingleLineReply();
|
||||||
// return protocol.runCommand(os,
|
|
||||||
// "PING").getSingleLineReply(inputStream);
|
|
||||||
// or even maybe
|
|
||||||
// return protocol.runCommand("PING").getSingleLineReply();
|
|
||||||
|
|
||||||
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 String set(String key, String value) {
|
public String set(String key, String value) {
|
||||||
String command = protocol.buildCommand("SET", key, value);
|
return sendCommand("SET", key, value).getSingleLineReply();
|
||||||
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) {
|
public String get(String key) {
|
||||||
String command = protocol.buildCommand("GET", key);
|
return sendCommand("GET", key).getBulkReply();
|
||||||
try {
|
|
||||||
outputStream.write(command.getBytes());
|
|
||||||
return protocol.getBulkReply(inputStream);
|
|
||||||
} catch (IOException e) {
|
|
||||||
// TODO Not sure what to do here
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package redis.clients.jedis;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -18,7 +19,7 @@ public class Protocol {
|
|||||||
public static final byte PLUS_BYTE = PLUS.getBytes()[0];
|
public static final byte PLUS_BYTE = PLUS.getBytes()[0];
|
||||||
public static final byte COLON_BYTE = COLON.getBytes()[0];
|
public static final byte COLON_BYTE = COLON.getBytes()[0];
|
||||||
|
|
||||||
public String buildCommand(String name, String... args) {
|
public void sendCommand(OutputStream os, String name, String... args) {
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
builder.append(ASTERISK).append(args.length + 1).append(
|
builder.append(ASTERISK).append(args.length + 1).append(
|
||||||
COMMAND_DELIMITER);
|
COMMAND_DELIMITER);
|
||||||
@@ -28,7 +29,11 @@ public class Protocol {
|
|||||||
builder.append(DOLLAR).append(arg.length()).append(
|
builder.append(DOLLAR).append(arg.length()).append(
|
||||||
COMMAND_DELIMITER).append(arg).append(COMMAND_DELIMITER);
|
COMMAND_DELIMITER).append(arg).append(COMMAND_DELIMITER);
|
||||||
}
|
}
|
||||||
return builder.toString();
|
try {
|
||||||
|
os.write(builder.toString().getBytes());
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO don't know what to do here!
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getBulkReply(InputStream is) {
|
public String getBulkReply(InputStream is) {
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
package redis.clients.jedis.tests;
|
package redis.clients.jedis.tests;
|
||||||
|
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.io.PipedInputStream;
|
||||||
|
import java.io.PipedOutputStream;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -13,13 +17,24 @@ import redis.clients.jedis.Protocol;
|
|||||||
|
|
||||||
public class ProtocolTest extends Assert {
|
public class ProtocolTest extends Assert {
|
||||||
@Test
|
@Test
|
||||||
public void buildACommand() {
|
public void buildACommand() throws IOException {
|
||||||
Protocol protocol = new Protocol();
|
PipedInputStream pis = new PipedInputStream();
|
||||||
String command = protocol.buildCommand("GET", "SOMEKEY");
|
BufferedInputStream bis = new BufferedInputStream(pis);
|
||||||
|
PipedOutputStream pos = new PipedOutputStream(pis);
|
||||||
|
|
||||||
|
Protocol protocol = new Protocol();
|
||||||
|
protocol.sendCommand(pos, "GET", "SOMEKEY");
|
||||||
|
|
||||||
|
pos.close();
|
||||||
String expectedCommand = "*2\r\n$3\r\nGET\r\n$7\r\nSOMEKEY\r\n";
|
String expectedCommand = "*2\r\n$3\r\nGET\r\n$7\r\nSOMEKEY\r\n";
|
||||||
|
|
||||||
assertEquals(expectedCommand, command);
|
int b;
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
while ((b = bis.read()) != -1) {
|
||||||
|
sb.append((char) b);
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(expectedCommand, sb.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user