forked from steger/pr3-ws202627
205 lines
5.4 KiB
Go
205 lines
5.4 KiB
Go
package airport
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
"time"
|
|
)
|
|
|
|
var distantFuture = time.Unix(1<<63-1, 0) // Max value for Unix timestamp
|
|
|
|
type GateNumber int
|
|
|
|
type Gate struct {
|
|
id GateNumber
|
|
walkingTimeFromSecurity time.Duration
|
|
taxiTimeToRunway time.Duration
|
|
//TODO: extend
|
|
flights map[FlightNumber]*flightState
|
|
queue chan func() // everything that changes the flights is executed by Start
|
|
}
|
|
|
|
// everything the gate knows about one flight. Only the goroutine of Start touches it
|
|
type flightState struct {
|
|
flight Flight
|
|
aircraft *Aircraft
|
|
waiting []waitingPassenger // at the gate, but cannot board yet
|
|
loads []waitingBaggage // cannot be loaded yet
|
|
open bool // boarding has started
|
|
closed bool // the departure time is over
|
|
left bool // the aircraft has left the gate
|
|
}
|
|
|
|
type waitingPassenger struct {
|
|
passenger Passenger
|
|
reply chan error
|
|
}
|
|
|
|
type waitingBaggage struct {
|
|
b Baggage
|
|
reply chan error
|
|
}
|
|
|
|
var _ BaggageProcessor = &Gate{}
|
|
|
|
func NewGate(id GateNumber, walkingTimeFromSecurity time.Duration, taxiTimeToRunway time.Duration) Gate {
|
|
|
|
return Gate{id,
|
|
walkingTimeFromSecurity,
|
|
taxiTimeToRunway,
|
|
make(map[FlightNumber]*flightState),
|
|
make(chan func()),
|
|
}
|
|
}
|
|
|
|
func NewGates(count int) map[GateNumber]*Gate {
|
|
gates := make(map[GateNumber]*Gate)
|
|
for i := range count {
|
|
walkingTime := time.Duration(5+rand.Intn(6)) * time.Second
|
|
taxiTime := time.Duration(5+rand.Intn(6)) * time.Second
|
|
|
|
gate := NewGate(GateNumber(i+1), walkingTime, taxiTime)
|
|
gates[GateNumber(i+1)] = &gate
|
|
}
|
|
return gates
|
|
}
|
|
|
|
func (g *Gate) SetFlights(flights []Flight) {
|
|
//TODO: implement
|
|
// must be called before Start
|
|
g.flights = make(map[FlightNumber]*flightState, len(flights))
|
|
for _, f := range flights {
|
|
g.flights[f.Id] = &flightState{flight: f}
|
|
}
|
|
}
|
|
|
|
// boards the waiting passengers as soon as boarding is open and the aircraft is there
|
|
// (or turns them away if it is too late for that)
|
|
func (fs *flightState) board() {
|
|
if fs.aircraft != nil && fs.open {
|
|
for _, w := range fs.waiting {
|
|
w.reply <- fs.aircraft.Process(w.passenger)
|
|
}
|
|
fs.waiting = nil
|
|
} else if fs.closed {
|
|
for _, w := range fs.waiting {
|
|
w.reply <- fmt.Errorf("%s: boarding for flight %v is closed", w.passenger.Name, fs.flight.Id)
|
|
}
|
|
fs.waiting = nil
|
|
}
|
|
}
|
|
|
|
// loads the waiting baggage as soon as the aircraft is there (or sends it back if it is too late for that)
|
|
func (fs *flightState) load() {
|
|
if fs.aircraft != nil {
|
|
for _, l := range fs.loads {
|
|
l.reply <- fs.aircraft.ProcessBaggage(fs.flight.Id, l.b)
|
|
}
|
|
fs.loads = nil
|
|
} else if fs.closed {
|
|
for _, l := range fs.loads {
|
|
l.reply <- fmt.Errorf("aircraft of flight %v is already gone", fs.flight.Id)
|
|
}
|
|
fs.loads = nil
|
|
}
|
|
}
|
|
|
|
// Blocks for the walking distance to the gate + the time until the passenger has boarded
|
|
func (g *Gate) Process(passenger Passenger) error {
|
|
//TODO: implement
|
|
if passenger.BoardingPass == nil {
|
|
return fmt.Errorf("%s has no boarding pass", passenger.Name)
|
|
}
|
|
|
|
// Whether the passenger is in time is calculated and not read from the clock after waking up,
|
|
// so that the jitter of the scheduler does not make a punctual passenger late.
|
|
// The small grace period covers the same jitter.
|
|
const grace = 500 * time.Microsecond
|
|
arrival := time.Now().Add(g.walkingTimeFromSecurity)
|
|
time.Sleep(g.walkingTimeFromSecurity)
|
|
|
|
reply := make(chan error, 1)
|
|
g.queue <- func() {
|
|
fs, ok := g.flights[passenger.BoardingPass.FlightNumber]
|
|
if !ok {
|
|
reply <- fmt.Errorf("%s: flight %v does not depart from gate %v", passenger.Name, passenger.BoardingPass.FlightNumber, g.id)
|
|
return
|
|
}
|
|
if fs.left || arrival.After(fs.flight.DepartureTime.Add(grace)) {
|
|
reply <- fmt.Errorf("%s arrived too late at the gate for flight %v", passenger.Name, fs.flight.Id)
|
|
return
|
|
}
|
|
fs.waiting = append(fs.waiting, waitingPassenger{passenger, reply})
|
|
fs.board()
|
|
}
|
|
return <-reply
|
|
}
|
|
|
|
// Blocks until departure (includes taxi time to runway)
|
|
func (g *Gate) ProcessAircraft(ac *Aircraft) {
|
|
//TODO: implement
|
|
done := make(chan struct{})
|
|
g.queue <- func() {
|
|
fs, ok := g.flights[ac.Flight.Id]
|
|
if !ok || fs.aircraft != nil {
|
|
fmt.Printf("\033[31maircraft of flight %v cannot use gate %v\033[0m\n", ac.Flight.Id, g.id)
|
|
close(done)
|
|
return
|
|
}
|
|
fs.aircraft = ac
|
|
fs.load()
|
|
fs.board()
|
|
|
|
time.AfterFunc(time.Until(fs.flight.DepartureTime.Add(g.taxiTimeToRunway)), func() {
|
|
g.queue <- func() {
|
|
fs.left = true
|
|
close(done)
|
|
}
|
|
})
|
|
}
|
|
<-done
|
|
}
|
|
|
|
func (g *Gate) ProcessBaggage(fn FlightNumber, b Baggage) error {
|
|
//TODO: implement
|
|
reply := make(chan error, 1)
|
|
g.queue <- func() {
|
|
fs, ok := g.flights[fn]
|
|
if !ok {
|
|
reply <- fmt.Errorf("flight %v does not depart from gate %v", fn, g.id)
|
|
return
|
|
}
|
|
if fs.closed {
|
|
reply <- fmt.Errorf("aircraft of flight %v is already gone", fn)
|
|
return
|
|
}
|
|
fs.loads = append(fs.loads, waitingBaggage{b, reply})
|
|
fs.load()
|
|
}
|
|
return <-reply
|
|
}
|
|
|
|
func (g Gate) Start() {
|
|
//TODO: implement
|
|
// the points in time of every flight are sent to the queue like any other request
|
|
for _, fs := range g.flights {
|
|
time.AfterFunc(time.Until(fs.flight.BoardingTime), func() {
|
|
g.queue <- func() {
|
|
fs.open = true
|
|
fs.board()
|
|
}
|
|
})
|
|
time.AfterFunc(time.Until(fs.flight.DepartureTime), func() {
|
|
g.queue <- func() {
|
|
fs.closed = true
|
|
fs.board()
|
|
fs.load()
|
|
}
|
|
})
|
|
}
|
|
|
|
for request := range g.queue {
|
|
request()
|
|
}
|
|
}
|