org.jivesoftware.smackx.ping.PingManager Java Examples

The following examples show how to use org.jivesoftware.smackx.ping.PingManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SmackGcmSenderChannel.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
/**
 * Connects to GCM Cloud Connection Server using the supplied credentials.
 *
 * @param senderId
 *            Your GCM project number
 * @param apiKey
 *            API Key of your project
 */
protected void connect(long senderId, String apiKey, int keepAliveInterval) throws XMPPException, IOException, SmackException {

    // Configure connection
    ConnectionConfiguration config = new ConnectionConfiguration(GcmServiceConstants.GCM_SERVER, GcmServiceConstants.GCM_PORT);
    config.setSecurityMode(SecurityMode.enabled);
    config.setReconnectionAllowed(true);
    config.setRosterLoadedAtLogin(false);
    config.setSendPresence(false);
    config.setSocketFactory(SSLSocketFactory.getDefault());

    // Create connection object and initiate connection
    connection = new XMPPTCPConnection(config);
    pingManager = PingManager.getInstanceFor(connection);
    pingManager.setPingInterval(keepAliveInterval);
    pingManager.registerPingFailedListener(this);
    connection.connect();

    // Register listener to log connection state events
    connection.addConnectionListener(new SmackLoggingConnectionListener());

    // Handle incoming messages (delivery receipts and Google control messages)
    connection.addPacketListener(upstreamListener, new PacketTypeFilter(Message.class));

    // Log in...
    connection.login(senderId + "@" + GcmServiceConstants.GCM_SERVER, apiKey);
}
 
Example #2
Source File: XMPP.java    From XMPPSample_Studio with Apache License 2.0 5 votes vote down vote up
public void login(String user, String pass, StatusItem status, String username)
            throws XMPPException, SmackException, IOException, InterruptedException {
        Log.i(TAG, "inside XMPP getlogin Method");
        long l = System.currentTimeMillis();
        XMPPTCPConnection connect = connect();
        if (connect.isAuthenticated()) {
            Log.i(TAG, "User already logged in");
            return;
        }

        Log.i(TAG, "Time taken to connect: " + (System.currentTimeMillis() - l));

        l = System.currentTimeMillis();
        connect.login(user, pass);
        Log.i(TAG, "Time taken to login: " + (System.currentTimeMillis() - l));

        Log.i(TAG, "login step passed");

        Presence p = new Presence(Presence.Type.available);
        p.setMode(Presence.Mode.available);
        p.setPriority(24);
        p.setFrom(connect.getUser());
        if (status != null) {
            p.setStatus(status.toJSON());
        } else {
            p.setStatus(new StatusItem().toJSON());
        }
//        p.setTo("");
        VCard ownVCard = new VCard();
        ownVCard.load(connect);
        ownVCard.setNickName(username);
        ownVCard.save(connect);

        PingManager pingManager = PingManager.getInstanceFor(connect);
        pingManager.setPingInterval(150000);
        connect.sendPacket(p);


    }
 
Example #3
Source File: ServerPingWithAlarmManager.java    From Smack with Apache License 2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    LOGGER.fine("Ping Alarm broadcast received");
    Set<Entry<XMPPConnection, ServerPingWithAlarmManager>> managers;
    synchronized (ServerPingWithAlarmManager.class) {
        // Make a copy to avoid ConcurrentModificationException when
        // iterating directly over INSTANCES and the Set is modified
        // concurrently by creating a new ServerPingWithAlarmManager.
        managers = new HashSet<>(INSTANCES.entrySet());
    }
    for (Entry<XMPPConnection, ServerPingWithAlarmManager> entry : managers) {
        XMPPConnection connection = entry.getKey();
        if (entry.getValue().isEnabled()) {
            LOGGER.fine("Calling pingServerIfNecessary for connection "
                    + connection);
            final PingManager pingManager = PingManager.getInstanceFor(connection);
            // Android BroadcastReceivers have a timeout of 60 seconds.
            // The connections reply timeout may be higher, which causes
            // timeouts of the broadcast receiver and a subsequent ANR
            // of the App of the broadcast receiver. We therefore need
            // to call pingServerIfNecessary() in a new thread to avoid
            // this. It could happen that the device gets back to sleep
            // until the Thread runs, but that's a risk we are willing
            // to take into account as it's unlikely.
            Async.go(new Runnable() {
                @Override
                public void run() {
                    pingManager.pingServerIfNecessary();
                }
            }, "PingServerIfNecessary (" + connection.getConnectionCounter() + ')');
        } else {
            LOGGER.fine("NOT calling pingServerIfNecessary (disabled) on connection "
                    + connection.getConnectionCounter());
        }
    }
}
 
Example #4
Source File: ReconnectionTest.java    From Smack with Apache License 2.0 4 votes vote down vote up
/**
 * Execute some server interaction in order to test that the regenerated connection works fine.
 */
private void executeSomeServerInteraction(TCPConnection connection) throws XMPPException {
    PingManager pingManager = PingManager.getInstanceFor(connection);
    pingManager.pingMyServer();
}
 
Example #5
Source File: SmackConnection.java    From SmackAndroidDemo with Apache License 2.0 3 votes vote down vote up
public void connect() throws IOException, XMPPException, SmackException {
    Log.i(TAG, "connect()");

    XMPPTCPConnectionConfiguration.XMPPTCPConnectionConfigurationBuilder builder = XMPPTCPConnectionConfiguration.builder();
    builder.setServiceName(mServiceName);
    builder.setResource("SmackAndroidTestClient");
    builder.setUsernameAndPassword(mUsername, mPassword);
    builder.setRosterLoadedAtLogin(true);


    mConnection = new XMPPTCPConnection(builder.build());

    //Set ConnectionListener here to catch initial connect();
    mConnection.addConnectionListener(this);

    mConnection.connect();
    mConnection.login();

    PingManager.setDefaultPingInterval(600); //Ping every 10 minutes
    PingManager pingManager = PingManager.getInstanceFor(mConnection);
    pingManager.registerPingFailedListener(this);

    setupSendMessageReceiver();

    ChatManager.getInstanceFor(mConnection).addChatListener(this);
    mConnection.getRoster().addRosterListener(this);

}