def __init__(self, connection: ConnectionAPI, context: BasePeerContext, event_bus: EndpointAPI = None, ) -> None: super().__init__(token=connection.cancel_token, loop=connection.cancel_token.loop) # Peer context object self.context = context # Connection instance self.connection = connection self.multiplexer = connection.get_multiplexer() self.base_protocol = self.connection.get_base_protocol() # TODO: need to remove this property but for now it is here to support # backwards compat for protocol_class in self.supported_sub_protocols: try: self.sub_proto = self.multiplexer.get_protocol_by_type(protocol_class) except UnknownProtocol: pass else: break else: raise ValidationError("No supported subprotocols found in multiplexer") # The self-identifying string that the remote names itself. self.client_version_string = self.connection.safe_client_version_string # Optional event bus handle self._event_bus = event_bus # Flag indicating whether the connection this peer represents was # established from a dial-out or dial-in (True: dial-in, False: # dial-out) # TODO: rename to `dial_in` and have a computed property for `dial_out` self.inbound = connection.is_dial_in self._subscribers: List[PeerSubscriber] = [] # A counter of the number of messages this peer has received for each # message type. self.received_msgs: Dict[CommandAPI, int] = collections.defaultdict(int) # Manages the boot process self.boot_manager = self.get_boot_manager() self.connection_tracker = self.setup_connection_tracker() self.process_handshake_receipts( connection.get_p2p_receipt(), connection.protocol_receipts, )
def applies_to(self, connection: ConnectionAPI) -> bool: return any( protocol.supports_command(self.cmd_type) for protocol in connection.get_multiplexer().get_protocols())