Commit 03aa0c1a authored by amir.yosef's avatar amir.yosef

clean up

parent a1b617f9
import director.Director; import director.Director;
import server.Server; import server.Server;
import server.ServerBuilder; import server.ServerBuilder;
import util.Settings;
import java.util.Map; import java.util.Arrays;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
Map<String, String> parameters = Settings.extractArgs(args);
int port = 6379;
port = Settings.extractPort(parameters, port);
Director director = new Director(); Director director = new Director();
ServerBuilder builder = new ServerBuilder(); // System.out.println(Arrays.toString(args));
ServerBuilder builder = new ServerBuilder(args);
director.buildMaster(builder); director.buildMaster(builder);
builder.setPort(port);
try (Server server = builder.build()) { try (Server server = builder.build()) {
server.start(); server.start();
} }
......
package client.primary; package client.master;
import client.Client; import client.Client;
...@@ -12,7 +12,6 @@ import java.util.List; ...@@ -12,7 +12,6 @@ import java.util.List;
public class MasterClient extends Client { public class MasterClient extends Client {
public MasterClient(Socket socket) { public MasterClient(Socket socket) {
super(socket); super(socket);
......
...@@ -2,7 +2,7 @@ package command; ...@@ -2,7 +2,7 @@ package command;
import model.Command; import model.Command;
import util.Response; import util.Response;
import util.ServerInfo; import server.ServerInfo;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
......
...@@ -18,6 +18,10 @@ public class SetCommand implements CommandHandler { ...@@ -18,6 +18,10 @@ public class SetCommand implements CommandHandler {
@Override @Override
public byte[] execute() { public byte[] execute() {
return getBytes(args, storage);
}
private byte[] getBytes(List<String> args, Storage storage) {
Map<String, String> commandsMap = new HashMap<>(); Map<String, String> commandsMap = new HashMap<>();
for (int i = 2; i < args.size(); i += 2) { for (int i = 2; i < args.size(); i += 2) {
...@@ -25,7 +29,6 @@ public class SetCommand implements CommandHandler { ...@@ -25,7 +29,6 @@ public class SetCommand implements CommandHandler {
} }
String value = args.get(1); String value = args.get(1);
String expiration = commandsMap.get(Command.PX.getValue().toLowerCase()); String expiration = commandsMap.get(Command.PX.getValue().toLowerCase());
if (expiration != null) { if (expiration != null) {
try { try {
...@@ -37,8 +40,6 @@ public class SetCommand implements CommandHandler { ...@@ -37,8 +40,6 @@ public class SetCommand implements CommandHandler {
} else { } else {
storage.save(args.getFirst().toLowerCase(), value); storage.save(args.getFirst().toLowerCase(), value);
} }
return ("+OK\r\n".getBytes()); return ("+OK\r\n".getBytes());
} }
} }
package server; package server;
import client.Client; import client.Client;
import client.primary.MasterClient; import client.master.MasterClient;
import storage.StorageManager; import storage.StorageManager;
import java.io.IOException; import java.io.IOException;
......
package server; package server;
import util.Settings;
import java.util.Map;
public class ServerBuilder { public class ServerBuilder {
private int port = 6379; private int port = 6379;
private String role; private String role;
String[] masterPortAndHost;
private final static ServerInfo info = ServerInfo.getInstance();
Map<String, String> parameters;
public void setPort(int port) { public void setPort(int port) {
this.port = port; this.port = Settings.extractPort(parameters, port);
}
public ServerBuilder(String[] args) {
this.parameters = Settings.extractArgs(args);
this.port = Settings.extractPort(parameters, port);
this.masterPortAndHost = info.findRole(parameters);
} }
public void setRole(String role) { public void setRole(String role) {
......
package util; package server;
import java.util.Map; import java.util.Map;
......
...@@ -6,8 +6,8 @@ import java.util.Map; ...@@ -6,8 +6,8 @@ import java.util.Map;
public class Settings { public class Settings {
public static int extractPort(Map<String, String> args, int port) { public static int extractPort(Map<String, String> args, int port) {
if (args.containsKey("--setPort")) { if (args.containsKey("--Port")) {
port = Integer.parseInt(args.get("--setPort")); port = Integer.parseInt(args.get("--Port"));
} }
return port; return port;
} }
......
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