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

Revision 1427, 6.1 KB checked in by wojtekp, 11 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.GridSimCore;
6import gridsim.GridSimTags;
7import gridsim.dcworms.DCWormsTags;
8
9import java.util.ArrayList;
10import java.util.LinkedList;
11import java.util.List;
12import java.util.Properties;
13
14import schedframe.Initializable;
15import schedframe.SimulatedEnvironment;
16import schedframe.events.ResourceEventCommand;
17import schedframe.exceptions.ModuleException;
18import schedframe.exceptions.ResourceException;
19import schedframe.resources.ResourceType;
20import schedframe.resources.computing.ComputingResource;
21import schedframe.resources.computing.profiles.energy.ResourceEvent;
22import schedframe.scheduling.Scheduler;
23import schedframe.scheduling.plugin.Module;
24import schedframe.scheduling.plugin.ModuleType;
25
26public class EventManager extends GridSimCore implements Module{
27
28        protected SimulatedEnvironment simEnv;
29       
30        public EventManager(String name, SimulatedEnvironment resourceController) throws Exception {
31                super(name, 1);
32                this.simEnv = resourceController;
33        }
34
35        public void body() {
36               
37                for(Initializable initObj: simEnv.getToInit()){
38                        initObj.initiate();
39                }
40                simEnv.setInitList(null);
41               
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_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       
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
93        public void sendToAllSchedulers(double delay, int tag, Object data){
94                List<Scheduler> allSchedulers =  new ArrayList<Scheduler>();
95                if (simEnv.getScheduler().getChildren() != null) {
96                        LinkedList<Scheduler> toExamine = new LinkedList<Scheduler>();
97                        toExamine.push(simEnv.getScheduler());
98                        allSchedulers.add(simEnv.getScheduler());
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               
112                for(Scheduler scheduler: allSchedulers){
113                        //sendInternal(delay, DCWormsTags.TO_SCHEDULERS, scheduler.get_id());
114                        super.send(scheduler.get_id(), delay, tag, data);
115                }
116        }
117       
118        public void sendToResources(ResourceType type, double delay, ResourceEvent event){
119                List<ComputingResource> computingResources = new ArrayList<ComputingResource>();
120
121                if (simEnv.getComputingResources() != null) {
122                        for(ComputingResource compRes: simEnv.getComputingResources()){
123                                computingResources.addAll(compRes.getDescendantsByType(type));
124                        }
125                }
126                sendInternal(delay, DCWormsTags.TO_RESOURCES, new ResourceBroadcastOrder(computingResources, event));
127        }
128       
129        public void sendToAllResources(double delay, ResourceEvent event){
130                List<ComputingResource> computingResources = new ArrayList<ComputingResource>();
131
132                if (simEnv.getComputingResources() != null) {
133                        LinkedList<ComputingResource> toExamine = new LinkedList<ComputingResource>();
134                        for(ComputingResource compRes: simEnv.getComputingResources()){
135                                toExamine.push(compRes);
136                                computingResources.add(compRes);
137                        }
138
139                        while (!toExamine.isEmpty()) {
140                                ComputingResource resource = toExamine.pop();
141                                List<ComputingResource> resources = resource.getChildren();
142                                int numberOfRes = resources.size();
143                                for (int i = 0; i < numberOfRes; i++) {
144                                        ComputingResource resourceChild = resources.get(i);
145                                        toExamine.addLast(resourceChild);
146                                        computingResources.add(resourceChild);
147                                }
148                        }
149                }
150                sendInternal(delay, DCWormsTags.TO_RESOURCES, new ResourceBroadcastOrder(computingResources, event));
151        }
152
153        public void sendToResource(String resourceName, double delay, ResourceEvent event){
154                List<ComputingResource> computingResources = new ArrayList<ComputingResource>();
155
156                try {
157                        computingResources.add(SimulatedEnvironment.getComputingResourceByName(resourceName));
158                } catch (ResourceException e) {
159
160                }
161                sendInternal(delay, DCWormsTags.TO_RESOURCES, new ResourceBroadcastOrder(computingResources, event));
162        }
163       
164        private void sendToResources(Sim_event ev){
165                ResourceEventCommand rec;
166               
167                ResourceBroadcastOrder rbo = (ResourceBroadcastOrder)ev.get_data();
168                List<ComputingResource> computingResources = rbo.getComputingResources();
169                ResourceEvent event = rbo.getEvent();
170                for(ComputingResource compRes: computingResources){
171                        rec = new ResourceEventCommand(compRes);
172                        rec.execute(event);
173                }
174        }
175       
176        public void sendInternal(double delay, int tag, Object data) {
177                this.send(this.get_id(), delay, tag, data);
178        }
179       
180        public void sendTo(List<String> ids){
181        }
182       
183        public void dispose() throws ModuleException {
184        }
185
186        public ModuleType getType() {
187                return ModuleType.EVENT_MANAGER;
188        }
189
190}
191
192class ResourceBroadcastOrder {
193
194        private List<ComputingResource> computingResources;
195        private ResourceEvent event;
196       
197        public ResourceBroadcastOrder(List<ComputingResource> computingResources, ResourceEvent event) {
198                super();
199                this.computingResources = computingResources;
200                this.event = event;
201        }
202
203        public List<ComputingResource> getComputingResources() {
204                return computingResources;
205        }
206
207        public ResourceEvent getEvent() {
208                return event;
209        }
210}
211
212
Note: See TracBrowser for help on using the repository browser.