-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.java
131 lines (106 loc) · 3.27 KB
/
Server.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import java.util.LinkedList;
import java.util.Queue;
public class Server {
// Queue of Jobs at this server
private Queue<Job> jobs;
// Current Job in the server
private Job currentJob;
// The departure time of the job in service
private int nextDepartureTime = Integer.MAX_VALUE;
// Current Time
private int currTime;
// Capacity of servings that this station can hold
private int itemsCapacity;
// Number of servings left in this server
private int itemsLeft;
// The index associated with this server in the larger system
private int index;
// The number of servers in the system
private int servers;
// The amount of food a station gives to a job
private double portionSize;
public Server(int index,int servers,int capacity, double portionSize) {
this.index = index;
this.servers = servers;
this.portionSize = portionSize;
setItemsLeft(capacity);
jobs = new LinkedList<Job>();
}
/**
* Adds a job to this server
* @param job job in the system: Job
*/
public void addJob(Job job) {
currTime = job.getArrivalTime();
if(currentJob == null) {
setCurrentJob(job);
nextDepartureTime = currTime + job.getSizes(index);
} else {
jobs.add(job);
}
}
/**
* Handles the departure of a job from the server
*/
public Job departure() {
currTime = nextDepartureTime;
Job oldJob = currentJob;
setCurrentJob(null);
//tracks items left
if (index != 0 && index != 1) {
itemsLeft--;
}
//adds dummy job if there are no items left (dummy jobs are when food is restocked in the servers)
if ((itemsLeft == 0) && (index != 0 && index != 1)){
setCurrentJob(new Job(-1, itemsCapacity/300, servers, index, 0, 0, 0));
nextDepartureTime = currTime + currentJob.getSizes(index);
itemsLeft = itemsCapacity;
} else if (!jobs.isEmpty()) {
setCurrentJob(jobs.remove());
nextDepartureTime = currTime + currentJob.getSizes(index);
} else {
nextDepartureTime = Integer.MAX_VALUE;
}
return oldJob;
}
/**
* Sets this server's current job
*/
public void setCurrentJob(Job job) {
currentJob = job;
}
/**
* Sets the capacity and items left of this server
*/
public void setItemsLeft(int left){
itemsCapacity = (int) (left - (0.5 - portionSize)*left);
itemsLeft = itemsCapacity;
}
/**
* Grabs the current job being serviced by this server
*/
public Job getCurrentJob() {
return currentJob;
}
/**
* Grabs the current number of jobs at the server
*/
public int getNumJobs() {
if(currentJob != null && currentJob.getStartTime() >= 0) {
return jobs.size() + 1;
} else
return jobs.size();
}
/**
* Grabs the departure time of the job currently in service
*/
public int getNextDepartureTime() {
return nextDepartureTime;
}
/**
* Grabs the index associated with this server in the larger system
*/
public int getIndex() {
return index;
}
}