Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Client to Server pings #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions wokkel/ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,21 @@ class PingClientProtocol(XMPPHandler):
This handler implements the protocol for sending out XMPP Ping requests.
"""

def ping(self, entity, sender=None):
def ping(self, entity=None, sender=None, timeout=None):
"""
Send out a ping request and wait for a response.

@param entity: Entity to be pinged.
@param entity: Entity to be pinged. L{None} to ping the server.
@type entity: L{JID<twisted.words.protocols.jabber.jid.JID>}

@return: A deferred that fires upon receiving a response.
@rtype: L{Deferred<twisted.internet.defer.Deferred>}

@param sender: Optional sender address.
@type sender: L{JID<twisted.words.protocols.jabber.jid.JID>}

@param entity: timeout in seconds for the ping to be responded before errback is called.
@type entity: L{int}
"""
def cb(response):
return None
Expand All @@ -60,7 +63,13 @@ def eb(failure):
if sender is not None:
request['from'] = sender.full()

d = request.send(entity.full())
if timeout is not None:
request.timeout = timeout

if entity is None:
d = request.send()
else:
d = request.send(entity.full())
d.addCallbacks(cb, eb)
return d

Expand Down
20 changes: 20 additions & 0 deletions wokkel/test/test_ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,26 @@ def cb(result):

return d

def test_pingToServer(self):
"""
Pinging a service should fire a deferred with None
"""

def cb(result):
self.assertIdentical(None, result)

d = self.protocol.ping()
d.addCallback(cb)

iq = self.stub.output[-1]
self.assertIsNone(iq.getAttribute(u'to'))
self.assertEqual(u'get', iq.getAttribute(u'type'))
self.assertEqual('urn:xmpp:ping', iq.ping.uri)

response = toResponse(iq, u'result')
self.stub.send(response)

return d

def test_pingWithSender(self):
"""
Expand Down