Updates for XMPP

Updates for XMPP, seems to fix an issue with holding the rest of the asyncio loops hostage after a disconnect.
This commit is contained in:
Brian Martin 2019-05-18 15:55:00 -04:00
parent d238ded6c1
commit 9509163464

View file

@ -41,8 +41,8 @@ class XMPPServer:
def client_done(task): def client_done(task):
del self.aclients[task] del self.aclients[task]
xmppserverlog.info("End Connection for {}".format(client_writer.get_extra_info("peername")))
client_writer.close() client_writer.close()
xmppserverlog.info("End Connection")
clientaddr = client_writer.get_extra_info("peername") clientaddr = client_writer.get_extra_info("peername")
xmppserverlog.info("New Connection from {}".format(clientaddr)) xmppserverlog.info("New Connection from {}".format(clientaddr))
@ -197,24 +197,29 @@ class XMPPAsyncClient:
self.devclass = "" self.devclass = ""
self.bumper_jid = "" self.bumper_jid = ""
self.uid = "" self.uid = ""
self.log_sent_message = False # Set to true to log sends self.log_sent_message = True # Set to true to log sends
self.log_incoming_data = True # Set to true to log sends self.log_incoming_data = True # Set to true to log sends
xmppserverlog.debug("new client with ip {}".format(self.address)) xmppserverlog.debug("new client with ip {}".format(self.address))
async def handle_async_client(self): async def handle_async_client(self):
# xmppserverlog.info('client connected - {}'.format(self.address)) # xmppserverlog.info('client connected - {}'.format(self.address))
#await self._set_state("READY")
await self._set_state("CONNECT") await self._set_state("CONNECT")
pingtask = asyncio.Task(self.send_ping(30)) #asyncio.Task(self.send_ping(30))
while not self.state == self.DISCONNECT: while True:
data = await self.client_reader.read(4096) await asyncio.sleep(0.1)
# data = await asyncio.wait_for(client_reader.readline(), timeout=10.0) if not self.state == self.DISCONNECT:
if data is None: data = await self.client_reader.read(4096)
xmppserverlog.warning("Received no data") # data = await asyncio.wait_for(client_reader.readline(), timeout=10.0)
# exit loop and disconnect if data is None:
return xmppserverlog.warning("Received no data")
# exit loop and disconnect
return
await self._parse_data(data) await self._parse_data(data)
else:
break
# exit loop and disconnect # exit loop and disconnect
return return
@ -303,6 +308,24 @@ class XMPPAsyncClient:
) )
return return
if "disco#items" in data:
# Return not-implemented for disco#items
await self.send(
'<iq type="error" id="{}"><error type="cancel" code="501"><feature-not-implemented xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></iq>'.format(
xml.get("id")
))
return
if "disco#info" in data:
# Return not-implemented for disco#info
await self.send(
'<iq type="error" id="{}"><error type="cancel" code="501"><feature-not-implemented xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></iq>'.format(
xml.get("id")
)
)
return
if xml.get("type") == "set": if xml.get("type") == "set":
if ( if (
"com:sf" in data and xml.get("to") == "rl.ecorobot.net" "com:sf" in data and xml.get("to") == "rl.ecorobot.net"
@ -316,7 +339,7 @@ class XMPPAsyncClient:
) )
) )
if xml[0][0]: if len(xml[0]) > 0:
ctl = xml[0][0] ctl = xml[0][0]
if ctl.get("admin") and self.type == self.BOT: if ctl.get("admin") and self.type == self.BOT:
xmppserverlog.debug( xmppserverlog.debug(
@ -580,6 +603,7 @@ class XMPPAsyncClient:
xmlauth = xml[0].getchildren() xmlauth = xml[0].getchildren()
# uid = "" # uid = ""
password = "" password = ""
authcode = ""
resource = "" resource = ""
for aitem in xmlauth: for aitem in xmlauth:
if "username" in aitem.tag: if "username" in aitem.tag:
@ -655,6 +679,7 @@ class XMPPAsyncClient:
saslauth = base64.b64decode(xml.text).decode("utf-8").split("/") saslauth = base64.b64decode(xml.text).decode("utf-8").split("/")
username = saslauth[0] username = saslauth[0]
username = saslauth[0].split("\x00")[1] username = saslauth[0].split("\x00")[1]
authcode = ""
self.uid = username self.uid = username
if len(saslauth) > 1: if len(saslauth) > 1:
resource = saslauth[1] resource = saslauth[1]
@ -763,6 +788,8 @@ class XMPPAsyncClient:
res = '<iq type="result" id="{}" />'.format(xml.get("id")) res = '<iq type="result" id="{}" />'.format(xml.get("id"))
await self._set_state("READY") await self._set_state("READY")
await self.send(res) await self.send(res)
asyncio.Task(self.send_ping(30))
except Exception as e: except Exception as e:
xmppserverlog.exception("{}".format(e)) xmppserverlog.exception("{}".format(e))
@ -781,6 +808,8 @@ class XMPPAsyncClient:
'<presence to="{}"> dummy </presence>'.format(self.bumper_jid) '<presence to="{}"> dummy </presence>'.format(self.bumper_jid)
) )
# If it is a BOT, send extras # If it is a BOT, send extras
if self.type == self.BOT: if self.type == self.BOT:
# get device info # get device info
@ -790,6 +819,8 @@ class XMPPAsyncClient:
) )
) )
else: else:
xmppserverlog.debug( xmppserverlog.debug(
"client presence - {} ".format(ET.tostring(xml, encoding="utf-8")) "client presence - {} ".format(ET.tostring(xml, encoding="utf-8"))