| /* -*- 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> |
| * Yingdi Yu <yingdi@cs.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 (uint64_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 (uint64_t session, uint64_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 |
| operator <= (const SeqNo &seq) const |
| { |
| return m_session == seq.m_session && m_seq <= seq.m_seq; |
| } |
| |
| SeqNo & |
| operator ++ () |
| { |
| if (m_valid) { |
| m_seq ++; |
| } |
| else { |
| m_valid = true; |
| } |
| return *this; |
| } |
| |
| bool |
| isValid () const |
| { |
| return m_valid; |
| } |
| |
| /** |
| * @brief Get session id |
| */ |
| uint64_t getSession () const |
| { return m_session; } |
| |
| /** |
| * @brief Get sequence number |
| */ |
| uint64_t getSeq () const |
| { return m_seq; } |
| |
| /** |
| * @brief Set sequence number |
| */ |
| void |
| setSeq(uint64_t seq) |
| { m_seq = 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 |
| */ |
| uint64_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 |
| */ |
| uint64_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 |