diff options
author | Lance Stout <lancestout@gmail.com> | 2010-11-17 02:00:20 -0500 |
---|---|---|
committer | Lance Stout <lancestout@gmail.com> | 2010-11-17 02:01:12 -0500 |
commit | 26aca2b789f62123819041229ba40ab10268f49e (patch) | |
tree | 20388fb0559ed85f7143eb3b8a669142cdc43f6b /sleekxmpp/plugins/xep_0199.py | |
parent | 4f69a03bb1a8caab7899bd04f97b7975a78f2f34 (diff) | |
parent | 5424ede413393db5b835686e8544a9b703fb113b (diff) | |
download | slixmpp-26aca2b789f62123819041229ba40ab10268f49e.tar.gz slixmpp-26aca2b789f62123819041229ba40ab10268f49e.tar.bz2 slixmpp-26aca2b789f62123819041229ba40ab10268f49e.tar.xz slixmpp-26aca2b789f62123819041229ba40ab10268f49e.zip |
Merge branch 'roster' of github.com:fritzy/SleekXMPP into roster
Conflicts:
sleekxmpp/basexmpp.py
sleekxmpp/roster.py
sleekxmpp/test/sleektest.py
tests/test_stream_presence.py
tests/test_stream_roster.py
Diffstat (limited to 'sleekxmpp/plugins/xep_0199.py')
-rw-r--r-- | sleekxmpp/plugins/xep_0199.py | 98 |
1 files changed, 51 insertions, 47 deletions
diff --git a/sleekxmpp/plugins/xep_0199.py b/sleekxmpp/plugins/xep_0199.py index 3fc62f55..2e99ae76 100644 --- a/sleekxmpp/plugins/xep_0199.py +++ b/sleekxmpp/plugins/xep_0199.py @@ -2,7 +2,7 @@ SleekXMPP: The Sleek XMPP Library Copyright (C) 2010 Nathanael C. Fritz This file is part of SleekXMPP. - + See the file LICENSE for copying permission. """ from xml.etree import cElementTree as ET @@ -10,50 +10,54 @@ from . import base import time import logging + +log = logging.getLogger(__name__) + + class xep_0199(base.base_plugin): - """XEP-0199 XMPP Ping""" - - def plugin_init(self): - self.description = "XMPP Ping" - self.xep = "0199" - self.xmpp.add_handler("<iq type='get' xmlns='%s'><ping xmlns='http://www.xmpp.org/extensions/xep-0199.html#ns'/></iq>" % self.xmpp.default_ns, self.handler_ping, name='XMPP Ping') - self.running = False - #if self.config.get('keepalive', True): - #self.xmpp.add_event_handler('session_start', self.handler_pingserver, threaded=True) - - def post_init(self): - base.base_plugin.post_init(self) - self.xmpp.plugin['xep_0030'].add_feature('http://www.xmpp.org/extensions/xep-0199.html#ns') - - def handler_pingserver(self, xml): - if not self.running: - time.sleep(self.config.get('frequency', 300)) - while self.sendPing(self.xmpp.server, self.config.get('timeout', 30)) is not False: - time.sleep(self.config.get('frequency', 300)) - logging.debug("Did not recieve ping back in time. Requesting Reconnect.") - self.xmpp.disconnect(reconnect=True) - - def handler_ping(self, xml): - iq = self.xmpp.makeIqResult(xml.get('id', 'unknown')) - iq.attrib['to'] = xml.get('from', self.xmpp.server) - self.xmpp.send(iq) - - def sendPing(self, jid, timeout = 30): - """ sendPing(jid, timeout) - Sends a ping to the specified jid, returning the time (in seconds) - to receive a reply, or None if no reply is received in timeout seconds. - """ - id = self.xmpp.getNewId() - iq = self.xmpp.makeIq(id) - iq.attrib['type'] = 'get' - iq.attrib['to'] = jid - ping = ET.Element('{http://www.xmpp.org/extensions/xep-0199.html#ns}ping') - iq.append(ping) - startTime = time.clock() - #pingresult = self.xmpp.send(iq, self.xmpp.makeIq(id), timeout) - pingresult = iq.send() - endTime = time.clock() - if pingresult == False: - #self.xmpp.disconnect(reconnect=True) - return False - return endTime - startTime + """XEP-0199 XMPP Ping""" + + def plugin_init(self): + self.description = "XMPP Ping" + self.xep = "0199" + self.xmpp.add_handler("<iq type='get' xmlns='%s'><ping xmlns='urn:xmpp:ping'/></iq>" % self.xmpp.default_ns, self.handler_ping, name='XMPP Ping') + if self.config.get('keepalive', True): + self.xmpp.add_event_handler('session_start', self.handler_pingserver, threaded=True) + + def post_init(self): + base.base_plugin.post_init(self) + self.xmpp.plugin['xep_0030'].add_feature('urn:xmpp:ping') + + def handler_pingserver(self, xml): + self.xmpp.schedule("xep-0119 ping", float(self.config.get('frequency', 300)), self.scheduled_ping, repeat=True) + + def scheduled_ping(self): + log.debug("pinging...") + if self.sendPing(self.xmpp.server, self.config.get('timeout', 30)) is False: + log.debug("Did not recieve ping back in time. Requesting Reconnect.") + self.xmpp.reconnect() + + def handler_ping(self, xml): + iq = self.xmpp.makeIqResult(xml.get('id', 'unknown')) + iq.attrib['to'] = xml.get('from', self.xmpp.boundjid.domain) + self.xmpp.send(iq) + + def sendPing(self, jid, timeout = 30): + """ sendPing(jid, timeout) + Sends a ping to the specified jid, returning the time (in seconds) + to receive a reply, or None if no reply is received in timeout seconds. + """ + id = self.xmpp.getNewId() + iq = self.xmpp.makeIq(id) + iq.attrib['type'] = 'get' + iq.attrib['to'] = jid + ping = ET.Element('{urn:xmpp:ping}ping') + iq.append(ping) + startTime = time.clock() + #pingresult = self.xmpp.send(iq, self.xmpp.makeIq(id), timeout) + pingresult = iq.send() + endTime = time.clock() + if pingresult == False: + #self.xmpp.disconnect(reconnect=True) + return False + return endTime - startTime |