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