Exemple #1
0
func handleBtnPress(btnPress def.BtnPress, outgoingMsg chan<- def.Message) {
	if btnPress.Button == def.BtnCab {
		q.AddRequest(btnPress.Floor, btnPress.Button, def.LocalIP)
	} else {
		outgoingMsg <- def.Message{Category: def.NewRequest, Floor: btnPress.Floor, Button: btnPress.Button, Cost: 0}
	}
}
// chooseBestElevator goes through a map of requests and finds the best elevator in each replyList
func chooseBestElevator(requestMap map[request][]reply, numOnline int, isTimeout bool) {
	var bestElevator string

	for request, replyList := range requestMap {
		if len(replyList) == numOnline || isTimeout {
			lowestCost := 10000
			for _, reply := range replyList {
				if reply.cost < lowestCost {
					lowestCost = reply.cost
					bestElevator = reply.elevator
				} else if reply.cost == lowestCost {
					// On equal cost, the elevator with lowest IP get the request
					if reply.elevator < bestElevator {
						bestElevator = reply.elevator
					}
				}
			}
			queue.AddRequest(request.floor, request.button, bestElevator)
			request.timer.Stop()
			delete(requestMap, request)
		}
	}
}