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