def handler(self, client_socket, address): print("Handling request") # get request packet request_packet = packet.read_command(client_socket, 2) print("Command Packet", request_packet) packet_reader = packet.CommandPacketReader(request_packet) # extract information type = packet_reader.get_type() id = packet_reader.get_id() command = packet_reader.get_command() value = packet_reader.get_value() packet_reader.report() # Let dispatcher dispatch command if type == packet.REQ_M_TYPE: self.m_dispatcher.handle(command, value) if type == packet.REQ_A_TYPE: self.a_dispatcher.handle(command, value) # send ACK and mirror request message packet_builder = packet.CommandPacketBuilder() # handle M_TYPE and A_TYPE separately if type == packet.REQ_M_TYPE: packet_builder.set_type(packet.ACK_M_TYPE) if type == packet.REQ_A_TYPE: packet_builder.set_type(packet.ACK_A_TYPE) # handle id, command, and value normally packet_builder.set_id(id) packet_builder.set_command(command) packet_builder.set_value(value) # create respond packet reply_packet = packet_builder.create() # send acknowledge packet back to phone client_socket.sendall(bytes((reply_packet[0], reply_packet[1]))) packet_builder.report() # close socket client_socket.close()
def handler(client_socket, address, src_path): global recorder_thread, recorder_running # get request packet request_packet = packet.read_command(client_socket) packet_reader = packet.BananaPacketReader(packet.byte_to_int(request_packet)) # extract information type = packet_reader.get_type() command = packet_reader.get_command() resolution = packet_reader.get_resolution() packet_reader.report() # handle record command if command == packet.COMMAND_ON: # reset flag recorder_running = True # start new recorder thread as respond to the request output_path = time.strftime("%d-%m-%Y-%H-%M-%S") recorder_thread = threading.Thread(target=record, args=(src_path, output_path, resolution)) recorder_thread.start() elif command == packet.COMMAND_OFF: # check if recorder thread exist, if so .. join it back if recorder_thread != None: # unset flag recorder_running = False recorder_thread = None # send ACK and mirror request message packet_builder = packet.BananaPacketBuilder() packet_builder.set_type(packet.ACK_TYPE) packet_builder.set_command(command) packet_builder.set_resolution(resolution) # create respond packet reply_packet = packet_builder.create() packet_builder.report() # send acknowledge packet back to phone client_socket.sendall(bytes((reply_packet, ))) # close socket client_socket.close()
def handler(self, client_socket, address): print("Handling HeartBeat") # Get packet request_packet = packet.read_command(client_socket, 1) packet_reader = packet.HeartBeatPacketReader(request_packet) # Extract information type = packet_reader.get_type() # Read from shared-memory region and replay with data content = self.ipc.read() # Handle values and set proper packet type packet_builder = packet.HeartBeatPacketBuilder() # packet_builder.set_type(packet.HEART_ACK) # handle DONE | WORKING | ERROR if content == 'DONE': packet_builder.set_type(packet.HEART_OK) elif content == 'HEALTHY': packet_builder.set_type(packet.HEART_ACK) elif content == 'ERROR': packet_builder.set_type(packet.HEART_ERR) # Create packet reply_packet = packet_builder.create() # send acknowledge packet back to phone client_socket.sendall(bytes((reply_packet, ))) packet_builder.report() # close socket client_socket.close()