private void tryScheduleReconnect()

in src/main/java/com/google/firebase/database/connection/PersistentConnectionImpl.java [453:517]


  private void tryScheduleReconnect() {
    if (shouldReconnect()) {
      hardAssert(
          this.connectionState == ConnectionState.Disconnected,
          "Not in disconnected state: %s",
          this.connectionState);
      final boolean forceRefresh = this.forceAuthTokenRefresh;
      logger.debug("{} Scheduling connection attempt", label);
      this.forceAuthTokenRefresh = false;
      retryHelper.retry(
          new Runnable() {
            @Override
            public void run() {
              logger.debug("{} Trying to fetch auth token", label);
              hardAssert(
                  connectionState == ConnectionState.Disconnected,
                  "Not in disconnected state: %s",
                  connectionState);
              connectionState = ConnectionState.GettingToken;
              currentGetTokenAttempt++;
              final long thisGetTokenAttempt = currentGetTokenAttempt;
              authTokenProvider.getToken(
                  forceRefresh,
                  new ConnectionAuthTokenProvider.GetTokenCallback() {
                    @Override
                    public void onSuccess(String token) {
                      if (thisGetTokenAttempt == currentGetTokenAttempt) {
                        // Someone could have interrupted us while fetching the token,
                        // marking the connection as Disconnected
                        if (connectionState == ConnectionState.GettingToken) {
                          logger.debug("{} Successfully fetched token, opening connection", label);
                          openNetworkConnection(token);
                        } else {
                          hardAssert(
                              connectionState == ConnectionState.Disconnected,
                              "Expected connection state disconnected, but was %s",
                              connectionState);
                          logger.debug(
                              "{} Not opening connection after token refresh, because connection "
                                  + "was set to disconnected", label);
                        }
                      } else {
                        logger.debug(
                            "{} Ignoring getToken result, because this was not the "
                                + "latest attempt.", label);
                      }
                    }

                    @Override
                    public void onError(String error) {
                      if (thisGetTokenAttempt == currentGetTokenAttempt) {
                        connectionState = ConnectionState.Disconnected;
                        logger.debug("{} Error fetching token: {}", label, error);
                        tryScheduleReconnect();
                      } else {
                        logger.debug(
                            "{} Ignoring getToken error, because this was not the "
                                + "latest attempt.", label);
                      }
                    }
                  });
            }
          });
    }
  }