def run(self): log_lib.debug("Starting ...") start_time = time.time() try: while not self.stop_event.is_set(): frame_size_bytes = self.socket.recv(4) frame_size = int.from_bytes(frame_size_bytes, byteorder='little', signed=False) frame_data = self.socket.recv(frame_size) log_lib.debug( "Received a frame of size of {} B. Expected frame size was {} B" .format(len(frame_data), frame_size)) self.queue.put(frame_data) log_lib.debug( "Added data to Queue. Current Queue size {}".format( queue.qsize())) time.sleep(self.sleeping_time) except Exception as e: log_lib.fatal(str(e)) finally: elapsed_time = time.time() - start_time log_lib.info("Completed in %s" % utils_lib.elapsed_time_string(elapsed_time))
def test(): log_lib.info("Starting ...") start_time = time.time() remote_addr = 'localhost' remote_port = 1717 receiver_lib.tcp_receiver(remote_addr, remote_port) elapsed_time = time.time() - start_time log_lib.info("Completed in %s" % utils_lib.elapsed_time_string(elapsed_time)) return
def run(self): log_lib.debug("Starting ...") start_time = time.time() while not self.stop_event.is_set(): try: log_lib.debug( "Trying to pull data from Queue. Current Queue size {}". format(queue.qsize())) if queue.qsize() == 0: time.sleep(sleeping_time) continue frame_data = queue.get(block=True) log_lib.debug( "Pulled a frame of size of {} B from Queue. Current Queue size {}" .format(len(frame_data), queue.qsize())) frame = cv2.imdecode(np.fromstring(frame_data, dtype=np.uint8), -1) cv2.imshow('Window', frame) cv2.wait(1) #time.sleep(sleeping_time) except Exception as e: log_lib.fatal(str(e)) break try: cv2.destroyAllWindows() except: pass elapsed_time = time.time() - start_time log_lib.info("Completed in %s" % utils_lib.elapsed_time_string(elapsed_time))
def test(): log_lib.info("Starting ...") start_time = time.time() try: remote_addr = 'localhost' remote_port = 1717 # Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_address = (remote_addr, remote_port) log_lib.info("Starting TCP Connection to {}:{} ...".format( remote_addr, remote_port)) sock.connect(server_address) log_lib.info( "TCP Connection to {}:{} successfully established!".format( remote_addr, remote_port)) # Retrieve Global Header Informartions #time.sleep(sleeping_time) globalHeader = sock.recv(24) version = globalHeader[0] header_size = globalHeader[1] pid = int.from_bytes(globalHeader[2:5], byteorder='little', signed=False) real_width = int.from_bytes(globalHeader[6:9], byteorder='little', signed=False) real_height = int.from_bytes(globalHeader[10:13], byteorder='little', signed=False) virtual_width = int.from_bytes(globalHeader[14:17], byteorder='little', signed=False) virtual_height = int.from_bytes(globalHeader[18:21], byteorder='little', signed=False) display_orientation = globalHeader[22] quirk_flag = globalHeader[23] window_name = str(pid) + " " + str(virtual_width) + "x" + str( virtual_height) # Thread for retrieving data from TCP time.sleep(main_loop_sleeping_time) # Thread for Processing/Displaying frames time.sleep(main_loop_sleeping_time) # Thread for Displaying frames time.sleep(main_loop_sleeping_time) while True: time.sleep(main_loop_sleeping_time) except KeyboardInterrupt: log_lib.info("Keyboard interrupt received. Terminating ...") except Exception as e: log_lib.fatal(str(e)) finally: log_lib.info("Closing TCP socket {}:{} ...".format( remote_addr, remote_port)) sock.close() elapsed_time = time.time() - start_time log_lib.info("Completed in %s" % utils_lib.elapsed_time_string(elapsed_time))
def tcp_receiver(remote_addr, remote_port, sleeping_time=0.0001): try: # Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_address = (remote_addr, remote_port) log_lib.info("Starting TCP Connection to {}:{} ...".format( remote_addr, remote_port)) sock.connect(server_address) log_lib.info( "TCP Connection to {}:{} successfully established!".format( remote_addr, remote_port)) # Retrieve Global Header Informartions time.sleep(sleeping_time) globalHeader = sock.recv(24) version = globalHeader[0] header_size = globalHeader[1] pid = int.from_bytes(globalHeader[2:5], byteorder='little', signed=False) real_width = int.from_bytes(globalHeader[6:9], byteorder='little', signed=False) real_height = int.from_bytes(globalHeader[10:13], byteorder='little', signed=False) virtual_width = int.from_bytes(globalHeader[14:17], byteorder='little', signed=False) virtual_hieght = int.from_bytes(globalHeader[18:21], byteorder='little', signed=False) display_orientation = globalHeader[22] quirk_flag = globalHeader[23] log_lib.debug("Version: {}".format(version)) log_lib.debug("Header Size: {}".format(header_size)) log_lib.debug("PID: {}".format(pid)) log_lib.debug("Real Width: {}".format(real_width)) log_lib.debug("Real Height: {}".format(real_height)) log_lib.debug("Virtual Width: {}".format(virtual_width)) log_lib.debug("Virtual Height: {}".format(virtual_hieght)) log_lib.debug("Display Orientation: {}".format(display_orientation)) log_lib.debug("Quirk Flag: {}".format(quirk_flag)) debug_counter = 0 amount_received = 0 while True: time.sleep(sleeping_time) frame_size_bytes = sock.recv(4) frame_size = int.from_bytes(frame_size_bytes, byteorder='little', signed=False) time.sleep(sleeping_time) frame_data = sock.recv(frame_size) amount_received += len(frame_data) log_lib.debug( "Received a frame of size of {} B. Expected frame size was {}B. Total amount data received so far is {} B" .format(len(frame_data), frame_size, amount_received)) debug_counter += 1 if debug_counter > 30: break except Exception as e: log_lib.fatal(str(e)) finally: log_lib.info("Closing TCP socket {}:{} ...".format( remote_addr, remote_port)) sock.close() return
def tcp_receiver(remote_addr, remote_port, sleeping_time = 0.001): try: # Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_address = (remote_addr, remote_port) log_lib.info("Starting TCP Connection to {}:{} ...".format(remote_addr, remote_port)) sock.connect(server_address) log_lib.info("TCP Connection to {}:{} successfully established!".format(remote_addr, remote_port)) # Retrieve Global Header Informartions #time.sleep(sleeping_time) globalHeader = sock.recv(24) version = globalHeader[0] header_size = globalHeader[1] pid = int.from_bytes(globalHeader[2:5], byteorder='little', signed=False) real_width = int.from_bytes(globalHeader[6:9], byteorder='little', signed=False) real_height = int.from_bytes(globalHeader[10:13], byteorder='little', signed=False) virtual_width = int.from_bytes(globalHeader[14:17], byteorder='little', signed=False) virtual_height = int.from_bytes(globalHeader[18:21], byteorder='little', signed=False) display_orientation = globalHeader[22] quirk_flag = globalHeader[23] #log_lib.debug("Version: {}".format(version)) #log_lib.debug("Header Size: {}".format(header_size)) #log_lib.debug("PID: {}".format(pid)) #log_lib.debug("Real Width: {}".format(real_width)) #log_lib.debug("Real Height: {}".format(real_height)) #log_lib.debug("Virtual Width: {}".format(virtual_width)) #log_lib.debug("Virtual Height: {}".format(virtual_height)) #log_lib.debug("Display Orientation: {}".format(display_orientation)) #log_lib.debug("Quirk Flag: {}".format(quirk_flag)) window_name = str(pid) + " " + str(virtual_width) + "x" + str(virtual_height) while True: time.sleep(sleeping_time) frame_size_bytes = sock.recv(4) frame_size = int.from_bytes(frame_size_bytes, byteorder='little', signed=False) time.sleep(sleeping_time) frame_data = sock.recv(frame_size) #log_lib.debug("Received a frame of size of {} B. Expected frame size was {} B".format(len(frame_data), frame_size,)) time.sleep(sleeping_time) frame = cv2.imdecode(np.fromstring(frame_data, dtype = np.uint8), -1) time.sleep(sleeping_time) cv2.imshow(window_name, frame) if cv2.waitKey(1) & 0xFF == ord('q'): break time.sleep(sleeping_time) except Exception as e: log_lib.fatal(str(e)) finally: cv2.destroyAllWindows() log_lib.info("Closing TCP socket {}:{} ...".format(remote_addr, remote_port)) sock.close() return
def test(): log_lib.info("Starting ...") start_time = time.time() main_loop_sleeping_time = 0.001 try: q_in = Queue() q_out = Queue() # Thread for retrieving data from TCP receiver_worker_stop = Event() receiver_worker = Thread(target=receiver_lib.tcp_receiver, args=( receiver_worker_stop, q_in, None, )) receiver_worker.setDaemon(False) receiver_worker.start() time.sleep(main_loop_sleeping_time) # Thread for Processing processing_worker_stop = Event() processing_worker = Thread(target=processing_lib.frame_processor, args=( processing_worker_stop, q_in, q_out, )) processing_worker.setDaemon(False) processing_worker.start() time.sleep(main_loop_sleeping_time) # Thread for Displaying frames viewer_worker_stop = Event() viewer_worker = Thread(target=processing_lib.frame_viewer, args=( viewer_worker_stop, q_out, )) viewer_worker.setDaemon(False) viewer_worker.start() time.sleep(main_loop_sleeping_time) while True: time.sleep(main_loop_sleeping_time) except KeyboardInterrupt: log_lib.info("Keyboard interrupt received. Terminating ...") receiver_worker_stop.set() log_lib.info("Receiver Thread terminating ...") receiver_worker.join() log_lib.info("Receiver Thread is done!") time.sleep(0.2) processing_worker_stop.set() log_lib.info("Processing Thread terminating ...") processing_worker.join() log_lib.info("Processing Thread is done!") time.sleep(0.2) viewer_worker_stop.set() log_lib.info("Viewer Thread terminating ...") viewer_worker.join() log_lib.info("Viewer Thread is done!") time.sleep(0.2) except Exception as e: log_lib.fatal(str(e)) finally: pass elapsed_time = time.time() - start_time log_lib.info("Completed in %s" % utils_lib.elapsed_time_string(elapsed_time)) return
def stop(self): self.stop_event.set() log_lib.info("Thread is terminating ...") self.join() log_lib.info("Thread is done!")
def test(): log_lib.info("Starting ...") start_time = time.time() main_loop_sleeping_time = 0.001 remote_addr = 'localhost' remote_port = 1717 try: # Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_address = (remote_addr, remote_port) log_lib.info("Starting TCP Connection to {}:{} ...".format( remote_addr, remote_port)) sock.connect(server_address) log_lib.info( "TCP Connection to {}:{} successfully established!".format( remote_addr, remote_port)) # Retrieve Global Header Informartions #time.sleep(sleeping_time) globalHeader = sock.recv(24) version = globalHeader[0] header_size = globalHeader[1] pid = int.from_bytes(globalHeader[2:5], byteorder='little', signed=False) real_width = int.from_bytes(globalHeader[6:9], byteorder='little', signed=False) real_height = int.from_bytes(globalHeader[10:13], byteorder='little', signed=False) virtual_width = int.from_bytes(globalHeader[14:17], byteorder='little', signed=False) virtual_height = int.from_bytes(globalHeader[18:21], byteorder='little', signed=False) display_orientation = globalHeader[22] quirk_flag = globalHeader[23] log_lib.info("Information from the Server: version {}".format(version)) log_lib.info( "Information from the Server: header_size {}".format(header_size)) log_lib.info("Information from the Server: pid {}".format(pid)) log_lib.info( "Information from the Server: real_width {}".format(real_width)) log_lib.info( "Information from the Server: real_height {}".format(real_height)) log_lib.info("Information from the Server: virtual_width {}".format( virtual_width)) log_lib.info("Information from the Server: virtual_height {}".format( virtual_height)) log_lib.info( "Information from the Server: display_orientation {}".format( display_orientation)) log_lib.info( "Information from the Server: quirk_flag {}".format(quirk_flag)) window_name = str(pid) + " " + str(virtual_width) + "x" + str( virtual_height) q_in = LifoQueue() q_out = LifoQueue() # Thread for retrieving data from TCP receiver_worker_stop = Event() receiver_worker = Thread(target=receiver_lib.tcp_receiver, args=( receiver_worker_stop, q_in, sock, )) receiver_worker.setDaemon(False) receiver_worker.start() time.sleep(main_loop_sleeping_time) # Thread for Processing processing_worker_stop = Event() processing_worker = Thread(target=processing_lib.frame_processor, args=( processing_worker_stop, q_in, q_out, )) processing_worker.setDaemon(False) processing_worker.start() time.sleep(main_loop_sleeping_time) # Thread for Displaying frames viewer_worker_stop = Event() viewer_worker = Thread(target=processing_lib.frame_viewer, args=( viewer_worker_stop, q_out, )) viewer_worker.setDaemon(False) viewer_worker.start() time.sleep(main_loop_sleeping_time) while True: time.sleep(main_loop_sleeping_time) except KeyboardInterrupt: log_lib.info("Keyboard interrupt received. Terminating ...") receiver_worker_stop.set() log_lib.info("Receiver Thread terminating ...") receiver_worker.join() log_lib.info("Receiver Thread is done!") time.sleep(0.2) processing_worker_stop.set() log_lib.info("Processing Thread terminating ...") processing_worker.join() log_lib.info("Processing Thread is done!") time.sleep(0.2) viewer_worker_stop.set() log_lib.info("Viewer Thread terminating ...") viewer_worker.join() log_lib.info("Viewer Thread is done!") time.sleep(0.2) except Exception as e: log_lib.fatal(str(e)) finally: log_lib.info("Closing TCP socket {}:{} ...".format( remote_addr, remote_port)) sock.close() elapsed_time = time.time() - start_time log_lib.info("Completed in %s" % utils_lib.elapsed_time_string(elapsed_time)) return