Commit c9425153 authored by amir.yosef's avatar amir.yosef

make the Invoker independent from command's args

parent 10acd291
...@@ -2,8 +2,7 @@ package command; ...@@ -2,8 +2,7 @@ package command;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.List;
public interface CommandHandler { public interface CommandHandler {
void execute(List<String> args, OutputStream os) throws IOException; void execute( OutputStream os) throws IOException;
} }
...@@ -2,10 +2,9 @@ package command; ...@@ -2,10 +2,9 @@ package command;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.List;
public class CommandInvoker { public class CommandInvoker {
public static void invoke(CommandHandler command, List<String> CommandAndArgs, OutputStream os) throws IOException { public static void invoke(CommandHandler command, OutputStream os) throws IOException {
command.execute(CommandAndArgs, os); command.execute(os);
} }
} }
...@@ -7,8 +7,13 @@ import java.io.OutputStream; ...@@ -7,8 +7,13 @@ 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;
public EchoCommand(List<String> args) {
this.args = args;
}
@Override @Override
public void execute(List<String> args, OutputStream os) { public void execute(OutputStream os) {
try { try {
os.write(Response.getResponse(args.getFirst())); os.write(Response.getResponse(args.getFirst()));
os.flush(); os.flush();
......
...@@ -10,10 +10,14 @@ import java.util.List; ...@@ -10,10 +10,14 @@ import java.util.List;
public class GetCommand implements CommandHandler { public class GetCommand implements CommandHandler {
private final Storage storage = Storage.getInstance(); private final Storage storage = Storage.getInstance();
private final List<String> args;
public GetCommand(List<String> args) {
this.args = args;
}
@Override @Override
public void execute(List<String> args, OutputStream os) { public void execute(OutputStream os) {
try { try {
String response = storage.get(args.getFirst().toLowerCase()); String response = storage.get(args.getFirst().toLowerCase());
......
...@@ -5,13 +5,11 @@ import model.Command; ...@@ -5,13 +5,11 @@ import model.Command;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.List;
public class PingCommand implements CommandHandler { public class PingCommand implements CommandHandler {
@Override @Override
public void execute(List<String> args, OutputStream os) { public void execute(OutputStream os) {
System.out.printf("Processing PING args %s%n", args);
try { try {
os.write(("+" + Command.PONG.getValue() + "\r\n").getBytes()); os.write(("+" + Command.PONG.getValue() + "\r\n").getBytes());
os.flush(); os.flush();
......
...@@ -10,10 +10,15 @@ import java.util.List; ...@@ -10,10 +10,15 @@ import java.util.List;
import java.util.Map; import java.util.Map;
public class SetCommand implements CommandHandler { public class SetCommand implements CommandHandler {
private final Storage redisStorage = Storage.getInstance(); private final Storage storage = Storage.getInstance();
private final List<String>args;
public SetCommand(List<String> args) {
this.args = args;
}
@Override @Override
public void execute(List<String> args, OutputStream os) { public void execute(OutputStream os) {
Map<String, String> commandsMap = new HashMap<>(); Map<String, String> commandsMap = new HashMap<>();
...@@ -27,12 +32,12 @@ public class SetCommand implements CommandHandler { ...@@ -27,12 +32,12 @@ public class SetCommand implements CommandHandler {
if (expiration != null) { if (expiration != null) {
try { try {
Long expirationTime = Long.parseLong(expiration); Long expirationTime = Long.parseLong(expiration);
redisStorage.save(args.getFirst().toLowerCase(), value, expirationTime); storage.save(args.getFirst().toLowerCase(), value, expirationTime);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
System.out.println("NumberFormatException: " + e.getMessage()); System.out.println("NumberFormatException: " + e.getMessage());
} }
} else { } else {
redisStorage.save(args.getFirst().toLowerCase(), value); storage.save(args.getFirst().toLowerCase(), value);
} }
try { try {
......
...@@ -2,11 +2,10 @@ package command; ...@@ -2,11 +2,10 @@ package command;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.List;
public class UnknownCommand implements CommandHandler { public class UnknownCommand implements CommandHandler {
@Override @Override
public void execute(List<String> args, OutputStream os) { public void execute(OutputStream os) {
try { try {
os.write(("-" + "Unknown Command" + "\r\n").getBytes()); os.write(("-" + "Unknown Command" + "\r\n").getBytes());
os.flush(); os.flush();
......
...@@ -3,6 +3,8 @@ package factory; ...@@ -3,6 +3,8 @@ package factory;
import command.*; import command.*;
import model.Command; import model.Command;
import java.util.List;
public class CommandFactory implements Factory { public class CommandFactory implements Factory {
private static final class FactoryHolder { private static final class FactoryHolder {
...@@ -18,12 +20,12 @@ public class CommandFactory implements Factory { ...@@ -18,12 +20,12 @@ public class CommandFactory implements Factory {
} }
@Override @Override
public CommandHandler getCommand(Command command) { public CommandHandler getCommand(Command command, List<String> args) {
return switch (command) { return switch (command) {
case PING -> new PingCommand(); case PING -> new PingCommand();
case ECHO -> new EchoCommand(); case ECHO -> new EchoCommand(args);
case SET -> new SetCommand(); case SET -> new SetCommand(args);
case GET -> new GetCommand(); case GET -> new GetCommand(args);
// case REPLCONF -> new ReplConfCommand(replicaReceiver); // case REPLCONF -> new ReplConfCommand(replicaReceiver);
// case PSYNC -> new FullResyncCommandProcessor(replicaSender); // case PSYNC -> new FullResyncCommandProcessor(replicaSender);
// case WAIT -> new WaitCommandProcessor(replicaSender, replicaReceiver); // case WAIT -> new WaitCommandProcessor(replicaSender, replicaReceiver);
......
...@@ -4,6 +4,8 @@ package factory; ...@@ -4,6 +4,8 @@ package factory;
import command.CommandHandler; import command.CommandHandler;
import model.Command; import model.Command;
import java.util.List;
public interface Factory { public interface Factory {
CommandHandler getCommand(Command command); CommandHandler getCommand(Command command, List<String> args);
} }
...@@ -24,13 +24,11 @@ public class ClientCommandHandler { ...@@ -24,13 +24,11 @@ public class ClientCommandHandler {
public void execute() { public void execute() {
Command command = CommandUtil.getCommand(commands.getFirst()); Command command = CommandUtil.getCommand(commands.getFirst());
commands.removeFirst(); commands.removeFirst();
CommandHandler commandProcessor = factory.getCommand(command); CommandHandler commandProcessor = factory.getCommand(command, commands);
try { try {
CommandInvoker.invoke(commandProcessor, commands, os); CommandInvoker.invoke(commandProcessor, os);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
} }
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