Skip to content

Commit

Permalink
add: doc
Browse files Browse the repository at this point in the history
  • Loading branch information
elblasco committed Aug 7, 2024
1 parent 74f10a8 commit 0286b02
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 21 deletions.
16 changes: 16 additions & 0 deletions src/main/java/it/unitn/disi/ds1/qtop/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ActorRef> group, int numberOfNodes) {
super();
this.clientId = clientId;
Expand All @@ -49,6 +56,15 @@ public Client(int clientId, List<ActorRef> 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<ActorRef> group, int numberOfNodes) {
return Props.create(
Client.class,
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/it/unitn/disi/ds1/qtop/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
26 changes: 24 additions & 2 deletions src/main/java/it/unitn/disi/ds1/qtop/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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(
Expand Down Expand Up @@ -573,7 +595,7 @@ private void onElection(@NotNull Election msg) {
false
);
}
else if (msg.isGreaterThanLocalData(
else if (msg.isBetterThanLocalData(
this.nodeId,
nodeLatest
))
Expand All @@ -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
))
Expand Down
35 changes: 20 additions & 15 deletions src/main/java/it/unitn/disi/ds1/qtop/QTop.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
3 changes: 3 additions & 0 deletions src/main/java/it/unitn/disi/ds1/qtop/Simulation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/it/unitn/disi/ds1/qtop/TimeOutManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,26 @@
* TimeOutManager class to manage the timeouts of a Node
*/
public class TimeOutManager extends EnumMap<Utils.TimeOutReason, ArrayList<Pair<Cancellable, Integer>>> {
// 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<Utils.TimeOutReason, Integer> 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);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/it/unitn/disi/ds1/qtop/UserInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
19 changes: 18 additions & 1 deletion src/main/java/it/unitn/disi/ds1/qtop/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ActorRef> group) implements Serializable {
/**
* Copy constructor
*
* @param group the list of participants
*/
public StartMessage(List<ActorRef> group) {
this.group = List.copyOf(group);
}
Expand Down Expand Up @@ -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 {
}
Expand Down Expand Up @@ -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));
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/it/unitn/disi/ds1/qtop/VotersMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
*/
public class VotersMap extends ArrayList<ArrayList<Utils.VotePair>> {

/**
* VotersMap constructor.
*/
public VotersMap() {
super();
}
Expand Down Expand Up @@ -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
)
);
}
}

0 comments on commit 0286b02

Please sign in to comment.