Skip to content

Commit

Permalink
Fix:
Browse files Browse the repository at this point in the history
    fix elevator not serving orders that was taken over, when it is
    a single elevator
  • Loading branch information
haavardsyslak committed Mar 11, 2024
1 parent 69e8f52 commit 2fd32e1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 45 deletions.
9 changes: 7 additions & 2 deletions localelevator/elevatorcontroller/elevatorcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package elevatorcontroller

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

Expand Down Expand Up @@ -91,14 +92,18 @@ func handleRequestUpdate(e *elevator.Elevator,
onDoorClosingCh chan bool,
obstructionCh chan bool) {
switch e.State {
case elevator.IDLE:
case elevator.IDLE :
e.Dir, e.State = requests.GetNewDirectionAndState(*e)
if e.State == elevator.DOOR_OPEN {
elevator.Stop()
go elevator.OpenDoors(onDoorClosingCh, obstructionCh)
} else {
elevio.SetMotorDirection(e.Dir)
}
case elevator.DOOR_OPEN:
elevator.Stop()
case elevator.MOVING:
elevio.SetMotorDirection(e.Dir)
}
}

Expand Down
80 changes: 40 additions & 40 deletions packethandler/packethandler.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package packethandler

import (
"crypto/md5"
"encoding/hex"
"errors"
"fmt"
"sanntid/localelevator/elevator"
"sync"
"time"
"crypto/md5"
"encoding/hex"
)

type ElevatorPacket struct {
SenderID string
SenderID string
Elevator elevator.Elevator
Checksum string
SequenceNumber uint32
Expand All @@ -21,18 +21,18 @@ var sequenceNumbers = make(map[string]uint32)
var sequenceNumbersMutex sync.Mutex

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

bcastTimer := time.NewTicker(5 * time.Millisecond)
elevators := make(map[string]elevator.Elevator)
var connectedPeers []string
var connectedPeers []string
for {
select {
case e := <- elevatorUpdateToNetworkCh:
case e := <-elevatorUpdateToNetworkCh:
elevators[e.Id] = e
incrementSequenceNumber(e.Id)

Expand All @@ -44,25 +44,26 @@ func HandleElevatorPackets(thisId string,
case packet := <-elevatorRxCh:
elevator, err := handleIncommingPacket(packet, elevators)
if err != nil {
continue
}

elevators[packet.Elevator.Id] = elevator
if (packet.Elevator.Id == thisId || isElevatorAlive(connectedPeers, packet.Elevator.Id)) && packet.SenderID != thisId {
elevatorUpdateFromNetworkCh <- elevator
}
case connectedPeers = <- connectedPeersCh:
}
}
continue
}

elevators[packet.Elevator.Id] = elevator
if (packet.Elevator.Id == thisId || isElevatorAlive(connectedPeers, packet.Elevator.Id)) &&
(packet.SenderID != thisId || len(connectedPeers) <= 1) {
elevatorUpdateFromNetworkCh <- elevator
}
case connectedPeers = <-connectedPeersCh:
}
}
}

func isElevatorAlive(elevators []string, elevatorId string) bool {
for _, id := range elevators {
if id == elevatorId {
return true
}
}
return false
for _, id := range elevators {
if id == elevatorId {
return true
}
}
return false
}

func incrementSequenceNumber(Id string) {
Expand All @@ -77,13 +78,13 @@ func updateSequenceNumber(Id string, number uint32) {
sequenceNumbers[Id] = number
}

func handleIncommingPacket(packet ElevatorPacket,
elevators map[string]elevator.Elevator) (elevator.Elevator, error) {
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
}

Expand All @@ -92,37 +93,36 @@ func shouldScrapPacket(packet *ElevatorPacket, elevators map[string]elevator.Ele
currentSequenceNumber := sequenceNumbers[packet.Elevator.Id]
sequenceNumbersMutex.Unlock()

if !verifyChecksum(*packet) {
return true
}
if !verifyChecksum(*packet) {
return true
}
if packet.SequenceNumber < currentSequenceNumber {
return true

} else {
} else {
return false
}
}

func makeElevatorPacket(e elevator.Elevator, id string) ElevatorPacket {
sequenceNumbersMutex.Lock()
defer sequenceNumbersMutex.Unlock()
packet := ElevatorPacket{
SenderID: id,
packet := ElevatorPacket{
SenderID: id,
SequenceNumber: sequenceNumbers[e.Id],
Checksum: "",
Elevator: e,
}
packet.Checksum = computeChecksum(packet)
return packet
packet.Checksum = computeChecksum(packet)
return packet
}

func computeChecksum(packet ElevatorPacket) string {
packetBytes := []byte(fmt.Sprintf("%v", packet.Elevator) + fmt.Sprintf("%d", packet.SequenceNumber))
hash := md5.Sum(packetBytes)
return hex.EncodeToString(hash[:])
hash := md5.Sum(packetBytes)
return hex.EncodeToString(hash[:])
}


func verifyChecksum(packet ElevatorPacket) bool {
return computeChecksum(packet) == packet.Checksum
return computeChecksum(packet) == packet.Checksum
}
5 changes: 2 additions & 3 deletions request_assigner/request_assigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package request_assigner

import (
"Driver-go/elevio"
"fmt"
"sanntid/localelevator/elevator"
"sanntid/localelevator/elevatorcontroller"
"sanntid/localelevator/requests"
Expand Down Expand Up @@ -60,7 +59,8 @@ func DistributeRequests(thisElevator elevator.Elevator,
elevatorToNetwork <- e

if len(elevators) == 1 {
requestUpdateCh <- e.Requests

requestUpdateCh <- requests.MergeHallRequests(elevators)
elevator.SetHallLights(e)
}

Expand All @@ -83,7 +83,6 @@ func DistributeRequests(thisElevator elevator.Elevator,
elevatorToNetwork <- e

case e := <-elevatorFromNetwork:
fmt.Println("mau")
if e.Id == thisId {
requestUpdateCh <- e.Requests
}
Expand Down

0 comments on commit 2fd32e1

Please sign in to comment.