示例#1
0
 def get_packet_payload(cls, packet_n):
     """
     Return a packet payload consisting of:
     - The packet number
     - The current counter value
     - A host 'ID' representing this client specifically
       (so that received packets can later be separated)
     """
     host_id = cls.guess_host_id()
     counter_value_send = logi_pi_timer.read_counter()
     payload = "%05d %05d %d" % (packet_n, counter_value_send, host_id)
     return payload
示例#2
0
    def run_server(self, server_listen_port, recv_buffer_size):
        """
        Receive packets sent from the client. Calculate the latency for each
        packet by comparing the counter value from the packet (the counter value
        at time of transmission) to the current counter value.
        """
        sock_in = \
            socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
        sock_in.bind(("0.0.0.0", server_listen_port))

        print("UDP server running...")

        all_hosts_expected_n_packets = {}
        packet_n_latency_tuples = {}

        first_packet = True

        try:
            while not self.all_clients_all_packets_received(
                    packet_n_latency_tuples, all_hosts_expected_n_packets):
                data = sock_in.recv(recv_buffer_size)
                if not data:
                    break
                data = data.decode()
                if first_packet:
                    timeout_seconds = 15
                    sock_in.settimeout(timeout_seconds)
                first_packet = False

                if len(data) < 128:
                    (host_id,
                     expected_n_packets) = [int(x) for x in data.split(' ')]
                    print("Expecting %d packets from host %d" %
                          (expected_n_packets, host_id))
                    packet_n_latency_tuples[host_id] = []
                    all_hosts_expected_n_packets[host_id] = expected_n_packets
                else:
                    counter_value_recv = logi_pi_timer.read_counter()
                    payload = data.rstrip('a')
                    (packet_n, counter_value_send, host_id) = \
                        [int(x) for x in payload.split(' ')]

                    delta = counter_value_recv - counter_value_send
                    #print (str(counter_value_recv) + " " + str(counter_value_send) + " " + str(delta))
                    latency_us = delta
                    packet_n_latency_tuples[host_id].append(
                        (packet_n, latency_us))
        except socket.timeout:
            print("Note: timed out waiting to receive packets")

        sock_in.close()

        for host_id in packet_n_latency_tuples.keys():
            print("Received %d packets from host %d" %
                  (len(packet_n_latency_tuples[host_id]), host_id))

        for host_id in packet_n_latency_tuples.keys():
            host_filename = self.test_output_filename + '_' + str(host_id)
            self.save_packet_latencies(packet_n_latency_tuples[host_id],
                                       all_hosts_expected_n_packets[host_id],
                                       host_filename)