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> |
| 27 | |
Alexander Afanasyev | 017784c | 2012-03-02 11:44:13 -0800 | [diff] [blame] | 28 | namespace Sync { |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * @brief Sequence number abstraction |
| 32 | * |
| 33 | * |
| 34 | */ |
| 35 | struct SeqNo |
| 36 | { |
| 37 | public: |
| 38 | /** |
| 39 | * @brief Constructor with just sequence number. Session assumed to be zero |
| 40 | * @param seq Sequence number |
| 41 | */ |
| 42 | SeqNo (uint32_t seq) |
| 43 | : m_session (0) |
| 44 | , m_seq (seq) |
| 45 | { } |
| 46 | |
| 47 | /** |
| 48 | * @brief Constructor with session and sequence id |
| 49 | * @param session Session ID |
| 50 | * @param seq Sequence number |
| 51 | */ |
| 52 | SeqNo (uint32_t session, uint32_t seq) |
| 53 | : m_session (session) |
| 54 | , m_seq (seq) |
| 55 | { } |
| 56 | |
| 57 | /** |
| 58 | * @brief Session ID (e.g., after crash, application will choose new session ID. |
| 59 | * |
| 60 | * Note that session IDs for the same name should always increase. So, the good choice |
| 61 | * for the session ID is client's timestamp |
| 62 | */ |
| 63 | uint32_t m_session; |
| 64 | |
| 65 | /** |
| 66 | * @brief Sequence number |
| 67 | * |
| 68 | * Sequence number for a session always starts with 0 and goes to max value. |
| 69 | * |
| 70 | * For now, wrapping sequence number after max to zero is not supported |
| 71 | */ |
| 72 | uint32_t m_seq; |
| 73 | |
| 74 | /** |
| 75 | * @brief Compare if one sequence number is lower |
| 76 | * @param seq Another sequence number to compare with |
| 77 | * |
| 78 | * tuple (session1, seq1) is less than (session2, seq2) in two cases: |
| 79 | * 1. session1 < session2 |
| 80 | * 2. session1 == session2 and seq1 < seq2 |
| 81 | */ |
| 82 | bool |
| 83 | operator < (const SeqNo &seq) const |
| 84 | { |
| 85 | return m_session < seq.m_session || (m_session == seq.m_session && m_seq < seq.m_seq); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @brief Compare if two sequence numbers are equal |
| 90 | * @param seq Another sequence number to compare with |
| 91 | */ |
| 92 | bool |
| 93 | operator == (const SeqNo &seq) const |
| 94 | { |
| 95 | return m_session == seq.m_session && m_seq == seq.m_seq; |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | } // Sync |
| 100 | |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 101 | #endif // SYNC_SEQ_NO_H |