Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 3 | * Copyright (c) 2012-2014 University of California, Los Angeles |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 5 | * This file is part of ChronoSync, synchronization library for distributed realtime |
| 6 | * applications for NDN. |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 8 | * 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 Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 12 | * 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 Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 16 | * 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 Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #include "sync-diff-state.h" |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 25 | #include "sync-diff-leaf.h" |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 26 | |
| 27 | #include <boost/make_shared.hpp> |
Alexander Afanasyev | 64d5069 | 2012-03-07 20:48:35 -0800 | [diff] [blame] | 28 | #include <boost/foreach.hpp> |
| 29 | #include <boost/assert.hpp> |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 30 | |
| 31 | using namespace boost; |
| 32 | |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 33 | namespace Sync { |
| 34 | |
| 35 | DiffState::DiffState () |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 36 | { |
| 37 | } |
| 38 | |
| 39 | DiffState::~DiffState () |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | DiffStatePtr |
| 44 | DiffState::diff () const |
| 45 | { |
| 46 | DiffStatePtr ret = make_shared<DiffState> (); |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 47 | |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 48 | DiffStatePtr state = m_next; |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 49 | while (state != 0) |
| 50 | { |
| 51 | *ret += *state; |
| 52 | state = state->m_next; |
| 53 | } |
| 54 | |
| 55 | return ret; |
| 56 | } |
| 57 | |
| 58 | DiffState & |
| 59 | DiffState::operator += (const DiffState &state) |
| 60 | { |
Alexander Afanasyev | a36b751 | 2012-03-12 16:03:03 -0700 | [diff] [blame] | 61 | 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 Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 75 | |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 76 | return *this; |
| 77 | } |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 78 | |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 79 | // from State |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 80 | boost::tuple<bool/*inserted*/, bool/*updated*/, SeqNo/*oldSeqNo*/> |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 81 | DiffState::update (NameInfoConstPtr info, const SeqNo &seq) |
| 82 | { |
Alexander Afanasyev | e4e2bf7 | 2012-03-12 12:44:54 -0700 | [diff] [blame] | 83 | m_leaves.erase (info); |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 84 | |
| 85 | DiffLeafPtr leaf = make_shared<DiffLeaf> (info, cref (seq)); |
| 86 | m_leaves.insert (leaf); |
Alexander Afanasyev | e4e2bf7 | 2012-03-12 12:44:54 -0700 | [diff] [blame] | 87 | |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 88 | return make_tuple (true, false, SeqNo ()); |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Alexander Afanasyev | e4e2bf7 | 2012-03-12 12:44:54 -0700 | [diff] [blame] | 91 | bool |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 92 | DiffState::remove (NameInfoConstPtr info) |
| 93 | { |
Alexander Afanasyev | e4e2bf7 | 2012-03-12 12:44:54 -0700 | [diff] [blame] | 94 | m_leaves.erase (info); |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 95 | |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 96 | DiffLeafPtr leaf = make_shared<DiffLeaf> (info); |
| 97 | m_leaves.insert (leaf); |
Alexander Afanasyev | e4e2bf7 | 2012-03-12 12:44:54 -0700 | [diff] [blame] | 98 | |
| 99 | return true; |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 102 | } // ns3 |