Voip.ms latency measurement, Full App

Contents

Indices and tables

Exercise

For this week, the goal is to finish the ping app. The new code should do the following:

  • There should be a method that calculates the average latency:

    • It should accept two arguments:

      • The data recieved from the server as done last week
      • A boolean that will print more details when True
    • It should calculate the new average for the last 10 pings to the host name

    • It should print a bar graph in place

      • IE. after one iteration it will overwrite the previous printed result
      • Think ANSI escape codes
  • The main program should run indefinately, looping through the list of host names retreieved from the voip api.

  • The program should count the number of times it completes a full loop through the server hosts

  • The program should cleanly terminate on a KeyboardInterrupt

    • It should move the cursor to the bottom of the screen, print the number of iterations and then exit

A useful data structure this week is collections.deque, however its use is not required. Deque (double ended queue) is a First In - First Out collection that shifts the values automatically as new ones are added. This could be useful for keeping the last 10 pings for each server, however feel free to use any method you can think of.

To print the graph you could use a unicode square, for example u25A0. A reasonable resolution would be each square represents 10 milliseconds.

On completion of this exercise, you should be able to run your program and have it do the following:

  • Print the hostnames that are received from the voip.ms api
  • Continously loop through the host names, for each one plotting the average latency over the last 10 pings
  • Terminate cleanly after seeing a KeyboardInterrupt, printing the number of completed loops before exit.

Hints

The new function signature follows, it is part of the PingRequest class.

    def update_average(self, data, print_output=False):
        """
        Extract the ping time from the input data, and add it to the average queue.
        :param data: The ping response, as a dictionary
        :param print_output: Debug flag, if true print out some information found in the
        response.
        """

Additionally, here is an example of a continuous main program loop.

    # Since run is True and never changes, this loop runs until a KeyboardInterrupt occurs.
    # If you want to run for x iterations, add a condition to set run=False when count = x
    while run:
        try:
            for host in servers:
                # For each hostname, query the server then update the average and plot
                parsed = ping_server.ping_request(host)
                ping_server.update_average(parsed)

            ping_server.row = 1
            count += 1
        except KeyboardInterrupt:
            # Move the cursor to the bottom first

Solution

When you are ready to see one possible solution, try this file