def listen_packets_loop(): """Initializes the socket used to interface with the C program, and listen for any incoming packets. When we hear one, parse it with scapy and update the display to show information about packet. """ global packet global c_socket print 'Listening for packets...' while True: # Receive any packet that the C side has sent over. print 'Sending request' c_packet = SEASIDE.request_SEASIDE(c_socket, c_socket_lock, SEASIDE_FLAGS.GET_PACKET.value) if c_packet != '': # Parse packet with scapy so we can pull it apart easier. packet = scapy.Ether(c_packet) num_packets_received = SEASIDE.request_SEASIDE( c_socket, c_socket_lock, SEASIDE_FLAGS.NUM_PACKETS.value) num_packets_received = struct.unpack('=I', num_packets_received) update_packet_info(packet, num_packets_received) time.sleep(2)
def update_statistics_loop(): """Gets cpu usage and bandwidth and displays it on the LCD. """ global c_socket_lock while True: d_bytes = SEASIDE.request_SEASIDE(c_socket, c_socket_lock, SEASIDE_FLAGS.GET_BANDWIDTH.value) print repr(d_bytes) d_bytes = struct.unpack('=Q', d_bytes) d_bytes = d_bytes[0] bw, bw_unit = conversions.convert_bandwidth_units(d_bytes) bandwidth_output = 'Bw:%5.1f %s' % (bw, bw_unit) thread_lcd.lock_and_display_bandwidth_LED(lcd, lcd_lock, bw, bw_unit) screen_output[Screens.Summary.value][1] = bandwidth_output avg_cpu_usage, per_core_cpu_usage = computations.read_cpu_usage() screen_output[Screens.CPU.value][0] = \ 'CPU Usage: %4.1f%%' % (avg_cpu_usage) screen_output[Screens.CPU.value][1] = \ '%2.0f%% %2.0f%% %2.0f%% %2.0f%%' % tuple(per_core_cpu_usage) time.sleep(1)
def command_and_respond(self, command, size): """Sends a command to the C side, and gets the response. Some flags are used for getting info from the C side, such as getting bandwidth, or the structure of the current packet. Args: command (int): The flag to send to the C side. size (str): The size of the reponse that you're expecting. See https://docs.python.org/2/library/struct.html for different sizes. Returns: str: The response that was received, encoded in hex format. The format looks like this: 12abc4394fa. I.e. there's no dashes or spaces in between each byte, and no \\x or equivalent. As a sidenote, that \\x used to have only one slash, but it threw errors, because apparently it couldn't be parsed... TODO: What if the value returned isn't an int? Make it more generic. """ temp = SEASIDE.request_SEASIDE(c_socket, c_socket_lock, int(command)) temp = struct.unpack('=' + size, temp)[0] temp = struct.pack('!' + size, temp) return temp.encode('hex')
def update_statistics_loop(): """Gets cpu usage and bandwidth and displays it on the LCD. """ global c_socket_lock while True: d_bytes = SEASIDE.request_SEASIDE(c_socket, c_socket_lock, SEASIDE_FLAGS.GET_BANDWIDTH.value) print repr(d_bytes) d_bytes = struct.unpack('=Q', d_bytes) d_bytes = d_bytes[0] bw, bw_unit = conversions.convert_bandwidth_units(d_bytes) bandwidth_output = 'Bw:%5.1f %s' % (bw, bw_unit) thread_lcd.lock_and_display_bandwidth_LED( lcd, lcd_lock, bw, bw_unit) screen_output[Screens.Summary.value][1] = bandwidth_output avg_cpu_usage, per_core_cpu_usage = computations.read_cpu_usage() screen_output[Screens.CPU.value][0] = \ 'CPU Usage: %4.1f%%' % (avg_cpu_usage) screen_output[Screens.CPU.value][1] = \ '%2.0f%% %2.0f%% %2.0f%% %2.0f%%' % tuple(per_core_cpu_usage) time.sleep(1)
def listen_packets_loop(): """Initializes the socket used to interface with the C program, and listen for any incoming packets. When we hear one, parse it with scapy and update the display to show information about packet. """ global packet global c_socket print 'Listening for packets...' while True: # Receive any packet that the C side has sent over. print 'Sending request' c_packet = SEASIDE.request_SEASIDE(c_socket, c_socket_lock, SEASIDE_FLAGS.GET_PACKET.value) if c_packet != '': # Parse packet with scapy so we can pull it apart easier. packet = scapy.Ether(c_packet) num_packets_received = SEASIDE.request_SEASIDE(c_socket, c_socket_lock, SEASIDE_FLAGS.NUM_PACKETS.value) num_packets_received = struct.unpack('=I', num_packets_received) update_packet_info(packet, num_packets_received) time.sleep(2)
def update_statistics_loop(c_socket, c_socket_lock): """Update the values of bandwidth and CPU use. Calculates bandwidth using change in bytes received over time, which is provided by the C-side. Calculates CPU using psutil. """ while True: d_bytes = SEASIDE.request_SEASIDE(c_socket, c_socket_lock, SEASIDE_FLAGS.GET_BANDWIDTH.value) print "D_bytes:", repr(d_bytes) d_bytes = struct.unpack('=Q', d_bytes) d_bytes = d_bytes[0] bw, bw_unit = conversions.convert_bandwidth_units(d_bytes) cpu, percore = computations.read_cpu_usage() # percore unused screen_output[0] = 'Bw:%2.1f %s' % (bw, bw_unit) screen_output[1] = 'CPU:%2.1f%%' % (cpu) time.sleep(1)
def update_statistics_loop(c_socket, c_socket_lock): """Update the values of bandwidth and CPU use. Calculates bandwidth using change in bytes received over time. Calculates CPU using psutil. """ while True: d_bytes = SEASIDE.request_SEASIDE(c_socket, c_socket_lock, SEASIDE_FLAGS.GET_BANDWIDTH.value) d_bytes = struct.unpack('=Q', d_bytes) d_bytes = d_bytes[0] bw, bw_unit = conversions.convert_bandwidth_units(d_bytes) cpu, percore = computations.read_cpu_usage() # percore unused screen_output[0] = 'Bw:%2.1f %s' % (bw, bw_unit) screen_output[1] = 'CPU:%2.1f%%' % (cpu) time.sleep(1)