Search in sources :

Example 1 with PingFailedListener

use of org.jivesoftware.smack.ping.PingFailedListener in project ecf by eclipse.

the class KeepAliveManager method schedulePingServerTask.

/**
 * Cancels any existing periodic ping task if there is one and schedules a new ping task if pingInterval is greater
 * then zero.
 *
 * This is designed so only one executor is used for scheduling all pings on all connections.  This results in only 1 thread used for pinging.
 */
private synchronized void schedulePingServerTask() {
    enableExecutorService();
    stopPingServerTask();
    if (pingInterval > 0) {
        periodicPingTask = periodicPingExecutorService.schedule(new Runnable() {

            public void run() {
                Ping ping = new Ping();
                PacketFilter responseFilter = new PacketIDFilter(ping.getPacketID());
                final PacketCollector response = pingFailedListeners.isEmpty() ? null : connection.createPacketCollector(responseFilter);
                connection.sendPacket(ping);
                if (response != null) {
                    // Schedule a collector for the ping reply, notify listeners if none is received.
                    periodicPingExecutorService.schedule(new Runnable() {

                        public void run() {
                            Packet result = response.nextResult(1);
                            // Stop queuing results
                            response.cancel();
                            // The actual result of the reply can be ignored since we only care if we actually got one.
                            if (result == null) {
                                for (PingFailedListener listener : pingFailedListeners) {
                                    listener.pingFailed();
                                }
                            }
                        }
                    }, SmackConfiguration.getPacketReplyTimeout(), TimeUnit.MILLISECONDS);
                }
            }
        }, getPingInterval(), TimeUnit.MILLISECONDS);
    }
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) PingFailedListener(org.jivesoftware.smack.ping.PingFailedListener) Ping(org.jivesoftware.smack.ping.packet.Ping) PacketCollector(org.jivesoftware.smack.PacketCollector) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Aggregations

PacketCollector (org.jivesoftware.smack.PacketCollector)1 PacketFilter (org.jivesoftware.smack.filter.PacketFilter)1 PacketIDFilter (org.jivesoftware.smack.filter.PacketIDFilter)1 Packet (org.jivesoftware.smack.packet.Packet)1 PingFailedListener (org.jivesoftware.smack.ping.PingFailedListener)1 Ping (org.jivesoftware.smack.ping.packet.Ping)1