source: DCWoRMS/branches/coolemall/src/test/EventManager.java @ 1398

Revision 1398, 6.0 KB checked in by wojtekp, 11 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package test;
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.Event;
17import schedframe.events.ResourceEventCommand;
18import schedframe.exceptions.ModuleException;
19import schedframe.exceptions.ResourceException;
20import schedframe.resources.ResourceType;
21import schedframe.resources.computing.ComputingResource;
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_COMP_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               
113                for(Scheduler scheduler: allSchedulers){
114                        //sendInternal(delay, DCWormsTags.TO_SCHEDULERS, scheduler.get_id());
115                        super.send(scheduler.get_id(), delay, tag, data);
116                }
117        }
118       
119        public void sendToResources(ResourceType type, double delay, Event event){
120                List<ComputingResource> allComputingResources = new ArrayList<ComputingResource>();
121
122                if (simEnv.getComputingResources() != null) {
123                        for(ComputingResource compRes: simEnv.getComputingResources()){
124                                allComputingResources.addAll(compRes.getDescendantsByType(type));
125                        }
126                }
127                sendInternal(delay, DCWormsTags.TO_COMP_RESOURCES, new ResourceBroadcastOrder(allComputingResources, event));
128        }
129       
130        public void sendToAllResources(double delay, Event event){
131                List<ComputingResource> allComputingResources = new ArrayList<ComputingResource>();
132
133                if (simEnv.getComputingResources() != null) {
134                        LinkedList<ComputingResource> toExamine = new LinkedList<ComputingResource>();
135                        for(ComputingResource compRes: simEnv.getComputingResources()){
136                                toExamine.push(compRes);
137                                allComputingResources.add(compRes);
138                        }
139
140                        while (!toExamine.isEmpty()) {
141                                ComputingResource resource = toExamine.pop();
142                                List<ComputingResource> resources = resource.getChildren();
143                                int numberOfRes = resources.size();
144                                for (int i = 0; i < numberOfRes; i++) {
145                                        ComputingResource resourceChild = resources.get(i);
146                                        toExamine.addLast(resourceChild);
147                                        allComputingResources.add(resourceChild);
148                                }
149                        }
150                }
151                sendInternal(delay, DCWormsTags.TO_COMP_RESOURCES, new ResourceBroadcastOrder(allComputingResources, event));
152        }
153
154        public void sendToResource(String resourceName, double delay, Event event){
155                List<ComputingResource> allComputingResources = new ArrayList<ComputingResource>();
156
157                try {
158                        allComputingResources.add(SimulatedEnvironment.getComputingResourceByName(resourceName));
159                } catch (ResourceException e) {
160
161                }
162                sendInternal(delay, DCWormsTags.TO_COMP_RESOURCES, new ResourceBroadcastOrder(allComputingResources, event));
163        }
164       
165        private void sendToResources(Sim_event ev){
166                ResourceEventCommand rec;
167               
168                ResourceBroadcastOrder rbo = (ResourceBroadcastOrder)ev.get_data();
169                List<ComputingResource> allComputingResources = rbo.getComputingResources();
170                Event event = rbo.getEvent();
171                for(ComputingResource compRes: allComputingResources){
172                        rec = new ResourceEventCommand(compRes);
173                        rec.execute(event);
174                }
175        }
176       
177        public void sendInternal(double delay, int tag, Object data) {
178                this.send(this.get_id(), delay, tag, data);
179        }
180       
181        public void sendTo(List<String> ids){
182        }
183       
184        public void dispose() throws ModuleException {
185        }
186
187        public ModuleType getType() {
188                return ModuleType.EVENT_MANAGER;
189        }
190
191}
192
193class ResourceBroadcastOrder {
194
195        private List<ComputingResource> computingResources;
196        private Event event;
197       
198        public ResourceBroadcastOrder(List<ComputingResource> computingResources, Event event) {
199                super();
200                this.computingResources = computingResources;
201                this.event = event;
202        }
203
204        public List<ComputingResource> getComputingResources() {
205                return computingResources;
206        }
207
208        public Event getEvent() {
209                return event;
210        }
211}
212
213
Note: See TracBrowser for help on using the repository browser.