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