A3: Documentation
This commit is contained in:
parent
c261e9c5c8
commit
089f6370de
|
@ -14,11 +14,6 @@ const (
|
|||
west
|
||||
)
|
||||
|
||||
//definiert die nächste Richtung in der Reihenfolge north -> east -> south -> west -> north ...
|
||||
func nextDirection(direction CardinalDirection) CardinalDirection {
|
||||
return (direction + 1) % (west + 1)
|
||||
}
|
||||
|
||||
func (direction CardinalDirection) String() string {
|
||||
return [...]string{"North", "East", "South", "West"}[direction]
|
||||
}
|
||||
|
@ -56,6 +51,7 @@ func getAxisChannel(direction CardinalDirection) chan string {
|
|||
return eastWestChannel
|
||||
}
|
||||
|
||||
//Channel für Kommunikation zur rechts folgendem TrafficLight erhalten.
|
||||
func getNextChannel(direction CardinalDirection) chan string {
|
||||
if direction == north || direction == east {
|
||||
return northEastChannel
|
||||
|
@ -66,6 +62,7 @@ func getNextChannel(direction CardinalDirection) chan string {
|
|||
func main() {
|
||||
var quitChannel = make(chan string)
|
||||
|
||||
//starten der Routinen
|
||||
go TrafficLight(north)
|
||||
go TrafficLight(south)
|
||||
go TrafficLight(east)
|
||||
|
@ -77,24 +74,28 @@ func main() {
|
|||
func TrafficLight(direction CardinalDirection) {
|
||||
var colour = red
|
||||
|
||||
//warten bis handover ausgelöst wurde wenn Osten/Westen
|
||||
if direction == east || direction == west {
|
||||
select {
|
||||
case <-getNextChannel(direction):
|
||||
}
|
||||
}
|
||||
|
||||
//Dauerschleife
|
||||
for {
|
||||
show(direction, colour)
|
||||
show(direction, colour) //Zustand zeigen
|
||||
|
||||
if colour == red {
|
||||
sync("wait", direction)
|
||||
handover("changeDirection", direction)
|
||||
if colour == red { //wenn zu rot gewechselt wurde
|
||||
sync("wait", direction) //synchronisieren
|
||||
handover("changeDirection", direction) //Nächstfolgende Ampel auslösen, warten bis diese fertig durchgelaufen ist.
|
||||
}
|
||||
sync("changeLight"+nextColour(colour).String(), direction)
|
||||
colour = nextColour(colour)
|
||||
sync("changeLight", direction) //synchronisieren
|
||||
colour = nextColour(colour) //Ampel umschalten
|
||||
}
|
||||
}
|
||||
|
||||
//der Routine für die rechts folgende Richtung eine Nachricht senden.
|
||||
//erst wenn eine Nachricht der linken Routine empfangen wird, wird fortgefahren.
|
||||
func handover(message string, direction CardinalDirection) {
|
||||
getNextChannel(direction) <- message
|
||||
|
||||
|
@ -104,6 +105,7 @@ func handover(message string, direction CardinalDirection) {
|
|||
|
||||
}
|
||||
|
||||
//mit der Routine für die gegenüberliegende Ampel synchronisieren.
|
||||
func sync(message string, direction CardinalDirection) {
|
||||
|
||||
select {
|
||||
|
@ -113,6 +115,7 @@ func sync(message string, direction CardinalDirection) {
|
|||
|
||||
}
|
||||
|
||||
//zeigen des aktuellen Zustands.
|
||||
func show(direction CardinalDirection, colour Colour) {
|
||||
fmt.Println(direction.String(), " : ", colour.String())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue