Java 类org.jivesoftware.smackx.ping.packet.Pong 实例源码

项目:androidPN-client.    文件:PingManager.java   
private PingManager(final Connection connection) {
    this.connection = connection;
    instances.put(connection, this);

    ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
    sdm.addFeature(NAMESPACE);

    PacketFilter pingPacketFilter = new PacketTypeFilter(Ping.class);
    connection.addPacketListener(new PacketListener() {
        /**
         * Sends a Pong for every Ping
         */
        public void processPacket(Packet packet) {
            if (pingMinDelta > 0) {
                // Ping flood protection enabled
                long currentMillies = System.currentTimeMillis();
                long delta = currentMillies - lastPingStamp;
                lastPingStamp = currentMillies;
                if (delta < pingMinDelta) {
                    return;
                }
            }
            Pong pong = new Pong((Ping)packet);
            connection.sendPacket(pong);
        }
    }
    , pingPacketFilter);
    connection.addConnectionListener(new ConnectionListener() {

        @Override
        public void connectionClosed() {
            maybeStopPingServerTask();
        }

        @Override
        public void connectionClosedOnError(Exception arg0) {
            maybeStopPingServerTask();
        }

        @Override
        public void reconnectionSuccessful() {
            maybeSchedulePingServerTask();
        }

        @Override
        public void reconnectingIn(int seconds) {
        }

        @Override
        public void reconnectionFailed(Exception e) {
        }
    });
    maybeSchedulePingServerTask();
}
项目:asmack-mini    文件:PingManager.java   
private void init() {
    periodicPingExecutorService = new ScheduledThreadPoolExecutor(1);
    PacketFilter pingPacketFilter = new PacketTypeFilter(Ping.class);
    connection.addPacketListener(new PacketListener() {
        /**
         * Sends a Pong for every Ping
         */
        public void processPacket(Packet packet) {
            if (pingMinDelta > 0) {
                // Ping flood protection enabled
                long currentMillies = System.currentTimeMillis();
                long delta = currentMillies - lastPingStamp;
                lastPingStamp = currentMillies;
                if (delta < pingMinDelta) {
                    return;
                }
            }
            Pong pong = new Pong((Ping)packet);
            connection.sendPacket(pong);
        }
    }
    , pingPacketFilter);
    connection.addConnectionListener(new ConnectionListener() {

        @Override
        public void connectionClosed() {
            maybeStopPingServerTask();
        }

        @Override
        public void connectionClosedOnError(Exception arg0) {
            maybeStopPingServerTask();
        }

        @Override
        public void reconnectionSuccessful() {
            maybeSchedulePingServerTask();
        }

        @Override
        public void reconnectingIn(int seconds) {
        }

        @Override
        public void reconnectionFailed(Exception e) {
        }
    });
    instances.put(connection, this);
    maybeSchedulePingServerTask();
}