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
RevLine 
[1427]1package simulator;
[776]2
[793]3import eduni.simjava.Sim_event;
4import eduni.simjava.Sim_system;
[776]5import gridsim.GridSimCore;
[793]6import gridsim.GridSimTags;
7import gridsim.dcworms.DCWormsTags;
[776]8
[780]9import java.util.ArrayList;
10import java.util.LinkedList;
11import java.util.List;
[776]12import java.util.Properties;
13
[1300]14import schedframe.Initializable;
[1226]15import schedframe.SimulatedEnvironment;
[780]16import schedframe.events.ResourceEventCommand;
[776]17import schedframe.exceptions.ModuleException;
[1300]18import schedframe.exceptions.ResourceException;
[1169]19import schedframe.resources.ResourceType;
[780]20import schedframe.resources.computing.ComputingResource;
[1421]21import schedframe.resources.computing.profiles.energy.ResourceEvent;
[780]22import schedframe.scheduling.Scheduler;
[1398]23import schedframe.scheduling.plugin.Module;
24import schedframe.scheduling.plugin.ModuleType;
[776]25
26public 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
[1421]65                case DCWormsTags.TO_RESOURCES:
[793]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               
112                for(Scheduler scheduler: allSchedulers){
[793]113                        //sendInternal(delay, DCWormsTags.TO_SCHEDULERS, scheduler.get_id());
[780]114                        super.send(scheduler.get_id(), delay, tag, data);
115                }
116        }
117       
[1421]118        public void sendToResources(ResourceType type, double delay, ResourceEvent event){
119                List<ComputingResource> computingResources = new ArrayList<ComputingResource>();
[1169]120
[1300]121                if (simEnv.getComputingResources() != null) {
122                        for(ComputingResource compRes: simEnv.getComputingResources()){
[1421]123                                computingResources.addAll(compRes.getDescendantsByType(type));
[1169]124                        }
125                }
[1421]126                sendInternal(delay, DCWormsTags.TO_RESOURCES, new ResourceBroadcastOrder(computingResources, event));
[1169]127        }
128       
[1421]129        public void sendToAllResources(double delay, ResourceEvent event){
130                List<ComputingResource> computingResources = new ArrayList<ComputingResource>();
[780]131
[1300]132                if (simEnv.getComputingResources() != null) {
[780]133                        LinkedList<ComputingResource> toExamine = new LinkedList<ComputingResource>();
[1300]134                        for(ComputingResource compRes: simEnv.getComputingResources()){
[780]135                                toExamine.push(compRes);
[1421]136                                computingResources.add(compRes);
[780]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);
[1421]146                                        computingResources.add(resourceChild);
[780]147                                }
148                        }
149                }
[1421]150                sendInternal(delay, DCWormsTags.TO_RESOURCES, new ResourceBroadcastOrder(computingResources, event));
[793]151        }
152
[1421]153        public void sendToResource(String resourceName, double delay, ResourceEvent event){
154                List<ComputingResource> computingResources = new ArrayList<ComputingResource>();
[1300]155
156                try {
[1421]157                        computingResources.add(SimulatedEnvironment.getComputingResourceByName(resourceName));
[1300]158                } catch (ResourceException e) {
159
160                }
[1421]161                sendInternal(delay, DCWormsTags.TO_RESOURCES, new ResourceBroadcastOrder(computingResources, event));
[1300]162        }
163       
[793]164        private void sendToResources(Sim_event ev){
165                ResourceEventCommand rec;
[780]166               
[793]167                ResourceBroadcastOrder rbo = (ResourceBroadcastOrder)ev.get_data();
[1421]168                List<ComputingResource> computingResources = rbo.getComputingResources();
169                ResourceEvent event = rbo.getEvent();
170                for(ComputingResource compRes: computingResources){
[780]171                        rec = new ResourceEventCommand(compRes);
172                        rec.execute(event);
173                }
174        }
175       
[793]176        public void sendInternal(double delay, int tag, Object data) {
177                this.send(this.get_id(), delay, tag, data);
178        }
179       
[780]180        public void sendTo(List<String> ids){
181        }
182       
[776]183        public void dispose() throws ModuleException {
184        }
185
186        public ModuleType getType() {
187                return ModuleType.EVENT_MANAGER;
188        }
189
190}
[793]191
192class ResourceBroadcastOrder {
193
194        private List<ComputingResource> computingResources;
[1421]195        private ResourceEvent event;
[793]196       
[1421]197        public ResourceBroadcastOrder(List<ComputingResource> computingResources, ResourceEvent event) {
[793]198                super();
199                this.computingResources = computingResources;
200                this.event = event;
201        }
202
203        public List<ComputingResource> getComputingResources() {
204                return computingResources;
205        }
206
[1421]207        public ResourceEvent getEvent() {
[793]208                return event;
209        }
210}
211
212
Note: See TracBrowser for help on using the repository browser.