def add_ack_nack_message(message): start_time = time.time() str_len = message[protocol.MSG_ORIG_LEN_INDEX] result = cracker.search_in_range( protocol.get_msg_range(message, True, str_len), protocol.get_msg_range(message, False, str_len), protocol.get_msg_hash(message), message[protocol.MSG_ORIG_LEN_INDEX]) print("--- Completed request in {} seconds and {} ---".format( (time.time() - start_time), "failed" if result is None else "succeeded")) return protocol.create_message(protocol.Messages.NACK.value, EMPTY, protocol.NULL, EMPTY, EMPTY)\ if result is None else protocol.create_message(protocol.Messages.ACK.value,EMPTY, message[protocol.MSG_ORIG_LEN_INDEX], bytes(protocol.encode_str(result)), EMPTY)
def add_nack_message(address): return protocol.create_message(protocol.Messages.NACK.value, EMPTY, protocol.NULL, EMPTY, EMPTY), address
def create_request_with_ranges(hash_str, str_len, range): lower, upper = range return protocol.create_message(protocol.Messages.REQUEST.value, bytes(protocol.encode_str(hash_str)), str_len, bytes(protocol.encode_str(lower)), bytes(protocol.encode_str(upper)))
def add_offer_message(message): return protocol.create_message(protocol.Messages.OFFER.value, EMPTY, protocol.NULL, EMPTY, EMPTY)
def create_request(hash_str, str_len): return protocol.create_message(protocol.Messages.DISCOVER.value, bytes(protocol.encode_str(hash_str)), int(str_len), EMPTY, EMPTY)