Index: xssim/branches/tpiontek/example/tomekp/nodestest/FCFSNodePowerManagementClusterLocalPlugin.java
===================================================================
--- xssim/branches/tpiontek/example/tomekp/nodestest/FCFSNodePowerManagementClusterLocalPlugin.java	(revision 275)
+++ xssim/branches/tpiontek/example/tomekp/nodestest/FCFSNodePowerManagementClusterLocalPlugin.java	(revision 275)
@@ -0,0 +1,173 @@
+package tomekp.nodestest;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import schedframe.resources.PowerState;
+import schedframe.resources.units.ResourceUnit;
+import schedframe.scheduling.TaskInterface;
+import schedframe.scheduling.events.SchedulingEvent;
+import schedframe.scheduling.plugin.grid.ModuleList;
+import schedframe.scheduling.utils.ResourceParameterName;
+import test.rewolucja.GSSIMJobInterface;
+import test.rewolucja.resources.ProcessingElements;
+import test.rewolucja.resources.ResourceStatus;
+import test.rewolucja.resources.manager.implementation.ClusterResourceManager;
+import test.rewolucja.resources.manager.interfaces.ResourceManagerInterface;
+import test.rewolucja.resources.physical.base.ComputingResource;
+import test.rewolucja.resources.physical.implementation.ComputingNode;
+import test.rewolucja.scheduling.JobRegistryInterface;
+import test.rewolucja.scheduling.plan.SchedulingPlanInterfaceNew;
+import test.rewolucja.scheduling.plan.SchedulingPlanNew;
+import test.rewolucja.scheduling.queue.Queue;
+import test.rewolucja.scheduling.queue.QueueList;
+import example.localplugin.BaseLocalPlugin;
+import gridsim.Gridlet;
+
+public class FCFSNodePowerManagementClusterLocalPlugin extends BaseLocalPlugin {
+
+	public FCFSNodePowerManagementClusterLocalPlugin () {
+	}
+
+	public SchedulingPlanInterfaceNew schedule(SchedulingEvent event, QueueList queues, JobRegistryInterface jobRegistry,
+			ResourceManagerInterface resManager, ModuleList modules) {
+
+		ClusterResourceManager resourceManager = (ClusterResourceManager) resManager;
+		SchedulingPlanNew plan = new SchedulingPlanNew();
+		// chose the events types to serve.
+		// Different actions for different events are possible.
+		switch (event.getType()) {
+		case START_TASK_EXECUTION:
+		case TASK_FINISHED:
+		case POWER_STATE_CHANGED:
+			// our tasks are placed only in first queue (see
+			// BaseLocalPlugin.placeJobsInQueues() method)
+			Queue q = queues.get(0);
+			// check all tasks in queue
+			boolean harnesStataus = false;
+			for (int i = 0; i < q.size(); i++) {
+				GSSIMJobInterface<?> job = q.get(i);
+				TaskInterface<?> task = (TaskInterface<?>) job;
+				// if status of the tasks in READY
+				if (task.getStatus() == Gridlet.READY) {
+
+					Map<ResourceParameterName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task);
+					if (choosenResources  != null) {
+						ProcessingElements pes = (ProcessingElements)choosenResources.get(ResourceParameterName.PROCESSINGELEMENTS); 
+						System.out.println(task.getJobId()+" on :"+pes.get(0).getName());
+						addToSchedulingPlan(plan, task, choosenResources);
+					} else if(harnesStataus) {
+						harnesStataus = false;
+					}
+					else {
+						if(harnessIdleNodesToWork(task, resourceManager.getComputingNodes())){
+							harnesStataus = true;
+							i--;
+						}
+						
+					}
+				}
+			}
+
+			turnOffIdleNodes(resourceManager.getComputingNodes());
+
+			break;
+		}
+		return plan;
+	}
+	
+	private HashMap<ResourceParameterName, ResourceUnit> chooseResourcesForExecution(ClusterResourceManager resourceManager, TaskInterface<?> task) {
+
+		List<ComputingNode> nodes = resourceManager.getComputingNodes();
+		nodes = findSuitableNodes(task, nodes);
+		Collections.sort(nodes, new Comparator<ComputingNode>(){
+		    public int compare(ComputingNode node1, ComputingNode node2){    
+		        return node1.getCategory().getName().compareTo(node2.getCategory().getName());
+		    }
+		});
+		if(nodes.size() > 0)
+		{
+			HashMap<ResourceParameterName, ResourceUnit> map = new HashMap<ResourceParameterName, ResourceUnit>();
+			List<ComputingResource> choosenResources =  new ArrayList<ComputingResource>();
+			int cpuRequest;
+			try {
+				cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
+			} catch (NoSuchFieldException e) {
+				cpuRequest = 1;
+			}
+			for (int i = 0; i < nodes.get(0).getProcessors().size() && cpuRequest > 0; i++) {
+				if (nodes.get(0).getProcessors().get(i).getStatus() == ResourceStatus.FREE ) {
+					choosenResources.add(nodes.get(0).getProcessors().get(i));
+					cpuRequest--;
+				}
+			}
+			ProcessingElements result = new ProcessingElements(nodes.get(0).getName());
+			result.addAll(choosenResources);
+			map.put(ResourceParameterName.PROCESSINGELEMENTS, result);
+			return map;
+		} else 
+			return null;
+	}
+	
+	private List<ComputingNode> findSuitableNodes(TaskInterface<?> task, List<ComputingNode> nodes){
+		int cpuRequest;
+		try {
+			cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
+		} catch (NoSuchFieldException e) {
+			cpuRequest = 1;
+		}
+		List<ComputingNode> avNodes = new ArrayList<ComputingNode>();
+		for(ComputingNode node: nodes){
+			if(node.getFreeProcessorsNumber() >= cpuRequest && node.getPowerInterface().getPowerState()==PowerState.ON){
+				avNodes.add(node);
+			}
+		}
+		return avNodes;
+	}
+
+	private void turnOffIdleNodes(List<ComputingNode> nodes){
+		for(ComputingNode node : nodes){
+			if(node.getFreeProcessorsNumber() == node.getProcessorsNumber() && node.getPowerInterface().getPowerState()==PowerState.ON){
+				System.out.println("wylaczam"+node.getName());
+				node.getPowerInterface().setPowerState(PowerState.OFF);
+			}
+		}
+	}
+	
+	private boolean harnessIdleNodesToWork(TaskInterface<?> task, List<ComputingNode> nodes){
+		int cpuRequest;
+		try {
+			cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
+		} catch (NoSuchFieldException e) {
+			cpuRequest = 1;
+		}
+		Collections.sort(nodes, new Comparator<ComputingNode>(){
+		    public int compare(ComputingNode node1, ComputingNode node2){    
+		        return node1.getCategory().getName().compareTo(node2.getCategory().getName());
+		    }
+		});
+		for (int i = 0; i < nodes.size() && cpuRequest > 0; i++) {
+			ComputingNode node = nodes.get(i);
+			if(node.getPowerInterface().getPowerState() == PowerState.OFF){
+				System.out.println("wlaczam"+node.getName());
+				node.getPowerInterface().setPowerState(PowerState.ON);
+				cpuRequest -= node.getProcessorsNumber();
+			}
+		}
+		return cpuRequest > 0 ? false : true;
+	}
+	
+	public String getName() {
+		return getClass().getName();
+	}
+
+	public void init(Properties properties) {
+		// no extra initialization is expected.
+	}
+
+}
Index: xssim/branches/tpiontek/example/tomekp/nodestest/ComputingNodePowerProfile.java
===================================================================
--- xssim/branches/tpiontek/example/tomekp/nodestest/ComputingNodePowerProfile.java	(revision 275)
+++ xssim/branches/tpiontek/example/tomekp/nodestest/ComputingNodePowerProfile.java	(revision 275)
@@ -0,0 +1,119 @@
+package tomekp.nodestest;
+
+import gridsim.gssim.GssimTags;
+
+import java.util.Arrays;
+import java.util.List;
+
+import schedframe.resources.PowerState;
+import test.rewolucja.energy.EnergyEvent;
+import test.rewolucja.energy.EnergyEventType;
+import test.rewolucja.energy.extension.EnergyExtension;
+import test.rewolucja.energy.profile.AbstractPowerProfile;
+import test.rewolucja.extensions.ExtensionType;
+import test.rewolucja.resources.ResourceStatus;
+import test.rewolucja.resources.physical.base.ComputingResource;
+import test.rewolucja.resources.physical.implementation.ComputingNode;
+
+public class ComputingNodePowerProfile extends AbstractPowerProfile{
+
+	public static double START_TIME = 600.0;
+	public static double SHUTDOWN_TIME = 300.0;
+	public static double START_COST = 1000;
+	public static double SHUTDOWN_COST = 500;
+	
+	protected ComputingNode computingNode;
+
+	public ComputingNodePowerProfile(){
+		powerState = PowerState.ON;
+	}
+	
+	public void visit(ComputingNode compNode){
+		computingNode = compNode;
+		energyExtension = (EnergyExtension) computingNode.getExtensionList().getExtension(ExtensionType.ENERGY_EXTENSION);
+	}
+	
+	public boolean setPowerState(PowerState state){
+		if(!supportPowerState(state))
+			return false;
+		//powerState = state;
+		if(state == PowerState.OFF){
+			if(powerState == PowerState.SHUTDOWNING)
+				powerState = PowerState.OFF;
+			else if(powerState == PowerState.ON)
+				powerState = PowerState.SHUTDOWNING;
+		}
+		else if(state == PowerState.ON){
+			if(powerState == PowerState.STARTING)
+				powerState = PowerState.ON;
+			else if(powerState == PowerState.OFF)
+				powerState = PowerState.STARTING;
+		}
+		if(computingNode.getProcessors() != null)
+		{
+			for(ComputingResource child:computingNode.getProcessors()){
+				child.getPowerInterface().setPowerState(state);
+			}
+		} else {
+			computingNode.handleEvent(new EnergyEvent(EnergyEventType.POWER_STATE_CHANGED, computingNode.getName()));
+		}
+		
+		if(state == PowerState.OFF){
+			if(powerState == PowerState.SHUTDOWNING)
+				computingNode.getLogicalResource().sendInternal(SHUTDOWN_TIME, GssimTags.SHUTDOWN, computingNode.getName());
+			computingNode.setStatus(ResourceStatus.UNAVAILABLE);
+		}
+		else if(state == PowerState.ON){
+			if(powerState == PowerState.STARTING)
+				computingNode.getLogicalResource().sendInternal(START_TIME, GssimTags.START, computingNode.getName());
+			computingNode.setStatus(ResourceStatus.FREE);
+		}
+		//computingNode.handleEvent(new EnergyEvent(EnergyEventType.POWER_STATE_CHANGED, computingNode.getName()));
+		return true;
+	}
+	
+	public double getPowerConsumption(PowerState state) {
+		if(powerState == PowerState.OFF)
+			return 0;
+		else if(powerState == PowerState.ON)
+		{	
+			if( computingNode.getCategory().getName().equals("A"))
+				return 240;
+			else if( computingNode.getCategory().getName().equals("B"))
+				return 95;
+		}else if(powerState == PowerState.STARTING)
+			return START_COST;
+		else if (powerState == PowerState.SHUTDOWNING)
+			return SHUTDOWN_COST;
+		
+		
+		return 150;
+	}
+
+	public boolean supportPowerState(PowerState state) {
+		switch(state){
+			case ON: 
+				return true;
+			case OFF: 
+				return true;
+			case SLEEP: 
+				return true;
+			case HIBERNATE: 
+				return true;
+			default: return false;
+		}
+	}
+
+	public List<PowerState> getSupportedPowerStates() {
+		return Arrays.asList(new PowerState[]{PowerState.ON, PowerState.OFF, PowerState.SLEEP, PowerState.HIBERNATE});
+	}
+
+	public void turnOn(){
+		setPowerState(PowerState.ON);
+	}
+	
+	public void turnOff(){
+		setPowerState(PowerState.OFF);
+	}
+
+}
