blob: 7b8399f651f8da8f2e95c5b55b975237af1abf06 [file] [log] [blame]
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Alexander Afanasyev8722d872014-07-02 13:00:29 -07003 * Copyright (c) 2012-2014 University of California, Los Angeles
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -08004 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -07005 * This file is part of ChronoSync, synchronization library for distributed realtime
6 * applications for NDN.
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -08007 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -07008 * ChronoSync is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation, either
10 * version 3 of the License, or (at your option) any later version.
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080011 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -070012 * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080015 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -070016 * You should have received a copy of the GNU General Public License along with
17 * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/>
20 * @author Chaoyi Bian <bcy@pku.edu.cn>
21 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
22 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/web/index.html>
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080023 */
24
25#ifndef SYNC_SEQ_NO_H
26#define SYNC_SEQ_NO_H
27
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080028#include <boost/cstdint.hpp>
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080029#include "sync-digest.h"
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080030
Alexander Afanasyev017784c2012-03-02 11:44:13 -080031namespace Sync {
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080032
33/**
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080034 * @ingroup sync
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080035 * @brief Sequence number abstraction
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080036 */
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080037class SeqNo
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080038{
39public:
40 /**
Alexander Afanasyev750d1872012-03-12 15:33:56 -070041 * @brief Default constructor. Creates an zero sequence number with zero session ID (basically is an invalid object)
42 */
43 SeqNo ()
44 : m_valid (false)
45 , m_session (0)
46 , m_seq (0)
47 {
48 }
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070049
Alexander Afanasyev750d1872012-03-12 15:33:56 -070050 /**
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080051 * @brief Copy constructor
52 * @param seq sequence number object to copy from
53 */
54 SeqNo (const SeqNo &seq)
55 {
56 *this = seq;
57 }
58
59 /**
60 * @brief Assignment operator
61 * @param seq sequence number object to copy from
62 */
63 SeqNo &
64 operator = (const SeqNo &seq)
65 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -070066 m_valid = seq.m_valid;
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080067 m_session = seq.m_session;
68 m_seq = seq.m_seq;
69
70 return *this;
71 }
72
73 /**
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080074 * @brief Constructor with just sequence number. Session assumed to be zero
75 * @param seq Sequence number
76 */
Yingdi Yu280bb962014-01-30 09:52:43 -080077 SeqNo (uint64_t seq)
Alexander Afanasyev750d1872012-03-12 15:33:56 -070078 : m_valid (true)
79 , m_session (0)
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080080 , m_seq (seq)
81 { }
82
83 /**
84 * @brief Constructor with session and sequence id
85 * @param session Session ID
86 * @param seq Sequence number
87 */
Yingdi Yu280bb962014-01-30 09:52:43 -080088 SeqNo (uint64_t session, uint64_t seq)
Alexander Afanasyev750d1872012-03-12 15:33:56 -070089 : m_valid (true)
90 , m_session (session)
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080091 , m_seq (seq)
92 { }
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080093
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080094 /**
95 * @brief Get sequence number digest
96 *
97 * Digest will be calculated every time it is requested
98 */
99 DigestConstPtr
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800100 getDigest () const;
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800101
102 /**
103 * @brief Compare if one sequence number is lower
104 * @param seq Another sequence number to compare with
105 *
106 * tuple (session1, seq1) is less than (session2, seq2) in two cases:
107 * 1. session1 < session2
108 * 2. session1 == session2 and seq1 < seq2
109 */
110 bool
111 operator < (const SeqNo &seq) const
112 {
113 return m_session < seq.m_session || (m_session == seq.m_session && m_seq < seq.m_seq);
114 }
115
116 /**
117 * @brief Compare if two sequence numbers are equal
118 * @param seq Another sequence number to compare with
119 */
120 bool
121 operator == (const SeqNo &seq) const
122 {
123 return m_session == seq.m_session && m_seq == seq.m_seq;
124 }
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800125
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700126 bool
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700127 operator <= (const SeqNo &seq) const
128 {
Zhenkai Zhua30e1782012-06-01 11:52:57 -0700129 return m_session == seq.m_session && m_seq <= seq.m_seq;
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700130 }
131
132 SeqNo &
133 operator ++ ()
134 {
Zhenkai Zhua30e1782012-06-01 11:52:57 -0700135 if (m_valid) {
136 m_seq ++;
137 }
138 else {
139 m_valid = true;
140 }
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700141 return *this;
142 }
143
144 bool
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700145 isValid () const
146 {
147 return m_valid;
148 }
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700149
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -0800150 /**
151 * @brief Get session id
152 */
Yingdi Yu280bb962014-01-30 09:52:43 -0800153 uint64_t getSession () const
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -0800154 { return m_session; }
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800155
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -0800156 /**
157 * @brief Get sequence number
158 */
Yingdi Yu280bb962014-01-30 09:52:43 -0800159 uint64_t getSeq () const
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -0800160 { return m_seq; }
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700161
162 /**
163 * @brief Set sequence number
164 */
165 void
Yingdi Yu280bb962014-01-30 09:52:43 -0800166 setSeq(uint64_t seq)
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700167 { m_seq = seq; }
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700168
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800169private:
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700170 bool m_valid;
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700171
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800172 /**
173 * @brief Session ID (e.g., after crash, application will choose new session ID.
174 *
175 * Note that session IDs for the same name should always increase. So, the good choice
176 * for the session ID is client's timestamp
177 */
Yingdi Yu280bb962014-01-30 09:52:43 -0800178 uint64_t m_session;
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800179
180 /**
181 * @brief Sequence number
182 *
183 * Sequence number for a session always starts with 0 and goes to max value.
184 *
185 * For now, wrapping sequence number after max to zero is not supported
186 */
Yingdi Yu280bb962014-01-30 09:52:43 -0800187 uint64_t m_seq;
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800188};
189
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -0800190inline std::ostream &
191operator << (std::ostream &os, const SeqNo &seqno)
192{
Alexander Afanasyev64d50692012-03-07 20:48:35 -0800193 os << "<session>" << seqno.getSession () << "</session><seqno>" << seqno.getSeq () << "</seqno>";
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -0800194 return os;
195}
196
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800197} // Sync
198
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800199#endif // SYNC_SEQ_NO_H