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