source: DCWoRMS/trunk/src/schedframe/resources/computing/ComputingResource.java @ 495

Revision 495, 10.5 KB checked in by wojtekp, 13 years ago (diff)
  • Property svn:mime-type set to text/plain
RevLine 
[477]1package schedframe.resources.computing;
2
3import gridsim.GridSimTags;
[493]4import gridsim.dcworms.DCWormsTags;
[477]5
6import java.util.ArrayList;
7import java.util.LinkedList;
8import java.util.List;
9import java.util.Properties;
10
11import schedframe.Initializable;
12import schedframe.events.Event;
13import schedframe.events.EventHandler;
14import schedframe.events.ResourceEventCommand;
15import schedframe.events.scheduling.EventReason;
16import schedframe.events.scheduling.SchedulingEvent;
17import schedframe.events.scheduling.SchedulingEventCommand;
18import schedframe.exceptions.ResourceException;
19import schedframe.resources.Resource;
20import schedframe.resources.ResourceStatus;
21import schedframe.resources.ResourceType;
22import schedframe.resources.computing.description.ComputingResourceDescription;
23import schedframe.resources.computing.extensions.Extension;
24import schedframe.resources.computing.extensions.ExtensionList;
25import schedframe.resources.computing.extensions.ExtensionListImpl;
26import schedframe.resources.computing.extensions.ExtensionType;
27import schedframe.resources.computing.profiles.energy.EnergyEvent;
28import schedframe.resources.computing.profiles.energy.EnergyEventType;
29import schedframe.resources.computing.profiles.energy.EnergyExtension;
[495]30import schedframe.resources.computing.profiles.energy.airthroughput.ui.AirThroughputInterface;
[477]31import schedframe.resources.computing.profiles.energy.power.ui.PowerInterface;
32import schedframe.resources.computing.properties.DefaultPropertiesBuilder;
33import schedframe.resources.computing.properties.PropertiesDirector;
34import schedframe.resources.computing.validator.ResourceNameValidator;
35import schedframe.resources.computing.validator.ResourcePropertiesValidator;
36import schedframe.resources.computing.validator.ResourceStatusValidator;
37import schedframe.resources.computing.validator.ResourceTypeValidator;
38import schedframe.resources.computing.validator.ResourceValidator;
39import schedframe.resources.units.PEUnit;
40import schedframe.resources.units.ResourceUnit;
41import schedframe.resources.units.StandardResourceUnitName;
42import schedframe.scheduling.Scheduler;
43
44public class ComputingResource implements Resource, Initializable{
45       
46        protected String name;
47        protected ResourceType type;
48        protected ResourceStatus status;
49
50        protected ComputingResource parent;
51        protected List<ComputingResource> children;
52       
53        protected Scheduler scheduler;
54        protected ResourceCharacteristics resourceCharacteristic;
55        protected ExtensionList extensionList;
56       
57
58        public ExtensionList getExtensionList() {
59                return extensionList;
60        }
61
62        public ComputingResource(ComputingResourceDescription resDesc) {
63                this.type = resDesc.getType();
64                this.name = resDesc.getId();
65                this.status = ResourceStatus.FREE;
66                this.extensionList = new ExtensionListImpl(1);
67                initCharacteristics(resDesc);
68                addFakeProcessors();
69        }
70
[495]71        //TODO remove if possible (check if all scenarios can be realized - statistics issue), since it's a temporary method
[477]72        private void addFakeProcessors() {
73                if(getResourceCharacteristic().getResourceUnits().get(StandardResourceUnitName.PE) != null){
74                        for(ResourceUnit resUnit: getResourceCharacteristic().getResourceUnits().get(StandardResourceUnitName.PE)){
75                                PEUnit peUnit = (PEUnit) resUnit;
76                                for(int i = 0; i < peUnit.getAmount(); i++){
77                                        schemas.ComputingResource fakeCompResource = new schemas.ComputingResource();
78                                        fakeCompResource.setClazz("Processor");
79                                        addChild(ResourceFactory.createResource(new ComputingResourceDescription(fakeCompResource)));
80                                }
81                        }
82                }
83        }
84
85        protected void initCharacteristics(ComputingResourceDescription resDesc){
86                resourceCharacteristic = new ResourceCharacteristics.Builder().resourceUnits(resDesc.getResourceUnits()).location(resDesc.getLocation()).parameters(resDesc.getParameters()).build();
87        }
88       
89        public ComputingResource getParent() {
90                return parent;
91        }
92
93        public void setParent(ComputingResource newParent) {
94                parent = newParent;
95        }
96
97        public List<ComputingResource> getChildren() {
98                if (children == null)
99                        return new ArrayList<ComputingResource>(1);
100                return children;
101        }
102
103        public void addChild(ComputingResource child) {
104                child.setParent(this);
105                if (children == null)
106                        children = new ArrayList<ComputingResource>();
107                children.add(child);
108        }
109
110        public String getName() {
111                return name;
112        }
113
114        public ResourceType getType() {
115                return type;
116        }
117
118        public ResourceCharacteristics getResourceCharacteristic() {
119                return resourceCharacteristic;
120        }
121
122        public ResourceStatus getStatus() {
123                return status;
124        }
125
126        public void setStatus(ResourceStatus newStatus) {
127                if(newStatus != status){
128                        status = newStatus;
129                        if(children != null) {
130                                for(ComputingResource child: children){
131                                        child.setStatus(status);
132                                }
133                        }       
134                }
135        }
136       
137        private void triggerEventUp(Event event) {
138                if(parent != null)
139                        parent.handleEvent(event);
140        }
141
142        public void handleEvent(Event event){
143                ResourceEventCommand rec = new ResourceEventCommand(this);
144                rec.execute(event);
145                SchedulingEventCommand sec = new SchedulingEventCommand(this);
146                sec.execute(event);
147
148                //old, correctly working method
149                /*if (extensionList != null) {
150                        for (Extension extension : extensionList) {
151                                if (extension.supportsEvent(event)) {
152                                        extension.handleEvent(event);
153                                }
154                        }
155                }*/
156                //TODO - delete, check before
157                //if(scheduler != null && (parent != null && scheduler != parent.getScheduler())/*scheduler.getResources().contains(this)*/){
158                //      String src = event.getSource() != null ? event.getSource() : name;
[494]159                //      scheduler.sendInternal(GridSimTags.SCHEDULE_NOW, DCWormsTags.UPDATE, src);
[477]160                //}
161                //triggerEventUp(event);
162        }
163       
164        public List <? extends ComputingResource> getDescendantsByType(ResourceType type) {
165                List<ResourceValidator> validators = new ArrayList<ResourceValidator>();
166                validators.add(new ResourceTypeValidator(type));
167                return searchDescendants(validators, true);
168        }
169
170        public List<? extends ComputingResource> getDescendantsByTypeAndStatus(ResourceType type, ResourceStatus status) {
171                List<ResourceValidator> validators = new ArrayList<ResourceValidator>();
172                validators.add(new ResourceStatusValidator(status));
173                validators.add(new ResourceTypeValidator(type));
174                return searchDescendants(validators, true);
175        }
176
177        public ComputingResource getDescendantByName(String resourceName) throws ResourceException {
178                List<ResourceValidator> validators = new ArrayList<ResourceValidator>();
179                validators.add(new ResourceNameValidator(resourceName));
180                List<? extends ComputingResource> resources = searchDescendants(validators, true);
181                return resources.size() == 0 ? null : resources.get(0);
182        }
183       
184        public List<? extends ComputingResource> filterDescendants(Properties properties)  {
185                List<ResourceValidator> validators = new ArrayList<ResourceValidator>();
186                validators.add(new ResourcePropertiesValidator(properties));
187                return searchDescendants(validators, false);
188        }
189       
[495]190        protected List<? extends ComputingResource> searchDescendants(List<ResourceValidator> validators, boolean cutOff) {
[477]191
192                List<ComputingResource> descendants = new ArrayList<ComputingResource>();
193                if (children != null) {
194                        LinkedList<ComputingResource> toExamine = new LinkedList<ComputingResource>();
195                        toExamine.push(this);
196
197                        while (!toExamine.isEmpty()) {
198                                ComputingResource resource = toExamine.pop();
199                                List<ComputingResource> resources = resource.getChildren();
200                                int numberOfRes = resources.size();
201                                for (int i = 0; i < numberOfRes; i++) {
202                                        ComputingResource resourceChild = resources.get(i);
203                                        if (resourceChild.match(validators)) {
204                                                descendants.add(resourceChild);
[495]205                                                if(cutOff == false) {
[477]206                                                        toExamine.addLast(resourceChild);
207                                                }
208                                        } else
209                                                //toExamine.insertElementAt(resourceChild, 0);
210                                                toExamine.addLast(resourceChild);
211                                }
212                        }
213                }
214                return descendants;
215        }
216       
217        protected boolean match(List<ResourceValidator> validators){
218                for(ResourceValidator validator: validators){
219                        if(validator.validate(this) == false)
220                                return false;
221                }
222                return true;
223        }
224       
225        public Properties getProperties(){
226                PropertiesDirector propDirector = new PropertiesDirector();
227                propDirector.setPropertiesBuilder(new DefaultPropertiesBuilder());
228                propDirector.constructProperties(this);
229                return propDirector.getProperties();
230        }
231       
232        public PowerInterface getPowerInterface(){
233                if (extensionList != null) {
234                        for (Extension extension : extensionList) {
235                                if (extension.getType() == ExtensionType.ENERGY_EXTENSION) {
236                                        EnergyExtension ee = (EnergyExtension)extension;
237                                        return ee.getPowerInterface();
238                                }
239                        }
240                }
241                return null;
242        }
[495]243       
244        public AirThroughputInterface getAirThroughputInterface(){
245                if (extensionList != null) {
246                        for (Extension extension : extensionList) {
247                                if (extension.getType() == ExtensionType.ENERGY_EXTENSION) {
248                                        EnergyExtension ee = (EnergyExtension)extension;
249                                        return ee.getAirThroughputInterface();
250                                }
251                        }
252                }
253                return null;
254        }
[477]255
256        public Scheduler getScheduler() {
257                return scheduler;
258        }
259       
260        public void setScheduler(Scheduler scheduler) {
261                this.scheduler = scheduler;
262                if(children != null){
263                        for(ComputingResource child: children){
264                                child.setScheduler(scheduler);
265                        }       
266                }
267        }
268
[495]269        class ComputingResourceEventHandler implements EventHandler{
[477]270               
271                public void handleResourceEvent(Event event){
272                        if (extensionList != null) {
273                                for (Extension extension : extensionList) {
274                                        if (extension.supportsEvent(event)) {
275                                                extension.handleEvent(event);
276                                        }
277                                }
278                        }
279                        if(event.getReason() != EventReason.SIM_INIT)
280                                triggerEventUp(event);
281                }
282               
283                public void handleSchedulingEvent(SchedulingEvent event){
284
285                        if(scheduler != null && (parent != null && scheduler != parent.getScheduler())/*scheduler.getResources().contains(this)*/){
286                                String src = event.getSource() != null ? event.getSource() : name;
[481]287                                scheduler.sendInternal(GridSimTags.SCHEDULE_NOW, DCWormsTags.UPDATE, src);
[477]288                        } else if(parent != null)
289                                parent.getEventHandler().handleSchedulingEvent(event);
290                }
291        }
292       
293        public EventHandler getEventHandler(){
[495]294                return new ComputingResourceEventHandler();
[477]295        }
296       
297        public void initiate(){
[495]298               
[477]299                ResourceEventCommand rec = new ResourceEventCommand(this);
[495]300                EnergyEvent event = new EnergyEvent(EnergyEventType.AIRFLOW_STATE_CHANGED, "Resource controller");
[477]301                event.setReason(EventReason.SIM_INIT);
302                rec.execute(event);
[495]303               
304                rec = new ResourceEventCommand(this);
305                event = new EnergyEvent(EnergyEventType.POWER_STATE_CHANGED, "Resource controller");
306                event.setReason(EventReason.SIM_INIT);
307                rec.execute(event);
308               
[477]309                //alternative way
[495]310                //getEventHandler().handleResourceEvent(new EnergyEvent(EnergyEventType.AIRFLOW_STATE_CHANGED, "Resource controller"));
[477]311                //getEventHandler().handleResourceEvent(new EnergyEvent(EnergyEventType.POWER_STATE_CHANGED, "Resource controller"));
312        }
313}
Note: See TracBrowser for help on using the repository browser.