Repository navigation
Expand file tree
/
Copy pathATC.java
More file actions
211 lines (162 loc) · 6.63 KB
/
Copy pathATC.java
File metadata and controls
211 lines (162 loc) · 6.63 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import java.util.*;
public class ATC implements Runnable {
private final String atcName;
private final APAirport airport;
private final int MAX_CAPACITY = 3;
// Queue for planes waiting to land & takeoff & refuel
private final LinkedList<Plane> landingQueue = new LinkedList<>();
private final LinkedList<Plane> takeoffQueue = new LinkedList<>();
private final LinkedList<Plane> refuelingQueue = new LinkedList<>();
// Runway control
private boolean runwayAvailable = true;
// Atc thread running status
private boolean running = true;
public ATC(String name) {
this.atcName = name;
this.airport = new APAirport();
}
public APAirport getAirport() {
return airport;
}
@Override
public void run() {
System.out.println(atcName + " created");
while (running) {
try {
// Clear the airport first
processTakeoffRequests();
processLandingRequests();
processRefuelingRequests();
Thread.sleep(10); // Loop every 0.01s
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// Assign truck
private synchronized void processRefuelingRequests() {
// Plane waiting for refuel
if (!refuelingQueue.isEmpty()) {
RefuelingTruck truck = airport.getRefuelingTruck();
// If truck is available
if (truck.isAvailable()) {
Plane nextPlane = refuelingQueue.peek(); // Get first plane
// If plane havent refuel yet
if (nextPlane.waitingForRefueling()) {
// Dispatch truck
refuelingQueue.removeFirst();
System.out.println("ATC: Refueling truck assigned to " + nextPlane.getName() + ".");
RefuelOperation refuelTask = new RefuelOperation(nextPlane, truck);
Thread refuelThread = new Thread(refuelTask, "Thread-Refuel");
refuelThread.start();
}
}
}
}
// Approve takeoff requests
private synchronized void processTakeoffRequests() {
// Runway clear && plane waiting for takeoff
if (runwayAvailable && !takeoffQueue.isEmpty()) {
System.out.println(atcName +": Runway is now free.");
// first in first out (peek read first elements)
Plane nextPlane = takeoffQueue.peek();
// If plane is waiting only
if (nextPlane.waitingTakeOffApproval()) {
// Mark runway occupied
runwayAvailable = false;
System.out.println(atcName + ": Takeoff granted for " + nextPlane.getName() + ".");
// Approve plane to takeoff
nextPlane.approveAndStartTakeOff();
// Wake up the plane
synchronized (nextPlane) {
nextPlane.notify();
}
}
}
}
// Approve landing requests
private synchronized void processLandingRequests() {
// Runway clear && airport not full && plane waiting for landing
if (runwayAvailable && airport.getPlanesInAirport() < MAX_CAPACITY && !landingQueue.isEmpty()) {
System.out.println(atcName +": Runway is now free.");
Plane nextPlane = landingQueue.peek();
if (nextPlane.waitingLandApproval()) {
runwayAvailable = false;
if (nextPlane.isEmergency()) {
System.out.println(atcName + ": Emergency landing authorized for " + nextPlane.getName() + ".");
}
else {
System.out.println(atcName + ": Landing permission granted for " + nextPlane.getName() + ".");
}
nextPlane.availableToLand();
synchronized (nextPlane) {
nextPlane.notify();
}
}
}
}
// Plane added into queue
public synchronized void addLandingQueue(Plane plane) {
String planeName = plane.getName();
boolean isEmergency = plane.isEmergency();
if (isEmergency) {
System.out.println(planeName + ": Requesting emergency landing!!!");
}
else {
System.out.println(planeName + ": Requesting landing.");
}
// Prioritize emergency plane(s)
if (isEmergency) {
int insertPos = 0;
for (int i = 0; i < landingQueue.size(); i++) {
// if current plane is not emergency, insert before it
if (!landingQueue.get(i).isEmergency()) {
insertPos = i;
break;
}
}
landingQueue.add(insertPos, plane);
System.out.println(atcName + ": " + planeName + " has been allowed to cut the queue.");
}
else { // Adding plane in queue
landingQueue.add(plane);
System.out.println(atcName + ": " + planeName + " has been added in the queue.");
}
}
// Remove plane from queue
public synchronized void planeLanded(Plane plane) {
landingQueue.remove(plane);
runwayAvailable = true;
}
// Assign gate to plane
public synchronized Gate assignGateNumber(Plane plane) {
Gate gate = airport.findAvailableGate();
if (gate != null) {
// Set gate unavailable
gate.occupy();
System.out.println(atcName + ": Gate-" + gate.getGateNumber() + " assigned for " + plane.getName() + ".");
}
return gate;
}
// Plane requests refueling
public synchronized void requestRefueling(Plane plane) {
System.out.println(plane.getName() + ": Requesting refueling.");
refuelingQueue.add(plane);
}
// Request takeoff permission
public synchronized void addTakeOffQueue(Plane plane) {
takeoffQueue.add(plane);
System.out.println(plane.getName() + ": Requesting take-off.");
}
// Called when plane departs (frees up a slot)
public synchronized void planeDeparted(Plane plane) {
takeoffQueue.remove(plane);
runwayAvailable = true;
}
// Stop ATC thread
public void shutdown() {
System.out.println("\n" + atcName + ": Shutting down.");
running = false;
}
}