Exemple #1
0
 async def _await_removal(self):
     self.log.debug("Awaiting removal")
     self.connected.clear()
     if self.timeout_client == math.inf:
         self.log.debug("Client is persistent, won't remove")
         return  # won't remove Client object
     self.log.debug("Sleeping {!s}".format(self.timeout_client))
     try:
         await asyncio.sleep(self.timeout_client)
     except asyncio.CancelledError:
         self.log.debug("Awaiting removal canceled")
         return
     self.log.debug("Done sleeping")
     if self.await_shutdown_task is not None and not self.await_shutdown_task.done(
     ):
         self.await_shutdown_task.cancel()
     self.closing.set()
     await asyncio.sleep(
         3)  # give apps time to receive and process information
     self.log.debug("Client removed from client list")
     try:
         _getNetwork().clients.pop(self.client_id)
     except KeyError:
         pass
     # self.log.debug("Client list: {!s}".format(_getNetwork().clients))
     self._removed = True
Exemple #2
0
 async def awaitConnection(self, timeout=math.inf):
     if timeout is None:
         timeout = math.inf
     if self.client_id is None:
         raise ValueError("Can't wait for client with id None")
     st = time.time()
     while time.time() - st < timeout:
         while self.client_id in _getNetwork().clients:
             if time.time() - st > timeout:
                 break
             try:
                 await asyncio.wait_for(
                     _getNetwork().clients[self.client_id].connected.wait(),
                     1)
             except asyncio.TimeoutError:
                 continue
             return True
         while self.client_id not in _getNetwork().clients:
             if time.time() - st > timeout:
                 break
             try:
                 await asyncio.wait_for(_getNetwork().new_client.wait(), 1)
             except asyncio.TimeoutError:
                 continue
     raise asyncio.TimeoutError("Timeout waiting for client connection")
Exemple #3
0
 def is_connected(self):
     if self.client_id is None:
         raise ValueError(
             "Can't check connection state for Client with id None")
     if self.client_id in _getNetwork().clients:
         if _getNetwork().clients[self.client_id].connected.is_set():
             return True
     return False
Exemple #4
0
def getClient(client_id, *args, **kwargs) -> Client:
    if _getNetwork() is None:
        raise TypeError("No network initialized")
    if client_id in _getNetwork().clients:
        return _getNetwork().clients[client_id]
    client = Client(client_id, *args, **kwargs)
    # basically a future client object without a transport/socket
    _getNetwork().clients[client_id] = client
    return client
Exemple #5
0
 async def _awaitConnection(self, client_id, timeout=math.inf):
     if timeout is None:
         timeout = math.inf
     st = time.time()
     while time.time() - st < timeout:
         if self._clientsInList(client_id):
             try:
                 await asyncio.wait_for(_getNetwork().clients[client_id].connected.wait(), 1)
             except asyncio.TimeoutError:
                 continue
             return True
         else:
             try:
                 await asyncio.wait_for(_getNetwork().new_client.wait(), 1)
             except asyncio.TimeoutError:
                 continue
     raise asyncio.TimeoutError("Timeout waiting for client connection")
Exemple #6
0
 async def awaitConnection(self, timeout=math.inf):
     if timeout is None:
         timeout = math.inf
     st = time.time()
     while time.time() - st < timeout:
         if self._clientsInList(self.client_ids):
             tasks = []
             for client_id in self.client_ids:
                 tasks.append(_getNetwork().clients[client_id].connected.wait())
             try:
                 await asyncio.wait_for(asyncio.gather(*tasks), 1)
             except asyncio.TimeoutError:
                 continue
             return True
         else:
             try:
                 await asyncio.wait_for(_getNetwork().new_client.wait(), 1)
             except asyncio.TimeoutError:
                 continue
     raise asyncio.TimeoutError("Timeout waiting for client connections")
Exemple #7
0
 async def _await_shutdown(self):
     # self.log.debug("Started awaiting network shutdown")
     while self._removed is False:
         try:
             await asyncio.wait_for(_getNetwork().shutdown_requested.wait(),
                                    1)
         except asyncio.TimeoutError:
             continue
         self.log.debug("Received shutdown signal")
         self.closing.set()
         if self.await_client_timeout_task is not None and not self.await_client_timeout_task.done(
         ):
             self.await_client_timeout_task.cancel()
         await asyncio.sleep(3)
         await self.stop()
         try:
             _getNetwork().clients.pop(self.client_id)
         except KeyError:
             pass
         # self.log.debug("Client list: {!s}".format(_getNetwork().clients))
         self._removed = True
         return
     self.log.debug("await shutdown, client already removed")
Exemple #8
0
 def _getClient(client_id) -> Client:
     if client_id in _getNetwork().clients:
         return _getNetwork().clients[client_id]
     else:
         raise IndexError("Client does not exist")
Exemple #9
0
 def _clientsInList(client_ids):
     for client in client_ids:
         if client not in _getNetwork().clients:
             return False
     return True