Skip to content

Commit

Permalink
adds listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
CharafeddineMechalikh committed Apr 4, 2024
1 parent b714b13 commit 51e8821
Show file tree
Hide file tree
Showing 22 changed files with 123 additions and 130 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Version 5.3 Changelog (April 4th 2024)

* Improved code quality (Listener Design Pattern)
* Switched back to Java 8 for broader compatibility

## Version 5.2 Changelog (June 11th 2023)

* Improved performance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@

import com.mechalikh.pureedgesim.scenariomanager.SimulationParameters;
import com.mechalikh.pureedgesim.simulationengine.Event;
import com.mechalikh.pureedgesim.simulationengine.OnSimulationStartListener;
import com.mechalikh.pureedgesim.simulationengine.SimEntity;
import com.mechalikh.pureedgesim.simulationmanager.SimulationManager;
/**
* This abstract class represents a computing node in the simulation.
*/
public abstract class AbstractNode extends SimEntity implements ComputingNode {
public abstract class AbstractNode extends SimEntity implements ComputingNode, OnSimulationStartListener {

/**
* The update status event ID.
Expand Down Expand Up @@ -82,7 +83,8 @@ protected AbstractNode(SimulationManager simulationManager) {
* Defines the logic to be performed by the computing node when the simulation
* starts.
*/
public void startInternal() {
@Override
public void onSimulationStart() {
scheduleNow(this, UPDATE_STATUS);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.w3c.dom.Element;

import com.mechalikh.pureedgesim.locationmanager.MobilityModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,6 @@ public void setApplicationPlacementLocation(ComputingNode node) {

}

@Override
protected void onSimulationEnd() {
// Do something when the simulation finishes.
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,14 @@ protected void generateDataCenters(String file, TYPES type) {
cloudOnlyList.add(computingNode);
mistAndCloudListSensorsExcluded.add(computingNode);
if (SimulationParameters.enableOrchestrators
&& SimulationParameters.deployOrchestrators == "CLOUD") {
&& "CLOUD".equals(SimulationParameters.deployOrchestrators)) {
orchestratorsList.add(computingNode);
}
} else {
edgeOnlyList.add(computingNode);
mistAndEdgeListSensorsExcluded.add(computingNode);
if (SimulationParameters.enableOrchestrators
&& SimulationParameters.deployOrchestrators == "EDGE") {
&& "EDGE".equals(SimulationParameters.deployOrchestrators)) {
orchestratorsList.add(computingNode);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ protected LocationAwareNode(SimulationManager simulationManager) {
}

@Override
public void startInternal() {
super.startInternal();
public void onSimulationStart() {
super.onSimulationStart();
mobilityModel.generatePath();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ public Router(SimulationManager simulationManager) {
super(simulationManager, 0, 0, 0, 0);
}

@Override
public void startInternal() {
// Do nothing
}

@Override
public void setApplicationPlacementLocation(ComputingNode node) {
// Do nothing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,21 +257,5 @@ protected void offloadingRequestRecievedByOrchestrator(TransferProgress transfer
scheduleNow(simulationManager, DefaultSimulationManager.SEND_TASK_FROM_ORCH_TO_DESTINATION, transfer.getTask());
}

/**
* Defines the logic to be performed by the default network model when the
* simulation starts.
*/
@Override
public void startInternal() {
// Do nothing.
}

/**
* Defines the logic to be performed by the default network model when the
* simulation ends.
*/
@Override
public void onSimulationEnd() {
// Do something when the simulation finishes.
}

}
17 changes: 0 additions & 17 deletions PureEdgeSim/com/mechalikh/pureedgesim/network/NetworkLink.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,6 @@ public NetworkLink setBandwidth(double bandwidth) {
return this;
}

/**
* Defines the logic to be performed by the network link when the simulation
* starts.
*/
@Override
public void startInternal() {
// Do nothing for now.
}

public ComputingNode getSrc() {
return src;
}
Expand Down Expand Up @@ -230,12 +221,4 @@ public double getTotalTransferredData() {
return totalTrasferredData;
}

/**
* Defines the logic to be performed by the network link when the simulation
* ends.
*/
@Override
public void onSimulationEnd() {
// Do something when the simulation finishes.
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mechalikh.pureedgesim.scenariomanager;

import java.util.Arrays;
import java.util.List;

import org.w3c.dom.Document;
Expand Down Expand Up @@ -34,9 +35,9 @@ protected boolean typeSpecificChecking(Document xmlDoc) {
}

protected void checkEdgeDevice(Element deviceElement) {
for (String element : List.of("connectivity", "mobility", "battery", "percentage", "speed", "minPauseDuration",
for (String element : Arrays.asList(new String[]{"connectivity", "mobility", "battery", "percentage", "speed", "minPauseDuration",
"maxPauseDuration", "minMobilityDuration", "maxMobilityDuration", "batteryCapacity", "generateTasks",
"isOrchestrator", "idleConsumption", "maxConsumption", "cores", "mips", "ram", "storage"))
"isOrchestrator", "idleConsumption", "maxConsumption", "cores", "mips", "ram", "storage"}))
isElementPresent(deviceElement, element);

for (String element : List.of("speed", "minPauseDuration", "minMobilityDuration", "batteryCapacity",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public class SimulationParameters {
/**
* Simualtion time in seconds.
*
* @see com.mechalikh.pureedgesim.simulationmanager.DefaultSimulationManager#startInternal()
* @see com.mechalikh.pureedgesim.simulationmanager.DefaultSimulationManager#onSimulationStart()
*/
public static double simulationDuration;

Expand Down Expand Up @@ -459,7 +459,7 @@ public enum TYPES {
/**
* The network model update interval.
*
* @see com.mechalikh.pureedgesim.network.NetworkLink#startInternal()
* @see com.mechalikh.pureedgesim.network.NetworkLink#onSimulationStart()
*/
public static double networkUpdateInterval;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.mechalikh.pureedgesim.simulationengine;

/**
* An interface to listen for events when the simulation ends.
*/
public interface OnSimulationEndListener {
/**
* Called when the simulation ends.
*/
void onSimulationEnd();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.mechalikh.pureedgesim.simulationengine;

/**
* An interface to listen for events when the simulation starts.
*/
public interface OnSimulationStartListener {
/**
* Called when the simulation starts.
*/
void onSimulationStart();
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,13 @@
* <p>
* Once the PureEdgeSim simulation engine has started, it notifies all
* simulation entities of the start of the simulation in order to schedule their
* first event. This is guaranteed by the {@link SimEntity#startInternal()
* startInternal()} method.
* first event. This is guaranteed by the {@link OnSimulationStartListener#onSimulationStart()} method.
*
* @see com.mechalikh.pureedgesim.simulationengine.PureEdgeSim#start()
* @see com.mechalikh.pureedgesim.simulationmanager.SimulationThread#startSimulation()
* @see SimEntity#startInternal()
* @see OnSimulationStartListener.onSimulationStart()
* @see com.mechalikh.pureedgesim.simulationmanager.DefaultSimulationManager#startSimulation()
* @see com.mechalikh.pureedgesim.simulationmanager.DefaultSimulationManager#startInternal()
* @see com.mechalikh.pureedgesim.simulationmanager.DefaultSimulationManager#onSimulationStart()
*
* @author Charafeddine Mechalikh
* @since PureEdgeSim 5.0
Expand Down Expand Up @@ -82,9 +81,9 @@ public PureEdgeSim() {
}

/**
* Starts the simulation. First, it notifies all simulation entities that the
* simulation has started by calling their {@link SimEntity#startInternal()
* startInternal()} method. The entities will then schedule their first events.
* Starts the simulation. First, it notifies all simulation entities that implement the {@link OnSimulationStartListener} interface that the
* simulation has started by calling their {@link OnSimulationStartListener#onSimulationStart()
* onSimulationStart()} method. The entities will then schedule their first events.
* Once, all the entities has scheduled their fist events, a loop will go
* through all those events to process them one by one, and the simulation time
* is updated according to the time of last processed event. With each processed
Expand All @@ -96,19 +95,28 @@ public PureEdgeSim() {
* @see com.mechalikh.pureedgesim.simulationmanager.Simulation#launchSimulation()
* @see com.mechalikh.pureedgesim.simulationmanager.SimulationThread#startSimulation()
* @see com.mechalikh.pureedgesim.simulationmanager.DefaultSimulationManager#startSimulation()
* @see SimEntity#startInternal()
* @see OnSimulationStartListener.onSimulationStart()
* @see #terminate()
*/
public void start() {
// Notify all entities that the simulation has started.
entitiesList.forEach(SimEntity::startInternal);
// Notify all entities that the simulation has started.
entitiesList.forEach(entity -> {
if (entity instanceof OnSimulationStartListener) {
((OnSimulationStartListener) entity).onSimulationStart();
}
});


while (runClockTickAndProcessFutureEvents(Double.MAX_VALUE) && isRunning) {
// All the processing happens inside the method called above
}

// Iteration finished, notify all entities and clear their list
entitiesList.forEach(SimEntity::onSimulationEnd);
entitiesList.forEach(entity -> {
if (entity instanceof OnSimulationEndListener) {
((OnSimulationEndListener) entity).onSimulationEnd();
}
});
entitiesList.clear();
}

Expand Down Expand Up @@ -185,7 +193,7 @@ protected void processEvent(final Event event) {
* @param event the new event.
* @see SimEntity#schedule(SimEntity, Double, int)
* @see SimEntity#schedule(SimEntity, Double, int, Object)
* @see SimEntity#startInternal()
* @see OnSimulationStartListener#onSimulationStart()
* @see FutureQueue
* @see #start()
* @see #runClockTickAndProcessFutureEvents(double)
Expand All @@ -201,7 +209,7 @@ void insert(Event event) {
* @param event the new event.
* @see SimEntity#schedule(SimEntity, Double, int)
* @see SimEntity#schedule(SimEntity, Double, int, Object)
* @see SimEntity#startInternal()
* @see OnSimulationStartListener#onSimulationStart()
* @see FutureQueue
* @see #start()
* @see #runClockTickAndProcessFutureEvents(double)
Expand All @@ -222,7 +230,7 @@ public void insertFirst(Event event) {
* @see com.mechalikh.pureedgesim.simulationmanager.SimulationThread#loadModels(DefaultSimulationManager
* simulationManager)
* @see #start()
* @see SimEntity#startInternal()
* @see OnSimulationStartListener#onSimulationStart()
*/
public void addEntity(SimEntity simEntity) {
entitiesList.add(simEntity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ protected Event scheduleNow(SimEntity simEntity, int tag, Object data) {
return e;
}

protected abstract void startInternal();

protected abstract void onSimulationEnd();

protected abstract void processEvent(Event e);

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.mechalikh.pureedgesim.scenariomanager.SimulationParameters;
import com.mechalikh.pureedgesim.scenariomanager.SimulationParameters.TYPES;
import com.mechalikh.pureedgesim.simulationengine.Event;
import com.mechalikh.pureedgesim.simulationengine.OnSimulationStartListener;
import com.mechalikh.pureedgesim.simulationengine.PureEdgeSim;
import com.mechalikh.pureedgesim.simulationvisualizer.SimulationVisualizer;
import com.mechalikh.pureedgesim.taskgenerator.Task;
Expand All @@ -50,7 +51,7 @@
* @author Charafeddine Mechalikh
* @since PureEdgeSim 4.2
*/
public class DefaultSimulationManager extends SimulationManager {
public class DefaultSimulationManager extends SimulationManager implements OnSimulationStartListener {

/**
* Simulation progress parameters.
Expand Down Expand Up @@ -111,7 +112,7 @@ public void startSimulation() {
* simulation starts.
*/
@Override
public void startInternal() {
public void onSimulationStart() {
// Initialize logger variables.
simLog.setGeneratedTasks(taskList.size());
simLog.setCurrentOrchPolicy(scenario.getStringOrchArchitecture());
Expand Down Expand Up @@ -448,13 +449,6 @@ protected boolean sameLocation(ComputingNode Dev1, ComputingNode Dev2) {
return (distance < RANGE);
}

/**
* Defines the logic to be performed by the simulation manager when the
* simulation starts.
*/
@Override
public void onSimulationEnd() {
// Do something when the simulation finishes.
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
import com.mechalikh.pureedgesim.energy.EnergyModelNetworkLink;
import com.mechalikh.pureedgesim.network.NetworkLink;
import com.mechalikh.pureedgesim.network.TransferProgress;
import com.mechalikh.pureedgesim.scenariomanager.SimulationParameters;
import com.mechalikh.pureedgesim.scenariomanager.SimulationParameters.TYPES;
import com.mechalikh.pureedgesim.scenariomanager.SimulationParameters;
import com.mechalikh.pureedgesim.taskgenerator.Task;

public class SimLog {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,8 @@ protected void loadModels(SimulationManager simulationManager) throws Exception
// Initialize the network model
SimLog.println(this.getClass().getSimpleName() + " - Initializing the Network Module...");
Constructor<?> networkConstructor = simulation.networkModel.getConstructor(SimulationManager.class);
networkConstructor.newInstance(simulationManager);

long startTime = System.currentTimeMillis();

networkConstructor.newInstance(simulationManager);

// Generate all data centers, servers, an devices
SimLog.println(this.getClass().getSimpleName() + " - Initializing the Datacenters Manager Module...");
new DataCentersManager(simulationManager, simulation.mobilityModel, simulation.computingNode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,7 @@ public void resultsReturned(Task task) {
// Do something with the task that has been finished

}

@Override
public void startInternal() {
// Do something when the simulation starts. e.g., schedule some events
}

@Override
public void onSimulationEnd() {
// Do something when the simulation finishes. e.g., print some results.

}


@Override
public void processEvent(Event e) {
// Process the scheduled events, if any.
Expand Down
Loading

0 comments on commit 51e8821

Please sign in to comment.