Smack

org.jivesoftware.smack.keepalive
Class KeepAliveManager

java.lang.Object
  extended by org.jivesoftware.smack.keepalive.KeepAliveManager

public class KeepAliveManager
extends java.lang.Object

Using an implementation of XMPP Ping (XEP-0199). This class provides keepalive functionality with the server that will periodically "ping" the server to maintain and/or verify that the connection still exists.

The ping is done at the application level and is therefore protocol agnostic. It will thus work for both standard TCP connections as well as BOSH or any other transport protocol. It will also work regardless of whether the server supports the Ping extension, since an error response to the ping serves the same purpose as a pong.

Author:
Florian Schmaus

Method Summary
 void addPingFailedListener(PingFailedListener listener)
          Add listener for notification when a server ping fails.
static KeepAliveManager getInstanceFor(Connection connection)
          Retrieves a KeepAliveManager for the specified Connection, creating one if it doesn't already exist.
 long getPingInterval()
          Gets the ping interval.
 long getTimeSinceLastContact()
          Returns the elapsed time (in milliseconds) since the last successful contact with the server (i.e.
 void removePingFailedListener(PingFailedListener listener)
          Remove the listener.
 void setPingInterval(long newPingInterval)
          Sets the ping interval.
 void stopPinging()
          Stops pinging the server.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getInstanceFor

public static KeepAliveManager getInstanceFor(Connection connection)
Retrieves a KeepAliveManager for the specified Connection, creating one if it doesn't already exist.

Parameters:
connection - The connection the manager is attached to.
Returns:
The new or existing manager.

setPingInterval

public void setPingInterval(long newPingInterval)
Sets the ping interval.

Parameters:
pingInterval - The new ping time interval in milliseconds.

stopPinging

public void stopPinging()
Stops pinging the server. This cannot stop a ping that has already started, but will prevent another from being triggered.

To restart, call setPingInterval(long).


getPingInterval

public long getPingInterval()
Gets the ping interval.

Returns:
The ping interval in milliseconds.

addPingFailedListener

public void addPingFailedListener(PingFailedListener listener)
Add listener for notification when a server ping fails.

Please note that this doesn't necessarily mean that the connection is lost, a slow to respond server could also cause a failure due to taking too long to respond and thus causing a reply timeout.

Parameters:
listener - The listener to be called

removePingFailedListener

public void removePingFailedListener(PingFailedListener listener)
Remove the listener.

Parameters:
listener - The listener to be removed.

getTimeSinceLastContact

public long getTimeSinceLastContact()
Returns the elapsed time (in milliseconds) since the last successful contact with the server (i.e. the last time any message was received).

Note: Result is -1 if no message has been received since manager was created and 0 if the elapsed time is negative due to a clock reset.

Returns:
Elapsed time since last message was received.

Smack

Copyright © 2003-2007 Jive Software.