// SendOrder sends the parameter order struct to the network func SendOrder(order OrderMessage) { if order.Direction == driver.DirectionNone { log.Warning("Transmitted order cannot have no direction") return } order.SenderID = elevatorID str := orderToStr(order) log.Debug("Sending message: ", str) udpSendCh <- udp.Udp_message{Raddr: "broadcast", Data: str} }
// OrderAcceptedRemotely yay! func OrderAcceptedRemotely(floor driver.Floor, dir driver.Direction) { if floor == 0 { dir = driver.DirectionUp } else if floor == driver.NumFloors-1 { dir = driver.DirectionUp } // Algorithmically excellent searching for o := pendingOrders.Front(); o != nil; o = o.Next() { v := o.Value.(*order) if v.floor == floor && v.dir == dir { v.timer.Reset(timeoutDelay + calculateTimeout(floor, dir)) return } } // Already completed? Maybe a late package or wtf log.Warning("Non-existant job accepted remotely") }
// InitAndHandle initializes network and handles receive func InitAndHandle(receiveCh chan<- OrderMessage, id uint) { udpSendCh = make(chan udp.Udp_message, 8) udpRecvCh = make(chan udp.Udp_message, 8) elevatorID = id udp.Udp_init(LPORT, BPORT, MSGLEN, udpSendCh, udpRecvCh) for { msg := <-udpRecvCh if msg.Length != 8 { // Disregard messages not 8 in length log.Warning("Non-8-byte message received") continue } order := strToOrder(msg.Data) if order.SenderID != elevatorID { // Don't loop log.Info("Received order: ID: ", order.SenderID, ", type: ", order.Type, ", floor: ", order.Floor) receiveCh <- order } } }
// Reset makes sure the elevator is at a safe floor on startup // Blocking, should never be called when listeners are running func Reset() Floor { log.Debug("Resetting floor") currentFloor := getFloor() if currentFloor == -1 { log.Warning("Unknown floor") // Move down until we hit something RunDown() for { currentFloor = getFloor() if currentFloor != -1 { break } } log.Info("At floor ", currentFloor, ", ready for service") setFloorIndicator(currentFloor) Stop() OpenDoor() time.Sleep(time.Second) CloseDoor() } return currentFloor }