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

Revision 1226, 5.5 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.SimulatedEnvironment;
15import schedframe.events.Event;
16import schedframe.events.ResourceEventCommand;
17import schedframe.exceptions.ModuleException;
18import schedframe.resources.ResourceType;
19import schedframe.resources.computing.ComputingResource;
20import schedframe.scheduling.Scheduler;
21import schedframe.scheduling.plugin.grid.Module;
22import schedframe.scheduling.plugin.grid.ModuleType;
23
24public class EventManager extends GridSimCore implements Module{
25
26        protected SimulatedEnvironment resourceController;
27       
28        public EventManager(String name, SimulatedEnvironment resourceController) throws Exception {
29                super(name, 1);
30                this.resourceController = resourceController;
31        }
32
33        public void body() {
34
35
36                // Process events until END_OF_SIMULATION is received from the
37                // GridSimShutdown Entity
38                Sim_event ev = new Sim_event();
39                sim_get_next(ev);
40                boolean run = true;
41                while (Sim_system.running()  && run) {
42                        // sim_get_next(ev);
43                        // if the simulation finishes then exit the loop
44                        if (ev.get_tag() == GridSimTags.END_OF_SIMULATION) {
45                                // managemetnSystem_.setEndSimulation();
46                                run = false;
47                                break;
48                        }
49                       
50                        // process the received event
51                        processRequest(ev);
52                        sim_get_next(ev);
53                }
54        }
55       
56        protected void processRequest(Sim_event ev) {
57                switch (ev.get_tag()) {
58
59                case DCWormsTags.TO_COMP_RESOURCES:
60                        sendToResources(ev);
61                        break;
62
63                case DCWormsTags.TO_SCHEDULERS:
64                        //super.send((Integer)ev.get_data(), 0, DCWormsTags.PHASE_CHANGED, null);
65                        break;
66
67                default:
68                        break;
69                }
70        }
71       
72        public void send(String entityName, double delay, int tag, Object data){
73                super.send(entityName, delay, tag, data);
74        }
75       
76        public void send(int destId, double delay, int tag){
77                super.send(destId, delay, tag);
78        }
79       
80        public void send(int destId, double delay, int tag, Object data){
81                super.send(destId, delay, tag, data);
82        }
83       
84        public void init(Properties properties) throws ModuleException {
85        }
86
87        public void sendToAllSchedulers(double delay, int tag, Object data){
88                List<Scheduler> allSchedulers =  new ArrayList<Scheduler>();
89                if (resourceController.getScheduler().getChildren() != null) {
90                        LinkedList<Scheduler> toExamine = new LinkedList<Scheduler>();
91                        toExamine.push(resourceController.getScheduler());
92                        allSchedulers.add(resourceController.getScheduler());
93
94                        while (!toExamine.isEmpty()) {
95                                Scheduler scheduler = toExamine.pop();
96                                List<Scheduler> schedulers = scheduler.getChildren();
97                                int numberOfSched = schedulers.size();
98                                for (int i = 0; i < numberOfSched; i++) {
99                                        Scheduler schedulerChild = schedulers.get(i);
100                                        toExamine.addLast(schedulerChild);
101                                        allSchedulers.add(schedulerChild);
102                                }
103                        }
104                }
105               
106               
107                for(Scheduler scheduler: allSchedulers){
108                        //sendInternal(delay, DCWormsTags.TO_SCHEDULERS, scheduler.get_id());
109                        super.send(scheduler.get_id(), delay, tag, data);
110                }
111        }
112       
113        public void sendToResources(ResourceType type, double delay, Event event){
114                List<ComputingResource> allComputingResources = new ArrayList<ComputingResource>();
115
116                if (resourceController.getComputingResources() != null) {
117                        for(ComputingResource compRes: resourceController.getComputingResources()){
118                                allComputingResources.addAll(compRes.getDescendantsByType(type));
119                        }
120                }
121                sendInternal(delay, DCWormsTags.TO_COMP_RESOURCES, new ResourceBroadcastOrder(allComputingResources, event));
122        }
123       
124        public void sendToAllResources(double delay, Event event){
125                List<ComputingResource> allComputingResources = new ArrayList<ComputingResource>();
126
127                if (resourceController.getComputingResources() != null) {
128                        LinkedList<ComputingResource> toExamine = new LinkedList<ComputingResource>();
129                        for(ComputingResource compRes: resourceController.getComputingResources()){
130                                toExamine.push(compRes);
131                                allComputingResources.add(compRes);
132                        }
133
134                        while (!toExamine.isEmpty()) {
135                                ComputingResource resource = toExamine.pop();
136                                List<ComputingResource> resources = resource.getChildren();
137                                int numberOfRes = resources.size();
138                                for (int i = 0; i < numberOfRes; i++) {
139                                        ComputingResource resourceChild = resources.get(i);
140                                        toExamine.addLast(resourceChild);
141                                        allComputingResources.add(resourceChild);
142                                }
143                        }
144                }
145                sendInternal(delay, DCWormsTags.TO_COMP_RESOURCES, new ResourceBroadcastOrder(allComputingResources, event));
146        }
147
148        private void sendToResources(Sim_event ev){
149                ResourceEventCommand rec;
150               
151                ResourceBroadcastOrder rbo = (ResourceBroadcastOrder)ev.get_data();
152                List<ComputingResource> allComputingResources = rbo.getComputingResources();
153                Event event = rbo.getEvent();
154                for(ComputingResource compRes: allComputingResources){
155                        rec = new ResourceEventCommand(compRes);
156                        rec.execute(event);
157                }
158        }
159       
160        public void sendInternal(double delay, int tag, Object data) {
161                this.send(this.get_id(), delay, tag, data);
162        }
163       
164        public void sendTo(List<String> ids){
165        }
166       
167        public void dispose() throws ModuleException {
168        }
169
170        public ModuleType getType() {
171                return ModuleType.EVENT_MANAGER;
172        }
173
174}
175
176class ResourceBroadcastOrder {
177
178        private List<ComputingResource> computingResources;
179        private Event event;
180       
181        public ResourceBroadcastOrder(List<ComputingResource> computingResources, Event event) {
182                super();
183                this.computingResources = computingResources;
184                this.event = event;
185        }
186
187        public List<ComputingResource> getComputingResources() {
188                return computingResources;
189        }
190
191        public Event getEvent() {
192                return event;
193        }
194}
195
196
Note: See TracBrowser for help on using the repository browser.