blob: 715ba47fc802bc6943e9fb1a12cb60a5a207123a [file] [log] [blame]
akmhoque66e66182014-02-21 17:56:03 -06001/* -*- 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>
Vince Lehman0a7da612014-10-29 14:39:29 -050020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
akmhoque66e66182014-02-21 17:56:03 -060021 */
22
23#include "sync-diff-state.h"
24#include "sync-diff-leaf.h"
25
akmhoque66e66182014-02-21 17:56:03 -060026#include <boost/foreach.hpp>
27#include <boost/assert.hpp>
28
akmhoque66e66182014-02-21 17:56:03 -060029namespace Sync {
30
31DiffState::DiffState ()
32{
33}
34
35DiffState::~DiffState ()
36{
37}
38
39DiffStatePtr
40DiffState::diff () const
41{
42 DiffStatePtr ret = make_shared<DiffState> ();
Vince Lehman0a7da612014-10-29 14:39:29 -050043
akmhoque66e66182014-02-21 17:56:03 -060044 DiffStatePtr state = m_next;
45 while (state != 0)
46 {
47 *ret += *state;
48 state = state->m_next;
49 }
50
51 return ret;
52}
53
54DiffState &
55DiffState::operator += (const DiffState &state)
56{
57 BOOST_FOREACH (LeafConstPtr _leaf, state.getLeaves ())
58 {
59 DiffLeafConstPtr leaf = dynamic_pointer_cast<const DiffLeaf> (_leaf);
60 BOOST_ASSERT (leaf != 0);
61
62 if (leaf->getOperation () == UPDATE)
63 update (leaf->getInfo (), leaf->getSeq ());
64 else if (leaf->getOperation () == REMOVE)
65 remove (leaf->getInfo ());
66 else
67 {
68 BOOST_ASSERT (false);
69 }
70 }
Vince Lehman0a7da612014-10-29 14:39:29 -050071
akmhoque66e66182014-02-21 17:56:03 -060072 return *this;
73}
Vince Lehman0a7da612014-10-29 14:39:29 -050074
akmhoque66e66182014-02-21 17:56:03 -060075// from State
Vince Lehman0a7da612014-10-29 14:39:29 -050076tuple<bool/*inserted*/, bool/*updated*/, SeqNo/*oldSeqNo*/>
akmhoque66e66182014-02-21 17:56:03 -060077DiffState::update (NameInfoConstPtr info, const SeqNo &seq)
78{
79 m_leaves.erase (info);
80
Vince Lehman0a7da612014-10-29 14:39:29 -050081 DiffLeafPtr leaf = make_shared<DiffLeaf> (info, seq);
akmhoque66e66182014-02-21 17:56:03 -060082 m_leaves.insert (leaf);
83
84 return make_tuple (true, false, SeqNo ());
85}
86
87bool
88DiffState::remove (NameInfoConstPtr info)
89{
90 m_leaves.erase (info);
91
92 DiffLeafPtr leaf = make_shared<DiffLeaf> (info);
93 m_leaves.insert (leaf);
94
95 return true;
96}
97
98} // ns3