Package org.jivesoftware.smackx.ping
Class PingManager
java.lang.Object
org.jivesoftware.smack.Manager
org.jivesoftware.smackx.ping.PingManager
Implements the XMPP Ping as defined by XEP-0199. The XMPP Ping protocol allows one entity to
ping any other entity by simply sending a ping to the appropriate JID. PingManger also
periodically sends XMPP pings to the server to avoid NAT timeouts and to test
the connection status.
The default server ping interval is 30 minutes and can be modified with
setDefaultPingInterval(int)
and setPingInterval(int)
.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic PingManager
getInstanceFor
(XMPPConnection connection) Retrieves aPingManager
for the specifiedXMPPConnection
, creating one if it doesn't already exist.int
Get the current ping interval.boolean
isPingSupported
(Jid jid) Query the specified entity to see if it supports the Ping protocol (XEP-0199).boolean
Same as callingping(Jid, long)
with the default packet reply timeout.boolean
Pings the given jid.boolean
Pings the server.boolean
pingMyServer
(boolean notifyListeners) Pings the server.boolean
pingMyServer
(boolean notifyListeners, long pingTimeout) Pings the server.void
Ping the server if deemed necessary because automatic server pings are enabled (setPingInterval(int)
) and the ping interval has expired.void
registerPingFailedListener
(PingFailedListener listener) Register a new PingFailedListener.static void
setDefaultPingInterval
(int interval) Set the default ping interval which will be used for new connections.void
setPingInterval
(int pingInterval) Set the interval in seconds between a automated server ping is send.void
Unregister a PingFailedListener.Methods inherited from class org.jivesoftware.smack.Manager
connection, getAuthenticatedConnectionOrThrow, schedule, schedule, scheduleBlocking
-
Method Details
-
getInstanceFor
Retrieves aPingManager
for the specifiedXMPPConnection
, creating one if it doesn't already exist.- Parameters:
connection
- TODO javadoc me please The connection the manager is attached to.- Returns:
- The new or existing manager.
-
setDefaultPingInterval
Set the default ping interval which will be used for new connections.- Parameters:
interval
- the interval in seconds
-
pingAsync
-
pingAsync
-
ping
public boolean ping(Jid jid, long pingTimeout) throws SmackException.NotConnectedException, SmackException.NoResponseException, InterruptedException Pings the given jid. This method will return false if an error occurs. The exception to this, is a server ping, which will always return true if the server is reachable, event if there is an error on the ping itself (i.e. ping not supported).Use
isPingSupported(Jid)
to determine if XMPP Ping is supported by the entity.- Parameters:
jid
- The id of the entity the ping is being sent topingTimeout
- The time to wait for a reply in milliseconds- Returns:
- true if a reply was received from the entity, false otherwise.
- Throws:
SmackException.NoResponseException
- if there was no response from the jid.SmackException.NotConnectedException
- if the XMPP connection is not connected.InterruptedException
- if the calling thread was interrupted.
-
ping
public boolean ping(Jid jid) throws SmackException.NotConnectedException, SmackException.NoResponseException, InterruptedException Same as callingping(Jid, long)
with the default packet reply timeout.- Parameters:
jid
- The id of the entity the ping is being sent to- Returns:
- true if a reply was received from the entity, false otherwise.
- Throws:
SmackException.NotConnectedException
- if the XMPP connection is not connected.SmackException.NoResponseException
- if there was no response from the jid.InterruptedException
- if the calling thread was interrupted.
-
isPingSupported
public boolean isPingSupported(Jid jid) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException Query the specified entity to see if it supports the Ping protocol (XEP-0199).- Parameters:
jid
- The id of the entity the query is being sent to- Returns:
- true if it supports ping, false otherwise.
- Throws:
XMPPException.XMPPErrorException
- An XMPP related error occurred during the requestSmackException.NoResponseException
- if there was no response from the jid.SmackException.NotConnectedException
- if the XMPP connection is not connected.InterruptedException
- if the calling thread was interrupted.
-
pingMyServer
Pings the server. This method will return true if the server is reachable. It is the equivalent of callingping
with the XMPP domain.Unlike the
ping(Jid)
case, this method will return true even ifisPingSupported(Jid)
is false.- Returns:
- true if a reply was received from the server, false otherwise.
- Throws:
SmackException.NotConnectedException
- if the XMPP connection is not connected.InterruptedException
- if the calling thread was interrupted.
-
pingMyServer
public boolean pingMyServer(boolean notifyListeners) throws SmackException.NotConnectedException, InterruptedException Pings the server. This method will return true if the server is reachable. It is the equivalent of callingping
with the XMPP domain.Unlike the
ping(Jid)
case, this method will return true even ifisPingSupported(Jid)
is false.- Parameters:
notifyListeners
- Notify the PingFailedListener in case of error if true- Returns:
- true if the user's server could be pinged.
- Throws:
SmackException.NotConnectedException
- if the XMPP connection is not connected.InterruptedException
- if the calling thread was interrupted.
-
pingMyServer
public boolean pingMyServer(boolean notifyListeners, long pingTimeout) throws SmackException.NotConnectedException, InterruptedException Pings the server. This method will return true if the server is reachable. It is the equivalent of callingping
with the XMPP domain.Unlike the
ping(Jid)
case, this method will return true even ifisPingSupported(Jid)
is false.- Parameters:
notifyListeners
- Notify the PingFailedListener in case of error if truepingTimeout
- The time to wait for a reply in milliseconds- Returns:
- true if the user's server could be pinged.
- Throws:
SmackException.NotConnectedException
- if the XMPP connection is not connected.InterruptedException
- if the calling thread was interrupted.
-
setPingInterval
Set the interval in seconds between a automated server ping is send. A negative value disables automatic server pings. All settings take effect immediately. If there is an active scheduled server ping it will be canceled and, ifpingInterval
is positive, a new one will be scheduled in pingInterval seconds.If the ping fails after 3 attempts waiting the connections reply timeout for an answer, then the ping failed listeners will be invoked.
- Parameters:
pingInterval
- the interval in seconds between the automated server pings
-
getPingInterval
Get the current ping interval.- Returns:
- the interval between pings in seconds
-
registerPingFailedListener
Register a new PingFailedListener.- Parameters:
listener
- the listener to invoke
-
unregisterPingFailedListener
Unregister a PingFailedListener.- Parameters:
listener
- the listener to remove
-
pingServerIfNecessary
Ping the server if deemed necessary because automatic server pings are enabled (setPingInterval(int)
) and the ping interval has expired.
-