package simulator; import eduni.simjava.Sim_event; import eduni.simjava.Sim_system; import gridsim.GridSim; import gridsim.GridSimCore; import gridsim.GridSimTags; import gridsim.dcworms.DCWormsTags; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Properties; import schedframe.Initializable; import schedframe.SimulatedEnvironment; import schedframe.events.ResourceEventCommand; import schedframe.exceptions.ModuleException; import schedframe.exceptions.ResourceException; import schedframe.resources.ResourceType; import schedframe.resources.computing.ComputingResource; import schedframe.resources.computing.profiles.energy.ResourceEvent; import schedframe.scheduling.Scheduler; import schedframe.scheduling.plugin.Module; import schedframe.scheduling.plugin.ModuleType; public class EventManager extends GridSimCore implements Module{ protected SimulatedEnvironment simEnv; public EventManager(String name, SimulatedEnvironment resourceController) throws Exception { super(name, 1); this.simEnv = resourceController; Integer schedulerIdObj = new Integer(get_id()); List schedRes = GridSim.getGridResourceList(); schedRes.add(schedulerIdObj); } public void body() { for(Initializable initObj: simEnv.getToInit()){ initObj.initiate(); } simEnv.setInitList(null); // Process events until END_OF_SIMULATION is received from the // GridSimShutdown Entity Sim_event ev = new Sim_event(); sim_get_next(ev); boolean run = true; while (Sim_system.running() && run) { // sim_get_next(ev); // if the simulation finishes then exit the loop if (ev.get_tag() == GridSimTags.END_OF_SIMULATION) { // managemetnSystem_.setEndSimulation(); run = false; break; } // process the received event processRequest(ev); sim_get_next(ev); } } protected void processRequest(Sim_event ev) { switch (ev.get_tag()) { case DCWormsTags.TO_RESOURCES: sendToResources(ev); break; case DCWormsTags.TO_SCHEDULERS: //super.send((Integer)ev.get_data(), 0, DCWormsTags.PHASE_CHANGED, null); break; default: break; } } public void send(String entityName, double delay, int tag, Object data){ super.send(entityName, delay, tag, data); } public void send(int destId, double delay, int tag){ super.send(destId, delay, tag); } public void send(int destId, double delay, int tag, Object data){ super.send(destId, delay, tag, data); } public void init(Properties properties) throws ModuleException { } public void sendToAllSchedulers(double delay, int tag, Object data){ List allSchedulers = new ArrayList(); if (simEnv.getScheduler().getChildren() != null) { LinkedList toExamine = new LinkedList(); toExamine.push(simEnv.getScheduler()); allSchedulers.add(simEnv.getScheduler()); while (!toExamine.isEmpty()) { Scheduler scheduler = toExamine.pop(); List schedulers = scheduler.getChildren(); int numberOfSched = schedulers.size(); for (int i = 0; i < numberOfSched; i++) { Scheduler schedulerChild = schedulers.get(i); toExamine.addLast(schedulerChild); allSchedulers.add(schedulerChild); } } } for(Scheduler scheduler: allSchedulers){ //sendInternal(delay, DCWormsTags.TO_SCHEDULERS, scheduler.get_id()); super.send(scheduler.get_id(), delay, tag, data); } } public void sendToScheduler(String name, double delay, int tag, Object data){ Scheduler scheduler = null; if(simEnv.getScheduler().getFullName().equals(name)) scheduler = simEnv.getScheduler(); else if (simEnv.getScheduler().getChildren() != null) { LinkedList toExamine = new LinkedList(); toExamine.push(simEnv.getScheduler()); while (!toExamine.isEmpty()) { Scheduler sched = toExamine.pop(); List schedulers = sched.getChildren(); int numberOfSched = schedulers.size(); for (int i = 0; i < numberOfSched; i++) { Scheduler schedulerChild = schedulers.get(i); toExamine.addLast(schedulerChild); if(schedulerChild.getFullName().equals(name)){ scheduler = schedulerChild; break; } } } } if(scheduler != null){ super.send(scheduler.get_id(), delay, tag, data); } } public void sendToResources(ResourceType type, double delay, ResourceEvent event){ List computingResources = new ArrayList(); if (simEnv.getComputingResources() != null) { for(ComputingResource compRes: simEnv.getComputingResources()){ computingResources.addAll(compRes.getDescendantsByType(type)); } } sendInternal(delay, DCWormsTags.TO_RESOURCES, new ResourceBroadcastOrder(computingResources, event)); } public void sendToAllResources(double delay, ResourceEvent event){ List computingResources = new ArrayList(); if (simEnv.getComputingResources() != null) { LinkedList toExamine = new LinkedList(); for(ComputingResource compRes: simEnv.getComputingResources()){ toExamine.push(compRes); computingResources.add(compRes); } while (!toExamine.isEmpty()) { ComputingResource resource = toExamine.pop(); List resources = resource.getChildren(); int numberOfRes = resources.size(); for (int i = 0; i < numberOfRes; i++) { ComputingResource resourceChild = resources.get(i); toExamine.addLast(resourceChild); computingResources.add(resourceChild); } } } sendInternal(delay, DCWormsTags.TO_RESOURCES, new ResourceBroadcastOrder(computingResources, event)); } public void sendToResource(String resourceName, double delay, ResourceEvent event){ List computingResources = new ArrayList(); try { computingResources.add(SimulatedEnvironment.getComputingResourceByName(resourceName)); } catch (ResourceException e) { } sendInternal(delay, DCWormsTags.TO_RESOURCES, new ResourceBroadcastOrder(computingResources, event)); } private void sendToResources(Sim_event ev){ ResourceEventCommand rec; ResourceBroadcastOrder rbo = (ResourceBroadcastOrder)ev.get_data(); List computingResources = rbo.getComputingResources(); ResourceEvent event = rbo.getEvent(); for(ComputingResource compRes: computingResources){ //rec = new ResourceEventCommand(compRes); //rec.execute(event); compRes.handleEvent(event); } } public void sendInternal(double delay, int tag, Object data) { this.send(this.get_id(), delay, tag, data); } public void sendTo(List ids){ } public void dispose() throws ModuleException { } public ModuleType getType() { return ModuleType.EVENT_MANAGER; } } class ResourceBroadcastOrder { private List computingResources; private ResourceEvent event; public ResourceBroadcastOrder(List computingResources, ResourceEvent event) { this.computingResources = computingResources; this.event = event; } public List getComputingResources() { return computingResources; } public ResourceEvent getEvent() { return event; } }