/** * Check the server connection, reconnect if needed. * * <p>This function will try to ping the server if we are connected, and try to reestablish a * connection otherwise. */ public void sendServerPing() { if (mXMPPConnection == null || !mXMPPConnection.isAuthenticated()) { debugLog("Ping: requested, but not connected to server."); requestConnectionState(ConnectionState.ONLINE, false); return; } if (mPingID != null) { debugLog("Ping: requested, but still waiting for " + mPingID); return; // a ping is still on its way } if (mStreamHandler.isSmEnabled()) { debugLog("Ping: sending SM request"); mPingID = "" + mStreamHandler.requestAck(); } else { Ping ping = new Ping(); ping.setType(Type.GET); ping.setTo(mConfig.server); mPingID = ping.getPacketID(); debugLog("Ping: sending ping " + mPingID); mXMPPConnection.sendPacket(ping); } // register ping timeout handler: PACKET_TIMEOUT(30s) + 3s registerPongTimeout(PACKET_TIMEOUT + 3000, mPingID); }
public boolean isAuthenticated() { if (mXMPPConnection != null) { return (mXMPPConnection.isConnected() && mXMPPConnection.isAuthenticated()); } return false; }
private void tryToConnect(boolean create_account) throws YaximXMPPException { try { if (mXMPPConnection.isConnected()) { try { mStreamHandler.quickShutdown(); // blocking shutdown prior to re-connection } catch (Exception e) { debugLog("conn.shutdown() failed: " + e); } } registerRosterListener(); boolean need_bind = !mStreamHandler.isResumePossible(); mXMPPConnection.connect(need_bind); // the following should not happen as of smack 3.3.1 if (!mXMPPConnection.isConnected()) { throw new YaximXMPPException("SMACK connect failed without exception!"); } if (mConnectionListener != null) mXMPPConnection.removeConnectionListener(mConnectionListener); mConnectionListener = new ConnectionListener() { public void connectionClosedOnError(Exception e) { onDisconnected(e); } public void connectionClosed() { // TODO: fix reconnect when we got kicked by the server or SM failed! // onDisconnected(null); updateConnectionState(ConnectionState.OFFLINE); } public void reconnectingIn(int seconds) {} public void reconnectionFailed(Exception e) {} public void reconnectionSuccessful() {} }; mXMPPConnection.addConnectionListener(mConnectionListener); // SMACK auto-logins if we were authenticated before if (!mXMPPConnection.isAuthenticated()) { if (create_account) { Log.d(TAG, "creating new server account..."); AccountManager am = new AccountManager(mXMPPConnection); am.createAccount(mConfig.userName, mConfig.password); } mXMPPConnection.login(mConfig.userName, mConfig.password, mConfig.ressource); } Log.d( TAG, "SM: can resume = " + mStreamHandler.isResumePossible() + " needbind=" + need_bind); if (need_bind) { mStreamHandler.notifyInitialLogin(); setStatusFromConfig(); } } catch (YaximXMPPException e) { throw e; } catch (Exception e) { // actually we just care for IllegalState or NullPointer or XMPPEx. throw new YaximXMPPException("tryToConnect failed", e); } }