blob: 5ddaace0790ab3026d92cc1d9d328582bc390c87 [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>
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080022 */
23
24#include "sync-diff-state.h"
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080025#include "sync-diff-leaf.h"
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080026
27#include <boost/make_shared.hpp>
Alexander Afanasyev64d50692012-03-07 20:48:35 -080028#include <boost/foreach.hpp>
29#include <boost/assert.hpp>
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080030
31using namespace boost;
32
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080033namespace Sync {
34
35DiffState::DiffState ()
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080036{
37}
38
39DiffState::~DiffState ()
40{
41}
42
43DiffStatePtr
44DiffState::diff () const
45{
46 DiffStatePtr ret = make_shared<DiffState> ();
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070047
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080048 DiffStatePtr state = m_next;
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080049 while (state != 0)
50 {
51 *ret += *state;
52 state = state->m_next;
53 }
54
55 return ret;
56}
57
58DiffState &
59DiffState::operator += (const DiffState &state)
60{
Alexander Afanasyeva36b7512012-03-12 16:03:03 -070061 BOOST_FOREACH (LeafConstPtr _leaf, state.getLeaves ())
62 {
63 DiffLeafConstPtr leaf = dynamic_pointer_cast<const DiffLeaf> (_leaf);
64 BOOST_ASSERT (leaf != 0);
65
66 if (leaf->getOperation () == UPDATE)
67 update (leaf->getInfo (), leaf->getSeq ());
68 else if (leaf->getOperation () == REMOVE)
69 remove (leaf->getInfo ());
70 else
71 {
72 BOOST_ASSERT (false);
73 }
74 }
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070075
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080076 return *this;
77}
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070078
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080079// from State
Alexander Afanasyev750d1872012-03-12 15:33:56 -070080boost::tuple<bool/*inserted*/, bool/*updated*/, SeqNo/*oldSeqNo*/>
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080081DiffState::update (NameInfoConstPtr info, const SeqNo &seq)
82{
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -070083 m_leaves.erase (info);
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080084
85 DiffLeafPtr leaf = make_shared<DiffLeaf> (info, cref (seq));
86 m_leaves.insert (leaf);
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -070087
Alexander Afanasyev750d1872012-03-12 15:33:56 -070088 return make_tuple (true, false, SeqNo ());
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080089}
90
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -070091bool
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080092DiffState::remove (NameInfoConstPtr info)
93{
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -070094 m_leaves.erase (info);
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080095
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080096 DiffLeafPtr leaf = make_shared<DiffLeaf> (info);
97 m_leaves.insert (leaf);
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -070098
99 return true;
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800100}
101
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800102} // ns3