]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/types/VirtualMachine.java
changed calculation of RAM,CPU & Size
[pub/jan/lsdc.git] / src / at / ac / tuwien / lsdc / types / VirtualMachine.java
1 package at.ac.tuwien.lsdc.types;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5
6 public class VirtualMachine {
7
8         public enum VMType {
9                 Resizable, NonResizable
10         }
11         
12         
13         private static int count = 0;
14         private int posOnPM;
15         
16         private PhysicalMachine runningOn;
17
18         private HashMap<Integer, Application> applications = new HashMap<Integer, Application>();
19
20         private int id;
21
22         public static final int initialSize = 100;
23         public static final int initialRAM = 50;
24         public static final int initialCPU = 150;
25
26         private int size;
27         private int RAM;
28         private int CPU;
29         
30         private VMType type;
31
32         public VirtualMachine(int size, int RAM, int CPU, PhysicalMachine pm, VMType type) {
33                 this.id = count;
34                 count++;
35                 this.size = size + initialSize;
36                 this.RAM = RAM + initialRAM;
37                 this.CPU = CPU + initialCPU;
38                 this.runningOn = pm;
39                 this.posOnPM = pm.countCurrentlyRunningVMs();
40                 this.type = type;
41         }
42
43         public boolean startApplication(Application app) {
44                 if (enoughResources(app)) {
45                         applications.put(app.getID(), app);
46                         app.setRunningOn(this);
47                         return true;
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);
54                         return true;
55                 } else
56                         return false;
57         }
58
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);
66                         return true;
67                 } else
68                         return false;
69         }
70
71         private boolean enoughResources(Application app) {
72                 return (app.getSize() <= availableSize())
73                                 && (app.getRam() <= availableRAM())
74                                 && (app.getCpu() <= availableCPU());
75         }
76
77         private int availableSize() {
78                 return size - initialSize;
79         }
80
81         private int availableRAM() {
82                 return RAM - initialRAM;
83         }
84
85         private int availableCPU() {
86                 return CPU - initialCPU;
87         }
88
89         public int getId() {
90                 return id;
91         }
92
93         public PhysicalMachine getRunningOn() {
94                 return runningOn;
95         }
96         
97         public int getPositionOnPM() {
98                 return posOnPM;
99         }
100
101         public ArrayList<Application> getApplications() {
102                 return new ArrayList<Application>(applications.values());
103         }
104
105         public int getSize() {
106                 int usedSize = initialSize;
107                 for (Application a : applications.values())
108                         usedSize += a.getSize();
109                 return usedSize;
110         }
111
112         public int getRAM() {
113                 int usedRAM = initialRAM;
114                 for (Application a : applications.values())
115                         usedRAM += a.getRam();
116                 return usedRAM;
117         }
118
119         public int getCPU() {
120                 int usedCPU = initialCPU;
121                 for (Application a : applications.values())
122                         usedCPU += a.getCpu();
123                 return usedCPU;
124         }
125
126 }