]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/types/PhysicalMachine.java
whitespace--
[pub/jan/lsdc.git] / src / at / ac / tuwien / lsdc / types / PhysicalMachine.java
1 package at.ac.tuwien.lsdc.types;
2
3 import java.util.HashMap;
4
5 public class PhysicalMachine {
6
7         private static int count = 0;
8
9         private HashMap<Integer, VirtualMachine> VMs = new HashMap<Integer, VirtualMachine>();
10
11         private int id;
12
13         private final int maxSize = 50000;
14         private final int maxRAM = 4700;
15         private final int maxCPU = 2400;
16
17         private int size;
18         private int RAM;
19         private int CPU;
20
21         private final int initialSize = 850;
22         private final int initialRAM = 300;
23         private final int initialCPU = 500;
24
25         private boolean running = false;
26
27         public PhysicalMachine() {
28                 id = count;
29                 count++;
30         }
31
32         public void start() {
33                 size = initialSize;
34                 RAM = initialRAM;
35                 CPU = initialCPU;
36                 running = true;
37         }
38
39         public void stop() {
40                 // TODO: anything else we need to do? maybe implement a stop method in
41                 // VirtualMachine and call it on every VM
42                 VMs = new HashMap<Integer, VirtualMachine>();
43                 size = 0;
44                 RAM = 0;
45                 CPU = 0;
46                 running = false;
47         }
48
49         public double getConsumption() {
50                 return 200 + 0.3 * CPU;
51         }
52
53         public int startVirtualMachine(int size, int RAM, int CPU) {
54                 if (checkVM(size, RAM, CPU)) {
55                         VirtualMachine vm = new VirtualMachine(size, RAM, CPU, this);
56                         VMs.put(vm.getId(), vm);
57                         size = size + vm.getSize();
58                         RAM = RAM + vm.getRAM();
59                         CPU = CPU + vm.getCPU();
60                         return vm.getId();
61                 } else
62                         return -1;
63         }
64
65         public boolean stopVirtualMachine(VirtualMachine vm) {
66                 if (VMs.containsKey(vm.getId())) {
67                         if (vm.getApplications().size() != 0) {
68                                 // apps must be migrated before stopping a VM
69                                 return false;
70                         } else {
71                                 VMs.remove(vm.getId());
72                                 size = size - vm.getSize();
73                                 RAM = RAM - vm.getRAM();
74                                 CPU = CPU - vm.getCPU();
75                                 return true;
76                         }
77                 } else
78                         return false;
79         }
80
81         private boolean checkVM(int size, int RAM, int CPU) {
82                 return (size <= availableSize()) && (RAM <= availableRAM())
83                                 && (CPU <= availableCPU());
84         }
85
86         private int availableSize() {
87                 return maxSize - size;
88         }
89
90         private int availableRAM() {
91                 return maxRAM - RAM;
92         }
93
94         private int availableCPU() {
95                 return maxCPU - CPU;
96         }
97
98         public int getId() {
99                 return id;
100         }
101
102         public boolean isRunning() {
103                 return running;
104         }
105
106         public VirtualMachine getVirtualMachine(int id) {
107                 return VMs.get(id);
108         }
109
110         public int countCurrentlyRunningVMs() {
111                 return VMs.values().size();
112         }
113 }