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;
15 private PhysicalMachine runningOn;
17 private HashMap<Integer, Application> applications = new HashMap<Integer, Application>();
21 public static final int initialSize = 100;
22 public static final int initialRAM = 50;
23 public static final int initialCPU = 150;
31 public VirtualMachine(int size, int RAM, int CPU, PhysicalMachine pm, VMType type) {
34 this.size = size + initialSize;
35 this.RAM = RAM + initialRAM;
36 this.CPU = CPU + initialCPU;
41 public boolean startApplication(Application app) {
42 if (enoughResources(app)) {
43 applications.put(app.getID(), app);
45 } else if (type == VMType.Resizable && runningOn.checkVM(app.getSize(), app.getRam(), app.getCpu())) {
46 applications.put(app.getID(), app);
47 size = size + app.getSize();
48 RAM = RAM + app.getRam();
49 CPU = CPU + app.getCpu();
55 public boolean stopApplication(Application app) {
56 if (applications.containsKey(app.getID())) {
57 size = size - app.getSize();
58 RAM = RAM - app.getRam();
59 CPU = CPU - app.getCpu();
60 applications.remove(app.getID());
66 private boolean enoughResources(Application app) {
67 return (app.getSize() <= availableSize())
68 && (app.getRam() <= availableRAM())
69 && (app.getCpu() <= availableCPU());
72 private int availableSize() {
73 return size - initialSize;
76 private int availableRAM() {
77 return RAM - initialRAM;
80 private int availableCPU() {
81 return CPU - initialCPU;
88 public PhysicalMachine getRunningOn() {
92 public ArrayList<Application> getApplications() {
93 return new ArrayList<Application>(applications.values());
96 public int getSize() {
100 public int getRAM() {
104 public int getCPU() {