From 6a8eef158b2a8a57de2e975546ca4390f4b5e880 Mon Sep 17 00:00:00 2001
From: Stefan Derkits <stefan@derkits.at>
Date: Wed, 12 Jun 2013 16:58:43 +0200
Subject: [PATCH] implemented SchedC

---
 src/at/ac/tuwien/lsdc/sched/SchedulerC.java   | 69 ++++++++++++++-----
 .../ac/tuwien/lsdc/types/PhysicalMachine.java | 12 +++-
 2 files changed, 61 insertions(+), 20 deletions(-)

diff --git a/src/at/ac/tuwien/lsdc/sched/SchedulerC.java b/src/at/ac/tuwien/lsdc/sched/SchedulerC.java
index afe1837..1539854 100644
--- a/src/at/ac/tuwien/lsdc/sched/SchedulerC.java
+++ b/src/at/ac/tuwien/lsdc/sched/SchedulerC.java
@@ -2,24 +2,31 @@ package at.ac.tuwien.lsdc.sched;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.Collections;
 import java.util.LinkedList;
+import java.util.List;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import at.ac.tuwien.lsdc.exception.OutOfPMsException;
 import at.ac.tuwien.lsdc.types.Application;
+import at.ac.tuwien.lsdc.types.PhysicalMachine;
 import at.ac.tuwien.lsdc.types.ScenarioType;
 import at.ac.tuwien.lsdc.types.SchedulerEvent;
 import at.ac.tuwien.lsdc.types.SchedulerType;
 import at.ac.tuwien.lsdc.types.VirtualMachine;
+import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
 
 public class SchedulerC extends AbstractSchedulerWithMigration {
 
-	private static final Logger log = LoggerFactory.getLogger(SchedulerC.class);
+	private static final Logger LOG = LoggerFactory.getLogger(SchedulerC.class);
 
 	public SchedulerC(int numPMs, int numCloudPartners, File schedulerLog, ScenarioType scenario)
 			throws IOException {
 		super(numPMs, numCloudPartners, schedulerLog, scenario);
+
+		this.vmType = VMType.NonResizable;
 	}
 
 	@Override
@@ -29,32 +36,58 @@ public class SchedulerC extends AbstractSchedulerWithMigration {
 
 	@Override
 	protected void handleEndEvents(LinkedList<SchedulerEvent> events) {
-		// TODO Auto-generated method stub
-
-	}
-
-	@Override
-	protected void handleDelayedApps() {
-		// TODO Auto-generated method stub
-
+		LOG.debug("stopApps():" + events);
+		for (SchedulerEvent evt : events) {
+			VirtualMachine vm = evt.getApp().getRunningOn();
+			vm.stopApplication(evt.getApp());
+			if (vm.getApplications().size() == 0) {
+				PhysicalMachine pm = vm.getRunningOn();
+				pm.stopVirtualMachine(vm);
+				manager.stopPhysicalMachine(pm.getId());
+			}
+		}
 	}
 
 	@Override
 	protected void handleStartEvents(LinkedList<SchedulerEvent> events) {
-		// TODO Auto-generated method stub
-
-	}
-
-	@Override
-	protected void runMigration() {
-		// TODO Auto-generated method stub
+		LOG.debug("startApps():" + events);
+		boolean deployed = false;
+		for (SchedulerEvent evt : events) {
+			deployed = false;
+			VirtualMachine vm = null;
+			List<PhysicalMachine> sortedPMs = manager.getSortedPMs();
+			Collections.reverse(sortedPMs);
+			for (PhysicalMachine pm : sortedPMs) {
+				if (pm.isRunning() && (pm.getTotalVMs() > 0)) {
+					vm = pm.getVirtualMachines().values().iterator().next();
+					deployed = deployApp(vm, evt.getApp());
+					if (deployed)
+						break;
+				}
+			}
+			if (vm == null) {
+				try {
+					vm = manager.startPhysicalMachine().startVirtualMachine(vmType);
+					deployed = deployApp(vm, evt.getApp());
+				} catch (OutOfPMsException e) {
+					if (federation.askToOutsource(evt.getApp())) {
+						insertOutsourcedStartEvent(currTime + 1, evt.getApp());
+					} else {
+						LOG.info("delaying the start of:" + evt.getApp());
+						delayedApps.add(evt.getApp());
+						return;
+					}
+				}
+			}
+			if (deployed)
+				insertStopEvent(currTime + evt.getApp().getDuration(), evt.getApp());
+		}
 
 	}
 
 	@Override
 	protected boolean deployApp(VirtualMachine vm, Application app) {
-		// TODO Auto-generated method stub
-		return false;
+		return vm.startApplication(app);
 	}
 
 }
diff --git a/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java b/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java
index 89e937b..4a482b0 100644
--- a/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java
+++ b/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java
@@ -65,6 +65,14 @@ public class PhysicalMachine implements Comparable<PhysicalMachine> {
 			return 0.0;
 	}
 
+	// Creates a VirtualMachine of maximum possible Size
+	public VirtualMachine startVirtualMachine(VMType type) {
+		VirtualMachine vm = new VirtualMachine(maxSize - initialSize - VirtualMachine.initialSize,
+				maxRAM - initialRAM - VirtualMachine.initialRAM, maxCPU - initialCPU
+						- VirtualMachine.initialCPU, this, type);
+		return vm;
+	}
+
 	public VirtualMachine startVirtualMachine(int sz, int ram, int cpu, VMType type) {
 		if (checkVM(sz, ram, cpu)) {
 			VirtualMachine vm = new VirtualMachine(sz, ram, cpu, this, type);
@@ -158,7 +166,7 @@ public class PhysicalMachine implements Comparable<PhysicalMachine> {
 	public VirtualMachine getVirtualMachine(int id) {
 		return VMs.get(id);
 	}
-	
+
 	public VirtualMachine getLatestVM() {
 		VirtualMachine vm = null;
 		for(Iterator<VirtualMachine> it = VMs.values().iterator(); it.hasNext(); ) {
@@ -166,7 +174,7 @@ public class PhysicalMachine implements Comparable<PhysicalMachine> {
 		}
 		return vm;
 	}
-	
+
 	public Integer getLatestVMID() {
 		return getLatestVM().getId();
 	}
-- 
2.43.0