示例#1
0
    def handle(self):
            global COORDINATOR_IP
            global LR_IP
            global FOLLOWER_IP
            global SENSOR_IP
            global BROOKELAPTOP_IP
            

            last_message_number = 0
            sent_messages = 0

            inter_count = 0
            prev_count = 0
            prev_time = 0
            inter_time = 0

            goodMsgTotal = 0
            badMsgTotal = 0
            msgTotal = 0

            minuteCount = 0
            minuteTick = 0

            prevSensorData = 0

            message = ''
            cur_thread = threading.current_thread()
            print("we have a connection from {0}".format(self.request.getpeername()))

            #IDENTIFY YOURSELF!
            name = "DEFAULT"
            if SENSOR_IP in self.request.getpeername():
               print("The sensor has connected.")
               name = "SENSOR"
               txrate = EXPECTED_SA_SEND
               rxrate = EXPECTED_SA_RECEIVE
            elif COORDINATOR_IP in self.request.getpeername():
               print("The coordinator has connected.")
               name = "COORD"
               txrate = EXPECTED_CD_SEND
               rxrate = EXPECTED_CD_RECEIVE
            elif LR_IP in self.request.getpeername():
               print("The lead rover has connected.")
               name = "LEAD"
               txrate = EXPECTED_LR_SEND
               rxrate = EXPECTED_LR_RECEIVE
            elif FOLLOWER_IP in self.request.getpeername():
               print("The follower rover has connected.")
               name = "FOLLOW"
               txrate = EXPECTED_FR_SEND
               rxrate = EXPECTED_FR_RECEIVE
            elif BROOKELAPTOP_IP in self.request.getpeername():
               print("Brooke has connected.")
               name = "BROOKE"
               txrate = EXPECTED_SA_SEND
               rxrate = EXPECTED_SA_RECEIVE
                


            while True:

                data = self.request.recv(1) #96

                if data == MESSAGE_START_BYTE:

                   #Account for messages with count byte = '~'
                   if len(message) == 2:
                      type = message[1] #hex(ord(message[1]))
                      
                      if self.typesAndBytes.has_key(type):
                         if self.typesAndBytes[type] == 125:
                            message = message + data
                            continue

                   message = MESSAGE_START_BYTE

                elif DANIEL_IP in self.request.getpeername():
                   if data == 'g':
                      print("GO")
                   elif data == 's':
                      print("STOP")
                   elif data == 't':
                      print("TOKEN")


                elif data == MESSAGE_STOP_BYTE:

                   if len(message) == 0:
                      continue

                   message = message + data

                   type = message[1] #hex(ord(message[1]))

                   #Account for messages with count byte = MESSAGE_STOP_BYTE
                   if len(message) == 3:
                      if self.typesAndBytes.has_key(type):
                         if self.typesAndBytes[type] == 45:
                            continue
                   
                   #inter_count = total_count - prev_count
                   #prev_count = total_count

                   #Print out message receive data
                   if type == TYPE_RECEIVE_STAT:
                      #print >>sys.stderr, " ".join(hex(ord(n)) for n in message)
                      #print(inter_count - ord(message[8]))

                      #Test accuracy of receive timer callback, calculate receive rate
                      inter_time = time.time() - prev_time
                      prev_time = time.time()

                      if inter_time != 0 and len(message) > 8:
                         
                         if COORDINATOR_IP in self.request.getpeername():
                            minuteTick = minuteTick + 1
                            if minuteTick == 6:
                               minuteTick = 0
                               minuteCount = minuteCount + 1
                               print("Runtime: {0} minutes.\n".format(minuteCount))

                         goodMsg = ord(message[8]) + ord(message[7])*16
                         badMsg = ord(message[6]) + ord(message[5])*16
                         goodMsgTotal = goodMsgTotal + goodMsg
                         badMsgTotal = badMsgTotal + badMsg
                         msgTotal = msgTotal + badMsg + goodMsg

                         goodrate = goodMsg/inter_time
                         badrate = badMsg/inter_time
                         totalrate = (goodMsg+badMsg)/inter_time
                         pushrate = sent_messages/inter_time

			 #uncommented by brooke 3/3/16
                         receiverate = abs((totalrate-rxrate)/(rxrate+0.000001))

                         if rxrate != 0:
                            receiverate = totalrate/rxrate


                         if totalrate == 0 and rxrate == 0:
                            rxreport = "RX Rate - GOOD!"
                         elif receiverate > 0.9 and receiverate < 1.1:
                            rxreport = "RX Rate - GOOD!"
                         elif receiverate > 1.1:
                            rxreport = "RX RATE FAST - {0:.3g}x SPEED".format(receiverate)
                         else:
                            rxreport = "RX RATE SLOW - {0:.3g}x SPEED".format(receiverate)


                         #sendcomp = abs((pushrate - txrate)/(txrate+0.000001))

                         if txrate != 0:
                           sendcomp = pushrate/txrate


                           if pushrate == 0 and txrate == 0:
                              txreport = "TX Rate - GOOD!"
                           elif sendcomp > 0.9 and sendcomp < 1.1:
                              txreport = "TX Rate - GOOD!"
                           elif sendcomp > 1.1:
                              txreport = "TX RATE FAST - {0:.3g}x SPEED".format(sendcomp)
                           else:
                              txreport = "TX RATE SLOW - {0:.3g}x SPEED".format(sendcomp)


                         if goodMsg == 0 and badMsg != 0:
                            errport = "BAD DATA"
                         elif goodMsg == 0:
                            errport = ""
                         elif badMsg == 0:
                            errport = "GOOD DATA"
                         elif (badMsg*10) < goodMsg:
                            errport = "MOSTLY GOOD DATA"
                         else:
                            errport = "ERRORS IN DATA"

                         #block commented OUT by John 4/13/16
                         """
                         if badMsg+goodMsg > 0 or sent_messages > 0:
                            print("<{0}> Messages received: {1} ({2:.3g}/s)  Sent: {3} ({4:.3g}/s)  Errors Reported: {5}".format(name, goodMsg, goodrate, sent_messages, pushrate, badMsg))
                            print("        {0} || {1} || {2}".format(rxreport, txreport, errport))
                            #print("        Rate/sec - Received: {0:.3g}     Sent: {1:.3g}".format(totalrate, pushrate))
                         else:
                            print("<{0}> received and sent 0 messages in {1:.3g}s. Expecting {2} received and {3} sent.".format(name, inter_time, txrate, rxrate))

                         print("")
                         """
                      message = ''
                      sent_messages = 0
                      continue



                   if COORDINATOR_IP in self.request.getpeername():
                      processCoordinatorMsg(type, message)


                   elif BROOKELAPTOP_IP in self.request.getpeername():
                      processBrookeMsg(type, message)

                   elif SENSOR_IP in self.request.getpeername():
                      processSensorMsg(type, message)

                   else:
                      sendToCoordinator(message)
                      if message[1] == TYPE_LR_SENSOR and message[3] == chr(88):
                         print("Sensor reads {0} cm".format(ord(message[8])))
                         #print(" ".join(hex(ord(n)) for n in MESSAGE))

                   sent_messages = sent_messages + 1
                      
                   if len(message) > 2:
                      num = ord(message[2])

                   #added by brooke
                   if type == 0:
                      print message
                   #end added by brooke

                   self.trackIncoming(type, int(num))

                   message = ''



                else:
                   message = message + data



            print("closing the connection")