-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlightManager.cs
More file actions
382 lines (349 loc) · 13.5 KB
/
FlightManager.cs
File metadata and controls
382 lines (349 loc) · 13.5 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using BaggageSortingSystem.classes;
using BaggageSortingSystem.events;
namespace BaggageSortingSystem
{
/// <summary>
/// FlightManager is essentially the Database Simulator. It generates new flights and the passengers to those.
/// </summary>
public class FlightManager
{
int nextFlightID;
int nextPassengerID;
bool stop;
DateTime lastGeneratedSegmentEnd;
Flight[] flightPlan;
Thread thread;
public Passenger[] passengersQueue = new Passenger[0];
public object passengerQueueLock = new object();
public event EventHandler PassengerQueueChanged;
public Flight[] flightsRequiringGate = new Flight[0];
public object flightsRequiringGateLock = new object();
public int NextFlightID
{
get { return this.nextFlightID; }
set { this.nextFlightID = value; }
}
public int NextPassengerID
{
get { return this.nextPassengerID; }
set { this.nextPassengerID = value; }
}
public bool Stop
{
get { return this.stop; }
set { this.stop = value; }
}
public DateTime LastGeneratedSegmentEnd
{
get { return this.lastGeneratedSegmentEnd; }
}
public Flight[] FlightPlan
{
get { return this.flightPlan; }
set { this.flightPlan = value; }
}
public Thread _Thread
{
get { return this.thread; }
}
public FlightManager()
{
this.stop = false;
this.nextFlightID = 0;
this.nextPassengerID = 0;
this.lastGeneratedSegmentEnd = DateTime.Now.AddMinutes(105 * CentralServer.timeScale);
this.flightPlan = new Flight[0];
}
public string ToString()
{
return "### Flight Manager: ###\n Flights Planned: " + this.flightPlan.Length + "\n Flights requesting Gates: " + this.flightsRequiringGate.Length + " | Passengers in Queue: " + this.passengersQueue.Length;
}
public void InitializeThread()
{
this.thread = new Thread(new ThreadStart(this.ManageFlights));
this.thread.Name = "The Flight Manager";
this.thread.Start();
}
public void ManageFlights()
{
while (!this.stop)
{
while(this.lastGeneratedSegmentEnd < DateTime.Now.AddMinutes(240 * CentralServer.timeScale))
{
// Generates random amounts of flights in sub-segment (1 Sub-segment being 1 hour at timeScale 1.0)
GenerateFlightsInSegment();
// Throw Flights to be Gated
for (int i = 0; i < flightPlan.Length; i++)
{
if(flightPlan[i].DepartureTime > lastGeneratedSegmentEnd)
{
// Set Flight up for getting a Gate
ReadyFlight(flightPlan[i]);
// Enqueue Passengers
EnqueuePassengers(flightPlan[i].PassengerList);
}
}
this.lastGeneratedSegmentEnd = this.lastGeneratedSegmentEnd.AddMinutes(60 * CentralServer.timeScale);
Thread.Sleep(Convert.ToInt32(10000 * CentralServer.timeScale));
}
// Checking once per 3 minutes seems good to me.
Thread.Sleep(Convert.ToInt32(180000 * CentralServer.timeScale));
}
}
void GenerateFlightsInSegment()
{
int flightsInSegment = CentralServer.rnd.Next((int)Math.Round(5 * CentralServer.busynessModifier), (int)Math.Round(10 * CentralServer.busynessModifier));
Flight newFlight;
DateTime departure;
for (int i = 0; i < flightsInSegment; i++)
{
departure = this.lastGeneratedSegmentEnd.AddMinutes(CentralServer.rnd.Next(0, 60) * CentralServer.timeScale);
newFlight = GenerateFlight(departure);
this.nextFlightID++;
newFlight.PassengerList = GeneratePassengers(newFlight);
AddToFlightPlan(newFlight);
}
SortFlightPlanChronologically();
}
Flight GenerateFlight(DateTime departureTime)
{
return new Flight(this.nextFlightID, departureTime, new Passenger[0]);
}
Passenger[] GeneratePassengers(Flight flight)
{
Passenger[] passengers = new Passenger[CentralServer.rnd.Next((int)Math.Round(50 * CentralServer.busynessModifier), (int)Math.Round(301 * CentralServer.busynessModifier))];
for (int i = 0; i < passengers.Length; i++)
{
passengers[i] = new Passenger(this.nextPassengerID, CentralServer.faker.Name.FullName(), flight);
this.nextPassengerID++;
}
return passengers;
}
public void AddToFlightPlan(Flight flight)
{
Flight[] newPlan = new Flight[this.flightPlan.Length + 1];
for (int i = 0; i < this.flightPlan.Length; i++)
{
newPlan[i] = this.flightPlan[i];
}
newPlan[newPlan.Length - 1] = flight;
this.flightPlan = newPlan;
}
void SortFlightPlanChronologically()
{
for (int i = 0; i < this.flightPlan.Length; i++)
{
for (int j = 0; j < this.flightPlan.Length-1; j++)
{
if(this.flightPlan[j].DepartureTime > this.flightPlan[j+1].DepartureTime)
{
Flight tempFlight = this.flightPlan[j];
this.flightPlan[j] = this.flightPlan[j + 1];
this.flightPlan[j + 1] = tempFlight;
}
}
}
}
void ReadyFlight(Flight flight)
{
bool flightReadied = false;
while (!stop && !flightReadied)
{
if (Monitor.IsEntered(flightsRequiringGateLock))
{
try
{
// Add Flight to the TOp of the FlightRequiringGate array
this.flightsRequiringGate = AddFlightToBack(flight, this.flightsRequiringGate);
}
finally
{
Monitor.Pulse(flightsRequiringGateLock);
Monitor.Exit(flightsRequiringGateLock);
flightReadied = true;
}
}
else
{
Monitor.Enter(flightsRequiringGateLock);
}
}
}
/// <summary>
/// Acquires Lock on passengerQueueLock and then adds all passengers to passengerQueue for the Counters to consume.
/// </summary>
/// <param name="passengers">Passengers to the added to the Queue</param>
void EnqueuePassengers(Passenger[] passengers)
{
bool passengersEnqueued = false;
while (!stop && !passengersEnqueued)
{
if (Monitor.IsEntered(passengerQueueLock))
{
try
{
// Add Passengers to the TOp of the passengersQueue array
for (int i = 0; i < passengers.Length; i++)
{
// IF the counters are unable to keep up, then it could result in some passengers being late forever, potentially.
CentralServer.totalPassengerBaggage += passengers[i].NumberOfBaggage;
passengersQueue = AddPassengerRandomly(passengers[i], passengersQueue);
}
}
finally
{
Monitor.PulseAll(passengerQueueLock);
Monitor.Exit(passengerQueueLock);
passengersEnqueued = true;
PassengerQueueEvent();
}
}
else
{
Monitor.Enter(passengerQueueLock);
}
}
}
private void PassengerQueueEvent()
{
PassengerQueueChanged?.Invoke(this, new PassengerQueueEventArgs());
}
public Passenger AcquirePassenger()
{
Passenger passenger = null;
bool passengerAcquired = false;
object _lock = CentralServer.FM.passengerQueueLock;
while (!passengerAcquired)
{
if (CentralServer.FM.passengersQueue.Length > 0 && Monitor.IsEntered(_lock))
{
try
{
passenger = CentralServer.FM.passengersQueue[0];
CentralServer.FM.passengersQueue = FlightManager.CutFrontPassenger(CentralServer.FM.passengersQueue);
}
finally
{
Monitor.PulseAll(_lock);
Monitor.Exit(_lock);
passengerAcquired = true;
PassengerQueueEvent();
}
}
else
{
if (Monitor.IsEntered(_lock))
{
// It has the lock, but there is nothing to consume, so we shall relinguish _lock and wait for pulse.
Monitor.Wait(_lock);
}
else
{
Monitor.Enter(_lock);
}
}
}
return passenger;
}
#region Array Handling
/// <summary>
/// Increases the length of the Flight Array by 1, and puts a flight there.
/// </summary>
/// <param name="flight"></param>
/// <param name="array"></param>
/// <returns></returns>
public static Flight[] AddFlightToBack(Flight flight, Flight[] array)
{
Flight[] arrayNew = new Flight[array.Length + 1];
arrayNew[arrayNew.Length - 1] = flight;
for (int i = 0; i < array.Length; i++)
{
arrayNew[i] = array[i];
}
return arrayNew;
}
/// <summary>
/// Cuts out the bottom passenger by not including them in the new, one element shorter array.
/// </summary>
/// <param name="array"></param>
/// <returns>A flight[] that is one shorter (missing element [0])</returns>
public static Flight[] CutFrontFlight(Flight[] array, int offset)
{
Flight[] newArray = new Flight[array.Length - 1];
for (int i = 1 + offset; i < array.Length; i++)
{
newArray[i - 1] = array[i];
}
return newArray;
}
/// <summary>
/// Increases the length of the Passenger Array by 1, and puts a passenger there.
/// </summary>
/// <param name="passenger"></param>
/// <param name="array"></param>
/// <returns></returns>
public static Passenger[] AddPassengerToBack(Passenger passenger, Passenger[] array)
{
Passenger[] arrayNew = new Passenger[array.Length + 1];
arrayNew[arrayNew.Length - 1] = passenger;
for (int i = 0; i < array.Length; i++)
{
arrayNew[i] = array[i];
}
return arrayNew;
}
/// <summary>
/// Will Randomly assign a Passenger to the Queue.
/// </summary>
/// <param name="passenger"></param>
/// <param name="array"></param>
/// <returns></returns>
public static Passenger[] AddPassengerRandomly(Passenger passenger, Passenger[] array)
{
Passenger[] arrayNew = new Passenger[array.Length + 1];
int randomPos = CentralServer.rnd.Next(0, arrayNew.Length);
arrayNew[randomPos] = passenger;
bool passedNewPassenger = false;
for (int i = 0; i < array.Length; i++)
{
if(passedNewPassenger)
{
arrayNew[i + 1] = array[i];
}
else
{
if (arrayNew[i] == null)
{
arrayNew[i] = array[i];
}
else
{
arrayNew[i + 1] = array[i];
passedNewPassenger = true;
}
}
}
return arrayNew;
}
/// <summary>
/// Cuts out the bottom passenger by not including them in the new, one element shorter array.
/// </summary>
/// <param name="array"></param>
/// <returns>A passenger[] that is one shorter (missing element [0])</returns>
public static Passenger[] CutFrontPassenger(Passenger[] array)
{
Passenger[] newArray = new Passenger[array.Length - 1];
for (int i = 1; i < array.Length; i++)
{
newArray[i - 1] = array[i];
}
return newArray;
}
#endregion
}
}