Example #1
0
  /**
   * 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);
  }
Example #2
0
  static void registerSmackProviders() {
    ProviderManager pm = ProviderManager.getInstance();
    // add IQ handling
    pm.addIQProvider("query", "http://jabber.org/protocol/disco#info", new DiscoverInfoProvider());
    pm.addIQProvider(
        "query", "http://jabber.org/protocol/disco#items", new DiscoverItemsProvider());
    // add delayed delivery notifications
    pm.addExtensionProvider("delay", "urn:xmpp:delay", new DelayInfoProvider());
    pm.addExtensionProvider("x", "jabber:x:delay", new DelayInfoProvider());
    // add XEP-0092 Software Version
    pm.addIQProvider("query", Version.NAMESPACE, new Version.Provider());

    // add carbons and forwarding
    pm.addExtensionProvider("forwarded", Forwarded.NAMESPACE, new Forwarded.Provider());
    pm.addExtensionProvider("sent", Carbon.NAMESPACE, new Carbon.Provider());
    pm.addExtensionProvider("received", Carbon.NAMESPACE, new Carbon.Provider());
    // add delivery receipts
    pm.addExtensionProvider(
        DeliveryReceipt.ELEMENT, DeliveryReceipt.NAMESPACE, new DeliveryReceipt.Provider());
    pm.addExtensionProvider(
        DeliveryReceiptRequest.ELEMENT,
        DeliveryReceipt.NAMESPACE,
        new DeliveryReceiptRequest.Provider());
    // add XMPP Ping (XEP-0199)
    pm.addIQProvider("ping", "urn:xmpp:ping", new PingProvider());

    ServiceDiscoveryManager.setDefaultIdentity(YAXIM_IDENTITY);

    // XEP-0115 Entity Capabilities
    pm.addExtensionProvider("c", "http://jabber.org/protocol/caps", new CapsExtensionProvider());

    XmppStreamHandler.addExtensionProviders();
  }
Example #3
0
  // this code runs a DNS resolver, might be blocking
  private synchronized void initXMPPConnection() {
    // allow custom server / custom port to override SRV record
    if (mConfig.customServer.length() > 0)
      mXMPPConfig = new ConnectionConfiguration(mConfig.customServer, mConfig.port, mConfig.server);
    else mXMPPConfig = new ConnectionConfiguration(mConfig.server); // use SRV
    mXMPPConfig.setReconnectionAllowed(false);
    mXMPPConfig.setSendPresence(false);
    mXMPPConfig.setCompressionEnabled(false); // disable for now
    mXMPPConfig.setDebuggerEnabled(mConfig.smackdebug);
    if (mConfig.require_ssl)
      this.mXMPPConfig.setSecurityMode(ConnectionConfiguration.SecurityMode.required);

    // register MemorizingTrustManager for HTTPS
    try {
      SSLContext sc = SSLContext.getInstance("TLS");
      sc.init(
          null,
          new X509TrustManager[] {YaximApplication.getApp(mService).mMTM},
          new java.security.SecureRandom());
      this.mXMPPConfig.setCustomSSLContext(sc);
    } catch (java.security.GeneralSecurityException e) {
      debugLog("initialize MemorizingTrustManager: " + e);
    }

    this.mXMPPConnection = new XmppStreamHandler.ExtXMPPConnection(mXMPPConfig);
    this.mStreamHandler = new XmppStreamHandler(mXMPPConnection, mConfig.smackdebug);
    mStreamHandler.addAckReceivedListener(
        new XmppStreamHandler.AckReceivedListener() {
          public void ackReceived(long handled, long total) {
            gotServerPong("" + handled);
          }
        });
    mConfig.reconnect_required = false;

    initServiceDiscovery();
  }
Example #4
0
  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);
    }
  }