akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 19 | * Chaoyi Bian <bcy@pku.edu.cn> |
| 20 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 21 | * Yingdi Yu <yingdi@cs.ucla.edu> |
| 22 | */ |
| 23 | |
| 24 | #ifndef SYNC_SEQ_NO_H |
| 25 | #define SYNC_SEQ_NO_H |
| 26 | |
| 27 | #include <boost/cstdint.hpp> |
| 28 | #include "sync-digest.h" |
| 29 | |
| 30 | namespace Sync { |
| 31 | |
| 32 | /** |
| 33 | * @ingroup sync |
| 34 | * @brief Sequence number abstraction |
| 35 | */ |
| 36 | class SeqNo |
| 37 | { |
| 38 | public: |
| 39 | /** |
| 40 | * @brief Default constructor. Creates an zero sequence number with zero session ID (basically is an invalid object) |
| 41 | */ |
| 42 | SeqNo () |
| 43 | : m_valid (false) |
| 44 | , m_session (0) |
| 45 | , m_seq (0) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @brief Copy constructor |
| 51 | * @param seq sequence number object to copy from |
| 52 | */ |
| 53 | SeqNo (const SeqNo &seq) |
| 54 | { |
| 55 | *this = seq; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @brief Assignment operator |
| 60 | * @param seq sequence number object to copy from |
| 61 | */ |
| 62 | SeqNo & |
| 63 | operator = (const SeqNo &seq) |
| 64 | { |
| 65 | m_valid = seq.m_valid; |
| 66 | m_session = seq.m_session; |
| 67 | m_seq = seq.m_seq; |
| 68 | |
| 69 | return *this; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @brief Constructor with just sequence number. Session assumed to be zero |
| 74 | * @param seq Sequence number |
| 75 | */ |
| 76 | SeqNo (uint64_t seq) |
| 77 | : m_valid (true) |
| 78 | , m_session (0) |
| 79 | , m_seq (seq) |
| 80 | { } |
| 81 | |
| 82 | /** |
| 83 | * @brief Constructor with session and sequence id |
| 84 | * @param session Session ID |
| 85 | * @param seq Sequence number |
| 86 | */ |
| 87 | SeqNo (uint64_t session, uint64_t seq) |
| 88 | : m_valid (true) |
| 89 | , m_session (session) |
| 90 | , m_seq (seq) |
| 91 | { } |
| 92 | |
| 93 | /** |
| 94 | * @brief Get sequence number digest |
| 95 | * |
| 96 | * Digest will be calculated every time it is requested |
| 97 | */ |
| 98 | DigestConstPtr |
| 99 | getDigest () const; |
| 100 | |
| 101 | /** |
| 102 | * @brief Compare if one sequence number is lower |
| 103 | * @param seq Another sequence number to compare with |
| 104 | * |
| 105 | * tuple (session1, seq1) is less than (session2, seq2) in two cases: |
| 106 | * 1. session1 < session2 |
| 107 | * 2. session1 == session2 and seq1 < seq2 |
| 108 | */ |
| 109 | bool |
| 110 | operator < (const SeqNo &seq) const |
| 111 | { |
| 112 | return m_session < seq.m_session || (m_session == seq.m_session && m_seq < seq.m_seq); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * @brief Compare if two sequence numbers are equal |
| 117 | * @param seq Another sequence number to compare with |
| 118 | */ |
| 119 | bool |
| 120 | operator == (const SeqNo &seq) const |
| 121 | { |
| 122 | return m_session == seq.m_session && m_seq == seq.m_seq; |
| 123 | } |
| 124 | |
| 125 | bool |
| 126 | operator <= (const SeqNo &seq) const |
| 127 | { |
| 128 | return m_session == seq.m_session && m_seq <= seq.m_seq; |
| 129 | } |
| 130 | |
| 131 | SeqNo & |
| 132 | operator ++ () |
| 133 | { |
| 134 | if (m_valid) { |
| 135 | m_seq ++; |
| 136 | } |
| 137 | else { |
| 138 | m_valid = true; |
| 139 | } |
| 140 | return *this; |
| 141 | } |
| 142 | |
| 143 | bool |
| 144 | isValid () const |
| 145 | { |
| 146 | return m_valid; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * @brief Get session id |
| 151 | */ |
| 152 | uint64_t getSession () const |
| 153 | { return m_session; } |
| 154 | |
| 155 | /** |
| 156 | * @brief Get sequence number |
| 157 | */ |
| 158 | uint64_t getSeq () const |
| 159 | { return m_seq; } |
| 160 | |
| 161 | /** |
| 162 | * @brief Set sequence number |
| 163 | */ |
| 164 | void |
| 165 | setSeq(uint64_t seq) |
| 166 | { m_seq = seq; } |
| 167 | |
| 168 | private: |
| 169 | bool m_valid; |
| 170 | |
| 171 | /** |
| 172 | * @brief Session ID (e.g., after crash, application will choose new session ID. |
| 173 | * |
| 174 | * Note that session IDs for the same name should always increase. So, the good choice |
| 175 | * for the session ID is client's timestamp |
| 176 | */ |
| 177 | uint64_t m_session; |
| 178 | |
| 179 | /** |
| 180 | * @brief Sequence number |
| 181 | * |
| 182 | * Sequence number for a session always starts with 0 and goes to max value. |
| 183 | * |
| 184 | * For now, wrapping sequence number after max to zero is not supported |
| 185 | */ |
| 186 | uint64_t m_seq; |
| 187 | }; |
| 188 | |
| 189 | inline std::ostream & |
| 190 | operator << (std::ostream &os, const SeqNo &seqno) |
| 191 | { |
| 192 | os << "<session>" << seqno.getSession () << "</session><seqno>" << seqno.getSeq () << "</seqno>"; |
| 193 | return os; |
| 194 | } |
| 195 | |
| 196 | } // Sync |
| 197 | |
| 198 | #endif // SYNC_SEQ_NO_H |