package test.rewolucja.resources.logical; import eduni.simjava.Sim_event; import eduni.simjava.Sim_port; import eduni.simjava.Sim_system; import gridsim.GridSim; import gridsim.GridSimTags; import gridsim.IO_data; import gridsim.gssim.GssimTags; import gridsim.gssim.network.ExtendedGridSimCore; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; import schedframe.resources.ResourceStateDescription; import schedframe.resources.providers.LocalSystem; import schedframe.resources.units.ResourceUnit; import schedframe.scheduling.events.SchedulingEventType; import schedframe.scheduling.plugin.SchedulingPluginConfiguration; import schedframe.scheduling.utils.ResourceParameterName; import test.rewolucja.GSSIMJobInterface; import test.rewolucja.resources.Resource; import test.rewolucja.resources.ResourceStatus; import test.rewolucja.resources.ResourceType; import test.rewolucja.resources.StructureEntityInterface; import test.rewolucja.resources.exception.ResourceException; import test.rewolucja.resources.physical.base.ComputingResource; import test.rewolucja.scheduling.implementation.ManagementSystem; public class LogicalResource extends ExtendedGridSimCore implements Resource, StructureEntityInterface{ protected ManagementSystem managementSystem; protected List resources; protected LogicalResource parent; protected List children; protected ResourceStatus status; public LogicalResource(ManagementSystem manSys) throws Exception { super(manSys.getName(), 1); this.managementSystem = manSys; this.resources = new ArrayList(); this.status = ResourceStatus.AVAILABLE; init(); } public LogicalResource(ManagementSystem manSys, List res) throws Exception { super(manSys.getName(), 1); this.managementSystem = manSys; this.resources = new ArrayList(); this.status = ResourceStatus.AVAILABLE; addResources(res); init(); } public void init(){ managementSystem.init(this); Integer lResIdObj = new Integer(get_id()); List res = GridSim.getGridResourceList(); res.add(lResIdObj); /*if (supportsAR) { res = GridSim.getAdvancedReservationList(); res.add(resIdObj); } */ } public void addChild(LogicalResource child) { child.setParent(this); if (children == null) { children = new ArrayList(); } children.add(child); } public List getChildren() { return children; } public void setParent(LogicalResource newParent) { parent = newParent; } public LogicalResource getParent() { return parent; } public void addResource(ComputingResource resource) { resources.add(resource); } public void addResources(List res) { for(ComputingResource resource:res){ resources.add(resource); resource.setLogicalResource(this); for(ComputingResource child: resource.filterDescendants(new Properties())){ child.setLogicalResource(this); } } } public List getResources() { return resources; } public void body() { SchedulingPluginConfiguration pluginConfig = managementSystem.getPluginConfiguration(); if (pluginConfig != null) { Map events = pluginConfig.getServedEvents(); if (events != null) { Object obj = events.get(SchedulingEventType.TIMER); if (obj != null) { int delay = (Integer) obj; send(this.get_id(), delay, GssimTags.TIMER); } } } // Process events until END_OF_SIMULATION is received from the // GridSimShutdown Entity Sim_event ev = new Sim_event(); sim_get_next(ev); // boolean run = true; boolean run = true; while (Sim_system.running() && run) { // sim_get_next(ev); // if the simulation finishes then exit the loop if (ev.get_tag() == GridSimTags.END_OF_SIMULATION) { // managemetnSystem_.setEndSimulation(); run = false; break; } // process the received event processRequest(ev); sim_get_next(ev); } } protected void processRequest(Sim_event ev) { switch (ev.get_tag()) { case GridSimTags.GRIDLET_SUBMIT: processGSSIMJobSubmit(ev, false); break; case GridSimTags.GRIDLET_SUBMIT_ACK: processGSSIMJobSubmit(ev, true); break; case GridSimTags.GRIDLET_RETURN: processGSSIMJobReturn(ev); break; default: processOtherRequest(ev); break; } } protected void processOtherRequest(Sim_event ev) { switch (ev.get_tag()) { case GssimTags.QUERY_RESOURCE_DESC: ResourceStateDescription desc = new ResourceStateDescription(new LocalSystem(get_name(), null, null)); Map units = new HashMap(); //ResourceComponentsNew resourceComponents = new ResourceComponentsNew(get_name()); //resourceComponents.addAll(resources); //units.put(ResourceParameterName.RESOURCECOMPONENTS, resourceComponents); desc.addResourceUnit(units); desc.setQueuesSize(managementSystem.getQueuesSize()); IO_data data = new IO_data(desc, 0, ev.get_src()); send(ev.get_src(), GridSimTags.SCHEDULE_NOW, GssimTags.QUERY_RESOURCE_DESC_RESULT, data); break; default: managementSystem.processEvent(ev); break; } } /*public boolean processOtherEvent(Sim_event ev) { return false; }*/ protected void processGSSIMJobReturn(Sim_event ev) { GSSIMJobInterface job = (GSSIMJobInterface) ev.get_data(); managementSystem.notifyReturnedJob(job); } protected void processGSSIMJobSubmit(Sim_event ev, boolean ack) { GSSIMJobInterface job = (GSSIMJobInterface) ev.get_data(); managementSystem.notifySubmittedJob(job, ack); } public void send(String entityName, double delay, int tag, Object data){ super.send(entityName, delay, tag, data); } public void send(int destID, double delay, int tag){ super.send(destID, delay, tag); } public void send(int destID, double delay, int tag, Object data){ super.send(destID, delay, tag, data); } public void send(Sim_port destPort, double delay, int tag, Object data) { super.send(destPort, delay, tag, data); } public void sendInternal(double delay, int tag, Object data) { this.send(this.get_id(), delay, tag, data); } /*protected void send(int dest, int tag, int transaction, Object obj){ IO_data data = new Transaction_IO_data(transaction, obj, 8, dest); super.send(dest, GridSimTags.SCHEDULE_NOW, tag, data); }*/ public Sim_port getOutputPort() { return output; } public LogicalResource getLogicalResource(String resourceName){ ComputingResource resourceWithName = null; for(int i = 0 ; i < resources.size() && resourceWithName == null; i++){ ComputingResource resource = resources.get(i); if(resource.getName().equals(resourceName)) resourceWithName = resource; else try { resourceWithName = resource.getDescendantsByName(resourceName); } catch (ResourceException e) { return null; } } if(resourceWithName == null) return null; List children = resourceWithName.getChildren(); Set childrenControllers = new HashSet(); for(ComputingResource child:children) { childrenControllers.add(child.getLogicalResource()); } Set tempChildrenControllers = new HashSet(childrenControllers); while(childrenControllers.size() != 1){ childrenControllers = new HashSet(); for(LogicalResource lr:tempChildrenControllers){ childrenControllers.add(lr.getParent()); } tempChildrenControllers = new HashSet(childrenControllers); } Iterator it = childrenControllers.iterator(); LogicalResource potentialLogicalResource = it.next(); if(potentialLogicalResource.getResources().containsAll(children)) return potentialLogicalResource; return null; } public long getQueueLoad(String queueName) throws NoSuchFieldException{ Map queue_size = managementSystem.getQueuesSize(); long load = 0; if(queueName == null){ for(String queue: queue_size.keySet()){ load += queue_size.get(queue); } return load; } else if(queue_size.containsKey(queueName)) return queue_size.get(queueName); else throw new NoSuchFieldException("Queue " + queueName + " is not available in resource " + get_name()); } public ResourceType getType() { return ResourceType.RESOURCE_PROVIDER; } public ResourceStatus getStatus() { return status; } public void setStatus(ResourceStatus newStatus) { status = newStatus; } }