forked from steger/pr3-ws202627
78 lines
1.6 KiB
Go
78 lines
1.6 KiB
Go
package airport
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
type BaggageProcessor interface {
|
|
ProcessBaggage(FlightNumber, Baggage) error
|
|
}
|
|
|
|
type BaggageHandlingSystem struct {
|
|
processingTime time.Duration
|
|
sinks map[FlightNumber]BaggageProcessor
|
|
//TODO: extend
|
|
queue chan baggageData
|
|
list []LostBaggage
|
|
mu sync.Mutex
|
|
}
|
|
|
|
type baggageData struct {
|
|
fn FlightNumber
|
|
b Baggage
|
|
}
|
|
|
|
type LostBaggage struct {
|
|
Baggage
|
|
FlightNumber FlightNumber
|
|
Err error
|
|
}
|
|
|
|
var _ BaggageProcessor = &BaggageHandlingSystem{}
|
|
|
|
func NewBaggageHandlingSystem(processingTime time.Duration, sinks map[FlightNumber]BaggageProcessor) BaggageHandlingSystem {
|
|
return BaggageHandlingSystem{
|
|
processingTime: processingTime,
|
|
sinks: sinks,
|
|
queue: make(chan baggageData),
|
|
list: make([]LostBaggage, 0),
|
|
}
|
|
}
|
|
|
|
func (bhs *BaggageHandlingSystem) ProcessBaggage(fn FlightNumber, b Baggage) error {
|
|
//TODO: implement
|
|
_, ok := bhs.sinks[fn]
|
|
if !ok {
|
|
return fmt.Errorf("Keine Flugnummer passend gefunden")
|
|
}
|
|
bhs.queue <- baggageData{fn: fn, b: b}
|
|
return nil
|
|
}
|
|
|
|
func (bhs *BaggageHandlingSystem) CollectLostBaggage() []LostBaggage {
|
|
//TODO: implement
|
|
bhs.mu.Lock()
|
|
verloren := bhs.list
|
|
bhs.list = make([]LostBaggage, 0)
|
|
bhs.mu.Unlock()
|
|
return verloren
|
|
|
|
}
|
|
|
|
func (bhs *BaggageHandlingSystem) Start() {
|
|
//TODO: implement
|
|
for element := range bhs.queue {
|
|
|
|
time.Sleep(bhs.processingTime)
|
|
sink := bhs.sinks[element.fn]
|
|
err := sink.ProcessBaggage(element.fn, element.b)
|
|
if err != nil {
|
|
bhs.mu.Lock()
|
|
bhs.list = append(bhs.list, LostBaggage{element.b, element.fn, err})
|
|
bhs.mu.Unlock()
|
|
}
|
|
}
|
|
}
|