Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 3 | * Copyright (c) 2012-2014 University of California, Los Angeles |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 5 | * This file is part of ChronoSync, synchronization library for distributed realtime |
| 6 | * applications for NDN. |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 8 | * 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 Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 12 | * 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 Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 16 | * 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 Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #include "sync-full-state.h" |
| 25 | |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 26 | #include <boost/make_shared.hpp> |
| 27 | #include <boost/lambda/lambda.hpp> |
| 28 | #include <boost/lambda/bind.hpp> |
Alexander Afanasyev | d94542d | 2012-03-05 08:41:46 -0800 | [diff] [blame] | 29 | #include <boost/foreach.hpp> |
| 30 | #include <boost/assert.hpp> |
| 31 | |
| 32 | #include "sync-full-leaf.h" |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 33 | |
| 34 | using namespace boost; |
| 35 | namespace ll = boost::lambda; |
| 36 | |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 37 | namespace Sync { |
| 38 | |
| 39 | |
| 40 | FullState::FullState () |
Zhenkai Zhu | ea02698 | 2012-06-01 16:00:25 -0700 | [diff] [blame] | 41 | // m_lastUpdated is initialized to "not_a_date_time" in normal lib mode and to "0" time in NS-3 mode |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 42 | { |
| 43 | } |
| 44 | |
| 45 | FullState::~FullState () |
| 46 | { |
| 47 | } |
| 48 | |
Yingdi Yu | 6e1c9cd | 2014-03-25 10:26:54 -0700 | [diff] [blame] | 49 | ndn::time::system_clock::Duration |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 50 | FullState::getTimeFromLastUpdate () const |
| 51 | { |
Yingdi Yu | 6e1c9cd | 2014-03-25 10:26:54 -0700 | [diff] [blame] | 52 | return ndn::time::system_clock::now() - m_lastUpdated; |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 53 | } |
Alexander Afanasyev | d94542d | 2012-03-05 08:41:46 -0800 | [diff] [blame] | 54 | |
| 55 | DigestConstPtr |
| 56 | FullState::getDigest () |
| 57 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 58 | if (!m_digest) |
Alexander Afanasyev | d94542d | 2012-03-05 08:41:46 -0800 | [diff] [blame] | 59 | { |
| 60 | m_digest = make_shared<Digest> (); |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 61 | if (m_leaves.get<ordered> ().size () > 0) |
Alexander Afanasyev | d94542d | 2012-03-05 08:41:46 -0800 | [diff] [blame] | 62 | { |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 63 | 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 Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 68 | } |
| 69 | m_digest->finalize (); |
Alexander Afanasyev | d94542d | 2012-03-05 08:41:46 -0800 | [diff] [blame] | 70 | } |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 71 | else |
| 72 | { |
| 73 | std::istringstream is ("00"); //zero state |
| 74 | is >> *m_digest; |
| 75 | } |
Alexander Afanasyev | d94542d | 2012-03-05 08:41:46 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | return m_digest; |
| 79 | } |
| 80 | |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 81 | // from State |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 82 | boost::tuple<bool/*inserted*/, bool/*updated*/, SeqNo/*oldSeqNo*/> |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 83 | FullState::update (NameInfoConstPtr info, const SeqNo &seq) |
| 84 | { |
Yingdi Yu | 6e1c9cd | 2014-03-25 10:26:54 -0700 | [diff] [blame] | 85 | m_lastUpdated = ndn::time::system_clock::now(); |
Alexander Afanasyev | 531803b | 2014-02-05 15:57:35 -0800 | [diff] [blame] | 86 | |
Alexander Afanasyev | 146a51b | 2012-03-05 10:47:35 -0800 | [diff] [blame] | 87 | |
Alexander Afanasyev | d94542d | 2012-03-05 08:41:46 -0800 | [diff] [blame] | 88 | m_digest.reset (); |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 89 | |
Alexander Afanasyev | e4e2bf7 | 2012-03-12 12:44:54 -0700 | [diff] [blame] | 90 | LeafContainer::iterator item = m_leaves.find (info); |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 91 | if (item == m_leaves.end ()) |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 92 | { |
Alexander Afanasyev | 58c77b0 | 2012-03-05 21:52:25 -0800 | [diff] [blame] | 93 | m_leaves.insert (make_shared<FullLeaf> (info, cref (seq))); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 94 | return make_tuple (true, false, SeqNo ()); |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 95 | } |
| 96 | else |
| 97 | { |
Alexander Afanasyev | e4e2bf7 | 2012-03-12 12:44:54 -0700 | [diff] [blame] | 98 | if ((*item)->getSeq () == seq || seq < (*item)->getSeq ()) |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 99 | { |
| 100 | return make_tuple (false, false, SeqNo ()); |
| 101 | } |
| 102 | |
| 103 | SeqNo old = (*item)->getSeq (); |
Alexander Afanasyev | e4e2bf7 | 2012-03-12 12:44:54 -0700 | [diff] [blame] | 104 | m_leaves.modify (item, |
| 105 | ll::bind (&Leaf::setSeq, *ll::_1, seq)); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 106 | return make_tuple (false, true, old); |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
Alexander Afanasyev | e4e2bf7 | 2012-03-12 12:44:54 -0700 | [diff] [blame] | 110 | bool |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 111 | FullState::remove (NameInfoConstPtr info) |
| 112 | { |
Yingdi Yu | 6e1c9cd | 2014-03-25 10:26:54 -0700 | [diff] [blame] | 113 | m_lastUpdated = ndn::time::system_clock::now(); |
Alexander Afanasyev | 146a51b | 2012-03-05 10:47:35 -0800 | [diff] [blame] | 114 | |
Alexander Afanasyev | d94542d | 2012-03-05 08:41:46 -0800 | [diff] [blame] | 115 | m_digest.reset (); |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 116 | |
Alexander Afanasyev | e4e2bf7 | 2012-03-12 12:44:54 -0700 | [diff] [blame] | 117 | LeafContainer::iterator item = m_leaves.find (info); |
| 118 | if (item != m_leaves.end ()) |
| 119 | { |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 120 | m_leaves.erase (item); |
Alexander Afanasyev | e4e2bf7 | 2012-03-12 12:44:54 -0700 | [diff] [blame] | 121 | return true; |
| 122 | } |
| 123 | else |
| 124 | return false; |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 127 | } // Sync |