Small refactoring to separate commands from connection
This commit is contained in:
@@ -6,16 +6,14 @@ import java.io.OutputStream;
|
|||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
|
||||||
import redis.clients.jedis.Protocol;
|
|
||||||
|
|
||||||
public class Client {
|
public class Client {
|
||||||
private String host;
|
protected String host;
|
||||||
private int port = Protocol.DEFAULT_PORT;
|
protected int port = Protocol.DEFAULT_PORT;
|
||||||
private Socket socket;
|
protected Socket socket;
|
||||||
private boolean connected = false;
|
protected boolean connected = false;
|
||||||
private Protocol protocol = new Protocol();
|
protected Protocol protocol = new Protocol();
|
||||||
private OutputStream outputStream;
|
protected OutputStream outputStream;
|
||||||
private InputStream inputStream;
|
protected InputStream inputStream;
|
||||||
|
|
||||||
public Client(String host) {
|
public Client(String host) {
|
||||||
super();
|
super();
|
||||||
@@ -47,17 +45,6 @@ public class Client {
|
|||||||
public Client() {
|
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 {
|
public void connect() throws UnknownHostException, IOException {
|
||||||
if (!connected) {
|
if (!connected) {
|
||||||
socket = new Socket(host, port);
|
socket = new Socket(host, port);
|
||||||
@@ -82,25 +69,4 @@ public class Client {
|
|||||||
return connected;
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -23,29 +23,6 @@ public class ClientTest extends Assert {
|
|||||||
client.disconnect();
|
client.disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void ping() throws UnknownHostException, IOException {
|
|
||||||
client.setHost("localhost");
|
|
||||||
client.connect();
|
|
||||||
|
|
||||||
assertTrue(client.isConnected());
|
|
||||||
|
|
||||||
String status = client.ping();
|
|
||||||
assertEquals("PONG", status);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void setAndGet() throws UnknownHostException, IOException {
|
|
||||||
client.setHost("localhost");
|
|
||||||
client.connect();
|
|
||||||
|
|
||||||
String status = client.set("foo", "bar");
|
|
||||||
assertEquals("OK", status);
|
|
||||||
|
|
||||||
String value = client.get("foo");
|
|
||||||
assertEquals("bar", value);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(expected = UnknownHostException.class)
|
@Test(expected = UnknownHostException.class)
|
||||||
public void checkUnkownHost() throws UnknownHostException, IOException {
|
public void checkUnkownHost() throws UnknownHostException, IOException {
|
||||||
client.setHost("someunknownhost");
|
client.setHost("someunknownhost");
|
||||||
|
|||||||
Reference in New Issue
Block a user