[1427] | 1 | package simulator; |
---|
[776] | 2 | |
---|
[793] | 3 | import eduni.simjava.Sim_event; |
---|
| 4 | import eduni.simjava.Sim_system; |
---|
[1447] | 5 | import gridsim.GridSim; |
---|
[776] | 6 | import gridsim.GridSimCore; |
---|
[793] | 7 | import gridsim.GridSimTags; |
---|
| 8 | import gridsim.dcworms.DCWormsTags; |
---|
[776] | 9 | |
---|
[780] | 10 | import java.util.ArrayList; |
---|
| 11 | import java.util.LinkedList; |
---|
| 12 | import java.util.List; |
---|
[776] | 13 | import java.util.Properties; |
---|
| 14 | |
---|
[1300] | 15 | import schedframe.Initializable; |
---|
[1226] | 16 | import schedframe.SimulatedEnvironment; |
---|
[780] | 17 | import schedframe.events.ResourceEventCommand; |
---|
[776] | 18 | import schedframe.exceptions.ModuleException; |
---|
[1300] | 19 | import schedframe.exceptions.ResourceException; |
---|
[1169] | 20 | import schedframe.resources.ResourceType; |
---|
[780] | 21 | import schedframe.resources.computing.ComputingResource; |
---|
[1421] | 22 | import schedframe.resources.computing.profiles.energy.ResourceEvent; |
---|
[780] | 23 | import schedframe.scheduling.Scheduler; |
---|
[1398] | 24 | import schedframe.scheduling.plugin.Module; |
---|
| 25 | import schedframe.scheduling.plugin.ModuleType; |
---|
[776] | 26 | |
---|
| 27 | public class EventManager extends GridSimCore implements Module{ |
---|
| 28 | |
---|
[1300] | 29 | protected SimulatedEnvironment simEnv; |
---|
[780] | 30 | |
---|
[1226] | 31 | public EventManager(String name, SimulatedEnvironment resourceController) throws Exception { |
---|
[776] | 32 | super(name, 1); |
---|
[1300] | 33 | this.simEnv = resourceController; |
---|
[1447] | 34 | |
---|
| 35 | Integer schedulerIdObj = new Integer(get_id()); |
---|
| 36 | List<Integer> schedRes = GridSim.getGridResourceList(); |
---|
| 37 | schedRes.add(schedulerIdObj); |
---|
[776] | 38 | } |
---|
| 39 | |
---|
[793] | 40 | public void body() { |
---|
[1300] | 41 | |
---|
| 42 | for(Initializable initObj: simEnv.getToInit()){ |
---|
| 43 | initObj.initiate(); |
---|
| 44 | } |
---|
| 45 | simEnv.setInitList(null); |
---|
| 46 | |
---|
[793] | 47 | // Process events until END_OF_SIMULATION is received from the |
---|
| 48 | // GridSimShutdown Entity |
---|
| 49 | Sim_event ev = new Sim_event(); |
---|
| 50 | sim_get_next(ev); |
---|
| 51 | boolean run = true; |
---|
| 52 | while (Sim_system.running() && run) { |
---|
| 53 | // sim_get_next(ev); |
---|
| 54 | // if the simulation finishes then exit the loop |
---|
| 55 | if (ev.get_tag() == GridSimTags.END_OF_SIMULATION) { |
---|
| 56 | // managemetnSystem_.setEndSimulation(); |
---|
| 57 | run = false; |
---|
| 58 | break; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | // process the received event |
---|
| 62 | processRequest(ev); |
---|
| 63 | sim_get_next(ev); |
---|
| 64 | } |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | protected void processRequest(Sim_event ev) { |
---|
| 68 | switch (ev.get_tag()) { |
---|
| 69 | |
---|
[1421] | 70 | case DCWormsTags.TO_RESOURCES: |
---|
[793] | 71 | sendToResources(ev); |
---|
| 72 | break; |
---|
| 73 | |
---|
| 74 | case DCWormsTags.TO_SCHEDULERS: |
---|
| 75 | //super.send((Integer)ev.get_data(), 0, DCWormsTags.PHASE_CHANGED, null); |
---|
| 76 | break; |
---|
| 77 | |
---|
| 78 | default: |
---|
| 79 | break; |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | |
---|
[776] | 83 | public void send(String entityName, double delay, int tag, Object data){ |
---|
| 84 | super.send(entityName, delay, tag, data); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | public void send(int destId, double delay, int tag){ |
---|
| 88 | super.send(destId, delay, tag); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | public void send(int destId, double delay, int tag, Object data){ |
---|
| 92 | super.send(destId, delay, tag, data); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | public void init(Properties properties) throws ModuleException { |
---|
| 96 | } |
---|
| 97 | |
---|
[780] | 98 | public void sendToAllSchedulers(double delay, int tag, Object data){ |
---|
| 99 | List<Scheduler> allSchedulers = new ArrayList<Scheduler>(); |
---|
[1300] | 100 | if (simEnv.getScheduler().getChildren() != null) { |
---|
[780] | 101 | LinkedList<Scheduler> toExamine = new LinkedList<Scheduler>(); |
---|
[1300] | 102 | toExamine.push(simEnv.getScheduler()); |
---|
| 103 | allSchedulers.add(simEnv.getScheduler()); |
---|
[780] | 104 | |
---|
| 105 | while (!toExamine.isEmpty()) { |
---|
| 106 | Scheduler scheduler = toExamine.pop(); |
---|
| 107 | List<Scheduler> schedulers = scheduler.getChildren(); |
---|
| 108 | int numberOfSched = schedulers.size(); |
---|
| 109 | for (int i = 0; i < numberOfSched; i++) { |
---|
| 110 | Scheduler schedulerChild = schedulers.get(i); |
---|
| 111 | toExamine.addLast(schedulerChild); |
---|
| 112 | allSchedulers.add(schedulerChild); |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | for(Scheduler scheduler: allSchedulers){ |
---|
[793] | 118 | //sendInternal(delay, DCWormsTags.TO_SCHEDULERS, scheduler.get_id()); |
---|
[780] | 119 | super.send(scheduler.get_id(), delay, tag, data); |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | |
---|
[1551] | 123 | public void sendToScheduler(String name, double delay, int tag, Object data){ |
---|
| 124 | Scheduler scheduler = null; |
---|
| 125 | if(simEnv.getScheduler().getFullName().equals(name)) |
---|
| 126 | scheduler = simEnv.getScheduler(); |
---|
| 127 | else if (simEnv.getScheduler().getChildren() != null) { |
---|
| 128 | LinkedList<Scheduler> toExamine = new LinkedList<Scheduler>(); |
---|
| 129 | toExamine.push(simEnv.getScheduler()); |
---|
| 130 | |
---|
| 131 | while (!toExamine.isEmpty()) { |
---|
| 132 | Scheduler sched = toExamine.pop(); |
---|
| 133 | List<Scheduler> schedulers = sched.getChildren(); |
---|
| 134 | int numberOfSched = schedulers.size(); |
---|
| 135 | for (int i = 0; i < numberOfSched; i++) { |
---|
| 136 | Scheduler schedulerChild = schedulers.get(i); |
---|
| 137 | toExamine.addLast(schedulerChild); |
---|
| 138 | if(schedulerChild.getFullName().equals(name)){ |
---|
| 139 | scheduler = schedulerChild; |
---|
| 140 | break; |
---|
| 141 | } |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | } |
---|
| 145 | if(scheduler != null){ |
---|
| 146 | super.send(scheduler.get_id(), delay, tag, data); |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | } |
---|
| 150 | |
---|
[1421] | 151 | public void sendToResources(ResourceType type, double delay, ResourceEvent event){ |
---|
| 152 | List<ComputingResource> computingResources = new ArrayList<ComputingResource>(); |
---|
[1169] | 153 | |
---|
[1300] | 154 | if (simEnv.getComputingResources() != null) { |
---|
| 155 | for(ComputingResource compRes: simEnv.getComputingResources()){ |
---|
[1421] | 156 | computingResources.addAll(compRes.getDescendantsByType(type)); |
---|
[1169] | 157 | } |
---|
| 158 | } |
---|
[1421] | 159 | sendInternal(delay, DCWormsTags.TO_RESOURCES, new ResourceBroadcastOrder(computingResources, event)); |
---|
[1169] | 160 | } |
---|
| 161 | |
---|
[1421] | 162 | public void sendToAllResources(double delay, ResourceEvent event){ |
---|
| 163 | List<ComputingResource> computingResources = new ArrayList<ComputingResource>(); |
---|
[780] | 164 | |
---|
[1300] | 165 | if (simEnv.getComputingResources() != null) { |
---|
[780] | 166 | LinkedList<ComputingResource> toExamine = new LinkedList<ComputingResource>(); |
---|
[1300] | 167 | for(ComputingResource compRes: simEnv.getComputingResources()){ |
---|
[780] | 168 | toExamine.push(compRes); |
---|
[1421] | 169 | computingResources.add(compRes); |
---|
[780] | 170 | } |
---|
| 171 | |
---|
| 172 | while (!toExamine.isEmpty()) { |
---|
| 173 | ComputingResource resource = toExamine.pop(); |
---|
| 174 | List<ComputingResource> resources = resource.getChildren(); |
---|
| 175 | int numberOfRes = resources.size(); |
---|
| 176 | for (int i = 0; i < numberOfRes; i++) { |
---|
| 177 | ComputingResource resourceChild = resources.get(i); |
---|
| 178 | toExamine.addLast(resourceChild); |
---|
[1421] | 179 | computingResources.add(resourceChild); |
---|
[780] | 180 | } |
---|
| 181 | } |
---|
| 182 | } |
---|
[1421] | 183 | sendInternal(delay, DCWormsTags.TO_RESOURCES, new ResourceBroadcastOrder(computingResources, event)); |
---|
[793] | 184 | } |
---|
| 185 | |
---|
[1421] | 186 | public void sendToResource(String resourceName, double delay, ResourceEvent event){ |
---|
| 187 | List<ComputingResource> computingResources = new ArrayList<ComputingResource>(); |
---|
[1300] | 188 | |
---|
| 189 | try { |
---|
[1421] | 190 | computingResources.add(SimulatedEnvironment.getComputingResourceByName(resourceName)); |
---|
[1300] | 191 | } catch (ResourceException e) { |
---|
| 192 | |
---|
| 193 | } |
---|
[1421] | 194 | sendInternal(delay, DCWormsTags.TO_RESOURCES, new ResourceBroadcastOrder(computingResources, event)); |
---|
[1300] | 195 | } |
---|
| 196 | |
---|
[793] | 197 | private void sendToResources(Sim_event ev){ |
---|
| 198 | ResourceEventCommand rec; |
---|
[780] | 199 | |
---|
[793] | 200 | ResourceBroadcastOrder rbo = (ResourceBroadcastOrder)ev.get_data(); |
---|
[1421] | 201 | List<ComputingResource> computingResources = rbo.getComputingResources(); |
---|
| 202 | ResourceEvent event = rbo.getEvent(); |
---|
| 203 | for(ComputingResource compRes: computingResources){ |
---|
[1454] | 204 | //rec = new ResourceEventCommand(compRes); |
---|
| 205 | //rec.execute(event); |
---|
| 206 | compRes.handleEvent(event); |
---|
[780] | 207 | } |
---|
| 208 | } |
---|
| 209 | |
---|
[793] | 210 | public void sendInternal(double delay, int tag, Object data) { |
---|
| 211 | this.send(this.get_id(), delay, tag, data); |
---|
| 212 | } |
---|
| 213 | |
---|
[780] | 214 | public void sendTo(List<String> ids){ |
---|
| 215 | } |
---|
| 216 | |
---|
[776] | 217 | public void dispose() throws ModuleException { |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | public ModuleType getType() { |
---|
| 221 | return ModuleType.EVENT_MANAGER; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | } |
---|
[793] | 225 | |
---|
| 226 | class ResourceBroadcastOrder { |
---|
| 227 | |
---|
| 228 | private List<ComputingResource> computingResources; |
---|
[1421] | 229 | private ResourceEvent event; |
---|
[793] | 230 | |
---|
[1421] | 231 | public ResourceBroadcastOrder(List<ComputingResource> computingResources, ResourceEvent event) { |
---|
[793] | 232 | this.computingResources = computingResources; |
---|
| 233 | this.event = event; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | public List<ComputingResource> getComputingResources() { |
---|
| 237 | return computingResources; |
---|
| 238 | } |
---|
| 239 | |
---|
[1421] | 240 | public ResourceEvent getEvent() { |
---|
[793] | 241 | return event; |
---|
| 242 | } |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | |
---|