| /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| /* |
| * Copyright (c) 2012-2014 University of California, Los Angeles |
| * |
| * This file is part of ChronoSync, synchronization library for distributed realtime |
| * applications for NDN. |
| * |
| * ChronoSync is free software: you can redistribute it and/or modify it under the terms |
| * of the GNU General Public License as published by the Free Software Foundation, either |
| * version 3 of the License, or (at your option) any later version. |
| * |
| * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| * PURPOSE. See the GNU General Public License for more details. |
| * |
| * You should have received a copy of the GNU General Public License along with |
| * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| * |
| * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/> |
| * @author Chaoyi Bian <bcy@pku.edu.cn> |
| * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
| */ |
| |
| #include "sync-full-state.h" |
| |
| #include <boost/make_shared.hpp> |
| #include <boost/lambda/lambda.hpp> |
| #include <boost/lambda/bind.hpp> |
| #include <boost/foreach.hpp> |
| #include <boost/assert.hpp> |
| |
| #include "sync-full-leaf.h" |
| |
| using namespace boost; |
| namespace ll = boost::lambda; |
| |
| namespace Sync { |
| |
| |
| FullState::FullState () |
| // m_lastUpdated is initialized to "not_a_date_time" in normal lib mode and to "0" time in NS-3 mode |
| { |
| } |
| |
| FullState::~FullState () |
| { |
| } |
| |
| ndn::time::system_clock::Duration |
| FullState::getTimeFromLastUpdate () const |
| { |
| return ndn::time::system_clock::now() - m_lastUpdated; |
| } |
| |
| DigestConstPtr |
| FullState::getDigest () |
| { |
| if (!m_digest) |
| { |
| m_digest = make_shared<Digest> (); |
| if (m_leaves.get<ordered> ().size () > 0) |
| { |
| BOOST_FOREACH (LeafConstPtr leaf, m_leaves.get<ordered> ()) |
| { |
| FullLeafConstPtr fullLeaf = dynamic_pointer_cast<const FullLeaf> (leaf); |
| BOOST_ASSERT (fullLeaf != 0); |
| *m_digest << fullLeaf->getDigest (); |
| } |
| m_digest->finalize (); |
| } |
| else |
| { |
| std::istringstream is ("00"); //zero state |
| is >> *m_digest; |
| } |
| } |
| |
| return m_digest; |
| } |
| |
| // from State |
| boost::tuple<bool/*inserted*/, bool/*updated*/, SeqNo/*oldSeqNo*/> |
| FullState::update (NameInfoConstPtr info, const SeqNo &seq) |
| { |
| m_lastUpdated = ndn::time::system_clock::now(); |
| |
| |
| m_digest.reset (); |
| |
| LeafContainer::iterator item = m_leaves.find (info); |
| if (item == m_leaves.end ()) |
| { |
| m_leaves.insert (make_shared<FullLeaf> (info, cref (seq))); |
| return make_tuple (true, false, SeqNo ()); |
| } |
| else |
| { |
| if ((*item)->getSeq () == seq || seq < (*item)->getSeq ()) |
| { |
| return make_tuple (false, false, SeqNo ()); |
| } |
| |
| SeqNo old = (*item)->getSeq (); |
| m_leaves.modify (item, |
| ll::bind (&Leaf::setSeq, *ll::_1, seq)); |
| return make_tuple (false, true, old); |
| } |
| } |
| |
| bool |
| FullState::remove (NameInfoConstPtr info) |
| { |
| m_lastUpdated = ndn::time::system_clock::now(); |
| |
| m_digest.reset (); |
| |
| LeafContainer::iterator item = m_leaves.find (info); |
| if (item != m_leaves.end ()) |
| { |
| m_leaves.erase (item); |
| return true; |
| } |
| else |
| return false; |
| } |
| |
| } // Sync |