def PingRespond(TargetID, TimeStamp): # check to see if the target is valid # a broadcast ping is not allowed if TargetID <= 0 or TargetID > 255: return -1 # get required data classes PingPacket = CustMes.MESSAGE_FRAME() PingData = CustMes.MESSAGE_PING() # set up message frame PingPacket.MessageID = 1 PingPacket.TargetID = TargetID PingPacket.SystemID = GlobalVals.SYSTEM_ID # set up payload PingData.Intiator = False PingData.TimeDiff = time.time() - TimeStamp PingPacket.Payload = PingData.data_to_bytes() # send the ping sendPacket(PingPacket) return 0
def PingTarget(TargetID): # check to see if the target is valid # a broadcast ping is not allowed if TargetID <= 0 or TargetID > 255: return -1 # get required data classes PingPacket = CustMes.MESSAGE_FRAME() PingData = CustMes.MESSAGE_PING() # set up message frame PingPacket.MessageID = 1 PingPacket.TargetID = TargetID PingPacket.SystemID = GlobalVals.SYSTEM_ID # set up payload PingData.Intiator = True PingPacket.Payload = PingData.data_to_bytes() # send the ping TimeSent = time.time() sendPacket(PingPacket) # intialise some variables recievedPacket = CustMes.MESSAGE_FRAME() loopIndex = 0 # wait for respnse from ping while True: time.sleep(GlobalVals.PING_WAIT_TIME) # if a packet hasn't been recieved yet if not GlobalVals.RECIEVED_PING: # check for time out and increment the loop counter if loopIndex >= GlobalVals.PING_LOOP_LIMIT: print("Network Manager: Ping Timeout Error.") return -2 else: loopIndex = loopIndex + 1 continue # if a packet has been recieved else: # reset the flag with GlobalVals.RECIEVED_PING_MUTEX: GlobalVals.RECIEVED_PING = False # get the packet with GlobalVals.PACKET_PING_BUFFER_MUTEX: found = False x = 0 while x < len(GlobalVals.PACKET_PING_BUFFER): if GlobalVals.PACKET_PING_BUFFER[x].SystemID == TargetID: # remove old ping requests if GlobalVals.PACKET_PING_BUFFER[ x].Timestamp < TimeSent: GlobalVals.PACKET_PING_BUFFER.pop(x) continue # keep only the newest packet else: recievedPacket = GlobalVals.PACKET_PING_BUFFER.pop( x) found = True x = x + 1 # if a ping packet hasn't been recieved loop back if not found: loopIndex = loopIndex + 1 continue break # get time stamp from packet and calculate the ping TimeRec = recievedPacket.Timestamp ping = int((TimeRec - TimeSent) * 1000) # return the ping return ping