Skip to content

Commit

Permalink
heis defeated
Browse files Browse the repository at this point in the history
  • Loading branch information
haavardsyslak committed Mar 9, 2024
1 parent 996f497 commit 14dd0bb
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 85 deletions.
5 changes: 5 additions & 0 deletions localelevator/elevator/printing.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import (
"fmt"
"strings"
"Driver-go/elevio"
"os"
"os/exec"
)

func PrintElevator(e Elevator) {
cmd := exec.Command("clear")
cmd.Stdout = os.Stdout
cmd.Run()
fmt.Printf("Current floor: %d\n", e.CurrentFloor)
fmt.Printf("ID: %s\n", e.Id)
printState(e)
Expand Down
42 changes: 11 additions & 31 deletions localelevator/elevatorcontroller/elevatorcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ package elevatorcontroller

import (
"Driver-go/elevio"
// "fmt"
"sanntid/localelevator/elevator"
"sanntid/localelevator/requests"
"os"
"os/exec"
"time"
)

Expand All @@ -20,13 +17,13 @@ func ListenAndServe(
e elevator.Elevator,
requestUpdateCh chan elevator.Requests,
elevatorStuckCh chan bool,
stopedAtFloor chan int,
orderChan chan elevator.Order,
stoppedAtFloor chan int,
orderCh chan elevator.Order,
elevatorUpdateCh chan elevator.Elevator,
printEnabled bool,
) {
buttonCh := make(chan elevio.ButtonEvent)
floorSensCh := make(chan int)
floorSensorCh := make(chan int)
stopButtonCh := make(chan bool)
onDoorsClosingCh := make(chan bool)
obstructionCh := make(chan bool)
Expand All @@ -37,7 +34,7 @@ func ListenAndServe(
floorTimer := time.NewTicker(floorTimeout)

watchdogTicker := time.NewTicker(time.Millisecond * 250)
elevator.PollElevatorIO(buttonCh, floorSensCh, stopButtonCh, obstructionCh)
elevator.PollElevatorIO(buttonCh, floorSensorCh, stopButtonCh, obstructionCh)

for {
select {
Expand All @@ -52,30 +49,27 @@ func ListenAndServe(
}

case event := <-buttonCh:
orderChan <- elevator.Order{
orderCh <- elevator.Order{
Type: event.Button,
AtFloor: event.Floor,
}

case event := <-floorSensCh:
floorTimer.Reset(doorTimeout)
case event := <-floorSensorCh:
floorTimer.Reset(floorTimeout)
elevatorStuckCh <- false
handleFloorArrival(event, &e, onDoorsClosingCh, obstructionCh)
elevatorUpdateCh <- e

case <-onDoorsClosingCh:
elevatorStuckCh <- false
stopedAtFloor <- e.CurrentFloor
stoppedAtFloor <- e.CurrentFloor
e.Requests = <-requestUpdateCh
elevator.SetCabLights(e)
handleDoorsClosing(&e, onDoorsClosingCh, obstructionCh)
elevatorUpdateCh <- e

case <- watchdogTicker.C:
if printEnabled {
cmd := exec.Command("clear")
cmd.Stdout = os.Stdout
cmd.Run()
elevator.PrintElevator(e)
}
if e.State != elevator.MOVING {
Expand All @@ -93,19 +87,6 @@ func ListenAndServe(
}
}

func hasNewRequest(e elevator.Elevator, new elevator.Requests) bool {
newRequest := false
for f := e.MinFloor; f <= e.MaxFloor; f++ {
if e.Requests.Up[f] != new.Up[f] ||
e.Requests.Down[f] != new.Down[f] ||
e.Requests.ToFloor[f] != new.ToFloor[f] {
newRequest = true
break
}
}
return newRequest
}

func handleRequestUpdate(e *elevator.Elevator,
onDoorClosingCh chan bool,
obstructionCh chan bool) {
Expand All @@ -131,30 +112,29 @@ func handleDoorsClosing(e *elevator.Elevator,
}
}

// TODO: return value is not used
func handleFloorArrival(floor int,
e *elevator.Elevator,
onDoorsClosingCh chan bool,
obstructionCh chan bool) bool {
obstructionCh chan bool) {
e.CurrentFloor = floor
elevio.SetFloorIndicator(floor)
if e.State == elevator.IDLE || e.State == elevator.DOOR_OPEN {
elevator.Stop()
return false
return
}
if requests.ShouldStop(*e) {
elevator.Stop()
e.State = elevator.DOOR_OPEN
go elevator.OpenDoors(onDoorsClosingCh, obstructionCh)
}
return false
}


func setInitialState(e elevator.Elevator, onDoorClosingCh chan bool, obstructionCh chan bool) {
elevator.PrintElevator(e)
elevio.SetMotorDirection(e.Dir)
elevator.SetCabLights(e)
elevio.SetDoorOpenLamp(false)
if e.State == elevator.DOOR_OPEN {
go elevator.OpenDoors(onDoorClosingCh, obstructionCh)
}
Expand Down
47 changes: 15 additions & 32 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"os"
"sanntid/localelevator/elevator"
"sanntid/packethandler"
"sanntid/recovery"
"sanntid/request_assigner"
"time"
)

func main() {
Expand All @@ -30,22 +30,26 @@ func main() {
elevatorToNetworkCh := make(chan elevator.Elevator, 1000)
elevatorFromNetworkCh := make(chan elevator.Elevator, 1000)

go peers.Transmitter(15647, id, peerTxEnable)
go peers.Receiver(15647, peerUpdateCh)

go bcast.Transmitter(16569, elevatorTxCh)
go bcast.Receiver(16569, elevatorRxCh)

go packethandler.TransmitRecieve(id,
go packethandler.HandleElevatorPackets(id,
elevatorToNetworkCh,
elevatorFromNetworkCh,
elevatorTxCh,
elevatorRxCh,
connectedPeersCh,
)

go peers.Transmitter(15647, id, peerTxEnable)
go peers.Receiver(15647, peerUpdateCh)

go bcast.Transmitter(16569, elevatorTxCh)
go bcast.Receiver(16569, elevatorRxCh)

go peers.PeerUpdateListener(peerUpdateCh, connectedPeersCh, lostPeersCh)



e := elevator.New(id)
networkElevator, hasRecovered := recoverFromNetwork(id, elevatorFromNetworkCh)
networkElevator, hasRecovered := recovery.AttemptNetworkRecovery(id, elevatorFromNetworkCh)

if hasRecovered {
elevator.Init(port, true)
Expand All @@ -61,17 +65,10 @@ func main() {
lostPeersCh,
peerTxEnable,
)

for {
select {
case p := <-peerUpdateCh:
fmt.Println(p.Peers)
connectedPeersCh <- p.Peers
lostPeersCh <- p.Lost
}
}
for {}
}


func parseCliArgs(id *string, port *int) {

flag.StringVar(id, "id", "", "Peer ID")
Expand All @@ -90,17 +87,3 @@ func parseCliArgs(id *string, port *int) {
}
}

func recoverFromNetwork(id string,
elevatorFromNetworkCh chan elevator.Elevator) (elevator.Elevator, bool) {
timeout := time.NewTicker(500 * time.Millisecond)
for {
select {
case <-timeout.C:
return elevator.Elevator{}, false
case e := <-elevatorFromNetworkCh:
if e.Id == id {
return e, true
}
}
}
}
25 changes: 7 additions & 18 deletions packethandler/packethandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ type ElevatorPacket struct {
var sequenceNumbers = make(map[string]uint32)
var sequenceNumbersMutex sync.Mutex

func TransmitRecieve(thisId string,
func HandleElevatorPackets(thisId string,
elevatorUpdateToNetworkCh <-chan elevator.Elevator,
elevatorUpdateFromNetworkCh chan<- elevator.Elevator,
elevatorTxCh chan <- ElevatorPacket,
elevatorRxCh <- chan ElevatorPacket,
connectedPeersCh <- chan []string) {

bcastTimer := time.NewTicker(25 * time.Millisecond)
bcastTimer := time.NewTicker(5 * time.Millisecond)
elevators := make(map[string]elevator.Elevator)
var connectedPeers []string
for {
Expand All @@ -48,12 +48,12 @@ func TransmitRecieve(thisId string,
}

elevators[packet.Elevator.Id] = packet.Elevator

if packet.SenderID == thisId && len(connectedPeers) <= 1 {
elevatorUpdateFromNetworkCh <- elevator
} else if packet.SenderID != thisId && isElevatorAlive(connectedPeers, packet.Elevator.Id) {
elevatorUpdateFromNetworkCh <- elevator
}

case connectedPeers = <- connectedPeersCh:
}
}
Expand Down Expand Up @@ -84,39 +84,28 @@ func handleIncommingPacket(packet ElevatorPacket,
elevators map[string]elevator.Elevator) (elevator.Elevator, error) {
if shouldScrapPacket(&packet, elevators) {
return elevator.Elevator{}, errors.New("Packet scraped was scraped")
}
} else {
updateSequenceNumber(packet.Elevator.Id, packet.SequenceNumber)
}
return packet.Elevator, nil
}

func shouldScrapPacket(packet *ElevatorPacket, elevators map[string]elevator.Elevator) bool {
sequenceNumbersMutex.Lock()
currentSequenceNumber := sequenceNumbers[packet.Elevator.Id]
sequenceNumbersMutex.Unlock()

if !verifyChecksum(*packet) {
fmt.Println("Cheksum failed")
return true
}
if packet.SequenceNumber < currentSequenceNumber {
return true

// } else if packet.SequenceNumber == currentSequenceNumber {
// mergeRequests(packet, elevators[packet.Elevator.Id])
// return false
} else {
updateSequenceNumber(packet.Elevator.Id, packet.SequenceNumber)
return false
}
}

func mergeRequests(packet *ElevatorPacket, localElevator elevator.Elevator) elevator.Elevator {
for f := localElevator.MinFloor; f <= localElevator.MaxFloor; f++ {
packet.Elevator.Requests.Up[f] = localElevator.Requests.Up[f] || packet.Elevator.Requests.Up[f]
packet.Elevator.Requests.Down[f] = localElevator.Requests.Down[f] || packet.Elevator.Requests.Down[f]
packet.Elevator.Requests.ToFloor[f] = localElevator.Requests.ToFloor[f] || packet.Elevator.Requests.ToFloor[f]
}
return packet.Elevator
}

func makeElevatorPacket(e elevator.Elevator, id string) ElevatorPacket {
sequenceNumbersMutex.Lock()
defer sequenceNumbersMutex.Unlock()
Expand Down
Binary file added packetloss
Binary file not shown.
22 changes: 22 additions & 0 deletions recovery/recover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package recovery

import(
"sanntid/localelevator/elevator"
"time"
)


func AttemptNetworkRecovery(id string,
elevatorFromNetworkCh chan elevator.Elevator) (elevator.Elevator, bool) {
timeout := time.NewTicker(500 * time.Millisecond)
for {
select {
case <-timeout.C:
return elevator.Elevator{}, false
case e := <-elevatorFromNetworkCh:
if e.Id == id {
return e, true
}
}
}
}
6 changes: 2 additions & 4 deletions request_assigner/request_assigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const (
)

var thisId string
//
func DistributeRequests(thisElevator elevator.Elevator,
elevatorToNetwork chan <- elevator.Elevator,
elevatorFromNetwork <- chan elevator.Elevator,
Expand All @@ -32,8 +31,6 @@ func DistributeRequests(thisElevator elevator.Elevator,
elevators[thisId] = thisElevator
elevatorUpdateCh := make(chan elevator.Elevator)

// var isStuck = false

elevatorToNetwork <- thisElevator

go elevatorcontroller.ListenAndServe(elevator.CopyElevator(thisElevator),
Expand Down Expand Up @@ -69,6 +66,7 @@ func DistributeRequests(thisElevator elevator.Elevator,

case isStuck := <-elevatorStuckCh:
if isStuck {
// Reassign my own requests when stuck
lostElevator := elevators[thisId]
delete(elevators, lostElevator.Id)
if len(elevators) >= 1 {
Expand All @@ -86,7 +84,6 @@ func DistributeRequests(thisElevator elevator.Elevator,

case e := <-elevatorFromNetwork:
if e.Id == thisId {
elevators[e.Id] = e
requestUpdateCh <- e.Requests
}
elevators[e.Id] = e
Expand All @@ -97,6 +94,7 @@ func DistributeRequests(thisElevator elevator.Elevator,
}

case lostElevators := <- lostElevatorsCh:
// Reassign the lost elevators requests
for _, lostId := range lostElevators {
if lostId == thisId {
continue
Expand Down

0 comments on commit 14dd0bb

Please sign in to comment.