<s> from sender import Sender <EOL> from collections import deque <EOL> from wiwo import frames <EOL> from wiwo . helpers import interface <EOL> class Monitor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , bpf_filter ) : <EOL> self . __bpf_filter = bpf_filter <EOL> def get_filter ( self ) : <EOL> return self . __bpf_filter <EOL> class Interface ( object ) : <EOL> """<STR_LIT>""" <EOL> INTERFACE_STOPPED = <NUM_LIT:0> <EOL> INTERFACE_STARTED = <NUM_LIT:1> <EOL> def __init__ ( self , name , protocol , channels , channel ) : <EOL> self . __name = name <EOL> self . __protocol = protocol <EOL> self . __channels = channels <EOL> self . __channel = channel <EOL> self . __status = Interface . INTERFACE_STOPPED <EOL> self . __monitors = list ( ) <EOL> def get_name ( self ) : <EOL> return self . __name <EOL> def set_iface_name ( self , iface_name ) : <EOL> self . __name = iface_name <EOL> def get_protocol ( self ) : <EOL> return self . __protocol <EOL> def set_protocol ( self , protocol ) : <EOL> self . __protocol = protocol <EOL> def get_channels ( self ) : <EOL> return self . __channels <EOL> def set_channels ( self , channels ) : <EOL> self . __channels = channels <EOL> def get_channel ( self ) : <EOL> return self . __channel <EOL> def set_channel ( self , channel ) : <EOL> self . __channel = channel <EOL> def get_status ( self ) : <EOL> return self . __status <EOL> def set_status ( self , status ) : <EOL> self . __status = status <EOL> if status == Interface . INTERFACE_STOPPED : <EOL> self . __monitors = [ ] <EOL> def get_monitors ( self ) : <EOL> return self . __monitors <EOL> def add_monitor ( self , monitor ) : <EOL> self . __monitors . append ( monitor ) <EOL> class InterfaceInformation ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , protocol , supported_channels , channel , status , monitors_list ) : <EOL> self . __name = name <EOL> self . __protocol = protocol <EOL> self . __supported_channels = supported_channels <EOL> self . __channel = channel <EOL> self . __status = status <EOL> self . __monitors_list = monitors_list <EOL> def name ( self ) : <EOL> return self . __name <EOL> def protocol ( self ) : <EOL> return self . __protocol <EOL> def supported_channels ( self ) : <EOL> return self . __supported_channels . tolist ( ) <EOL> def is_a_supported_channel ( self , channel ) : <EOL> return channel in self . __supported_channels <EOL> def channel ( self ) : <EOL> return self . __channel <EOL> def status ( self ) : <EOL> return self . __status <EOL> def monitors ( self ) : <EOL> return self . __monitors_list <EOL> class Worker ( object ) : <EOL> """<STR_LIT>""" <EOL> COMMUNICATION_IDLE = <NUM_LIT:0> <EOL> COMMUNICATION_WAITING = <NUM_LIT:1> <EOL> def __init__ ( self , mac ) : <EOL> self . __mac = mac <EOL> self . __interfaces_list = list ( ) <EOL> self . __communication_status = Worker . COMMUNICATION_IDLE <EOL> self . __last_frame_sent = None <EOL> self . __frame_queue = deque ( ) <EOL> self . __frame_log = list ( ) <EOL> def get_mac ( self ) : <EOL> return self . __mac <EOL> def get_interfaces ( self ) : <EOL> return self . __interfaces_list <EOL> def get_interface ( self , iface_name ) : <EOL> for interface in self . __interfaces_list : <EOL> if interface . get_name ( ) == iface_name : <EOL> return interface <EOL> return None <EOL> def add_interface ( self , interface ) : <EOL> self . __interfaces_list . append ( interface ) <EOL> def remove_interfaces ( self ) : <EOL> self . __interfaces_list = [ ] <EOL> def is_waiting_for_response ( self ) : <EOL> return self . __communication_status == self . COMMUNICATION_WAITING <EOL> def set_worker_to_wait_response ( self , frame ) : <EOL> self . __communication_status = Worker . COMMUNICATION_WAITING <EOL> self . __last_frame_sent = frame <EOL> def get_frame_log ( self ) : <EOL> return self . __frame_log <EOL> def _send ( self , frame , iface_name ) : <EOL> self . __communication_status = Worker . COMMUNICATION_WAITING <EOL> self . __last_frame_sent = frame <EOL> frame = Sender . send ( frame , iface_name ) <EOL> log_line = "<STR_LIT>" % repr ( frame ) <EOL> self . __frame_log . append ( log_line ) <EOL> return frame <EOL> def _queue ( self , frame ) : <EOL> self . __frame_queue . append ( frame ) <EOL> frame = frame . get_packet ( ) <EOL> log_line = "<STR_LIT>" % repr ( frame ) <EOL> self . __frame_log . append ( log_line ) <EOL> return "<STR_LIT>" <EOL> def update_after_error ( self ) : <EOL> self . __communication_status = Worker . COMMUNICATION_IDLE <EOL> self . __last_frame_sent = None <EOL> self . __frame_queue . clear ( ) <EOL> def get_type_from_last_frame_sent ( self ) : <EOL> lc = self . __last_frame_sent <EOL> if lc is None : <EOL> return - <NUM_LIT:1> <EOL> buff = lc . get_packet ( ) <EOL> lc_wiwo_frame = buff [ lc . get_header_size ( ) : ] <EOL> lcw = frames . WiwoFrame ( lc_wiwo_frame ) <EOL> return lcw . get_type ( ) <EOL> def send ( self , frame , iface_name ) : <EOL> if self . __communication_status == Worker . COMMUNICATION_IDLE : <EOL> return self . _send ( frame , iface_name ) <EOL> else : <EOL> return self . _queue ( frame ) <EOL> def get_worker_info ( self ) : <EOL> interfaces_list = list ( ) <EOL> for interface in self . __interfaces_list : <EOL> iface_info = InterfaceInformation ( interface . get_name ( ) , <EOL> interface . get_protocol ( ) , <EOL> interface . get_channels ( ) [ : ] , <EOL> interface . get_channel ( ) , <EOL> interface . get_status ( ) , <EOL> interface . get_monitors ( ) ) <EOL> interfaces_list . append ( iface_info ) <EOL> return WorkerInformation ( self . __mac , interfaces_list ) <EOL> def process_ctrl_and_mgnt_frame_received ( self , e , iface_name ) : <EOL> w = e . child ( ) <EOL> log_line = "<STR_LIT>" % repr ( e . get_packet ( ) ) <EOL> self . __frame_log . append ( log_line ) <EOL> if w . get_type ( ) == frames . WiwoAckFrame . frametype : <EOL> lc = self . __last_frame_sent <EOL> if lc is None : <EOL> return <EOL> buff = lc . get_packet ( ) <EOL> lc_wiwo_frame = buff [ lc . get_header_size ( ) : ] <EOL> lcw = frames . WiwoFrame ( lc_wiwo_frame ) <EOL> lc_wiwo_sub_frame = lcw . get_body_as_string ( ) <EOL> if lcw . get_type ( ) == frames . WiwoSetChannelFrame . frametype : <EOL> wsc = frames . WiwoSetChannelFrame ( lc_wiwo_sub_frame ) <EOL> worker_iface_name = wsc . get_iface_as_string ( ) <EOL> channel = wsc . get_channel ( ) <EOL> iface = self . get_interface ( worker_iface_name ) <EOL> if iface is not None : <EOL> iface . set_channel ( channel ) <EOL> elif lcw . get_type ( ) == frames . WiwoStartFrame . frametype : <EOL> ws = frames . WiwoStartFrame ( lc_wiwo_sub_frame ) <EOL> worker_iface_name = ws . get_iface_as_string ( ) <EOL> bpf_filter = ws . get_filter_as_string ( ) <EOL> iface = self . get_interface ( worker_iface_name ) <EOL> if iface is not None : <EOL> iface . add_monitor ( Monitor ( bpf_filter ) ) <EOL> iface . set_status ( Interface . INTERFACE_STARTED ) <EOL> elif lcw . get_type ( ) == frames . WiwoStopFrame . frametype : <EOL> for iface in self . __interfaces_list : <EOL> iface . set_status ( Interface . INTERFACE_STOPPED ) <EOL> elif w . get_type ( ) == frames . WiwoInfoResponseFrame . frametype : <EOL> self . remove_interfaces ( ) <EOL> wiwo_info_response_frame = w . get_body_as_string ( ) <EOL> wir = frames . WiwoInfoResponseFrame ( wiwo_info_response_frame ) <EOL> interfaces = wir . get_interfaces ( ) <EOL> for i in interfaces : <EOL> iface = i . get_iface_as_string ( ) <EOL> protocol = i . get_protocol_as_string ( ) <EOL> channels = i . get_channels ( ) <EOL> channel = i . get_channel ( ) <EOL> ii = Interface ( iface , protocol , channels , channel ) <EOL> self . add_interface ( ii ) <EOL> if self . __frame_queue . __len__ ( ) > <NUM_LIT:0> : <EOL> frame = self . __frame_queue . popleft ( ) <EOL> self . _send ( frame , iface_name ) <EOL> else : <EOL> self . __communication_status = Worker . COMMUNICATION_IDLE <EOL> class WorkerInformation ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , mac_address , interfaces_list ) : <EOL> self . __raw_mac_address = mac_address <EOL> self . __mac_address = interface . transform_mac_address_to_string_mac_address ( mac_address ) <EOL> self . __interfaces_list = interfaces_list <EOL> def raw_mac_address ( self ) : <EOL> return self . __raw_mac_address <EOL> def mac_address ( self ) : <EOL> return self . __mac_address <EOL> def interfaces_list ( self ) : <EOL> return self . __interfaces_list </s>
