blob: bf217045e4d1a0ded355fbd6c16b5294236b281e [file] [log] [blame]
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -08001/* -*- 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>
Chaoyi Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Yingdi Yu280bb962014-01-30 09:52:43 -080021 * Yingdi Yu <yingdi@cs.ucla.edu>
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080022 */
23
24#ifndef SYNC_SEQ_NO_H
25#define SYNC_SEQ_NO_H
26
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080027#include <boost/cstdint.hpp>
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080028#include "sync-digest.h"
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080029
Alexander Afanasyev017784c2012-03-02 11:44:13 -080030namespace Sync {
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080031
32/**
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080033 * @ingroup sync
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080034 * @brief Sequence number abstraction
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080035 */
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080036class SeqNo
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080037{
38public:
39 /**
Alexander Afanasyev750d1872012-03-12 15:33:56 -070040 * @brief Default constructor. Creates an zero sequence number with zero session ID (basically is an invalid object)
41 */
42 SeqNo ()
43 : m_valid (false)
44 , m_session (0)
45 , m_seq (0)
46 {
47 }
48
49 /**
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080050 * @brief Copy constructor
51 * @param seq sequence number object to copy from
52 */
53 SeqNo (const SeqNo &seq)
54 {
55 *this = seq;
56 }
57
58 /**
59 * @brief Assignment operator
60 * @param seq sequence number object to copy from
61 */
62 SeqNo &
63 operator = (const SeqNo &seq)
64 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -070065 m_valid = seq.m_valid;
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080066 m_session = seq.m_session;
67 m_seq = seq.m_seq;
68
69 return *this;
70 }
71
72 /**
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080073 * @brief Constructor with just sequence number. Session assumed to be zero
74 * @param seq Sequence number
75 */
Yingdi Yu280bb962014-01-30 09:52:43 -080076 SeqNo (uint64_t seq)
Alexander Afanasyev750d1872012-03-12 15:33:56 -070077 : m_valid (true)
78 , m_session (0)
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080079 , m_seq (seq)
80 { }
81
82 /**
83 * @brief Constructor with session and sequence id
84 * @param session Session ID
85 * @param seq Sequence number
86 */
Yingdi Yu280bb962014-01-30 09:52:43 -080087 SeqNo (uint64_t session, uint64_t seq)
Alexander Afanasyev750d1872012-03-12 15:33:56 -070088 : m_valid (true)
89 , m_session (session)
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080090 , m_seq (seq)
91 { }
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080092
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080093 /**
94 * @brief Get sequence number digest
95 *
96 * Digest will be calculated every time it is requested
97 */
98 DigestConstPtr
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080099 getDigest () const;
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800100
101 /**
102 * @brief Compare if one sequence number is lower
103 * @param seq Another sequence number to compare with
104 *
105 * tuple (session1, seq1) is less than (session2, seq2) in two cases:
106 * 1. session1 < session2
107 * 2. session1 == session2 and seq1 < seq2
108 */
109 bool
110 operator < (const SeqNo &seq) const
111 {
112 return m_session < seq.m_session || (m_session == seq.m_session && m_seq < seq.m_seq);
113 }
114
115 /**
116 * @brief Compare if two sequence numbers are equal
117 * @param seq Another sequence number to compare with
118 */
119 bool
120 operator == (const SeqNo &seq) const
121 {
122 return m_session == seq.m_session && m_seq == seq.m_seq;
123 }
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800124
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700125 bool
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700126 operator <= (const SeqNo &seq) const
127 {
Zhenkai Zhua30e1782012-06-01 11:52:57 -0700128 return m_session == seq.m_session && m_seq <= seq.m_seq;
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700129 }
130
131 SeqNo &
132 operator ++ ()
133 {
Zhenkai Zhua30e1782012-06-01 11:52:57 -0700134 if (m_valid) {
135 m_seq ++;
136 }
137 else {
138 m_valid = true;
139 }
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700140 return *this;
141 }
142
143 bool
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700144 isValid () const
145 {
146 return m_valid;
147 }
148
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -0800149 /**
150 * @brief Get session id
151 */
Yingdi Yu280bb962014-01-30 09:52:43 -0800152 uint64_t getSession () const
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -0800153 { return m_session; }
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800154
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -0800155 /**
156 * @brief Get sequence number
157 */
Yingdi Yu280bb962014-01-30 09:52:43 -0800158 uint64_t getSeq () const
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -0800159 { return m_seq; }
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700160
161 /**
162 * @brief Set sequence number
163 */
164 void
Yingdi Yu280bb962014-01-30 09:52:43 -0800165 setSeq(uint64_t seq)
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700166 { m_seq = seq; }
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800167
168private:
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700169 bool m_valid;
170
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800171 /**
172 * @brief Session ID (e.g., after crash, application will choose new session ID.
173 *
174 * Note that session IDs for the same name should always increase. So, the good choice
175 * for the session ID is client's timestamp
176 */
Yingdi Yu280bb962014-01-30 09:52:43 -0800177 uint64_t m_session;
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800178
179 /**
180 * @brief Sequence number
181 *
182 * Sequence number for a session always starts with 0 and goes to max value.
183 *
184 * For now, wrapping sequence number after max to zero is not supported
185 */
Yingdi Yu280bb962014-01-30 09:52:43 -0800186 uint64_t m_seq;
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800187};
188
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -0800189inline std::ostream &
190operator << (std::ostream &os, const SeqNo &seqno)
191{
Alexander Afanasyev64d50692012-03-07 20:48:35 -0800192 os << "<session>" << seqno.getSession () << "</session><seqno>" << seqno.getSeq () << "</seqno>";
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -0800193 return os;
194}
195
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800196} // Sync
197
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800198#endif // SYNC_SEQ_NO_H