Commit 921ae604 authored by amir.yosef's avatar amir.yosef

Applying Commands Response correctly

parent 988c6bf3
package command;
import java.io.IOException;
import java.io.OutputStream;
public interface CommandHandler {
void execute( OutputStream os) throws IOException;
byte [] execute( ) throws IOException;
}
package command;
import java.io.IOException;
import java.io.OutputStream;
public class CommandInvoker {
public static void invoke(CommandHandler command, OutputStream os) throws IOException {
command.execute(os);
public static byte[] invoke(CommandHandler command) throws IOException {
return command.execute();
}
}
......@@ -2,23 +2,17 @@ package command;
import util.Response;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
public class EchoCommand implements CommandHandler {
private final List<String> args;
public EchoCommand(List<String> args) {
this.args = args;
}
@Override
public void execute(OutputStream os) {
try {
os.write(Response.getResponse(args.getFirst()));
os.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
public byte[] execute() {
return (Response.getResponse(args.getFirst()));
}
}
......@@ -3,8 +3,6 @@ package command;
import storage.Storage;
import util.Response;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
......@@ -17,19 +15,12 @@ public class GetCommand implements CommandHandler {
}
@Override
public void execute(OutputStream os) {
public byte[] execute() {
try {
String response = storage.get(args.getFirst().toLowerCase());
if (response == null || response.isEmpty() || response.isBlank()) {
os.write("$-1\r\n".getBytes());
os.flush();
return;
}
os.write(Response.getResponse(response));
os.flush();
} catch (IOException e) {
throw new RuntimeException(e);
String response = storage.get(args.getFirst().toLowerCase());
if (response == null || response.isEmpty() || response.isBlank()) {
return ("$-1\r\n".getBytes());
}
return (Response.getResponse(response));
}
}
......@@ -3,18 +3,10 @@ package command;
import model.Command;
import java.io.IOException;
import java.io.OutputStream;
public class PingCommand implements CommandHandler {
@Override
public void execute(OutputStream os) {
try {
os.write(("+" + Command.PONG.getValue() + "\r\n").getBytes());
os.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
public byte[] execute() {
return (("+" + Command.PONG.getValue() + "\r\n").getBytes());
}
}
\ No newline at end of file
......@@ -3,8 +3,6 @@ package command;
import model.Command;
import storage.Storage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -18,7 +16,7 @@ public class SetCommand implements CommandHandler {
}
@Override
public void execute(OutputStream os) {
public byte[] execute() {
Map<String, String> commandsMap = new HashMap<>();
......@@ -40,11 +38,7 @@ public class SetCommand implements CommandHandler {
storage.save(args.getFirst().toLowerCase(), value);
}
try {
os.write("+OK\r\n".getBytes());
os.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
return ("+OK\r\n".getBytes());
}
}
package command;
import java.io.IOException;
import java.io.OutputStream;
public class UnknownCommand implements CommandHandler {
@Override
public void execute(OutputStream os) {
try {
os.write(("-" + "Unknown Command" + "\r\n").getBytes());
os.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
public byte[] execute() {
return (("-" + "Unknown Command" + "\r\n").getBytes());
}
}
......@@ -26,7 +26,9 @@ public class ClientCommandHandler {
commands.removeFirst();
CommandHandler commandProcessor = factory.getCommand(command, commands);
try {
CommandInvoker.invoke(commandProcessor, os);
byte[] result = CommandInvoker.invoke(commandProcessor);
os.write(result);
os.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
......
......@@ -62,6 +62,7 @@ public class Storage {
timeToExpiration.remove(key);
currentTimeForKey.remove(key);
}
void runCachePolicy() {
storage.runMaintenance();
}
......
......@@ -14,6 +14,7 @@ public class StorageManager {
this.scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(this::performMaintenance, 2, 2, TimeUnit.MINUTES);
}
private void performMaintenance() {
storage.runCachePolicy();
System.out.println("Maintenance performed at: " + Date.from(java.time.Clock.systemUTC().instant()));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment