/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
/*
 * Copyright (c) 2012 University of California, Los Angeles
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
 *         卞超轶 Chaoyi Bian <bcy@pku.edu.cn>
 *	   Alexander Afanasyev <alexander.afanasyev@ucla.edu>
 */

#ifndef SYNC_SEQ_NO_H
#define SYNC_SEQ_NO_H

#include <boost/cstdint.hpp>
#include "sync-digest.h"

namespace Sync {

/**
 * @ingroup sync
 * @brief Sequence number abstraction
 */
class SeqNo
{
public:
  /**
   * @brief Default constructor.  Creates an zero sequence number with zero session ID (basically is an invalid object)
   */
  SeqNo ()
    : m_valid (false)
    , m_session (0)
    , m_seq (0)
  {
  }
  
  /**
   * @brief Copy constructor
   * @param seq sequence number object to copy from
   */
  SeqNo (const SeqNo &seq)
  {
    *this = seq;
  }

  /**
   * @brief Assignment operator
   * @param seq sequence number object to copy from
   */
  SeqNo &
  operator = (const SeqNo &seq)
  {
    m_valid = seq.m_valid;
    m_session = seq.m_session;
    m_seq = seq.m_seq;

    return *this;
  }

  /**
   * @brief Constructor with just sequence number. Session assumed to be zero
   * @param seq Sequence number
   */
  SeqNo (uint32_t seq)
    : m_valid (true)
    , m_session (0)
    , m_seq (seq)
  { }

  /**
   * @brief Constructor with session and sequence id
   * @param session Session ID
   * @param seq Sequence number
   */
  SeqNo (uint32_t session, uint32_t seq)
    : m_valid (true)
    , m_session (session)
    , m_seq (seq)
  { }

  /**
   * @brief Get sequence number digest
   *
   * Digest will be calculated every time it is requested
   */
  DigestConstPtr
  getDigest () const;

  /**
   * @brief Compare if one sequence number is lower
   * @param seq Another sequence number to compare with
   *
   * tuple (session1, seq1) is less than (session2, seq2) in two cases:
   * 1. session1 < session2
   * 2. session1 == session2 and seq1 < seq2
   */
  bool
  operator < (const SeqNo &seq) const
  {
    return m_session < seq.m_session || (m_session == seq.m_session && m_seq < seq.m_seq);
  }

  /**
   * @brief Compare if two sequence numbers are equal
   * @param seq Another sequence number to compare with
   */
  bool
  operator == (const SeqNo &seq) const
  {
    return m_session == seq.m_session && m_seq == seq.m_seq;
  }

  bool
  isValid () const
  {
    return m_valid;
  }
  
  /**
   * @brief Get session id
   */
  uint32_t getSession () const
  { return m_session; }

  /**
   * @brief Get sequence number
   */
  uint32_t getSeq () const
  { return m_seq; }
  
private:
  bool m_valid;
  
  /**
   * @brief Session ID (e.g., after crash, application will choose new session ID.
   *
   * Note that session IDs for the same name should always increase. So, the good choice
   * for the session ID is client's timestamp
   */
  uint32_t m_session;

  /**
   * @brief Sequence number
   *
   * Sequence number for a session always starts with 0 and goes to max value.
   *
   * For now, wrapping sequence number after max to zero is not supported
   */
  uint32_t m_seq;
};

inline std::ostream &
operator << (std::ostream &os, const SeqNo &seqno)
{
  os << "<session>" << seqno.getSession () << "</session><seqno>" << seqno.getSeq () << "</seqno>";
  return os;
}

} // Sync

#endif // SYNC_SEQ_NO_H
