blob: e2ed0162c0152ebcd2abc0f3e36ff08c2df263da [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>
21 */
22
23#ifndef SYNC_SEQ_NO_H
24#define SYNC_SEQ_NO_H
25
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080026#include <boost/cstdint.hpp>
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080027#include "sync-digest.h"
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080028
Alexander Afanasyev017784c2012-03-02 11:44:13 -080029namespace Sync {
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080030
31/**
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080032 * @ingroup sync
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080033 * @brief Sequence number abstraction
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080034 */
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080035class SeqNo
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080036{
37public:
38 /**
Alexander Afanasyev750d1872012-03-12 15:33:56 -070039 * @brief Default constructor. Creates an zero sequence number with zero session ID (basically is an invalid object)
40 */
41 SeqNo ()
42 : m_valid (false)
43 , m_session (0)
44 , m_seq (0)
45 {
46 }
47
48 /**
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080049 * @brief Copy constructor
50 * @param seq sequence number object to copy from
51 */
52 SeqNo (const SeqNo &seq)
53 {
54 *this = seq;
55 }
56
57 /**
58 * @brief Assignment operator
59 * @param seq sequence number object to copy from
60 */
61 SeqNo &
62 operator = (const SeqNo &seq)
63 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -070064 m_valid = seq.m_valid;
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080065 m_session = seq.m_session;
66 m_seq = seq.m_seq;
67
68 return *this;
69 }
70
71 /**
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080072 * @brief Constructor with just sequence number. Session assumed to be zero
73 * @param seq Sequence number
74 */
75 SeqNo (uint32_t seq)
Alexander Afanasyev750d1872012-03-12 15:33:56 -070076 : m_valid (true)
77 , m_session (0)
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080078 , m_seq (seq)
79 { }
80
81 /**
82 * @brief Constructor with session and sequence id
83 * @param session Session ID
84 * @param seq Sequence number
85 */
86 SeqNo (uint32_t session, uint32_t seq)
Alexander Afanasyev750d1872012-03-12 15:33:56 -070087 : m_valid (true)
88 , m_session (session)
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080089 , m_seq (seq)
90 { }
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080091
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080092 /**
93 * @brief Get sequence number digest
94 *
95 * Digest will be calculated every time it is requested
96 */
97 DigestConstPtr
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080098 getDigest () const;
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080099
100 /**
101 * @brief Compare if one sequence number is lower
102 * @param seq Another sequence number to compare with
103 *
104 * tuple (session1, seq1) is less than (session2, seq2) in two cases:
105 * 1. session1 < session2
106 * 2. session1 == session2 and seq1 < seq2
107 */
108 bool
109 operator < (const SeqNo &seq) const
110 {
111 return m_session < seq.m_session || (m_session == seq.m_session && m_seq < seq.m_seq);
112 }
113
114 /**
115 * @brief Compare if two sequence numbers are equal
116 * @param seq Another sequence number to compare with
117 */
118 bool
119 operator == (const SeqNo &seq) const
120 {
121 return m_session == seq.m_session && m_seq == seq.m_seq;
122 }
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800123
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700124 bool
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700125 operator <= (const SeqNo &seq) const
126 {
Zhenkai Zhua30e1782012-06-01 11:52:57 -0700127 return m_session == seq.m_session && m_seq <= seq.m_seq;
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700128 }
129
130 SeqNo &
131 operator ++ ()
132 {
Zhenkai Zhua30e1782012-06-01 11:52:57 -0700133 if (m_valid) {
134 m_seq ++;
135 }
136 else {
137 m_valid = true;
138 }
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700139 return *this;
140 }
141
142 bool
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700143 isValid () const
144 {
145 return m_valid;
146 }
147
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -0800148 /**
149 * @brief Get session id
150 */
151 uint32_t getSession () const
152 { return m_session; }
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800153
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -0800154 /**
155 * @brief Get sequence number
156 */
157 uint32_t getSeq () const
158 { return m_seq; }
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700159
160 /**
161 * @brief Set sequence number
162 */
163 void
164 setSeq(uint32_t seq)
165 { m_seq = seq; }
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800166
167private:
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700168 bool m_valid;
169
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800170 /**
171 * @brief Session ID (e.g., after crash, application will choose new session ID.
172 *
173 * Note that session IDs for the same name should always increase. So, the good choice
174 * for the session ID is client's timestamp
175 */
176 uint32_t m_session;
177
178 /**
179 * @brief Sequence number
180 *
181 * Sequence number for a session always starts with 0 and goes to max value.
182 *
183 * For now, wrapping sequence number after max to zero is not supported
184 */
185 uint32_t m_seq;
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800186};
187
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -0800188inline std::ostream &
189operator << (std::ostream &os, const SeqNo &seqno)
190{
Alexander Afanasyev64d50692012-03-07 20:48:35 -0800191 os << "<session>" << seqno.getSession () << "</session><seqno>" << seqno.getSeq () << "</seqno>";
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -0800192 return os;
193}
194
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800195} // Sync
196
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800197#endif // SYNC_SEQ_NO_H