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

Info Command ( Replication step 2 )

parent 6bafb578
package command;
import model.Command;
import util.Response;
import util.ServerInfo;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class InfoCommand implements CommandHandler {
private final ServerInfo configuration = ServerInfo.getInstance();
List<String> args;
public InfoCommand(List<String> args) {
this.args = args;
}
@Override
public byte[] execute() {
String command = args.getFirst();
if (command.equalsIgnoreCase(Command.REPLICATION.getValue())) {
Map<String, String> info = configuration.getInfo();
String response = info.entrySet()
.stream()
.map(data -> data.getKey() + ":" + data.getValue())
.collect(Collectors.joining());
return Response.getResponse(response);
}
return null;
}
}
\ No newline at end of file
......@@ -25,6 +25,7 @@ public class CommandFactory implements Factory {
case ECHO -> new EchoCommand(args);
case SET -> new SetCommand(args);
case GET -> new GetCommand(args);
case INFO -> new InfoCommand(args);
// case REPLCONF -> new ReplConfCommand(replicaReceiver);
// case PSYNC -> new FullResyncCommandProcessor(replicaSender);
// case WAIT -> new WaitCommandProcessor(replicaSender, replicaReceiver);
......
......@@ -23,7 +23,7 @@ public class Server implements AutoCloseable {
public void start() {
try (ServerSocket serverSocket = new ServerSocket(PORT)) {
System.out.println("Server started on setPort " + PORT);
System.out.println("Server started on Port " + PORT);
while (true) {
serverSocket.setReuseAddress(true);
......
package util;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class ServerInfo {
private static ServerInfo serverInfo;
private final Map<String, String> info = new ConcurrentHashMap<>();
private ServerInfo() {
info.put("role", "master");
}
public static ServerInfo getInstance() {
if (serverInfo == null) {
serverInfo = new ServerInfo();
}
return serverInfo;
}
public String getRole() {
return serverInfo.info.get("role");
}
public String[] findRole(Map<String, String> parameters) {
String[] masterPortAndHost = new String[]{};
String role = "master";
if (parameters.containsKey("--replicaof")) {
masterPortAndHost = parameters.get("--replicaof").split(" ");
role = "slave";
} else {
info.put("master_repl_offset", "0");
info.put("master_replid", "8371b4fb1155b71f4a04d3e1bc3e18c4a990aeeb");
}
info.put("role", role);
return masterPortAndHost;
}
public Map<String, String> getInfo() {
return this.info;
}
}
......@@ -19,7 +19,6 @@ public class Settings {
parameters.put(args[i], args[i + 1]);
}
System.out.println(parameters);
return parameters;
}
}
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