Improve reconnecting ability

And increase timeout to 5 seconds.
This commit is contained in:
Alexei Sorokin 2017-06-14 21:02:26 +03:00
parent fd078c43ad
commit 30e84cc7e6
1 changed files with 30 additions and 10 deletions

View File

@ -7,7 +7,6 @@ import os
import re import re
import signal import signal
import sys import sys
import time
import types import types
import slixmpp import slixmpp
@ -84,13 +83,16 @@ class HptoadPlugin:
class Hptoad: class Hptoad:
plugins = {} plugins = {}
def __init__(self, opts): def __init__(self, opts, timeout=5.0):
self.client = slixmpp.ClientXMPP("%s/%s" % (opts["jid"], self.client = slixmpp.ClientXMPP("%s/%s" % (opts["jid"],
opts["resource"]), opts["resource"]),
opts["password"]) opts["password"])
self.client.register_plugin("xep_0199") # XMPP Ping. self.client.register_plugin("xep_0199") # XMPP Ping.
self.client.register_plugin("xep_0045") # XMPP MUC. self.client.register_plugin("xep_0045") # XMPP MUC.
self.muc_obj = self.client.plugin["xep_0045"] self.muc_obj = self.client.plugin["xep_0045"]
self.muc_is_joined = False
self.timeout = timeout
self.logger = logging.getLogger(self.__class__.__name__) self.logger = logging.getLogger(self.__class__.__name__)
self.logger.addHandler(logging.NullHandler()) self.logger.addHandler(logging.NullHandler())
@ -106,7 +108,8 @@ class Hptoad:
self.client.add_event_handler("failed_all_auth", self.client.add_event_handler("failed_all_auth",
self.on_failed_all_auth) self.on_failed_all_auth)
self.client.add_event_handler("session_start", self.on_session_start) self.client.add_event_handler("session_start", self.on_session_start)
self.client.add_event_handler("session_end", self.on_session_end) self.client.add_event_handler("got_online", self.on_got_online)
self.client.add_event_handler("disconnected", self.on_disconnected)
self.client.add_event_handler("message", self.on_message) self.client.add_event_handler("message", self.on_message)
self.client.add_event_handler("muc::%s::presence" % self.muc, self.client.add_event_handler("muc::%s::presence" % self.muc,
self.on_muc_presence) self.on_muc_presence)
@ -126,8 +129,14 @@ class Hptoad:
self.client.connect(connect) self.client.connect(connect)
def join_muc(self): @asyncio.coroutine
def join_muc_loop(self):
while not self.muc_is_joined:
self.muc_obj.join_muc(self.muc, self.bot_nick) self.muc_obj.join_muc(self.muc, self.bot_nick)
yield from asyncio.sleep(self.timeout)
def join_muc(self):
asyncio.async(self.join_muc_loop())
def log_exception(self, ex): def log_exception(self, ex):
self.logger.error("%s: %s" % (type(ex).__name__, str(ex))) self.logger.error("%s: %s" % (type(ex).__name__, str(ex)))
@ -242,13 +251,19 @@ class Hptoad:
sys.exit(1) sys.exit(1)
def on_session_start(self, event): def on_session_start(self, event):
self.client.get_roster()
self.client.send_presence(pstatus="is there some food in this world?", self.client.send_presence(pstatus="is there some food in this world?",
ppriority=12) ppriority=12)
self.client.get_roster()
def on_got_online(self, event):
self.join_muc() self.join_muc()
def on_session_end(self, event): @asyncio.coroutine
time.sleep(2.0) def on_disconnected(self, event):
self.muc_is_joined = False
self.logger.error("Conn: Connection lost, reattempting in %d seconds" %
self.timeout)
yield from asyncio.sleep(self.timeout)
self.connect() self.connect()
@asyncio.coroutine @asyncio.coroutine
@ -274,6 +289,7 @@ class Hptoad:
except Exception as e: except Exception as e:
self.log_exception(e) self.log_exception(e)
@asyncio.coroutine
def on_muc_presence(self, event): def on_muc_presence(self, event):
try: try:
typ = event["muc"]["type"] typ = event["muc"]["type"]
@ -285,15 +301,20 @@ class Hptoad:
if not nick: if not nick:
nick = self.muc_obj.get_nick(self.muc, from_id) nick = self.muc_obj.get_nick(self.muc, from_id)
if typ == "error": if typ == "available":
self.muc_is_joined = True
elif typ == "error":
self.muc_is_joined = False
if event["error"]["code"] == "409": if event["error"]["code"] == "409":
self.bot_nick = self.bot_nick + "_" self.bot_nick = self.bot_nick + "_"
self.join_muc() self.join_muc()
elif typ == "unavailable": elif typ == "unavailable":
if nick == self.bot_nick: if nick == self.bot_nick:
self.muc_is_joined = False
self.bot_nick = self.pure_bot_nick self.bot_nick = self.pure_bot_nick
time.sleep(0.5) yield from asyncio.sleep(0.5)
self.join_muc() self.join_muc()
except Exception as e: except Exception as e:
self.log_exception(e) self.log_exception(e)
@ -347,4 +368,3 @@ if __name__ == "__main__":
while True: while True:
hptoad.run() hptoad.run()
logging.error("Unknown: WTF am I doing here?") logging.error("Unknown: WTF am I doing here?")
time.sleep(0.5)