blob: 651f6ab1f8b16c701021c168532eeaf82e3105b1 [file] [log] [blame]
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Alexander Afanasyev8722d872014-07-02 13:00:29 -07003 * Copyright (c) 2012-2014 University of California, Los Angeles
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -08004 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -07005 * This file is part of ChronoSync, synchronization library for distributed realtime
6 * applications for NDN.
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -08007 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -07008 * 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 Afanasyev7a696fb2012-03-01 17:17:22 -080011 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -070012 * 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 Afanasyev7a696fb2012-03-01 17:17:22 -080015 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -070016 * 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 Afanasyev7a696fb2012-03-01 17:17:22 -080022 */
23
24#include "sync-full-state.h"
25
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080026#include <boost/make_shared.hpp>
27#include <boost/lambda/lambda.hpp>
28#include <boost/lambda/bind.hpp>
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080029#include <boost/foreach.hpp>
30#include <boost/assert.hpp>
31
32#include "sync-full-leaf.h"
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080033
34using namespace boost;
35namespace ll = boost::lambda;
36
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080037namespace Sync {
38
39
40FullState::FullState ()
Zhenkai Zhuea026982012-06-01 16:00:25 -070041// 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 -080042{
43}
44
45FullState::~FullState ()
46{
47}
48
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -070049ndn::time::system_clock::Duration
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080050FullState::getTimeFromLastUpdate () const
51{
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -070052 return ndn::time::system_clock::now() - m_lastUpdated;
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080053}
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080054
55DigestConstPtr
56FullState::getDigest ()
57{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -080058 if (!m_digest)
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080059 {
60 m_digest = make_shared<Digest> ();
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070061 if (m_leaves.get<ordered> ().size () > 0)
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080062 {
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070063 BOOST_FOREACH (LeafConstPtr leaf, m_leaves.get<ordered> ())
64 {
65 FullLeafConstPtr fullLeaf = dynamic_pointer_cast<const FullLeaf> (leaf);
66 BOOST_ASSERT (fullLeaf != 0);
67 *m_digest << fullLeaf->getDigest ();
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070068 }
69 m_digest->finalize ();
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080070 }
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070071 else
72 {
73 std::istringstream is ("00"); //zero state
74 is >> *m_digest;
75 }
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080076 }
77
78 return m_digest;
79}
80
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080081// from State
Alexander Afanasyev750d1872012-03-12 15:33:56 -070082boost::tuple<bool/*inserted*/, bool/*updated*/, SeqNo/*oldSeqNo*/>
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080083FullState::update (NameInfoConstPtr info, const SeqNo &seq)
84{
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -070085 m_lastUpdated = ndn::time::system_clock::now();
Alexander Afanasyev531803b2014-02-05 15:57:35 -080086
Alexander Afanasyev146a51b2012-03-05 10:47:35 -080087
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080088 m_digest.reset ();
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080089
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -070090 LeafContainer::iterator item = m_leaves.find (info);
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080091 if (item == m_leaves.end ())
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080092 {
Alexander Afanasyev58c77b02012-03-05 21:52:25 -080093 m_leaves.insert (make_shared<FullLeaf> (info, cref (seq)));
Alexander Afanasyev750d1872012-03-12 15:33:56 -070094 return make_tuple (true, false, SeqNo ());
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080095 }
96 else
97 {
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -070098 if ((*item)->getSeq () == seq || seq < (*item)->getSeq ())
Alexander Afanasyev750d1872012-03-12 15:33:56 -070099 {
100 return make_tuple (false, false, SeqNo ());
101 }
102
103 SeqNo old = (*item)->getSeq ();
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700104 m_leaves.modify (item,
105 ll::bind (&Leaf::setSeq, *ll::_1, seq));
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700106 return make_tuple (false, true, old);
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800107 }
108}
109
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700110bool
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800111FullState::remove (NameInfoConstPtr info)
112{
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700113 m_lastUpdated = ndn::time::system_clock::now();
Alexander Afanasyev146a51b2012-03-05 10:47:35 -0800114
Alexander Afanasyevd94542d2012-03-05 08:41:46 -0800115 m_digest.reset ();
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800116
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700117 LeafContainer::iterator item = m_leaves.find (info);
118 if (item != m_leaves.end ())
119 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700120 m_leaves.erase (item);
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700121 return true;
122 }
123 else
124 return false;
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800125}
126
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800127} // Sync