def transfer_file(self, share_ID, file_name, return_address, start_offset=0, end_offset=999999999999, block_skip=1): """Sends a piece of a file to a peer in pieces. Initiated by JSONRPC: get_file() start_offset: start sending file from this offset end_offset: if this bytes is reached, send no more. block_skip: send a block, then skip this many blocks, then send the next etc. """ #continue to send all file blocks full_file_path = os.path.join(self.storage_directory, share_ID, file_name) f = open(full_file_path, 'rb') f.seek(start_offset) block_size = 5 file_offset = start_offset logging.debug('file_name: %s start_offset: %d, end_offset: %d' %(file_name, start_offset, end_offset)) while (file_offset < end_offset): #@TODO: stop sending if given signal from return_address logging.debug('file_offset: %d' %(file_offset)) block_bytes = f.read(64*block_size) if block_bytes == "": logging.debug('no bytes read from file') break p_out_block = Packet() p_out_block.json_RPC_object = dict(jsonrpc="2.0", method="save_file_block", params=[1.0, None, [self.my_machine_ID, share_ID, file_name, file_offset]], ) #id=rpc_id p_out_block.binary_blob = block_bytes p_out_block.to_address = return_address self.send_block_choke(p_out_block, return_address, 3) time.sleep(0.002) file_offset+=block_size logging.debug('finished file transfer') f.close()
def transfer_file(self, share_ID, file_name, return_address, start_offset=0, end_offset=999999999999, block_skip=1): """Sends a piece of a file to a peer in pieces. Initiated by JSONRPC: get_file() start_offset: start sending file from this offset end_offset: if this bytes is reached, send no more. block_skip: send a block, then skip this many blocks, then send the next etc. """ #continue to send all file blocks full_file_path = os.path.join(self.storage_directory, share_ID, file_name) f = open(full_file_path, 'rb') f.seek(start_offset) block_size = 5 file_offset = start_offset logging.debug('file_name: %s start_offset: %d, end_offset: %d' % (file_name, start_offset, end_offset)) while (file_offset < end_offset ): #@TODO: stop sending if given signal from return_address logging.debug('file_offset: %d' % (file_offset)) block_bytes = f.read(64 * block_size) if block_bytes == "": logging.debug('no bytes read from file') break p_out_block = Packet() p_out_block.json_RPC_object = dict( jsonrpc="2.0", method="save_file_block", params=[ 1.0, None, [self.my_machine_ID, share_ID, file_name, file_offset] ], ) #id=rpc_id p_out_block.binary_blob = block_bytes p_out_block.to_address = return_address self.send_block_choke(p_out_block, return_address, 3) time.sleep(0.002) file_offset += block_size logging.debug('finished file transfer') f.close()