def _peer_message_handler(self, peer_sock, peer_addr): """ Handle messages sent by another peer. :param peer_sock: socket to communicate with the other peer. :param peer_addr: address of the other peer. :return: None """ self.logger.debug("Accepted connection from %s", peer_addr) peer_exch = MessageExchanger(peer_sock) open_conn = True while open_conn != False: self.logger.debug("%s: %s", repr(peer_sock), open_conn) action = peer_exch.pkl_recv() if action is None: break self.logger.debug(repr(action)) action['exch'] = peer_exch open_conn = self._generic_action(action) peer_sock.shutdown(1) peer_sock.close() self.parent.client.peers_sock[peer_addr] = None self.parent.client.peers_check[peer_addr] = time.time()
def _lookup(self, name): """ Search for peers where a given file is stored and then request these peers for the file until the file is entirely stored locally. :param name: name of the file to obtain. :return: False, True if the file has been downloaded or False if not. """ _, available_peers = self._search(name, pprint=False) file_obtained = False while not file_obtained and available_peers: peer_id = available_peers.pop(0) # Establish connection to the peer to obtain file peer_sock = self._get_peer_sock(peer_id) self.logger.debug('peer_sock in lookup: %s', repr(peer_sock)) if peer_sock: peer_exch = MessageExchanger(peer_sock) peer_action = dict(type='obtain', name=name) peer_exch.pkl_send(peer_action) f_path = os.path.join(self.download_dir, name) file_exists = peer_exch.pkl_recv() if file_exists: peer_exch.file_recv(f_path, show_progress=False) file_obtained = True return False, file_obtained