From 0d77334bff2ca1c70690cf70db6368ef73a25a0b Mon Sep 17 00:00:00 2001 From: Yuxin Wang Date: Wed, 26 Sep 2018 22:18:56 -0400 Subject: [PATCH] Check if same file is being downloaded. --- p2pfs/core/peer.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/p2pfs/core/peer.py b/p2pfs/core/peer.py index ddb0096..c0d29a8 100644 --- a/p2pfs/core/peer.py +++ b/p2pfs/core/peer.py @@ -77,17 +77,22 @@ class Peer(MessageServer): with self._file_list_lock: if self._file_list is None or file not in self._file_list.keys(): return False, 'Requested file {} does not exist, try list_file?'.format(file) + with self._download_lock: + if file in self._download_results: + return False, 'Download {} already in progress.'.format(file) + self._download_results[file] = Queue() self._write_message(self._server_sock, { 'type': MessageType.REQUEST_FILE_LOCATION, 'filename': file }) - - self._download_results[file] = Queue() # wait until reply is ready fileinfo, chunkinfo = self._download_results[file].get() logger.debug('{}: {} ==> {}'.format(file, fileinfo, chunkinfo)) + with self._download_lock: + del self._download_results[file] + return True, 'File {} dowloaded to {}'.format(file, destination) def exit(self):