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

Applying Commands Response correctly

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