def close_connection(self):
     """
     Close the connection with the indexing server and the peers.
     :return: None
     """
     self.idx_server_proxy.close_connection(self.id)
     for peer_id, sock in self.peers_sock.iteritems():
         if sock:
             try:
                 exch = MessageExchanger(sock)
                 peer_action = dict(type='exit', id=peer_id)
                 exch.pkl_send(peer_action)
                 sock.shutdown(1)
                 sock.close()
             except error:
                 pass
    def _register(self, f_path, regex=False):
        """
        Register a given file to the indexing server.
        :param f_path: path to the file to register.
        :param regex: if not False, then f_path is processed as a regular expression.
        :return: False, True if replication has been done or False otherwise.
        """
        if regex != False:
            files = glob.glob(f_path)
            results = []
            for f in files:
                term, ret = self._register(f)
                results.append(ret)
            return False, Counter(results)

        if not os.path.isfile(f_path):
            self.logger.error("%s does not exist or is not a file." % f_path)
            return False, False

        # Register to the indexing server
        name = os.path.basename(f_path)
        replicate_to = self.idx_server_proxy.register(self.id, name)

        if replicate_to == False:
            self.logger.debug("Main node and replica are done for metadata.")
            return False, False

        # Register locally
        local_files = self.parent.local_files
        local_files[name] = os.path.abspath(f_path)
        self.parent.local_files = local_files

        # Replicate files
        if replicate_to:
            self.logger.debug("Replicate to %s", replicate_to)
            for peer_id in replicate_to:
                peer_sock = self._get_peer_sock(peer_id)
                if peer_sock:
                    peer_exch = MessageExchanger(peer_sock)
                    peer_action = dict(type='replicate', name=name)
                    peer_exch.pkl_send(peer_action)
                    peer_exch.file_send(f_path)

        return False, True
 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