def test_bytes2int(self): self.assertEqual(utils.bytes2int('78563412'.decode('hex')), 0x12345678, 'test bytes2int fail') self.assertEqual(utils.bytes2int('12efcdab'.decode('hex')), 0xabcdef12, 'test bytes2int fail') self.assertEqual(utils.bytes2int('7b000000'.decode('hex')), 123, 'test bytes2int fail')
def run(self): self.__thread_running = True self.__receiving_controller.setDaemon(True) self.__sending_controller.setDaemon(True) self.__receiving_controller.start() self.__sending_controller.start() while self.__thread_running: if not self.__receiving_queue.empty(): route_address, now_packet = self.__receiving_queue.get() try: self.__confirm_packet(now_packet) except packet.VersionException: print '%s is not an available version' % utils.bytes2int(now_packet.version) except packet.CommandException: print '%s is not an available command' % now_packet.get_command_string() except packet.ChecksumException: if now_packet.get_command_string == 'DATA': print 'checksum is not available, ready to resend a GETDATA packet' self.__send_get_data_packet(route_address, now_packet.uuid.encode('hex')) else: print 'checksum is not available, drop the packet' else: if now_packet.get_command_string() == 'DATA': self.__send_ack_packet(route_address, now_packet.uuid.encode('hex')) self.__handle_packet(route_address, now_packet) else: time.sleep(0.2)
def run(self): self.__thread_running = True self.__receiving_controller.setDaemon(True) self.__sending_controller.setDaemon(True) self.__receiving_controller.start() self.__sending_controller.start() while self.__thread_running: if not self.__receiving_queue.empty(): route_address, now_packet = self.__receiving_queue.get() try: self.__confirm_packet(now_packet) except packet.VersionException: print '%s is not an available version' % utils.bytes2int( now_packet.version) except packet.CommandException: print '%s is not an available command' % now_packet.get_command_string( ) except packet.ChecksumException: if now_packet.get_command_string == 'DATA': print 'checksum is not available, ready to resend a GETDATA packet' self.__send_get_data_packet( route_address, now_packet.uuid.encode('hex')) else: print 'checksum is not available, drop the packet' else: if now_packet.get_command_string() == 'DATA': self.__send_ack_packet(route_address, now_packet.uuid.encode('hex')) self.__handle_packet(route_address, now_packet) else: time.sleep(0.2)
def __confirm_packet(confirmation_packet): if utils.bytes2int(confirmation_packet.version) != 0x0209: raise packet.VersionException if confirmation_packet.get_command_string() not in ['POST', 'GETDATA', 'DATA', 'ACK']: raise packet.CommandException if hashlib.sha256(confirmation_packet.payload).digest()[:8] != confirmation_packet.checksum: raise packet.ChecksumException return True
def __confirm_packet(confirmation_packet): if utils.bytes2int(confirmation_packet.version) != 0x0209: raise packet.VersionException if confirmation_packet.get_command_string() not in [ 'POST', 'GETDATA', 'DATA', 'ACK' ]: raise packet.CommandException if hashlib.sha256(confirmation_packet.payload).digest( )[:8] != confirmation_packet.checksum: raise packet.ChecksumException return True