blob: 7000606a5a6a376ea7bf5481708bc07031d5973b [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>
20 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
22
23#include "sync-full-state.h"
24
25#include <boost/make_shared.hpp>
26#include <boost/lambda/lambda.hpp>
27#include <boost/lambda/bind.hpp>
28#include <boost/foreach.hpp>
29#include <boost/assert.hpp>
30
31#include "sync-full-leaf.h"
32
33using namespace boost;
34namespace ll = boost::lambda;
35
36namespace Sync {
37
38
39FullState::FullState ()
40// m_lastUpdated is initialized to "not_a_date_time" in normal lib mode and to "0" time in NS-3 mode
41{
42}
43
44FullState::~FullState ()
45{
46}
47
akmhoque05d5fcf2014-04-15 14:58:45 -050048ndn::time::system_clock::Duration
akmhoque66e66182014-02-21 17:56:03 -060049FullState::getTimeFromLastUpdate () const
50{
akmhoque05d5fcf2014-04-15 14:58:45 -050051 return ndn::time::system_clock::now() - m_lastUpdated;
akmhoque66e66182014-02-21 17:56:03 -060052}
53
54DigestConstPtr
55FullState::getDigest ()
56{
57 if (!m_digest)
58 {
59 m_digest = make_shared<Digest> ();
60 if (m_leaves.get<ordered> ().size () > 0)
61 {
62 BOOST_FOREACH (LeafConstPtr leaf, m_leaves.get<ordered> ())
63 {
64 FullLeafConstPtr fullLeaf = dynamic_pointer_cast<const FullLeaf> (leaf);
65 BOOST_ASSERT (fullLeaf != 0);
66 *m_digest << fullLeaf->getDigest ();
67 }
68 m_digest->finalize ();
69 }
70 else
71 {
72 std::istringstream is ("00"); //zero state
73 is >> *m_digest;
74 }
75 }
76
77 return m_digest;
78}
79
80// from State
81boost::tuple<bool/*inserted*/, bool/*updated*/, SeqNo/*oldSeqNo*/>
82FullState::update (NameInfoConstPtr info, const SeqNo &seq)
83{
akmhoque05d5fcf2014-04-15 14:58:45 -050084 m_lastUpdated = ndn::time::system_clock::now();
akmhoque66e66182014-02-21 17:56:03 -060085
86
87 m_digest.reset ();
88
89 LeafContainer::iterator item = m_leaves.find (info);
90 if (item == m_leaves.end ())
91 {
92 m_leaves.insert (make_shared<FullLeaf> (info, cref (seq)));
93 return make_tuple (true, false, SeqNo ());
94 }
95 else
96 {
97 if ((*item)->getSeq () == seq || seq < (*item)->getSeq ())
98 {
99 return make_tuple (false, false, SeqNo ());
100 }
101
102 SeqNo old = (*item)->getSeq ();
103 m_leaves.modify (item,
104 ll::bind (&Leaf::setSeq, *ll::_1, seq));
105 return make_tuple (false, true, old);
106 }
107}
108
109bool
110FullState::remove (NameInfoConstPtr info)
111{
akmhoque05d5fcf2014-04-15 14:58:45 -0500112 m_lastUpdated = ndn::time::system_clock::now();
akmhoque66e66182014-02-21 17:56:03 -0600113
114 m_digest.reset ();
115
116 LeafContainer::iterator item = m_leaves.find (info);
117 if (item != m_leaves.end ())
118 {
119 m_leaves.erase (item);
120 return true;
121 }
122 else
123 return false;
124}
125
126} // Sync