2011-02-11 05:30:45 +00:00
|
|
|
"""
|
|
|
|
SleekXMPP: The Sleek XMPP Library
|
|
|
|
Copyright (C) 2010 Nathanael C. Fritz
|
|
|
|
This file is part of SleekXMPP.
|
|
|
|
|
|
|
|
See the file LICENSE for copying permission.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import time
|
|
|
|
import logging
|
|
|
|
|
2013-01-21 04:14:16 +00:00
|
|
|
from sleekxmpp.jid import JID
|
|
|
|
from sleekxmpp.stanza import Iq
|
2011-08-18 04:30:47 +00:00
|
|
|
from sleekxmpp.exceptions import IqError, IqTimeout
|
2011-02-11 05:30:45 +00:00
|
|
|
from sleekxmpp.xmlstream import register_stanza_plugin
|
|
|
|
from sleekxmpp.xmlstream.matcher import StanzaPath
|
|
|
|
from sleekxmpp.xmlstream.handler import Callback
|
2012-03-12 07:18:33 +00:00
|
|
|
from sleekxmpp.plugins import BasePlugin
|
2011-02-11 05:30:45 +00:00
|
|
|
from sleekxmpp.plugins.xep_0199 import stanza, Ping
|
|
|
|
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2012-03-12 07:18:33 +00:00
|
|
|
class XEP_0199(BasePlugin):
|
2011-02-11 05:30:45 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
XEP-0199: XMPP Ping
|
|
|
|
|
|
|
|
Given that XMPP is based on TCP connections, it is possible for the
|
|
|
|
underlying connection to be terminated without the application's
|
|
|
|
awareness. Ping stanzas provide an alternative to whitespace based
|
|
|
|
keepalive methods for detecting lost connections.
|
|
|
|
|
|
|
|
Also see <http://www.xmpp.org/extensions/xep-0199.html>.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
keepalive -- If True, periodically send ping requests
|
|
|
|
to the server. If a ping is not answered,
|
|
|
|
the connection will be reset.
|
2013-01-21 04:14:16 +00:00
|
|
|
interval -- Time in seconds between keepalive pings.
|
2011-02-11 05:30:45 +00:00
|
|
|
Defaults to 300 seconds.
|
|
|
|
timeout -- Time in seconds to wait for a ping response.
|
|
|
|
Defaults to 30 seconds.
|
|
|
|
Methods:
|
|
|
|
send_ping -- Send a ping to a given JID, returning the
|
|
|
|
round trip time.
|
|
|
|
"""
|
|
|
|
|
2012-03-12 07:18:33 +00:00
|
|
|
name = 'xep_0199'
|
|
|
|
description = 'XEP-0199: XMPP Ping'
|
|
|
|
dependencies = set(['xep_0030'])
|
|
|
|
stanza = stanza
|
2012-07-27 06:04:16 +00:00
|
|
|
default_config = {
|
|
|
|
'keepalive': False,
|
2013-01-21 04:14:16 +00:00
|
|
|
'interval': 300,
|
2012-07-27 06:04:16 +00:00
|
|
|
'timeout': 30
|
|
|
|
}
|
2012-03-12 07:18:33 +00:00
|
|
|
|
2011-02-11 05:30:45 +00:00
|
|
|
def plugin_init(self):
|
|
|
|
"""
|
|
|
|
Start the XEP-0199 plugin.
|
|
|
|
"""
|
2013-01-21 04:14:16 +00:00
|
|
|
|
2011-02-11 05:30:45 +00:00
|
|
|
register_stanza_plugin(Iq, Ping)
|
|
|
|
|
|
|
|
self.xmpp.register_handler(
|
|
|
|
Callback('Ping',
|
|
|
|
StanzaPath('iq@type=get/ping'),
|
|
|
|
self._handle_ping))
|
|
|
|
|
|
|
|
if self.keepalive:
|
|
|
|
self.xmpp.add_event_handler('session_start',
|
2013-01-21 04:14:16 +00:00
|
|
|
self.enable_keepalive,
|
2011-02-11 05:30:45 +00:00
|
|
|
threaded=True)
|
2011-09-01 22:51:43 +00:00
|
|
|
self.xmpp.add_event_handler('session_end',
|
2013-01-21 04:14:16 +00:00
|
|
|
self.disable_keepalive)
|
2011-02-11 05:30:45 +00:00
|
|
|
|
2012-07-10 08:37:44 +00:00
|
|
|
def plugin_end(self):
|
|
|
|
self.xmpp['xep_0030'].del_feature(feature=Ping.namespace)
|
|
|
|
self.xmpp.remove_handler('Ping')
|
|
|
|
if self.keepalive:
|
|
|
|
self.xmpp.del_event_handler('session_start',
|
2013-01-21 04:14:16 +00:00
|
|
|
self.enable_keepalive)
|
2012-07-10 08:37:44 +00:00
|
|
|
self.xmpp.del_event_handler('session_end',
|
2013-01-21 04:14:16 +00:00
|
|
|
self.disable_keepalive)
|
2012-07-10 08:37:44 +00:00
|
|
|
|
|
|
|
def session_bind(self, jid):
|
2011-02-11 05:30:45 +00:00
|
|
|
self.xmpp['xep_0030'].add_feature(Ping.namespace)
|
|
|
|
|
2013-01-21 04:14:16 +00:00
|
|
|
def enable_keepalive(self, interval=None, timeout=None):
|
|
|
|
if interval:
|
|
|
|
self.interval = interval
|
|
|
|
if timeout:
|
|
|
|
self.timeout = timeout
|
2011-02-11 05:30:45 +00:00
|
|
|
|
2013-01-21 04:14:16 +00:00
|
|
|
self.keepalive = True
|
|
|
|
self.xmpp.schedule('Ping keepalive',
|
|
|
|
self.interval,
|
|
|
|
self._keepalive,
|
2011-02-11 05:30:45 +00:00
|
|
|
repeat=True)
|
|
|
|
|
2013-01-21 04:14:16 +00:00
|
|
|
def disable_keepalive(self, event=None):
|
|
|
|
self.xmpp.scheduler.remove('Ping keepalive')
|
2011-09-01 22:51:43 +00:00
|
|
|
|
2013-01-26 22:12:44 +00:00
|
|
|
def _keepalive(self, event=None):
|
2013-01-21 04:14:16 +00:00
|
|
|
log.debug("Keepalive ping...")
|
|
|
|
try:
|
2013-03-07 05:35:07 +00:00
|
|
|
rtt = self.ping(self.xmpp.boundjid.host, timeout=self.timeout)
|
2013-01-21 04:14:16 +00:00
|
|
|
except IqTimeout:
|
|
|
|
log.debug("Did not recieve ping back in time." + \
|
|
|
|
"Requesting Reconnect.")
|
|
|
|
self.xmpp.reconnect()
|
|
|
|
else:
|
|
|
|
log.debug('Keepalive RTT: %s' % rtt)
|
2011-02-11 05:30:45 +00:00
|
|
|
|
2013-01-21 04:14:16 +00:00
|
|
|
def _handle_ping(self, iq):
|
|
|
|
"""Automatically reply to ping requests."""
|
2011-11-19 20:07:57 +00:00
|
|
|
log.debug("Pinged by %s", iq['from'])
|
2011-08-10 16:05:43 +00:00
|
|
|
iq.reply().send()
|
2011-02-11 05:30:45 +00:00
|
|
|
|
2013-01-21 04:14:16 +00:00
|
|
|
def send_ping(self, jid, ifrom=None, block=True, timeout=None, callback=None):
|
|
|
|
"""Send a ping request.
|
2011-02-11 05:30:45 +00:00
|
|
|
|
|
|
|
Arguments:
|
|
|
|
jid -- The JID that will receive the ping.
|
|
|
|
ifrom -- Specifiy the sender JID.
|
|
|
|
block -- Indicate if execution should block until
|
|
|
|
a pong response is received. Defaults
|
|
|
|
to True.
|
2013-01-21 04:14:16 +00:00
|
|
|
timeout -- Time in seconds to wait for a response.
|
|
|
|
Defaults to self.timeout.
|
2011-02-11 05:30:45 +00:00
|
|
|
callback -- Optional handler to execute when a pong
|
|
|
|
is received. Useful in conjunction with
|
|
|
|
the option block=False.
|
|
|
|
"""
|
2013-01-21 04:14:16 +00:00
|
|
|
if not timeout:
|
2011-02-11 05:30:45 +00:00
|
|
|
timeout = self.timeout
|
|
|
|
|
|
|
|
iq = self.xmpp.Iq()
|
|
|
|
iq['type'] = 'get'
|
|
|
|
iq['to'] = jid
|
2011-08-27 05:04:06 +00:00
|
|
|
iq['from'] = ifrom
|
2011-02-11 05:30:45 +00:00
|
|
|
iq.enable('ping')
|
|
|
|
|
2013-01-21 04:14:16 +00:00
|
|
|
return iq.send(block=block, timeout=timeout, callback=callback)
|
2011-08-18 04:30:47 +00:00
|
|
|
|
2013-01-21 04:14:16 +00:00
|
|
|
def ping(self, jid=None, ifrom=None, timeout=None):
|
|
|
|
"""Send a ping request and calculate RTT.
|
2011-08-18 04:30:47 +00:00
|
|
|
|
2013-01-21 04:14:16 +00:00
|
|
|
Arguments:
|
|
|
|
jid -- The JID that will receive the ping.
|
|
|
|
ifrom -- Specifiy the sender JID.
|
|
|
|
timeout -- Time in seconds to wait for a response.
|
|
|
|
Defaults to self.timeout.
|
|
|
|
"""
|
|
|
|
if not jid:
|
|
|
|
if self.xmpp.is_component:
|
|
|
|
jid = self.xmpp.server
|
|
|
|
else:
|
|
|
|
jid = self.xmpp.boundjid.host
|
|
|
|
jid = JID(jid)
|
|
|
|
if jid == self.xmpp.boundjid.host or \
|
|
|
|
self.xmpp.is_component and jid == self.xmpp.server:
|
|
|
|
own_host = True
|
|
|
|
|
|
|
|
if not timeout:
|
|
|
|
timeout = self.timeout
|
2011-02-11 05:30:45 +00:00
|
|
|
|
2013-01-21 04:14:16 +00:00
|
|
|
start = time.time()
|
2011-02-11 05:30:45 +00:00
|
|
|
|
2013-01-21 04:14:16 +00:00
|
|
|
log.debug('Pinging %s' % jid)
|
|
|
|
try:
|
|
|
|
self.send_ping(jid, ifrom=ifrom, timeout=timeout)
|
|
|
|
except IqError as e:
|
|
|
|
if own_host:
|
|
|
|
rtt = time.time() - start
|
|
|
|
log.debug('Pinged %s, RTT: %s', jid, rtt)
|
|
|
|
return rtt
|
|
|
|
else:
|
|
|
|
raise e
|
|
|
|
else:
|
|
|
|
rtt = time.time() - start
|
|
|
|
log.debug('Pinged %s, RTT: %s', jid, rtt)
|
|
|
|
return rtt
|