Setting initial parameters from Coordinator How to pass them to the Workers
LSim allows to set parameters to the Workers from the Coordinator.
There are two ways: Sending a parameter to a specific worker or sending a parameter to all workers of the same type.
The first thing to do is to implement an init handler.
The Init Handler
In this handler is where the parameters will be added to the Workers. The default InitHandler class that LSim contains is the following:
package lsim.application.handler;
import edu.uoc.dpcs.lsim.utils.LSimParameters;
import lsim.LSim;
/**
* An implementation of a handler for a initial phase. It is useful in
* coordinator and evaluator cases.
*
* @author Esteve Verdura Gelmà
*
*/
public class InitHandler implements Handler {
private LSim lsim;
private int time;
private LSimParameters param;
/**
* Create an instance of initial handler
*
* @param LSim instance to set experiment time
* @param Time (in minutes) that suppose that will expend the experiment. The
* application will stop in case that time will pass.
*/
public InitHandler(LSim lsim, int time) {
this.lsim = lsim;
this.time = time;
}
public Object execute(Object obj) {
if (time > 0) lsim.setExperimentTime(time);
param = (LSimParameters) obj;
return null;
}
/**
* Gets the parameters for the element
*
* @return Parameters
*/
public LSimParameters getParameters() {
return param;
}
}
This class can be used as a template to create our own init handler.
The addInitParam method
It is as simple as calling lsim.addInitParam(worker, name, param)
in the execute method, where:
worker
is a String which can be a workerId or a worker type. Depending on the case, the parameters will be sent to a specific worker or to all workers of a certain type.name
is a String which is the key name of the parameter.param
is an Object which is the value of the parameter.
Modifying our Handler
It’s time to modify the execute method of our handler. For example, following with the tutorial example, imagine that we want to add a parameter to a specific Worker (an instance of Server Worker). We need to add:
lsim.addInitParam("Wserver0", "name", "value");
Now, imagine that we want to add a parameter to all workers of a certain type (application2 in this case). We need to add:
lsim.addInitParam("application2", "name", "value");
As we see, Wserver0
is a workerId while application2
is a worker type. So, the Wserver0
instance will receive a parameter and also all the instances of the type application2
.
If we want to send a parameter to the Evaluator, is as simple as calling the same method this way:
lsim.addInitParam("evaluator", "name", "value");
It is also possible to send parameters to the Evaluator, we only need to call the method using the String evaluator
.
The execute method of our handler will look like this:
public Object execute(Object obj) {
if (time > 0) lsim.setExperimentTime(time);
param = (LSimParameters) obj;
lsim.addInitParam("Wserver0", "name", "value");
lsim.addInitParam("application2", "name", "value");
lsim.addInitParam("evaluator", "name", "value");
return null;
}
Getting the parameters from the Worker
They way to get the parameters setted by Coordinator is the same than getting them from the XML, that has been explained in the tutorial.