source: DCWoRMS/branches/coolemall/src/simulator/EventManager.java @ 1454

Revision 1454, 6.3 KB checked in by wojtekp, 10 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package simulator;
2
3import eduni.simjava.Sim_event;
4import eduni.simjava.Sim_system;
5import gridsim.GridSim;
6import gridsim.GridSimCore;
7import gridsim.GridSimTags;
8import gridsim.dcworms.DCWormsTags;
9
10import java.util.ArrayList;
11import java.util.LinkedList;
12import java.util.List;
13import java.util.Properties;
14
15import schedframe.Initializable;
16import schedframe.SimulatedEnvironment;
17import schedframe.events.ResourceEventCommand;
18import schedframe.exceptions.ModuleException;
19import schedframe.exceptions.ResourceException;
20import schedframe.resources.ResourceType;
21import schedframe.resources.computing.ComputingResource;
22import schedframe.resources.computing.profiles.energy.ResourceEvent;
23import schedframe.scheduling.Scheduler;
24import schedframe.scheduling.plugin.Module;
25import schedframe.scheduling.plugin.ModuleType;
26
27public 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 sendToResources(ResourceType type, double delay, ResourceEvent event){
124                List<ComputingResource> computingResources = new ArrayList<ComputingResource>();
125
126                if (simEnv.getComputingResources() != null) {
127                        for(ComputingResource compRes: simEnv.getComputingResources()){
128                                computingResources.addAll(compRes.getDescendantsByType(type));
129                        }
130                }
131                sendInternal(delay, DCWormsTags.TO_RESOURCES, new ResourceBroadcastOrder(computingResources, event));
132        }
133       
134        public void sendToAllResources(double delay, ResourceEvent event){
135                List<ComputingResource> computingResources = new ArrayList<ComputingResource>();
136
137                if (simEnv.getComputingResources() != null) {
138                        LinkedList<ComputingResource> toExamine = new LinkedList<ComputingResource>();
139                        for(ComputingResource compRes: simEnv.getComputingResources()){
140                                toExamine.push(compRes);
141                                computingResources.add(compRes);
142                        }
143
144                        while (!toExamine.isEmpty()) {
145                                ComputingResource resource = toExamine.pop();
146                                List<ComputingResource> resources = resource.getChildren();
147                                int numberOfRes = resources.size();
148                                for (int i = 0; i < numberOfRes; i++) {
149                                        ComputingResource resourceChild = resources.get(i);
150                                        toExamine.addLast(resourceChild);
151                                        computingResources.add(resourceChild);
152                                }
153                        }
154                }
155                sendInternal(delay, DCWormsTags.TO_RESOURCES, new ResourceBroadcastOrder(computingResources, event));
156        }
157
158        public void sendToResource(String resourceName, double delay, ResourceEvent event){
159                List<ComputingResource> computingResources = new ArrayList<ComputingResource>();
160
161                try {
162                        computingResources.add(SimulatedEnvironment.getComputingResourceByName(resourceName));
163                } catch (ResourceException e) {
164
165                }
166                sendInternal(delay, DCWormsTags.TO_RESOURCES, new ResourceBroadcastOrder(computingResources, event));
167        }
168       
169        private void sendToResources(Sim_event ev){
170                ResourceEventCommand rec;
171               
172                ResourceBroadcastOrder rbo = (ResourceBroadcastOrder)ev.get_data();
173                List<ComputingResource> computingResources = rbo.getComputingResources();
174                ResourceEvent event = rbo.getEvent();
175                for(ComputingResource compRes: computingResources){
176                        //rec = new ResourceEventCommand(compRes);
177                        //rec.execute(event);
178                        compRes.handleEvent(event);
179                }
180        }
181       
182        public void sendInternal(double delay, int tag, Object data) {
183                this.send(this.get_id(), delay, tag, data);
184        }
185       
186        public void sendTo(List<String> ids){
187        }
188       
189        public void dispose() throws ModuleException {
190        }
191
192        public ModuleType getType() {
193                return ModuleType.EVENT_MANAGER;
194        }
195
196}
197
198class ResourceBroadcastOrder {
199
200        private List<ComputingResource> computingResources;
201        private ResourceEvent event;
202       
203        public ResourceBroadcastOrder(List<ComputingResource> computingResources, ResourceEvent event) {
204                this.computingResources = computingResources;
205                this.event = event;
206        }
207
208        public List<ComputingResource> getComputingResources() {
209                return computingResources;
210        }
211
212        public ResourceEvent getEvent() {
213                return event;
214        }
215}
216
217
Note: See TracBrowser for help on using the repository browser.