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

Revision 1447, 6.2 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;
[1447]5import gridsim.GridSim;
[776]6import gridsim.GridSimCore;
[793]7import gridsim.GridSimTags;
8import gridsim.dcworms.DCWormsTags;
[776]9
[780]10import java.util.ArrayList;
11import java.util.LinkedList;
12import java.util.List;
[776]13import java.util.Properties;
14
[1300]15import schedframe.Initializable;
[1226]16import schedframe.SimulatedEnvironment;
[780]17import schedframe.events.ResourceEventCommand;
[776]18import schedframe.exceptions.ModuleException;
[1300]19import schedframe.exceptions.ResourceException;
[1169]20import schedframe.resources.ResourceType;
[780]21import schedframe.resources.computing.ComputingResource;
[1421]22import schedframe.resources.computing.profiles.energy.ResourceEvent;
[780]23import schedframe.scheduling.Scheduler;
[1398]24import schedframe.scheduling.plugin.Module;
25import schedframe.scheduling.plugin.ModuleType;
[776]26
27public class EventManager extends GridSimCore implements Module{
28
[1300]29        protected SimulatedEnvironment simEnv;
[780]30       
[1226]31        public EventManager(String name, SimulatedEnvironment resourceController) throws Exception {
[776]32                super(name, 1);
[1300]33                this.simEnv = resourceController;
[1447]34               
35                Integer schedulerIdObj = new Integer(get_id());
36                List<Integer> schedRes = GridSim.getGridResourceList();
37                schedRes.add(schedulerIdObj);
[776]38        }
39
[793]40        public void body() {
[1300]41               
42                for(Initializable initObj: simEnv.getToInit()){
43                        initObj.initiate();
44                }
45                simEnv.setInitList(null);
46               
[793]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
[1421]70                case DCWormsTags.TO_RESOURCES:
[793]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       
[776]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
[780]98        public void sendToAllSchedulers(double delay, int tag, Object data){
99                List<Scheduler> allSchedulers =  new ArrayList<Scheduler>();
[1300]100                if (simEnv.getScheduler().getChildren() != null) {
[780]101                        LinkedList<Scheduler> toExamine = new LinkedList<Scheduler>();
[1300]102                        toExamine.push(simEnv.getScheduler());
103                        allSchedulers.add(simEnv.getScheduler());
[780]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){
[793]118                        //sendInternal(delay, DCWormsTags.TO_SCHEDULERS, scheduler.get_id());
[780]119                        super.send(scheduler.get_id(), delay, tag, data);
120                }
121        }
122       
[1421]123        public void sendToResources(ResourceType type, double delay, ResourceEvent event){
124                List<ComputingResource> computingResources = new ArrayList<ComputingResource>();
[1169]125
[1300]126                if (simEnv.getComputingResources() != null) {
127                        for(ComputingResource compRes: simEnv.getComputingResources()){
[1421]128                                computingResources.addAll(compRes.getDescendantsByType(type));
[1169]129                        }
130                }
[1421]131                sendInternal(delay, DCWormsTags.TO_RESOURCES, new ResourceBroadcastOrder(computingResources, event));
[1169]132        }
133       
[1421]134        public void sendToAllResources(double delay, ResourceEvent event){
135                List<ComputingResource> computingResources = new ArrayList<ComputingResource>();
[780]136
[1300]137                if (simEnv.getComputingResources() != null) {
[780]138                        LinkedList<ComputingResource> toExamine = new LinkedList<ComputingResource>();
[1300]139                        for(ComputingResource compRes: simEnv.getComputingResources()){
[780]140                                toExamine.push(compRes);
[1421]141                                computingResources.add(compRes);
[780]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);
[1421]151                                        computingResources.add(resourceChild);
[780]152                                }
153                        }
154                }
[1421]155                sendInternal(delay, DCWormsTags.TO_RESOURCES, new ResourceBroadcastOrder(computingResources, event));
[793]156        }
157
[1421]158        public void sendToResource(String resourceName, double delay, ResourceEvent event){
159                List<ComputingResource> computingResources = new ArrayList<ComputingResource>();
[1300]160
161                try {
[1421]162                        computingResources.add(SimulatedEnvironment.getComputingResourceByName(resourceName));
[1300]163                } catch (ResourceException e) {
164
165                }
[1421]166                sendInternal(delay, DCWormsTags.TO_RESOURCES, new ResourceBroadcastOrder(computingResources, event));
[1300]167        }
168       
[793]169        private void sendToResources(Sim_event ev){
170                ResourceEventCommand rec;
[780]171               
[793]172                ResourceBroadcastOrder rbo = (ResourceBroadcastOrder)ev.get_data();
[1421]173                List<ComputingResource> computingResources = rbo.getComputingResources();
174                ResourceEvent event = rbo.getEvent();
175                for(ComputingResource compRes: computingResources){
[780]176                        rec = new ResourceEventCommand(compRes);
177                        rec.execute(event);
178                }
179        }
180       
[793]181        public void sendInternal(double delay, int tag, Object data) {
182                this.send(this.get_id(), delay, tag, data);
183        }
184       
[780]185        public void sendTo(List<String> ids){
186        }
187       
[776]188        public void dispose() throws ModuleException {
189        }
190
191        public ModuleType getType() {
192                return ModuleType.EVENT_MANAGER;
193        }
194
195}
[793]196
197class ResourceBroadcastOrder {
198
199        private List<ComputingResource> computingResources;
[1421]200        private ResourceEvent event;
[793]201       
[1421]202        public ResourceBroadcastOrder(List<ComputingResource> computingResources, ResourceEvent event) {
[793]203                this.computingResources = computingResources;
204                this.event = event;
205        }
206
207        public List<ComputingResource> getComputingResources() {
208                return computingResources;
209        }
210
[1421]211        public ResourceEvent getEvent() {
[793]212                return event;
213        }
214}
215
216
Note: See TracBrowser for help on using the repository browser.