def test_recovery_random_dead(self): dc = DataClient(host, port, storage_sender, gui_data_sender, active_channels) dc.start_sync_recovery_thread() # play the normal_sequence twice so we get in sync for i in range(2): for x in normal_sequence: dc.incoming_queue.put(x) # play the normal_sequence with a random DEAD to break sync for x in dead_sequence: dc.incoming_queue.put(x) time.sleep(0.2) assert not dc.synchronized # play the normal_sequence again twice, this is where we recover for i in range(2): for x in normal_sequence: dc.incoming_queue.put(x) time.sleep(0.2) assert dc.synchronized
def test_verify_and_parse_speed(self): dc = DataClient(host, port, storage_sender, gui_data_sender, active_channels) dc.start_sync_verification_thread() dc.start_parser_thread() input_length = 10000 chunk_size = 20 with dc.expected_readings_parsed_lock: dc.expected_readings_parsed = input_length normal_bytearray_chunk = bytearray(0) for i in range(chunk_size): normal_bytearray_chunk += normal_bytearray start = time.time() for i in range(input_length / chunk_size): dc.fast_path_sender.send(normal_bytearray_chunk) with dc.frame_to_be_verified_cond: dc.frame_to_be_verified_cond.notify() with dc.parser_done_cond: dc.parser_done_cond.wait() elapsed = time.time() - start speed = 1 / (elapsed / input_length) print '\n\nParser Stage: effective frequency over %d samples is %d Hz\n' % ( input_length, speed)
def test_recovery_missing_byte(self): dc = DataClient(host, port, storage_sender, gui_data_sender, active_channels) dc.start_sync_recovery_thread() # play the normal_sequence twice so we get in sync, but skip the last byte for x in normal_sequence: dc.incoming_queue.put(x) for i in range(len(normal_sequence) - 1): dc.incoming_queue.put(normal_sequence[i]) # play the normal_sequence once again for x in normal_sequence: dc.incoming_queue.put(x) time.sleep(0.2) assert not dc.synchronized # play the normal_sequence once again, this is where we recover for x in normal_sequence: dc.incoming_queue.put(x) time.sleep(0.2) assert dc.synchronized
def test_recovery_added_byte(self): dc = DataClient(host, port, storage_sender, gui_data_sender, active_channels) dc.start_sync_recovery_thread() # play the normal_sequence twice so we get in sync for i in range(2): for x in normal_sequence: dc.incoming_queue.put(x) # throw a wrench in the pipeline dc.incoming_queue.put('c') # play the normal_sequence once again for x in normal_sequence: dc.incoming_queue.put(x) time.sleep(0.2) assert not dc.synchronized # play the normal_sequence once again, this is where we recover for x in normal_sequence: dc.incoming_queue.put(x) time.sleep(0.2) assert dc.synchronized
def __init__(self, ip): self.log = Logger(MAIN_CLIENT_LOG_FILE, D_VERB) self.log.info('[MAIN THREAD] Instantiated client') self.receiving = False self.define_headers() self.targets = {} self.transmit = Queue.Queue() self.data_client = DataClient(self.transmit, ip) self.data_processor = DataProcessor(self.transmit, self.headers, self.targets) self.connect(ip)
def test_initial_sync(self): dc = DataClient(host, port, storage_sender, gui_data_sender, active_channels) dc.start_sync_verification_thread() # play the normal_sequence twice so we get in sync for i in range(2): for x in normal_sequence: dc.incoming_queue.put(x) time.sleep(0.2) assert dc.synchronized
def test_sync_to_parser_handoff(self): dc = DataClient(host, port, storage_sender, gui_data_sender, active_channels) dc.start_sync_recovery_thread() dc.start_parser_thread() # play the normal_sequence twice so we get in sync for i in range(3): for x in normal_sequence: dc.incoming_queue.put(x) time.sleep(0.2) assert not dc.storage_queue.empty() assert not dc.gui_queue.empty()
def test_receive_recovery_speed(self): dc = DataClient(storage_sender, gui_data_sender, reading_to_be_stored_cond, readings_to_be_plotted_cond) server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_sock.bind(('localhost', 10002)) server_sock.listen(1) print 'listening on %s:%d' % ('localhost', 10002) dc.connect_data_port('localhost', 10002) conn, addr = server_sock.accept() print 'accepted connection from %s:%d' % (addr[0], addr[1]) input_length = 1000 bytes_sent = 0 start = time.time() for i in range(input_length): for j in range(len(normal_reading)): bytes_sent += conn.send(np.uint16(normal_reading[j])) with dc.expected_bytes_sent_lock: dc.expected_bytes_sent = bytes_sent dc.receiver_done_event.wait() elapsed = time.time() - start speed = 1 / (elapsed / input_length) print '\n\nReceive Recover Stage: effective frequency over %d samples is %d Hz\n' % ( input_length, speed) dc.close_data_port() conn.close() server_sock.close()
def test_recv_and_verify_speed(self): dc = DataClient(host, port, storage_sender, gui_data_sender, active_channels) server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_sock.bind(('localhost', 10002)) server_sock.listen(1) print 'listening on %s:%d' % ('localhost', 10002) dc.connect_data_port() conn, addr = server_sock.accept() print 'accepted connection from %s:%d' % (addr[0], addr[1]) input_length = 1000 with dc.expected_readings_verified_lock: dc.expected_readings_verified = input_length - 2 start = time.time() for i in range(input_length): for j in range(len(normal_reading)): conn.send(np.uint16(normal_reading[j])) with dc.sync_filter_done_cond: dc.sync_filter_done_cond.wait() elapsed = time.time() - start speed = 1 / (elapsed / input_length) print '\n\nReceive and Verify Stages: effective frequency over %d samples is %d Hz\n' % ( input_length, speed) dc.close_data_port() conn.close() server_sock.close()
def test_receive_and_sync_verification(self): dc = DataClient(host, port, storage_sender, gui_data_sender, active_channels) server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_sock.bind(('localhost', 10002)) server_sock.listen(1) print 'listening on %s:%d' % ('localhost', 10002) dc.connect_data_port() conn, addr = server_sock.accept() print 'accepted connection from %s:%d' % (addr[0], addr[1]) input_length = 4 bytes_sent = 0 with dc.expected_bytes_sent_lock: dc.expected_bytes_sent = 99999999 with dc.expected_readings_verified_lock: dc.expected_readings_verified = 99999999 for i in range(input_length): for j in range(len(normal_reading)): bytes_sent += conn.send(np.uint16(normal_reading[j])) with dc.expected_bytes_sent_lock: dc.expected_bytes_sent = bytes_sent print 'Test: finished sending part 1, bytes_sent = %d' % bytes_sent # first two readings will get dropped by sync. recovery filter with dc.expected_readings_verified_lock: dc.expected_readings_verified = input_length - 2 # with dc.receiver_done_cond: # dc.receiver_done_cond.wait() # print 'Receiver finished task 1' with dc.sync_filter_done_cond: dc.sync_filter_done_cond.wait() print 'Sync filter finished task 1' assert dc.synchronized print 'Part 1 passed, synchronization achieved' with dc.expected_bytes_sent_lock: dc.expected_bytes_sent = 99999999 for j in range(len(corrupt_reading)): bytes_sent += conn.send(np.uint16(corrupt_reading[j])) print 'Test: finished sending part 2, bytes_sent = %d' % bytes_sent with dc.expected_bytes_sent_lock: dc.expected_bytes_sent = bytes_sent with dc.receiver_done_cond: dc.receiver_done_cond.wait() print 'Receiver finished task 2' assert not dc.synchronized print 'Part 2 passed, synchronization lost as expected' with dc.expected_bytes_sent_lock: dc.expected_bytes_sent = 99999999 with dc.expected_readings_verified_lock: dc.expected_readings_verified = 99999999 for i in range(input_length): for j in range(len(normal_reading)): bytes_sent += conn.send(np.uint16(normal_reading[j])) print 'Test: finished sending part 3, bytes_sent = %d' % bytes_sent with dc.expected_bytes_sent_lock: dc.expected_bytes_sent = bytes_sent with dc.expected_readings_verified_lock: dc.expected_readings_verified = (input_length - 2) * 2 with dc.sync_filter_done_cond: dc.sync_filter_done_cond.wait() print 'Sync filter finished task 3' assert dc.synchronized dc.close_data_port() conn.close() server_sock.close()
def __init__(self, storage_sender, gui_control_conn, gui_data_queue, file_header_sender, file_header_available_event, reading_to_be_stored_event, readings_to_be_plotted_event, control_msg_from_gui_event, control_msg_from_nc_event): super(NetworkController, self).__init__() # mp.Connection for sending readings from DataClient to StorageController self.storage_sender = storage_sender # mp.Connection for sending and receiving control messages (protobufs) back and forth to GUI # Note: full duplex Pipe self.gui_control_conn = gui_control_conn # mp.Connection for sending ADC readings to GUI for plotting self.gui_data_queue = gui_data_queue # mp.Connection for sending start_time, channel_bitmask, and chunk_size to SC self.file_header_sender = file_header_sender # IPC condition variables self.file_header_available_event = file_header_available_event self.reading_to_be_stored_event = reading_to_be_stored_event self.readings_to_be_plotted_event = readings_to_be_plotted_event # mp.Condition variable for wait/notify on duplex control message connection GUI <--> NC self.control_msg_from_gui_event = control_msg_from_gui_event self.control_msg_from_nc_event = control_msg_from_nc_event # used to stop listener threads and terminate the process gracefully self.stop_event = mp.Event() # mp.Event variable for ControlClient to notify NC that an ACK is available self.ack_msg_from_cc_event = mp.Event() # threading.Event variable to wait on for async client to connect self.control_client_connected_event = threading.Event() self.control_client_disconnected_event = threading.Event() # shared with control client, sends request messages to be sent over TCP # receives ACK messages self.nc_control_conn, self.cc_control_conn = mp.Pipe(duplex=True) # control client will write ACK'd requests here self.ack_queue = mp.Queue() # default to all channels being active # NOTE: this needs to match up with the default state of the channel checkboxes on GUI # and needs to be propagated to DataClient upon any change self.active_channels = [ '0.0', '0.1', '0.2', '0.3', '0.4', '0.5', '0.6', '0.7', '1.0', '1.1', '1.2', '1.3', '1.4', '1.5', '1.6', '1.7', '2.0', '2.1', '2.2', '2.3', '2.4', '2.5', '2.6', '2.7', '3.0', '3.1', '3.2', '3.3', '3.4', '3.5', '3.6', '3.7' ] # host and port will be extracted from GUI connect message self.host = '' self.port = 0 # used to keep track of messages that have been sent to ControlClient but not yet ACKed self.sent_dict = {} self.control_client = ControlClient( control_protobuf_conn=self.cc_control_conn, ack_msg_from_cc_event=self.ack_msg_from_cc_event, connected_event=self.control_client_connected_event, disconnected_event=self.control_client_disconnected_event) self.data_client = DataClient( gui_data_queue=self.gui_data_queue, storage_sender=self.storage_sender, reading_to_be_stored_event=self.reading_to_be_stored_event, readings_to_be_plotted_event=self.readings_to_be_plotted_event) self.stop_listener_thread = threading.Thread( target=self.listen_for_stop_event) # receives request protobuf messages triggered by GUI events self.gui_receiver_thread = threading.Thread(target=self.recv_from_gui) self.gui_receiver_thread.daemon = True # listens for ACK messages being passed back from control client self.ack_listener_thread = threading.Thread( target=self.read_ack_messages) self.ack_listener_thread.daemon = True # handle asyncore blocking loop in a separate thread # NOTE: lambda needed so loop() doesn't get called right away and block # 1.0 sets the polling frequency (default=30.0) # use_poll=True is a workaround to avoid "bad file descriptor" upon closing # for python 2.7.X according to GitHub Issue...but it still gives the error self.loop_thread = threading.Thread(target=self.asyncore_loop) self.loop_thread.daemon = True