diff --git a/src/main/java/it/unitn/disi/ds1/qtop/Client.java b/src/main/java/it/unitn/disi/ds1/qtop/Client.java index d13a161..72b59e3 100644 --- a/src/main/java/it/unitn/disi/ds1/qtop/Client.java +++ b/src/main/java/it/unitn/disi/ds1/qtop/Client.java @@ -31,6 +31,13 @@ public class Client extends AbstractActor { private int timeOutCounter; private int requestNumber; + /** + * Constructor for the Client actor. + * + * @param clientId the client id + * @param group the list of Nodes in the network + * @param numberOfNodes the number of Nodes in the network + */ public Client(int clientId, List group, int numberOfNodes) { super(); this.clientId = clientId; @@ -49,6 +56,15 @@ public Client(int clientId, List group, int numberOfNodes) { ); } + /** + * Wrapper for the Client actor. + * + * @param clientId the client id + * @param group the list of Nodes in the network + * @param numberOfNodes the number of Nodes in the network + * + * @return the Props object + */ static public Props props(int clientId, List group, int numberOfNodes) { return Props.create( Client.class, diff --git a/src/main/java/it/unitn/disi/ds1/qtop/Controller.java b/src/main/java/it/unitn/disi/ds1/qtop/Controller.java index 6887fb7..6fce650 100644 --- a/src/main/java/it/unitn/disi/ds1/qtop/Controller.java +++ b/src/main/java/it/unitn/disi/ds1/qtop/Controller.java @@ -8,6 +8,12 @@ public class Controller { private final Simulation simulation; private final UserInterface ui; + /** + * Constructor for the Controller. + * + * @param simulation the simulation + * @param ui the user interface + */ public Controller(Simulation simulation, UserInterface ui) { this.simulation = simulation; this.ui = ui; diff --git a/src/main/java/it/unitn/disi/ds1/qtop/Node.java b/src/main/java/it/unitn/disi/ds1/qtop/Node.java index 6aac5eb..2337313 100644 --- a/src/main/java/it/unitn/disi/ds1/qtop/Node.java +++ b/src/main/java/it/unitn/disi/ds1/qtop/Node.java @@ -41,6 +41,16 @@ public class Node extends AbstractActor { private final Logger logger = Logger.getInstance(); + /** + * Constructor for the Node class. + * + * @param coordinator the coordinator ActorRef + * @param nodeId the node ID + * @param voteTimeout the vote timeout + * @param writeTimeout the write timeout + * @param electionGlobalTimeout the global election timeout + * @param numberOfNodes the number of nodes in the network + */ public Node(ActorRef coordinator, int nodeId, int voteTimeout, int writeTimeout, int electionGlobalTimeout, int numberOfNodes) { this.history = new PairsHistory(); @@ -65,6 +75,18 @@ public Node(ActorRef coordinator, int nodeId, int voteTimeout, int writeTimeout, } } + /** + * Create the Props for the Node class. + * + * @param coordinator the coordinator ActorRef + * @param nodeId the node ID + * @param voteTimeout the vote timeout + * @param writeTimeout the write timeout + * @param electionGlobalTimeout the global election timeout + * @param numberOfNodes the number of nodes in the network + * + * @return the Props object + */ static public Props props(ActorRef coordinator, int nodeId, int voteTimeout, int writeTimeout, int electionGlobalTimeout, int numberOfNodes) { return Props.create( @@ -573,7 +595,7 @@ private void onElection(@NotNull Election msg) { false ); } - else if (msg.isGreaterThanLocalData( + else if (msg.isBetterThanLocalData( this.nodeId, nodeLatest )) @@ -597,7 +619,7 @@ else if (msg.isGreaterThanLocalData( this.becomeVoter(); this.timeOutManager.startElectionState(); this.isElection = true; - if (msg.isGreaterThanLocalData( + if (msg.isBetterThanLocalData( this.nodeId, nodeLatest )) diff --git a/src/main/java/it/unitn/disi/ds1/qtop/QTop.java b/src/main/java/it/unitn/disi/ds1/qtop/QTop.java index 73e0bf8..88e11de 100644 --- a/src/main/java/it/unitn/disi/ds1/qtop/QTop.java +++ b/src/main/java/it/unitn/disi/ds1/qtop/QTop.java @@ -3,20 +3,25 @@ /** * Main class to start the simulation. */ -public class QTop{ +public class QTop { - public static void main(String[] args) { - //Initializes the simulation - Simulation simulation = new Simulation(); - - //User interface and client interface - UserInterface ui = new UserInterface(null); - - //Object to handle ui commands - Controller controller = new Controller(simulation, ui); - - //set the controller to handle callbacks - ui.setController(controller); - ui.start(); - } + /** + * Main method to start the simulation. + * + * @param args Command line arguments. + */ + public static void main(String[] args) { + //Initializes the simulation + Simulation simulation = new Simulation(); + //User interface and client interface + UserInterface ui = new UserInterface(null); + //Object to handle ui commands + Controller controller = new Controller( + simulation, + ui + ); + //set the controller to handle callbacks + ui.setController(controller); + ui.start(); + } } \ No newline at end of file diff --git a/src/main/java/it/unitn/disi/ds1/qtop/Simulation.java b/src/main/java/it/unitn/disi/ds1/qtop/Simulation.java index d83ebc7..c7bdd04 100644 --- a/src/main/java/it/unitn/disi/ds1/qtop/Simulation.java +++ b/src/main/java/it/unitn/disi/ds1/qtop/Simulation.java @@ -17,6 +17,9 @@ public class Simulation { private final Logger logger = Logger.getInstance(); + /** + * Create a new simulation. + */ public Simulation() { group = new ArrayList<>(); system = ActorSystem.create("qtop"); diff --git a/src/main/java/it/unitn/disi/ds1/qtop/TimeOutManager.java b/src/main/java/it/unitn/disi/ds1/qtop/TimeOutManager.java index 76149b1..ae2f940 100644 --- a/src/main/java/it/unitn/disi/ds1/qtop/TimeOutManager.java +++ b/src/main/java/it/unitn/disi/ds1/qtop/TimeOutManager.java @@ -12,10 +12,26 @@ * TimeOutManager class to manage the timeouts of a Node */ public class TimeOutManager extends EnumMap>> { - // Phony map to associate a reason with its specific refresh ratio + /** + * Phony map to associate a reason with its specific refresh ratio + */ private final EnumMap customTimeouts; + /** + * Refresh ration of the timeouts + */ private final int refresh; + /** + * TimeOutManager constructor. + * + * @param voteTimeout the timeout for the vote + * @param heartbeatTimeout the timeout for the heartbeat + * @param writeTimeout the timeout to write + * @param crashResponseTimeout the timeout for the crash response + * @param electionGlobalTimeout the timeout for the global election phase + * @param clientRequestTimeout the timeout for the client request + * @param refresh the refresh ratio + */ public TimeOutManager(int voteTimeout, int heartbeatTimeout, int writeTimeout, int crashResponseTimeout, int electionGlobalTimeout, int clientRequestTimeout, int refresh) { super(Utils.TimeOutReason.class); diff --git a/src/main/java/it/unitn/disi/ds1/qtop/UserInterface.java b/src/main/java/it/unitn/disi/ds1/qtop/UserInterface.java index 7fdc7a6..db71533 100644 --- a/src/main/java/it/unitn/disi/ds1/qtop/UserInterface.java +++ b/src/main/java/it/unitn/disi/ds1/qtop/UserInterface.java @@ -8,6 +8,11 @@ public class UserInterface implements SimulationCallback { private Controller controller; + /** + * UserInterface constructor. + * + * @param controller the controller + */ public UserInterface(Controller controller) { this.controller = controller; } diff --git a/src/main/java/it/unitn/disi/ds1/qtop/Utils.java b/src/main/java/it/unitn/disi/ds1/qtop/Utils.java index c3cf5aa..4ee2ef2 100644 --- a/src/main/java/it/unitn/disi/ds1/qtop/Utils.java +++ b/src/main/java/it/unitn/disi/ds1/qtop/Utils.java @@ -161,8 +161,15 @@ public enum CrashType { /** * Start message that sends the list of participants to everyone. + * + * @param group the list of participants */ public record StartMessage(List group) implements Serializable { + /** + * Copy constructor + * + * @param group the list of participants + */ public StartMessage(List group) { this.group = List.copyOf(group); } @@ -221,6 +228,8 @@ public record HeartBeat() implements Serializable { /** * Message to ask for a crash. + * + * @param crashType the type of crash to trigger */ public record CrashRequest(CrashType crashType) implements Serializable { } @@ -294,7 +303,15 @@ public EpochPair(EpochPair ep) { * @param bestCandidateId node that have that EpochPair */ public record Election(int highestEpoch, int highestIteration, int bestCandidateId) implements Serializable { - public boolean isGreaterThanLocalData(int nodeId, @NotNull EpochPair ep) { + /** + * Check if the data received is better than the local data. + * + * @param nodeId the id of the node that sent the data + * @param ep the EpochPair received + * + * @return true if the data received is greater than the local data + */ + public boolean isBetterThanLocalData(int nodeId, @NotNull EpochPair ep) { return ((this.highestEpoch() > ep.e()) || (this.highestEpoch() == ep.e() && this.highestIteration() > ep.i()) || (this.highestEpoch() == ep.e() && this.highestIteration() == ep.i() && this.bestCandidateId() < nodeId)); } } diff --git a/src/main/java/it/unitn/disi/ds1/qtop/VotersMap.java b/src/main/java/it/unitn/disi/ds1/qtop/VotersMap.java index da04248..e56fa7f 100644 --- a/src/main/java/it/unitn/disi/ds1/qtop/VotersMap.java +++ b/src/main/java/it/unitn/disi/ds1/qtop/VotersMap.java @@ -10,6 +10,9 @@ */ public class VotersMap extends ArrayList> { + /** + * VotersMap constructor. + */ public VotersMap() { super(); } @@ -57,7 +60,12 @@ public void insert(int e, int i, ActorRef actorRef, Utils.Vote vote) { * @param e the epoch * @param i the index */ - public void setDecision(Utils.Decision d, int e, int i){ - this.get(e).set(i, new Utils.VotePair(this.get(e).get(i).votes(), d)); + public void setDecision(Utils.Decision d, int e, int i) { + this.get(e).set(i, + new Utils.VotePair( + this.get(e).get(i).votes(), + d + ) + ); } }