A3: Documentation

This commit is contained in:
Johannes Theiner 2020-05-30 13:53:02 +02:00
parent c261e9c5c8
commit 089f6370de
1 changed files with 14 additions and 11 deletions

View File

@ -14,11 +14,6 @@ const (
west 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 { func (direction CardinalDirection) String() string {
return [...]string{"North", "East", "South", "West"}[direction] return [...]string{"North", "East", "South", "West"}[direction]
} }
@ -56,6 +51,7 @@ func getAxisChannel(direction CardinalDirection) chan string {
return eastWestChannel return eastWestChannel
} }
//Channel für Kommunikation zur rechts folgendem TrafficLight erhalten.
func getNextChannel(direction CardinalDirection) chan string { func getNextChannel(direction CardinalDirection) chan string {
if direction == north || direction == east { if direction == north || direction == east {
return northEastChannel return northEastChannel
@ -66,6 +62,7 @@ func getNextChannel(direction CardinalDirection) chan string {
func main() { func main() {
var quitChannel = make(chan string) var quitChannel = make(chan string)
//starten der Routinen
go TrafficLight(north) go TrafficLight(north)
go TrafficLight(south) go TrafficLight(south)
go TrafficLight(east) go TrafficLight(east)
@ -77,24 +74,28 @@ func main() {
func TrafficLight(direction CardinalDirection) { func TrafficLight(direction CardinalDirection) {
var colour = red var colour = red
//warten bis handover ausgelöst wurde wenn Osten/Westen
if direction == east || direction == west { if direction == east || direction == west {
select { select {
case <-getNextChannel(direction): case <-getNextChannel(direction):
} }
} }
//Dauerschleife
for { for {
show(direction, colour) show(direction, colour) //Zustand zeigen
if colour == red { if colour == red { //wenn zu rot gewechselt wurde
sync("wait", direction) sync("wait", direction) //synchronisieren
handover("changeDirection", direction) handover("changeDirection", direction) //Nächstfolgende Ampel auslösen, warten bis diese fertig durchgelaufen ist.
} }
sync("changeLight"+nextColour(colour).String(), direction) sync("changeLight", direction) //synchronisieren
colour = nextColour(colour) 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) { func handover(message string, direction CardinalDirection) {
getNextChannel(direction) <- message 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) { func sync(message string, direction CardinalDirection) {
select { select {
@ -113,6 +115,7 @@ func sync(message string, direction CardinalDirection) {
} }
//zeigen des aktuellen Zustands.
func show(direction CardinalDirection, colour Colour) { func show(direction CardinalDirection, colour Colour) {
fmt.Println(direction.String(), " : ", colour.String()) fmt.Println(direction.String(), " : ", colour.String())
} }