2023-12-28 15:50:23 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2024-01-24 19:11:39 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
TODO
|
|
|
|
|
|
|
|
1) Check interval, and if no connection is establish after 30 seconds
|
|
|
|
then disconnect and reconnect again.
|
|
|
|
|
|
|
|
2) or Check ping, and if no ping is received after 30 seconds then
|
|
|
|
disconnect and try to reconnect again.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2024-02-04 18:08:12 +01:00
|
|
|
import asyncio
|
2024-06-19 22:28:17 +02:00
|
|
|
from slixfeed.log import Logger, Message
|
2024-02-04 18:08:12 +01:00
|
|
|
from slixmpp.exceptions import IqTimeout, IqError
|
2023-12-28 15:50:23 +01:00
|
|
|
from time import sleep
|
2024-06-13 17:53:53 +02:00
|
|
|
|
|
|
|
logger = Logger(__name__)
|
2023-12-28 15:50:23 +01:00
|
|
|
|
|
|
|
|
2024-02-07 01:26:42 +01:00
|
|
|
class XmppConnect:
|
|
|
|
|
|
|
|
|
|
|
|
async def ping(self, jid=None):
|
|
|
|
"""
|
|
|
|
Check for ping and disconnect if no ping has been received.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
jid : str, optional
|
|
|
|
Jabber ID. The default is None.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
None.
|
|
|
|
"""
|
2024-03-12 18:13:01 +01:00
|
|
|
jid_from = str(self.boundjid) if self.is_component else None
|
2024-02-07 01:26:42 +01:00
|
|
|
if not jid:
|
|
|
|
jid = self.boundjid.bare
|
|
|
|
while True:
|
|
|
|
rtt = None
|
|
|
|
try:
|
2024-03-12 18:13:01 +01:00
|
|
|
rtt = await self['xep_0199'].ping(jid,
|
|
|
|
ifrom=jid_from,
|
|
|
|
timeout=10)
|
2024-06-13 17:53:53 +02:00
|
|
|
logger.info('Success! RTT: %s', rtt)
|
2024-02-07 01:26:42 +01:00
|
|
|
except IqError as e:
|
2024-06-13 17:53:53 +02:00
|
|
|
logger.error('Error pinging %s: %s', jid,
|
2024-03-12 18:13:01 +01:00
|
|
|
e.iq['error']['condition'])
|
2024-02-07 01:26:42 +01:00
|
|
|
except IqTimeout:
|
2024-06-13 17:53:53 +02:00
|
|
|
logger.warning('No response from %s', jid)
|
2024-02-07 01:26:42 +01:00
|
|
|
if not rtt:
|
2024-06-13 17:53:53 +02:00
|
|
|
logger.warning('Disconnecting...')
|
2024-02-07 01:26:42 +01:00
|
|
|
self.disconnect()
|
2024-02-19 21:50:53 +01:00
|
|
|
break
|
2024-02-07 01:26:42 +01:00
|
|
|
await asyncio.sleep(60 * 1)
|
|
|
|
|
|
|
|
|
2024-02-19 21:50:53 +01:00
|
|
|
def recover(self, message):
|
2024-06-13 17:53:53 +02:00
|
|
|
logger.warning(message)
|
2024-06-19 22:28:17 +02:00
|
|
|
Message.printer('Slixfeed ittempting to reconnect...')
|
2024-02-07 01:26:42 +01:00
|
|
|
self.connection_attempts += 1
|
|
|
|
# if self.connection_attempts <= self.max_connection_attempts:
|
|
|
|
# self.reconnect(wait=5.0) # wait a bit before attempting to reconnect
|
|
|
|
# else:
|
|
|
|
# print(current_time(),"Maximum connection attempts exceeded.")
|
|
|
|
# logging.error("Maximum connection attempts exceeded.")
|
2024-06-19 22:28:17 +02:00
|
|
|
Message.printer('Attempt number {}'.format(self.connection_attempts))
|
2024-03-12 18:13:01 +01:00
|
|
|
seconds = self.reconnect_timeout or 30
|
2024-02-07 01:26:42 +01:00
|
|
|
seconds = int(seconds)
|
2024-06-19 22:28:17 +02:00
|
|
|
Message.printer('Next attempt be made within {} seconds'.format(seconds))
|
2024-02-07 01:26:42 +01:00
|
|
|
# NOTE asyncio.sleep doesn't interval as expected
|
|
|
|
# await asyncio.sleep(seconds)
|
|
|
|
sleep(seconds)
|
|
|
|
self.reconnect(wait=5.0)
|
|
|
|
|
|
|
|
|
2024-02-19 21:50:53 +01:00
|
|
|
def inspect(self):
|
2024-06-19 22:28:17 +02:00
|
|
|
Message.printer('Disconnected')
|
|
|
|
sleep(3)
|
|
|
|
Message.printer('Reconnecting...')
|
2024-02-04 18:08:12 +01:00
|
|
|
try:
|
2024-02-07 01:26:42 +01:00
|
|
|
self.reconnect
|
|
|
|
except:
|
2024-02-04 18:08:12 +01:00
|
|
|
self.disconnect()
|
2024-06-19 22:28:17 +02:00
|
|
|
Message.printer('Problem reconnecting')
|
2024-06-13 17:53:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
class XmppConnectTask:
|
|
|
|
|
|
|
|
|
|
|
|
def ping(self):
|
|
|
|
try:
|
|
|
|
self.task_ping_instance.cancel()
|
|
|
|
except:
|
|
|
|
logger.info('No ping task to cancel.')
|
|
|
|
self.task_ping_instance = asyncio.create_task(XmppConnect.ping(self))
|