Ejemplo n.º 1
0
 def __init__(
     self,
     privkey: datatypes.PrivateKey,
     address: Address,
     network_id: int,
     min_peers: int = 0,
     peer_class: Type[BasePeer] = ShardingPeer,
     peer_pool_class: Type[PeerPool] = PeerPool,
     bootstrap_nodes: List[str] = [],
 ) -> None:
     BaseService.__init__(self, CancelToken('ShardingServer'))
     self.privkey = privkey
     self.address = address
     self.network_id = network_id
     self.peer_class = peer_class
     self.discovery = DiscoveryProtocol(self.privkey,
                                        self.address,
                                        bootstrap_nodes=bootstrap_nodes)
     # XXX: This is not supposed to work and causes both the PeerPool and Server to crash, but
     # the tests in test_sharding.py don't seem to care
     self.headerdb = None
     self.peer_pool = peer_pool_class(
         peer_class,
         self.headerdb,
         self.network_id,
         self.privkey,
         self.discovery,
         min_peers=min_peers,
     )
     shard_db = ShardDB(MemoryDB())
     shard = Shard(shard_db, 0)
     self.syncer = ShardSyncer(shard, self.peer_pool,
                               self.cancel_token)  # type: ignore
Ejemplo n.º 2
0
 def __init__(self,
              headerdb: BaseAsyncHeaderDB,
              peer_pool: LESPeerPool,
              token: CancelToken = None) -> None:
     PeerSubscriber.__init__(self)
     BaseService.__init__(self, token)
     self.headerdb = headerdb
     self.peer_pool = peer_pool
     self._pending_replies = weakref.WeakValueDictionary()
Ejemplo n.º 3
0
 def __init__(self, connection: ConnectionAPI,
              request_protocol: ProtocolAPI,
              response_cmd_type: Type[CommandAPI]) -> None:
     # This style of initialization keeps `mypy` happy.
     BaseService.__init__(self, token=connection.cancel_token)
     self._connection = connection
     self.request_protocol = request_protocol
     self.response_cmd_type = response_cmd_type
     self._lock = asyncio.Lock()
Ejemplo n.º 4
0
 def __init__(self,
              headerdb: BaseAsyncHeaderDB,
              peer_pool: LESPeerPool,
              token: CancelToken = None) -> None:
     PeerSubscriber.__init__(self)
     BaseService.__init__(self, token)
     self.headerdb = headerdb
     self.peer_pool = peer_pool
     self._pending_replies: Dict[int, Callable[[Payload], None]] = {}
Ejemplo n.º 5
0
 def __init__(self,
              headerdb: 'BaseAsyncHeaderDB',
              peer_pool: PeerPool,
              token: CancelToken = None) -> None:
     PeerPoolSubscriber.__init__(self)
     BaseService.__init__(self, token)
     self.headerdb = headerdb
     self.peer_pool = peer_pool
     self._pending_replies: Dict[int, Callable[[protocol._DecodedMsgType],
                                               None]] = {}
Ejemplo n.º 6
0
async def exit_signal_with_service(
        service_to_exit: BaseService) -> AsyncGenerator[None, None]:
    loop = service_to_exit.get_event_loop()
    async with exit_signal(loop):
        await service_to_exit.cancel()
        yield
        service_to_exit._executor.shutdown(wait=True)
Ejemplo n.º 7
0
    def __init__(self, connection: ConnectionAPI,
                 request_protocol_type: Type[ProtocolAPI],
                 response_cmd_type: Type[CommandAPI]) -> None:
        # This style of initialization keeps `mypy` happy.
        BaseService.__init__(self, token=connection.cancel_token)
        self._connection = connection
        self.request_protocol_type = request_protocol_type
        try:
            self.request_protocol = self._connection.get_multiplexer(
            ).get_protocol_by_type(request_protocol_type, )
        except UnknownProtocol as err:
            raise UnknownProtocol(
                f"Response candidate stream configured to use "
                f"{request_protocol_type} which is not available in the "
                f"Multiplexer") from err

        self.response_cmd_type = response_cmd_type
        self._lock = asyncio.Lock()
Ejemplo n.º 8
0
async def exit_on_signal(service_to_exit: BaseService) -> None:
    loop = service_to_exit.get_event_loop()
    sigint_received = asyncio.Event()
    for sig in [signal.SIGINT, signal.SIGTERM]:
        # TODO also support Windows
        loop.add_signal_handler(sig, sigint_received.set)

    await sigint_received.wait()
    try:
        await service_to_exit.cancel()
    finally:
        loop.stop()
Ejemplo n.º 9
0
async def exit_on_signal(service_to_exit: BaseService,
                         endpoint: Endpoint = None) -> None:
    loop = service_to_exit.get_event_loop()
    sigint_received = asyncio.Event()
    for sig in [signal.SIGINT, signal.SIGTERM]:
        # TODO also support Windows
        loop.add_signal_handler(sig, sigint_received.set)

    await sigint_received.wait()
    try:
        await service_to_exit.cancel()
        if endpoint is not None:
            endpoint.stop()
        service_to_exit._executor.shutdown(wait=True)
    finally:
        loop.stop()
Ejemplo n.º 10
0
def run_service_until_quit(service: BaseService) -> None:
    loop = asyncio.get_event_loop()
    asyncio.ensure_future(exit_on_signal(service))
    asyncio.ensure_future(service.run())
    loop.run_forever()
    loop.close()