예제 #1
0
func eventCabAtFloor(ch chan<- int) {
	//initialize with invalid values
	var FloorReached = -2
	var prevFloor = -3
	for {
		if hw.GetFloor() != -1 {
			FloorReached = hw.GetFloor()
			if prevFloor != FloorReached {
				ch <- FloorReached
				prevFloor = FloorReached
			}
		}
		time.Sleep(100 * time.Millisecond)
	}
}
예제 #2
0
func handleMessage(incomingMsg def.Message, msgCh def.MessageChan) {
	switch incomingMsg.Category {
	case def.Alive:
		addr := incomingMsg.Addr
		if connection, exist := onlineElevatorMap[addr]; exist {
			connection.Timer.Reset(def.ElevTimeoutDuration)
		} else {
			newConnection := def.UdpConnection{Addr: addr, Timer: time.NewTimer(def.ElevTimeoutDuration)}
			onlineElevatorMap[addr] = newConnection
			msgCh.NumOnline <- len(onlineElevatorMap)
			go connectionTimer(&newConnection, msgCh.Outgoing, msgCh.NumOnline)
			log.Println(def.ColG, "New elevator: ", addr, " | Number online: ", len(onlineElevatorMap), def.ColN)
		}
	case def.NewRequest:
		log.Println(def.ColC, "New request incomming", def.ColN)
		cost := q.CalcCost(fsm.Elevator.Dir, hw.GetFloor(), fsm.Elevator.Floor, incomingMsg.Floor, incomingMsg.Button)
		msgCh.Outgoing <- def.Message{Category: def.Cost, Floor: incomingMsg.Floor, Button: incomingMsg.Button, Cost: cost}
	case def.CompleteRequest:
		log.Println(def.ColG, "Request is completed", def.ColN)
		q.RemoveRequest(incomingMsg.Floor, incomingMsg.Button)
	case def.Cost:
		log.Println(def.ColC, "Cost reply", def.ColN)
		msgCh.CostReply <- incomingMsg
	}
}