]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/types/VirtualMachine.java
Sched B. Ein wenig getestet.
[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 import at.ac.tuwien.lsdc.exception.VMResizeException;
7
8 public class VirtualMachine {
9
10         public enum VMType {
11                 Resizable, NonResizable
12         }
13
14         private static int count = 0;
15         private int posOnPM;
16
17         private PhysicalMachine runningOn;
18
19         private HashMap<Integer, Application> applications = new HashMap<Integer, Application>();
20
21         private int id;
22
23         public static final int initialSize = 100;
24         public static final int initialRAM = 50;
25         public static final int initialCPU = 150;
26
27         private int reservedSize;
28         private int reservedRAM;
29         private int reservedCPU;
30
31         private VMType type;
32
33         public VirtualMachine(int size, int RAM, int CPU, PhysicalMachine pm, VMType type) {
34                 id = count;
35                 count++;
36                 reservedSize = size + initialSize;
37                 reservedRAM = RAM + initialRAM;
38                 reservedCPU = CPU + initialCPU;
39                 runningOn = pm;
40                 posOnPM = pm.countCurrentlyRunningVMs();
41                 this.type = type;
42         }
43
44         public boolean startApplication(Application app) {
45                 if (enoughResources(app)) {
46                         applications.put(app.getID(), app);
47                         app.setRunningOn(this);
48                         return true;
49                 } else
50                         return false;
51         }
52
53         /**
54          * Set the VM size to the given new values.
55          * 
56          * @param newSize
57          * @param newRAM
58          * @param newCPU
59          * @return
60          */
61         public boolean resizeVM(int newSize, int newRAM, int newCPU) {
62                 if (type == VMType.Resizable && runningOn.checkVM(newSize, newRAM, newCPU)) {
63                         // Resize VM
64                         reservedSize = initialSize + newSize;
65                         reservedRAM = initialRAM + newRAM;
66                         reservedCPU = initialCPU + newCPU;
67                         return true;
68                 } else
69                         throw new VMResizeException("Could not resize VM!", this);
70         }
71
72         public boolean stopApplication(Application app) {
73                 if (applications.containsKey(app.getID())) {
74                         applications.remove(app.getID());
75                         app.setRunningOn(null);
76                         return true;
77                 } else
78                         return false;
79         }
80
81         private boolean enoughResources(Application app) {
82                 return (app.getSize() <= availableSize()) && (app.getRam() <= availableRAM()) && (app.getCpu() <= availableCPU());
83         }
84
85         private int availableSize() {
86                 return reservedSize - getSize();
87         }
88
89         private int availableRAM() {
90                 return reservedRAM - getRAM();
91         }
92
93         private int availableCPU() {
94                 return reservedCPU - getCPU();
95         }
96
97         public int getId() {
98                 return id;
99         }
100
101         public PhysicalMachine getRunningOn() {
102                 return runningOn;
103         }
104
105         public int getPositionOnPM() {
106                 return posOnPM;
107         }
108
109         public ArrayList<Application> getApplications() {
110                 return new ArrayList<Application>(applications.values());
111         }
112
113         public int getSize() {
114                 int usedSize = initialSize;
115                 for (Application a : applications.values())
116                         usedSize += a.getSize();
117                 return usedSize;
118         }
119
120         public int getRAM() {
121                 int usedRAM = initialRAM;
122                 for (Application a : applications.values())
123                         usedRAM += a.getRam();
124                 return usedRAM;
125         }
126
127         public int getCPU() {
128                 int usedCPU = initialCPU;
129                 for (Application a : applications.values())
130                         usedCPU += a.getCpu();
131                 return usedCPU;
132         }
133
134         public int getReservedSize() {
135                 return reservedSize;
136         }
137
138         public int getReservedRAM() {
139                 return reservedRAM;
140         }
141
142         public int getReservedCPU() {
143                 return reservedCPU;
144         }
145
146         @Override
147         public String toString() {
148                 return "VirtualMachine [posOnPM=" + posOnPM + ", runningOn=" + runningOn + ", id=" + id + ", reservedSize="
149                                 + reservedSize + ", reservedRAM=" + reservedRAM + ", reservedCPU=" + reservedCPU + ", type=" + type + "]";
150         }
151
152 }