Commit 6bafb578 authored by amir.yosef's avatar amir.yosef

Configure listening port ( Replication step 1 )

parent 03b93694
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JsBuildToolPackageJson" sorting="DEFINITION_ORDER" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_22" default="true" project-jdk-name="22" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_22" default="true" project-jdk-name="ibm-22" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
import director.Director;
import server.ServerBuilder;
import server.Server;
import server.ServerBuilder;
import util.Settings;
import java.util.Map;
public class Main {
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();
ServerBuilder builder = new ServerBuilder();
director.buildMaster(builder);
builder.port(16379);
builder.setPort(port);
try (Server server = builder.build()) {
server.start();
}
......
......@@ -4,10 +4,10 @@ import server.ServerBuilder;
public class Director {
public void buildMaster(ServerBuilder builder) {
builder.role("Master");
builder.setRole("Master");
}
public void buildReplica(ServerBuilder builder) {
builder.role("Replica");
builder.setRole("Replica");
}
}
......@@ -13,19 +13,17 @@ import java.util.concurrent.Executors;
public class Server implements AutoCloseable {
private final int PORT;
private final ExecutorService executor;
private final String role;
StorageManager manager = new StorageManager();
Server(int port, String role) {
PORT = port;
this.role = role;
this.executor = Executors.newVirtualThreadPerTaskExecutor();
}
public void start() {
try (ServerSocket serverSocket = new ServerSocket(PORT)) {
System.out.println("Server started on port " + PORT);
System.out.println("Server started on setPort " + PORT);
while (true) {
serverSocket.setReuseAddress(true);
......
package server;
public class ServerBuilder {
private int port;
private int port = 6379;
private String role;
public void port(int port) {
public void setPort(int port) {
this.port = port;
}
public void role(String role) {
public void setRole(String role) {
this.role = role;
}
......
......@@ -12,7 +12,7 @@ public class StorageManager {
public StorageManager() {
this.storage = Storage.getInstance();
this.scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(this::performMaintenance, 30, 30, TimeUnit.SECONDS);
scheduler.scheduleAtFixedRate(this::performMaintenance, 2, 2, TimeUnit.MINUTES);
}
private void performMaintenance() {
......
package util;
import java.util.HashMap;
import java.util.Map;
public class Settings {
public static int extractPort(Map<String, String> args, int port) {
if (args.containsKey("--setPort")) {
port = Integer.parseInt(args.get("--setPort"));
}
return port;
}
public static Map<String, String> extractArgs(String[] args) {
Map<String, String> parameters = new HashMap<>();
for (int i = 0; i < args.length; i++) {
if (i + 1 >= args.length) break;
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