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;
32 public VirtualMachine(int size, int RAM, int CPU, PhysicalMachine pm, VMType type) {
35 this.size = size + initialSize;
36 this.RAM = RAM + initialRAM;
37 this.CPU = 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);
48 } else if (type == VMType.Resizable && runningOn.checkVM(app.getSize(), app.getRam(), app.getCpu())) {
49 applications.put(app.getID(), app);
50 size = size + app.getSize();
51 RAM = RAM + app.getRam();
52 CPU = CPU + app.getCpu();
53 app.setRunningOn(this);
59 public boolean stopApplication(Application app) {
60 if (applications.containsKey(app.getID())) {
61 size = size - app.getSize();
62 RAM = RAM - app.getRam();
63 CPU = CPU - app.getCpu();
64 applications.remove(app.getID());
65 app.setRunningOn(null);
71 private boolean enoughResources(Application app) {
72 return (app.getSize() <= availableSize())
73 && (app.getRam() <= availableRAM())
74 && (app.getCpu() <= availableCPU());
77 private int availableSize() {
78 return size - initialSize;
81 private int availableRAM() {
82 return RAM - initialRAM;
85 private int availableCPU() {
86 return CPU - initialCPU;
93 public PhysicalMachine getRunningOn() {
97 public int getPositionOnPM() {
101 public ArrayList<Application> getApplications() {
102 return new ArrayList<Application>(applications.values());
105 public int getSize() {
106 int usedSize = initialSize;
107 for (Application a : applications.values())
108 usedSize += a.getSize();
112 public int getRAM() {
113 int usedRAM = initialRAM;
114 for (Application a : applications.values())
115 usedRAM += a.getRam();
119 public int getCPU() {
120 int usedCPU = initialCPU;
121 for (Application a : applications.values())
122 usedCPU += a.getCpu();