Implement request/reply file location.

This commit is contained in:
Yuxin Wang 2018-09-26 21:12:24 -04:00
parent bb06947048
commit 930be3cb96
2 changed files with 18 additions and 5 deletions

View file

@ -74,12 +74,19 @@ class Peer(MessageServer):
return self._file_list
def download(self, file, destination, progress):
if file not in self._file_list.keys():
# refresh the file list
self.list_file()
# still not in list
with self._file_list_lock:
if file not in self._file_list.keys():
return False, 'Requested file {} does not exist'.format(file)
return False, 'Requested file {} does not exist, try list_file?'.format(file)
self._write_message(self._server_sock, {
'type': MessageType.REQUEST_FILE_LOCATION,
'filename': file
})
self._download_results[file] = Queue()
# wait until reply is ready
chunkinfo = self._download_results[file].get()
logger.debug(chunkinfo)
return True, 'File {} dowloaded to {}'.format(file, destination)
def exit(self):

View file

@ -76,6 +76,12 @@ class Tracker(MessageServer):
'type': MessageType.REPLY_FILE_LIST,
'file_list': self._file_list
})
elif message['type'] == MessageType.REQUEST_FILE_LOCATION:
self._write_message(client, {
'type': MessageType.REPLY_FILE_LOCATION,
'filename': message['filename'],
'chunkinfo': self._chunkinfo[message['filename']]
})
else:
logger.error('Undefined message with {} type, full packet: {}'.format(message['type'], message))