public void logErrorInCheckStatusResponse(
      ClientSubscriptionChannel channel,
      CheckStatusResponseStructure response,
      boolean isNewer,
      boolean isInError) {

    StringBuilder b = new StringBuilder();
    b.append("check status failed for channel:");
    b.append(" address=").append(channel.getAddress());

    if (isNewer) {
      b.append(" prevServiceStartedTime=");
      b.append(channel.getLastServiceStartedTime());
      b.append(" newServiceStartedTime=");
      b.append(response.getServiceStartedTime());
    }

    ErrorCondition error = response.getErrorCondition();
    if (isInError && error != null) {
      ClientSupport.appendError(error.getServiceNotAvailableError(), b);
      ClientSupport.appendError(error.getOtherError(), b);
      if (error.getDescription() != null && error.getDescription().getValue() != null)
        b.append(" errorDescription=" + error.getDescription().getValue());
    }

    _log.warn(b.toString());
  }
  /**
   * Submit a CheckStatusResponse received from a SIRI endpoint. The subscription will be reset if
   * the CheckStatusResponse has errors or if the SIRI endpoint has been restarted since the
   * most-recent check.
   *
   * @param response the CheckStatusResponse
   */
  public void handleCheckStatusResponse(CheckStatusResponseStructure response) {

    _log.debug("handle check status response");

    MessageRefStructure messageRef = response.getRequestMessageRef();

    if (messageRef == null || messageRef.getValue() == null)
      throw new SiriMissingArgumentException("RequestMessageRef");

    PendingCheckStatusRequest pending = _pendingCheckStatusRequests.remove(messageRef.getValue());

    if (pending == null) {
      _log.warn("Pending CheckStatus channel not found for messageId=" + messageRef.getValue());
      return;
    }

    /** Cancel the timeout task */
    ScheduledFuture<?> task = pending.getTimeoutTask();
    task.cancel(true);

    ClientSubscriptionChannel channel = pending.getChannel();

    boolean isNewer = isCheckStatusNewer(channel, response);
    boolean isInError = response.isStatus() == null || !response.isStatus();

    /**
     * If the channel hasn't been rebooted (aka it's not newer) and the channel is not in error,
     * we're cool!
     */
    if (!(isNewer || isInError)) return;

    logErrorInCheckStatusResponse(channel, response, isNewer, isInError);

    _subscriptionManager.handleChannelDisconnectAndReconnect(channel);
  }
  private boolean isCheckStatusNewer(
      ClientSubscriptionChannel channel, CheckStatusResponseStructure response) {

    Date serviceStartedTime = response.getServiceStartedTime();

    if (serviceStartedTime == null) return false;

    /** Has the service start time been adjusted since our last status check? */
    Date lastServiceStartedTime = channel.getLastServiceStartedTime();

    if (lastServiceStartedTime == null) {
      channel.setLastServiceStartedTime(serviceStartedTime);
    } else if (serviceStartedTime.after(lastServiceStartedTime)) {
      return true;
    }

    return false;
  }