1 | package simulator; |
---|
2 | |
---|
3 | import eduni.simjava.Sim_event; |
---|
4 | import eduni.simjava.Sim_system; |
---|
5 | import gridsim.GridSim; |
---|
6 | import gridsim.GridSimCore; |
---|
7 | import gridsim.GridSimTags; |
---|
8 | import gridsim.dcworms.DCWormsTags; |
---|
9 | |
---|
10 | import java.util.ArrayList; |
---|
11 | import java.util.LinkedList; |
---|
12 | import java.util.List; |
---|
13 | import java.util.Properties; |
---|
14 | |
---|
15 | import schedframe.Initializable; |
---|
16 | import schedframe.SimulatedEnvironment; |
---|
17 | import schedframe.events.ResourceEventCommand; |
---|
18 | import schedframe.exceptions.ModuleException; |
---|
19 | import schedframe.exceptions.ResourceException; |
---|
20 | import schedframe.resources.ResourceType; |
---|
21 | import schedframe.resources.computing.ComputingResource; |
---|
22 | import schedframe.resources.computing.profiles.energy.ResourceEvent; |
---|
23 | import schedframe.scheduling.Scheduler; |
---|
24 | import schedframe.scheduling.plugin.Module; |
---|
25 | import schedframe.scheduling.plugin.ModuleType; |
---|
26 | |
---|
27 | public class EventManager extends GridSimCore implements Module{ |
---|
28 | |
---|
29 | protected SimulatedEnvironment simEnv; |
---|
30 | |
---|
31 | public EventManager(String name, SimulatedEnvironment resourceController) throws Exception { |
---|
32 | super(name, 1); |
---|
33 | this.simEnv = resourceController; |
---|
34 | |
---|
35 | Integer schedulerIdObj = new Integer(get_id()); |
---|
36 | List<Integer> schedRes = GridSim.getGridResourceList(); |
---|
37 | schedRes.add(schedulerIdObj); |
---|
38 | } |
---|
39 | |
---|
40 | public void body() { |
---|
41 | |
---|
42 | for(Initializable initObj: simEnv.getToInit()){ |
---|
43 | initObj.initiate(); |
---|
44 | } |
---|
45 | simEnv.setInitList(null); |
---|
46 | |
---|
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 | |
---|
70 | case DCWormsTags.TO_RESOURCES: |
---|
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 | |
---|
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 | |
---|
98 | public void sendToAllSchedulers(double delay, int tag, Object data){ |
---|
99 | List<Scheduler> allSchedulers = new ArrayList<Scheduler>(); |
---|
100 | if (simEnv.getScheduler().getChildren() != null) { |
---|
101 | LinkedList<Scheduler> toExamine = new LinkedList<Scheduler>(); |
---|
102 | toExamine.push(simEnv.getScheduler()); |
---|
103 | allSchedulers.add(simEnv.getScheduler()); |
---|
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){ |
---|
118 | //sendInternal(delay, DCWormsTags.TO_SCHEDULERS, scheduler.get_id()); |
---|
119 | super.send(scheduler.get_id(), delay, tag, data); |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
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 | |
---|
151 | public void sendToResources(ResourceType type, double delay, ResourceEvent event){ |
---|
152 | List<ComputingResource> computingResources = new ArrayList<ComputingResource>(); |
---|
153 | |
---|
154 | if (simEnv.getComputingResources() != null) { |
---|
155 | for(ComputingResource compRes: simEnv.getComputingResources()){ |
---|
156 | computingResources.addAll(compRes.getDescendantsByType(type)); |
---|
157 | } |
---|
158 | } |
---|
159 | sendInternal(delay, DCWormsTags.TO_RESOURCES, new ResourceBroadcastOrder(computingResources, event)); |
---|
160 | } |
---|
161 | |
---|
162 | public void sendToAllResources(double delay, ResourceEvent event){ |
---|
163 | List<ComputingResource> computingResources = new ArrayList<ComputingResource>(); |
---|
164 | |
---|
165 | if (simEnv.getComputingResources() != null) { |
---|
166 | LinkedList<ComputingResource> toExamine = new LinkedList<ComputingResource>(); |
---|
167 | for(ComputingResource compRes: simEnv.getComputingResources()){ |
---|
168 | toExamine.push(compRes); |
---|
169 | computingResources.add(compRes); |
---|
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); |
---|
179 | computingResources.add(resourceChild); |
---|
180 | } |
---|
181 | } |
---|
182 | } |
---|
183 | sendInternal(delay, DCWormsTags.TO_RESOURCES, new ResourceBroadcastOrder(computingResources, event)); |
---|
184 | } |
---|
185 | |
---|
186 | public void sendToResource(String resourceName, double delay, ResourceEvent event){ |
---|
187 | List<ComputingResource> computingResources = new ArrayList<ComputingResource>(); |
---|
188 | |
---|
189 | try { |
---|
190 | computingResources.add(SimulatedEnvironment.getComputingResourceByName(resourceName)); |
---|
191 | } catch (ResourceException e) { |
---|
192 | |
---|
193 | } |
---|
194 | sendInternal(delay, DCWormsTags.TO_RESOURCES, new ResourceBroadcastOrder(computingResources, event)); |
---|
195 | } |
---|
196 | |
---|
197 | private void sendToResources(Sim_event ev){ |
---|
198 | ResourceEventCommand rec; |
---|
199 | |
---|
200 | ResourceBroadcastOrder rbo = (ResourceBroadcastOrder)ev.get_data(); |
---|
201 | List<ComputingResource> computingResources = rbo.getComputingResources(); |
---|
202 | ResourceEvent event = rbo.getEvent(); |
---|
203 | for(ComputingResource compRes: computingResources){ |
---|
204 | //rec = new ResourceEventCommand(compRes); |
---|
205 | //rec.execute(event); |
---|
206 | compRes.handleEvent(event); |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | public void sendInternal(double delay, int tag, Object data) { |
---|
211 | this.send(this.get_id(), delay, tag, data); |
---|
212 | } |
---|
213 | |
---|
214 | public void sendTo(List<String> ids){ |
---|
215 | } |
---|
216 | |
---|
217 | public void dispose() throws ModuleException { |
---|
218 | } |
---|
219 | |
---|
220 | public ModuleType getType() { |
---|
221 | return ModuleType.EVENT_MANAGER; |
---|
222 | } |
---|
223 | |
---|
224 | } |
---|
225 | |
---|
226 | class ResourceBroadcastOrder { |
---|
227 | |
---|
228 | private List<ComputingResource> computingResources; |
---|
229 | private ResourceEvent event; |
---|
230 | |
---|
231 | public ResourceBroadcastOrder(List<ComputingResource> computingResources, ResourceEvent event) { |
---|
232 | this.computingResources = computingResources; |
---|
233 | this.event = event; |
---|
234 | } |
---|
235 | |
---|
236 | public List<ComputingResource> getComputingResources() { |
---|
237 | return computingResources; |
---|
238 | } |
---|
239 | |
---|
240 | public ResourceEvent getEvent() { |
---|
241 | return event; |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | |
---|