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