]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/types/VirtualMachine.java
started implementing MachineManager, VM & PM
[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         private static int count = 0;
9
10         private PhysicalMachine runningOn;
11
12         private HashMap<Integer, Application> applications = new HashMap<Integer, Application>();
13
14         private int id;
15
16         public static final int initialSize = 100;
17         public static final int initialRAM = 50;
18         public static final int initialCPU = 150;
19
20         private int size;
21         private int RAM;
22         private int CPU;
23
24         public VirtualMachine(int size, int RAM, int CPU, PhysicalMachine pm) {
25                 this.id = count;
26                 count++;
27                 this.size = size + initialSize;
28                 this.RAM = RAM + initialRAM;
29                 this.CPU = CPU + initialCPU;
30                 this.runningOn = pm;
31         }
32
33         public boolean startApplication(Application app) {
34                 if (checkApp(app)) {
35                         applications.put(app.getID(), app);
36                         size = size + app.getSize();
37                         RAM = RAM + app.getRam();
38                         CPU = CPU + app.getCpu();
39                         return true;
40                 } else
41                         return false;
42         }
43
44         public boolean stopApplication(Application app) {
45                 if (applications.containsKey(app.getID())) {
46                         size = size - app.getSize();
47                         RAM = RAM - app.getRam();
48                         CPU = CPU - app.getCpu();
49                         applications.remove(app.getID());
50                         return true;
51                 } else
52                         return false;
53         }
54
55         private boolean checkApp(Application app) {
56                 return (app.getSize() <= availableSize())
57                                 && (app.getRam() <= availableRAM())
58                                 && (app.getCpu() <= availableCPU());
59         }
60
61         private int availableSize() {
62                 return size - initialSize;
63         }
64
65         private int availableRAM() {
66                 return RAM - initialRAM;
67         }
68
69         private int availableCPU() {
70                 return CPU - initialCPU;
71         }
72
73         public int getId() {
74                 return id;
75         }
76
77         public PhysicalMachine getRunningOn() {
78                 return runningOn;
79         }
80
81         public ArrayList<Application> getApplications() {
82                 return new ArrayList<Application>(applications.values());
83         }
84
85         public int getSize() {
86                 return size;
87         }
88
89         public int getRAM() {
90                 return RAM;
91         }
92
93         public int getCPU() {
94                 return CPU;
95         }
96
97 }