source: DCWoRMS/trunk/src/test/EventManager.java @ 793

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