1 package at.ac.tuwien.lsdc.types;
3 import java.util.ArrayList;
4 import java.util.HashMap;
6 public class VirtualMachine {
9 Resizable, NonResizable
13 private static int count = 0;
16 private PhysicalMachine runningOn;
18 private HashMap<Integer, Application> applications = new HashMap<Integer, Application>();
22 public static final int initialSize = 100;
23 public static final int initialRAM = 50;
24 public static final int initialCPU = 150;
26 private int reservedSize;
27 private int reservedRAM;
28 private int reservedCPU;
32 public VirtualMachine(int size, int RAM, int CPU, PhysicalMachine pm, VMType type) {
35 this.reservedSize = size + initialSize;
36 this.reservedRAM = RAM + initialRAM;
37 this.reservedCPU = CPU + initialCPU;
39 this.posOnPM = pm.countCurrentlyRunningVMs();
43 public boolean startApplication(Application app) {
44 if (enoughResources(app)) {
45 applications.put(app.getID(), app);
46 app.setRunningOn(this);
52 public boolean resizeVM(int newSize, int newRAM, int newCPU) {
53 if (type == VMType.Resizable && runningOn.checkVM(newSize-reservedSize, newRAM-reservedRAM, newCPU-reservedCPU )) {
55 reservedSize = reservedSize + newSize;
56 reservedRAM = reservedRAM + newRAM;
57 reservedCPU = reservedCPU + newCPU;
63 public boolean stopApplication(Application app) {
64 if (applications.containsKey(app.getID())) {
65 applications.remove(app.getID());
66 app.setRunningOn(null);
72 private boolean enoughResources(Application app) {
73 return (app.getSize() <= availableSize())
74 && (app.getRam() <= availableRAM())
75 && (app.getCpu() <= availableCPU());
78 private int availableSize() {
79 return reservedSize - getSize();
82 private int availableRAM() {
83 return reservedRAM - getRAM();
86 private int availableCPU() {
87 return reservedCPU - getCPU();
94 public PhysicalMachine getRunningOn() {
98 public int getPositionOnPM() {
102 public ArrayList<Application> getApplications() {
103 return new ArrayList<Application>(applications.values());
106 public int getSize() {
107 int usedSize = initialSize;
108 for (Application a : applications.values())
109 usedSize += a.getSize();
113 public int getRAM() {
114 int usedRAM = initialRAM;
115 for (Application a : applications.values())
116 usedRAM += a.getRam();
120 public int getCPU() {
121 int usedCPU = initialCPU;
122 for (Application a : applications.values())
123 usedCPU += a.getCpu();
127 public int getReservedSize() {
131 public int getReservedRAM() {
135 public int getReservedCPU() {