Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [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 | */ |
| 22 | |
| 23 | #ifndef SYNC_SEQ_NO_H |
| 24 | #define SYNC_SEQ_NO_H |
| 25 | |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 26 | #include <boost/cstdint.hpp> |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 27 | #include "sync-digest.h" |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 28 | |
Alexander Afanasyev | 017784c | 2012-03-02 11:44:13 -0800 | [diff] [blame] | 29 | namespace Sync { |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 30 | |
| 31 | /** |
Alexander Afanasyev | 1fbdcdd | 2012-03-05 23:14:33 -0800 | [diff] [blame] | 32 | * @ingroup sync |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 33 | * @brief Sequence number abstraction |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 34 | */ |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 35 | class SeqNo |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 36 | { |
| 37 | public: |
| 38 | /** |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 39 | * @brief Default constructor. Creates an zero sequence number with zero session ID (basically is an invalid object) |
| 40 | */ |
| 41 | SeqNo () |
| 42 | : m_valid (false) |
| 43 | , m_session (0) |
| 44 | , m_seq (0) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | /** |
Alexander Afanasyev | 1fbdcdd | 2012-03-05 23:14:33 -0800 | [diff] [blame] | 49 | * @brief Copy constructor |
| 50 | * @param seq sequence number object to copy from |
| 51 | */ |
| 52 | SeqNo (const SeqNo &seq) |
| 53 | { |
| 54 | *this = seq; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @brief Assignment operator |
| 59 | * @param seq sequence number object to copy from |
| 60 | */ |
| 61 | SeqNo & |
| 62 | operator = (const SeqNo &seq) |
| 63 | { |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 64 | m_valid = seq.m_valid; |
Alexander Afanasyev | 1fbdcdd | 2012-03-05 23:14:33 -0800 | [diff] [blame] | 65 | m_session = seq.m_session; |
| 66 | m_seq = seq.m_seq; |
| 67 | |
| 68 | return *this; |
| 69 | } |
| 70 | |
| 71 | /** |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 72 | * @brief Constructor with just sequence number. Session assumed to be zero |
| 73 | * @param seq Sequence number |
| 74 | */ |
| 75 | SeqNo (uint32_t seq) |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 76 | : m_valid (true) |
| 77 | , m_session (0) |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 78 | , m_seq (seq) |
| 79 | { } |
| 80 | |
| 81 | /** |
| 82 | * @brief Constructor with session and sequence id |
| 83 | * @param session Session ID |
| 84 | * @param seq Sequence number |
| 85 | */ |
| 86 | SeqNo (uint32_t session, uint32_t seq) |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 87 | : m_valid (true) |
| 88 | , m_session (session) |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 89 | , m_seq (seq) |
| 90 | { } |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 91 | |
Alexander Afanasyev | 1fbdcdd | 2012-03-05 23:14:33 -0800 | [diff] [blame] | 92 | /** |
| 93 | * @brief Get sequence number digest |
| 94 | * |
| 95 | * Digest will be calculated every time it is requested |
| 96 | */ |
| 97 | DigestConstPtr |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 98 | getDigest () const; |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 99 | |
| 100 | /** |
| 101 | * @brief Compare if one sequence number is lower |
| 102 | * @param seq Another sequence number to compare with |
| 103 | * |
| 104 | * tuple (session1, seq1) is less than (session2, seq2) in two cases: |
| 105 | * 1. session1 < session2 |
| 106 | * 2. session1 == session2 and seq1 < seq2 |
| 107 | */ |
| 108 | bool |
| 109 | operator < (const SeqNo &seq) const |
| 110 | { |
| 111 | return m_session < seq.m_session || (m_session == seq.m_session && m_seq < seq.m_seq); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @brief Compare if two sequence numbers are equal |
| 116 | * @param seq Another sequence number to compare with |
| 117 | */ |
| 118 | bool |
| 119 | operator == (const SeqNo &seq) const |
| 120 | { |
| 121 | return m_session == seq.m_session && m_seq == seq.m_seq; |
| 122 | } |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 123 | |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 124 | bool |
| 125 | isValid () const |
| 126 | { |
| 127 | return m_valid; |
| 128 | } |
| 129 | |
Alexander Afanasyev | 1fbdcdd | 2012-03-05 23:14:33 -0800 | [diff] [blame] | 130 | /** |
| 131 | * @brief Get session id |
| 132 | */ |
| 133 | uint32_t getSession () const |
| 134 | { return m_session; } |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 135 | |
Alexander Afanasyev | 1fbdcdd | 2012-03-05 23:14:33 -0800 | [diff] [blame] | 136 | /** |
| 137 | * @brief Get sequence number |
| 138 | */ |
| 139 | uint32_t getSeq () const |
| 140 | { return m_seq; } |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 141 | |
| 142 | private: |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 143 | bool m_valid; |
| 144 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 145 | /** |
| 146 | * @brief Session ID (e.g., after crash, application will choose new session ID. |
| 147 | * |
| 148 | * Note that session IDs for the same name should always increase. So, the good choice |
| 149 | * for the session ID is client's timestamp |
| 150 | */ |
| 151 | uint32_t m_session; |
| 152 | |
| 153 | /** |
| 154 | * @brief Sequence number |
| 155 | * |
| 156 | * Sequence number for a session always starts with 0 and goes to max value. |
| 157 | * |
| 158 | * For now, wrapping sequence number after max to zero is not supported |
| 159 | */ |
| 160 | uint32_t m_seq; |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 161 | }; |
| 162 | |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 163 | inline std::ostream & |
| 164 | operator << (std::ostream &os, const SeqNo &seqno) |
| 165 | { |
Alexander Afanasyev | 64d5069 | 2012-03-07 20:48:35 -0800 | [diff] [blame] | 166 | os << "<session>" << seqno.getSession () << "</session><seqno>" << seqno.getSeq () << "</seqno>"; |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 167 | return os; |
| 168 | } |
| 169 | |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 170 | } // Sync |
| 171 | |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 172 | #endif // SYNC_SEQ_NO_H |