Пример #1
0
 async def send_locations(self,
                          node: Node,
                          *,
                          request_id: int,
                          locations: Collection[Node]) -> int:
     if node.node_id == self.local_node_id:
         raise ValueError("Cannot send to self")
     batches = tuple(partition_all(NODES_PER_PAYLOAD, locations))
     self.logger.debug("Sending Locations with %d nodes to %s", len(locations), node)
     if batches:
         total_batches = len(batches)
         for batch in batches:
             payload = tuple(
                 node.to_payload()
                 for node in batch
             )
             response = Message(
                 Locations(request_id, total_batches, payload),
                 node,
             )
             await self.message_dispatcher.send_message(response)
             await self.events.sent_locations.trigger(response)
         return total_batches
     else:
         response = Message(
             Locations(request_id, 1, ()),
             node,
         )
         await self.message_dispatcher.send_message(response)
         await self.events.sent_locations.trigger(response)
         return 1
Пример #2
0
 async def advertise(self, node: Node, *, key: bytes, who: Node) -> MessageAPI[Ack]:
     if node.node_id == self.local_node_id:
         raise ValueError("Cannot send to self")
     request_id = self.message_dispatcher.get_free_request_id(node.node_id)
     message = Message(Advertise(request_id, key, who.to_payload()), node)
     async with self.message_dispatcher.subscribe_request(message, Ack) as subscription:
         await self.events.sent_advertise.trigger(message)
         return await subscription.receive()
Пример #3
0
 async def send_advertise(self, node: Node, *, key: bytes, who: Node) -> int:
     if node.node_id == self.local_node_id:
         raise ValueError("Cannot send to self")
     request_id = self.message_dispatcher.get_free_request_id(node.node_id)
     message = Message(
         Advertise(request_id, key, who.to_payload()),
         node,
     )
     self.logger.debug("Sending %s", message)
     await self.message_dispatcher.send_message(message)
     await self.events.sent_advertise.trigger(message)
     return request_id