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