blob: fb9e6b87c569096963fae798eab765c25f95dae8 [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/**
32 * @brief Sequence number abstraction
33 *
34 *
35 */
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080036class SeqNo
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080037{
38public:
39 /**
40 * @brief Constructor with just sequence number. Session assumed to be zero
41 * @param seq Sequence number
42 */
43 SeqNo (uint32_t seq)
44 : m_session (0)
45 , m_seq (seq)
46 { }
47
48 /**
49 * @brief Constructor with session and sequence id
50 * @param session Session ID
51 * @param seq Sequence number
52 */
53 SeqNo (uint32_t session, uint32_t seq)
54 : m_session (session)
55 , m_seq (seq)
56 { }
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080057
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080058 inline Digest
59 getDigest () const;
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080060
61 /**
62 * @brief Compare if one sequence number is lower
63 * @param seq Another sequence number to compare with
64 *
65 * tuple (session1, seq1) is less than (session2, seq2) in two cases:
66 * 1. session1 < session2
67 * 2. session1 == session2 and seq1 < seq2
68 */
69 bool
70 operator < (const SeqNo &seq) const
71 {
72 return m_session < seq.m_session || (m_session == seq.m_session && m_seq < seq.m_seq);
73 }
74
75 /**
76 * @brief Compare if two sequence numbers are equal
77 * @param seq Another sequence number to compare with
78 */
79 bool
80 operator == (const SeqNo &seq) const
81 {
82 return m_session == seq.m_session && m_seq == seq.m_seq;
83 }
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080084
85 SeqNo &
86 operator = (const SeqNo &seq)
87 {
88 m_session = seq.m_session;
89 m_seq = seq.m_seq;
90
91 return *this;
92 }
93
94private:
95 inline void
96 updateDigest ();
97
98private:
99 /**
100 * @brief Session ID (e.g., after crash, application will choose new session ID.
101 *
102 * Note that session IDs for the same name should always increase. So, the good choice
103 * for the session ID is client's timestamp
104 */
105 uint32_t m_session;
106
107 /**
108 * @brief Sequence number
109 *
110 * Sequence number for a session always starts with 0 and goes to max value.
111 *
112 * For now, wrapping sequence number after max to zero is not supported
113 */
114 uint32_t m_seq;
115
116 Digest m_digest;
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800117};
118
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800119
120void
121SeqNo::updateDigest ()
122{
123 m_digest.reset ();
124 m_digest << m_session << m_seq;
125}
126
127Digest
128SeqNo::getDigest () const
129{
130 Digest digest;
131 return digest;
132}
133
134
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800135} // Sync
136
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800137#endif // SYNC_SEQ_NO_H