blob: b4329d20ae01d7ce28d1ea134121142ebf2a9558 [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>
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 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
125 isValid () const
126 {
127 return m_valid;
128 }
129
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -0800130 /**
131 * @brief Get session id
132 */
133 uint32_t getSession () const
134 { return m_session; }
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800135
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -0800136 /**
137 * @brief Get sequence number
138 */
139 uint32_t getSeq () const
140 { return m_seq; }
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800141
142private:
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700143 bool m_valid;
144
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800145 /**
146 * @brief Session ID (e.g., after crash, application will choose new session ID.
147 *
148 * Note that session IDs for the same name should always increase. So, the good choice
149 * for the session ID is client's timestamp
150 */
151 uint32_t m_session;
152
153 /**
154 * @brief Sequence number
155 *
156 * Sequence number for a session always starts with 0 and goes to max value.
157 *
158 * For now, wrapping sequence number after max to zero is not supported
159 */
160 uint32_t m_seq;
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800161};
162
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -0800163inline std::ostream &
164operator << (std::ostream &os, const SeqNo &seqno)
165{
Alexander Afanasyev64d50692012-03-07 20:48:35 -0800166 os << "<session>" << seqno.getSession () << "</session><seqno>" << seqno.getSeq () << "</seqno>";
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -0800167 return os;
168}
169
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800170} // Sync
171
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800172#endif // SYNC_SEQ_NO_H