blob: 7000606a5a6a376ea7bf5481708bc07031d5973b [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>
Chaoyi Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
22
23#include "sync-full-state.h"
24
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080025#include <boost/make_shared.hpp>
26#include <boost/lambda/lambda.hpp>
27#include <boost/lambda/bind.hpp>
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080028#include <boost/foreach.hpp>
29#include <boost/assert.hpp>
30
31#include "sync-full-leaf.h"
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080032
33using namespace boost;
34namespace ll = boost::lambda;
35
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080036namespace Sync {
37
38
39FullState::FullState ()
Zhenkai Zhuea026982012-06-01 16:00:25 -070040// m_lastUpdated is initialized to "not_a_date_time" in normal lib mode and to "0" time in NS-3 mode
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080041{
42}
43
44FullState::~FullState ()
45{
46}
47
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -070048ndn::time::system_clock::Duration
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080049FullState::getTimeFromLastUpdate () const
50{
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -070051 return ndn::time::system_clock::now() - m_lastUpdated;
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080052}
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080053
54DigestConstPtr
55FullState::getDigest ()
56{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -080057 if (!m_digest)
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080058 {
59 m_digest = make_shared<Digest> ();
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070060 if (m_leaves.get<ordered> ().size () > 0)
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080061 {
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070062 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 ();
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070067 }
68 m_digest->finalize ();
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080069 }
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070070 else
71 {
72 std::istringstream is ("00"); //zero state
73 is >> *m_digest;
74 }
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080075 }
76
77 return m_digest;
78}
79
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080080// from State
Alexander Afanasyev750d1872012-03-12 15:33:56 -070081boost::tuple<bool/*inserted*/, bool/*updated*/, SeqNo/*oldSeqNo*/>
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080082FullState::update (NameInfoConstPtr info, const SeqNo &seq)
83{
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -070084 m_lastUpdated = ndn::time::system_clock::now();
Alexander Afanasyev531803b2014-02-05 15:57:35 -080085
Alexander Afanasyev146a51b2012-03-05 10:47:35 -080086
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080087 m_digest.reset ();
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080088
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -070089 LeafContainer::iterator item = m_leaves.find (info);
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080090 if (item == m_leaves.end ())
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080091 {
Alexander Afanasyev58c77b02012-03-05 21:52:25 -080092 m_leaves.insert (make_shared<FullLeaf> (info, cref (seq)));
Alexander Afanasyev750d1872012-03-12 15:33:56 -070093 return make_tuple (true, false, SeqNo ());
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080094 }
95 else
96 {
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -070097 if ((*item)->getSeq () == seq || seq < (*item)->getSeq ())
Alexander Afanasyev750d1872012-03-12 15:33:56 -070098 {
99 return make_tuple (false, false, SeqNo ());
100 }
101
102 SeqNo old = (*item)->getSeq ();
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700103 m_leaves.modify (item,
104 ll::bind (&Leaf::setSeq, *ll::_1, seq));
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700105 return make_tuple (false, true, old);
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800106 }
107}
108
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700109bool
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800110FullState::remove (NameInfoConstPtr info)
111{
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700112 m_lastUpdated = ndn::time::system_clock::now();
Alexander Afanasyev146a51b2012-03-05 10:47:35 -0800113
Alexander Afanasyevd94542d2012-03-05 08:41:46 -0800114 m_digest.reset ();
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800115
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700116 LeafContainer::iterator item = m_leaves.find (info);
117 if (item != m_leaves.end ())
118 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700119 m_leaves.erase (item);
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700120 return true;
121 }
122 else
123 return false;
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800124}
125
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800126} // Sync