akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 1 | /* -*- 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 Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame^] | 20 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include "sync-diff-state.h" |
| 24 | #include "sync-diff-leaf.h" |
| 25 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 26 | #include <boost/foreach.hpp> |
| 27 | #include <boost/assert.hpp> |
| 28 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 29 | namespace Sync { |
| 30 | |
| 31 | DiffState::DiffState () |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | DiffState::~DiffState () |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | DiffStatePtr |
| 40 | DiffState::diff () const |
| 41 | { |
| 42 | DiffStatePtr ret = make_shared<DiffState> (); |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame^] | 43 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 44 | DiffStatePtr state = m_next; |
| 45 | while (state != 0) |
| 46 | { |
| 47 | *ret += *state; |
| 48 | state = state->m_next; |
| 49 | } |
| 50 | |
| 51 | return ret; |
| 52 | } |
| 53 | |
| 54 | DiffState & |
| 55 | DiffState::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 Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame^] | 71 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 72 | return *this; |
| 73 | } |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame^] | 74 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 75 | // from State |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame^] | 76 | tuple<bool/*inserted*/, bool/*updated*/, SeqNo/*oldSeqNo*/> |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 77 | DiffState::update (NameInfoConstPtr info, const SeqNo &seq) |
| 78 | { |
| 79 | m_leaves.erase (info); |
| 80 | |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame^] | 81 | DiffLeafPtr leaf = make_shared<DiffLeaf> (info, seq); |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 82 | m_leaves.insert (leaf); |
| 83 | |
| 84 | return make_tuple (true, false, SeqNo ()); |
| 85 | } |
| 86 | |
| 87 | bool |
| 88 | DiffState::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 |