Improve reconnecting ability
And increase timeout to 5 seconds.
This commit is contained in:
parent
fd078c43ad
commit
30e84cc7e6
40
hptoad.py
40
hptoad.py
|
@ -7,7 +7,6 @@ import os
|
|||
import re
|
||||
import signal
|
||||
import sys
|
||||
import time
|
||||
import types
|
||||
import slixmpp
|
||||
|
||||
|
@ -84,13 +83,16 @@ class HptoadPlugin:
|
|||
class Hptoad:
|
||||
plugins = {}
|
||||
|
||||
def __init__(self, opts):
|
||||
def __init__(self, opts, timeout=5.0):
|
||||
self.client = slixmpp.ClientXMPP("%s/%s" % (opts["jid"],
|
||||
opts["resource"]),
|
||||
opts["password"])
|
||||
self.client.register_plugin("xep_0199") # XMPP Ping.
|
||||
self.client.register_plugin("xep_0045") # XMPP MUC.
|
||||
|
||||
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.addHandler(logging.NullHandler())
|
||||
|
@ -106,7 +108,8 @@ class Hptoad:
|
|||
self.client.add_event_handler("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_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("muc::%s::presence" % self.muc,
|
||||
self.on_muc_presence)
|
||||
|
@ -126,8 +129,14 @@ class Hptoad:
|
|||
|
||||
self.client.connect(connect)
|
||||
|
||||
@asyncio.coroutine
|
||||
def join_muc_loop(self):
|
||||
while not self.muc_is_joined:
|
||||
self.muc_obj.join_muc(self.muc, self.bot_nick)
|
||||
yield from asyncio.sleep(self.timeout)
|
||||
|
||||
def join_muc(self):
|
||||
self.muc_obj.join_muc(self.muc, self.bot_nick)
|
||||
asyncio.async(self.join_muc_loop())
|
||||
|
||||
def log_exception(self, ex):
|
||||
self.logger.error("%s: %s" % (type(ex).__name__, str(ex)))
|
||||
|
@ -242,13 +251,19 @@ class Hptoad:
|
|||
sys.exit(1)
|
||||
|
||||
def on_session_start(self, event):
|
||||
self.client.get_roster()
|
||||
self.client.send_presence(pstatus="is there some food in this world?",
|
||||
ppriority=12)
|
||||
self.client.get_roster()
|
||||
|
||||
def on_got_online(self, event):
|
||||
self.join_muc()
|
||||
|
||||
def on_session_end(self, event):
|
||||
time.sleep(2.0)
|
||||
@asyncio.coroutine
|
||||
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()
|
||||
|
||||
@asyncio.coroutine
|
||||
|
@ -274,6 +289,7 @@ class Hptoad:
|
|||
except Exception as e:
|
||||
self.log_exception(e)
|
||||
|
||||
@asyncio.coroutine
|
||||
def on_muc_presence(self, event):
|
||||
try:
|
||||
typ = event["muc"]["type"]
|
||||
|
@ -285,15 +301,20 @@ class Hptoad:
|
|||
if not nick:
|
||||
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":
|
||||
self.bot_nick = self.bot_nick + "_"
|
||||
self.join_muc()
|
||||
|
||||
elif typ == "unavailable":
|
||||
if nick == self.bot_nick:
|
||||
self.muc_is_joined = False
|
||||
self.bot_nick = self.pure_bot_nick
|
||||
time.sleep(0.5)
|
||||
yield from asyncio.sleep(0.5)
|
||||
self.join_muc()
|
||||
except Exception as e:
|
||||
self.log_exception(e)
|
||||
|
@ -347,4 +368,3 @@ if __name__ == "__main__":
|
|||
while True:
|
||||
hptoad.run()
|
||||
logging.error("Unknown: WTF am I doing here?")
|
||||
time.sleep(0.5)
|
||||
|
|
Loading…
Reference in New Issue