blob: 5a53e81370fc8f47e0c156070ace3a0d16fe67ba [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#include "sync-diff-state.h"
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080024#include "sync-diff-leaf.h"
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080025
26#include <boost/make_shared.hpp>
Alexander Afanasyev64d50692012-03-07 20:48:35 -080027#include <boost/foreach.hpp>
28#include <boost/assert.hpp>
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080029
30using namespace boost;
31
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080032namespace Sync {
33
34DiffState::DiffState ()
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080035{
36}
37
38DiffState::~DiffState ()
39{
40}
41
42DiffStatePtr
43DiffState::diff () const
44{
45 DiffStatePtr ret = make_shared<DiffState> ();
46
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080047 DiffStatePtr state = m_next;
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080048 while (state != 0)
49 {
50 *ret += *state;
51 state = state->m_next;
52 }
53
54 return ret;
55}
56
57DiffState &
58DiffState::operator += (const DiffState &state)
59{
60 return *this;
61}
62
63// from State
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080064void
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080065DiffState::update (NameInfoConstPtr info, const SeqNo &seq)
66{
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080067 m_leaves.erase (*info);
68
69 DiffLeafPtr leaf = make_shared<DiffLeaf> (info, cref (seq));
70 m_leaves.insert (leaf);
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080071}
72
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080073void
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080074DiffState::remove (NameInfoConstPtr info)
75{
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080076 m_leaves.erase (*info);
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080077
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080078 DiffLeafPtr leaf = make_shared<DiffLeaf> (info);
79 m_leaves.insert (leaf);
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080080}
81
Alexander Afanasyev64d50692012-03-07 20:48:35 -080082#ifdef _DEBUG
83#define DEBUG_ENDL os << "\n";
84#else
85#define DEBUG_ENDL
86#endif
87
88std::ostream &
89operator << (std::ostream &os, const DiffState &state)
90{
91 os << "<state type=\"diff\">"; DEBUG_ENDL;
92
93 BOOST_FOREACH (shared_ptr<const Leaf> _leaf, state.getLeaves ())
94 {
95 shared_ptr<const DiffLeaf> leaf = dynamic_pointer_cast<const DiffLeaf> (_leaf);
96 BOOST_ASSERT (leaf != 0);
97
98 os << "<item action=\"" << leaf->getOperation () << "\">"; DEBUG_ENDL;
99 os << "<name>" << leaf->getInfo () << "</name>"; DEBUG_ENDL;
100 if (leaf->getOperation () == UPDATE)
101 {
102 os << "<seq>" << leaf->getSeq () << "</seq>"; DEBUG_ENDL;
103 }
104 os << "</item>"; DEBUG_ENDL;
105 }
106 os << "</state>";
107}
108
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800109
110} // ns3