(self, peer)
Add a peer whose inv messages we want to monitor.
Add a peer whose inv messages we want to monitor.
Add a peer whose inv messages we want to monitor .
def add_peer(self, peer):
        """
        Add a peer whose inv messages we want to monitor.
        """
        self.fetchers_by_peer[peer] = Fetcher(peer)
        q = asyncio.Queue()
        self.advertise_queues.add(q)

        @asyncio.coroutine
        def _advertise_to_peer(peer, q):
            while True:
                items = []
                while True:
                    inv_item = yield from q.get()
                    if peer not in self.inv_item_db.get(inv_item.data, []):
                        items.append(inv_item)
                    if q.qsize() == 0:
                        break
                # advertise the presence of the item!
                if len(items) > 0:
                    peer.send_msg("inv", items=items)

        @asyncio.coroutine
        def _watch_peer(peer, next_message, advertise_task):
            try:
                while True:
                    name, data = yield from next_message()
                    if name == 'inv':
                        for inv_item in data["items"]:
                            logging.debug("noting %s available from %s", inv_item, peer)
                            self._register_inv_item(inv_item, peer)
                    if name == 'notfound':
                        for inv_item in data["items"]:
                            logging.debug("noting %s not available from %s", inv_item, peer)
                            self._unregister_inv_item(inv_item, peer)
            except EOFError:
                del self.fetchers_by_peer[peer]
                advertise_task.cancel()
                for q in self.inv_item_queues:
                    q.put_nowait(None)

        advertise_task = asyncio.Task(_advertise_to_peer(peer, q))

        next_message = peer.new_get_next_message_f(lambda name, data: name in ("inv", "notfound"))
        peer.add_task(_watch_peer(peer, next_message, advertise_task))
def add_peer ( self , peer ) : self . fetchers_by_peer [ peer ] = Fetcher ( peer ) q = asyncio . Queue ( ) self . advertise_queues . add ( q ) @ asyncio . coroutine def _advertise_to_peer ( peer , q ) : while True : items = [ ] while True : inv_item = yield from q . get ( ) if peer not in self . inv_item_db . get ( inv_item . data , [ ] ) : items . append ( inv_item ) if q . qsize ( ) == 0 : break # advertise the presence of the item! if len ( items ) > 0 : peer . send_msg ( "inv" , items = items ) @ asyncio . coroutine def _watch_peer ( peer , next_message , advertise_task ) : try : while True : name , data = yield from next_message ( ) if name == 'inv' : for inv_item in data [ "items" ] : logging . debug ( "noting %s available from %s" , inv_item , peer ) self . _register_inv_item ( inv_item , peer ) if name == 'notfound' : for inv_item in data [ "items" ] : logging . debug ( "noting %s not available from %s" , inv_item , peer ) self . _unregister_inv_item ( inv_item , peer ) except EOFError : del self . fetchers_by_peer [ peer ] advertise_task . cancel ( ) for q in self . inv_item_queues : q . put_nowait ( None ) advertise_task = asyncio . Task ( _advertise_to_peer ( peer , q ) ) next_message = peer . new_get_next_message_f ( lambda name , data : name in ( "inv" , "notfound" ) ) peer . add_task ( _watch_peer ( peer , next_message , advertise_task ) )