コード例 #1
0
def connection_validator(): #checks if we lost a connection woth one of the elevators
  lost_ids = []
  for ID in shared.elevators:
    if (ID == shared.get_local_elevator_ID()):  #checks the local elevator
      continue  #can't loose the local elevator
    elevator_state = shared.elevators[ID]
    
    if ((time.time() - elevator_state.last_ping) > 4.0): #if we don't hear from a elevator for over 4.0 seconds we have lost it, by shut down or bad internet connection or no internet connection at all
      print "Lost elevator with id:", ID, "which is ", (time.time() - elevator_state.last_ping), " seconds old"
      lost_ids.append(ID) #puts all the lost elevators ips in one list
  for ID in lost_ids:
    del shared.elevators[ID]  #deletes the lost elevators
    
  for ID in lost_ids: #checks through the lost elevators if we have to reassign some of the orders the lost elevator had
    for key in orderlist.get_order_map():
        order = orderlist.get_order_map()[key]
        if (order.completed): #if the order is completed we shall ont reassign that order
          continue
        if not (order.assigned):  #the order is not given to anyone
          continue
        if (order.direction == shared.NODIR): #checks if the we have a local command, "BUTTON_COMMANDS"
          continue  #Can't reassign command orders
        if (order.assigned_to_id == ID):  #if the assigned ID and the elevator ID is the same 
          orderlist.assign_order(order) #we reassign the orders
コード例 #2
0
def sending():  #this function sends everything we want to broadcast
  last_time = time.time()
  while True:
    delta_time = time.time() - last_time
    last_time = time.time()
    if (delta_time > 1.5):  #checks if the sending is being stalled
      print "Warning, sending thread has stalled!"
    
    #broadcast the functions with 0.3 seconds between them
    send_orderlist(orderlist.get_order_map())
    time.sleep(0.3)
    
    send_ping(elevator.get_elevators())
    time.sleep(0.3)
    
    connection_validator()
    time.sleep(0.3)
    
  return