]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/types/VirtualMachine.java
changed names of resoure variables to reserved*, change calculation of checkVM, make...
[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 reservedSize;
27         private int reservedRAM;
28         private int reservedCPU;
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.reservedSize = size + initialSize;
36                 this.reservedRAM = RAM + initialRAM;
37                 this.reservedCPU = 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
49                         return false;
50         }
51         
52         public boolean resizeVM(int newSize, int newRAM, int newCPU) {
53                 if (type == VMType.Resizable  &&  runningOn.checkVM(newSize-reservedSize, newRAM-reservedRAM, newCPU-reservedCPU )) {
54                         //Resize VM
55                         reservedSize = reservedSize + newSize;
56                         reservedRAM = reservedRAM + newRAM;
57                         reservedCPU = reservedCPU + newCPU;
58                         return true;
59                 } else
60                         return false;
61         }
62
63         public boolean stopApplication(Application app) {
64                 if (applications.containsKey(app.getID())) {
65                         applications.remove(app.getID());
66                         app.setRunningOn(null);
67                         return true;
68                 } else
69                         return false;
70         }
71
72         private boolean enoughResources(Application app) {
73                 return (app.getSize() <= availableSize())
74                                 && (app.getRam() <= availableRAM())
75                                 && (app.getCpu() <= availableCPU());
76         }
77
78         private int availableSize() {
79                 return reservedSize - getSize();
80         }
81
82         private int availableRAM() {
83                 return reservedRAM - getRAM();
84         }
85
86         private int availableCPU() {
87                 return reservedCPU - getCPU();
88         }
89
90         public int getId() {
91                 return id;
92         }
93
94         public PhysicalMachine getRunningOn() {
95                 return runningOn;
96         }
97         
98         public int getPositionOnPM() {
99                 return posOnPM;
100         }
101
102         public ArrayList<Application> getApplications() {
103                 return new ArrayList<Application>(applications.values());
104         }
105
106         public int getSize() {
107                 int usedSize = initialSize;
108                 for (Application a : applications.values())
109                         usedSize += a.getSize();
110                 return usedSize;
111         }
112
113         public int getRAM() {
114                 int usedRAM = initialRAM;
115                 for (Application a : applications.values())
116                         usedRAM += a.getRam();
117                 return usedRAM;
118         }
119
120         public int getCPU() {
121                 int usedCPU = initialCPU;
122                 for (Application a : applications.values())
123                         usedCPU += a.getCpu();
124                 return usedCPU;
125         }
126
127         public int getReservedSize() {
128                 return reservedSize;
129         }
130
131         public int getReservedRAM() {
132                 return reservedRAM;
133         }
134
135         public int getReservedCPU() {
136                 return reservedCPU;
137         }
138
139 }