forked from steger/pr3-ws202627
gate
parent
d1d2753280
commit
026466b01e
|
|
@ -1,6 +1,7 @@
|
||||||
package airport
|
package airport
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
@ -14,6 +15,29 @@ type Gate struct {
|
||||||
walkingTimeFromSecurity time.Duration
|
walkingTimeFromSecurity time.Duration
|
||||||
taxiTimeToRunway time.Duration
|
taxiTimeToRunway time.Duration
|
||||||
//TODO: extend
|
//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{}
|
var _ BaggageProcessor = &Gate{}
|
||||||
|
|
@ -23,6 +47,8 @@ func NewGate(id GateNumber, walkingTimeFromSecurity time.Duration, taxiTimeToRun
|
||||||
return Gate{id,
|
return Gate{id,
|
||||||
walkingTimeFromSecurity,
|
walkingTimeFromSecurity,
|
||||||
taxiTimeToRunway,
|
taxiTimeToRunway,
|
||||||
|
make(map[FlightNumber]*flightState),
|
||||||
|
make(chan func()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -40,24 +66,139 @@ func NewGates(count int) map[GateNumber]*Gate {
|
||||||
|
|
||||||
func (g *Gate) SetFlights(flights []Flight) {
|
func (g *Gate) SetFlights(flights []Flight) {
|
||||||
//TODO: implement
|
//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
|
// Blocks for the walking distance to the gate + the time until the passenger has boarded
|
||||||
func (g *Gate) Process(passenger Passenger) error {
|
func (g *Gate) Process(passenger Passenger) error {
|
||||||
//TODO: implement
|
//TODO: implement
|
||||||
return nil
|
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)
|
// Blocks until departure (includes taxi time to runway)
|
||||||
func (g *Gate) ProcessAircraft(ac *Aircraft) {
|
func (g *Gate) ProcessAircraft(ac *Aircraft) {
|
||||||
//TODO: implement
|
//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 {
|
func (g *Gate) ProcessBaggage(fn FlightNumber, b Baggage) error {
|
||||||
//TODO: implement
|
//TODO: implement
|
||||||
return nil
|
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() {
|
func (g Gate) Start() {
|
||||||
//TODO: implement
|
//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()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue