akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 1 | /* -*- 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> |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame] | 20 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include "sync-full-state.h" |
| 24 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 25 | #include <boost/assert.hpp> |
| 26 | |
| 27 | #include "sync-full-leaf.h" |
| 28 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 29 | namespace Sync { |
| 30 | |
| 31 | |
| 32 | FullState::FullState () |
| 33 | // m_lastUpdated is initialized to "not_a_date_time" in normal lib mode and to "0" time in NS-3 mode |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | FullState::~FullState () |
| 38 | { |
| 39 | } |
| 40 | |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 41 | ndn::time::system_clock::Duration |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 42 | FullState::getTimeFromLastUpdate () const |
| 43 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 44 | return ndn::time::system_clock::now() - m_lastUpdated; |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | DigestConstPtr |
| 48 | FullState::getDigest () |
| 49 | { |
| 50 | if (!m_digest) |
| 51 | { |
| 52 | m_digest = make_shared<Digest> (); |
| 53 | if (m_leaves.get<ordered> ().size () > 0) |
| 54 | { |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame] | 55 | for (LeafConstPtr leaf : m_leaves.get<ordered> ()) |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 56 | { |
| 57 | FullLeafConstPtr fullLeaf = dynamic_pointer_cast<const FullLeaf> (leaf); |
| 58 | BOOST_ASSERT (fullLeaf != 0); |
| 59 | *m_digest << fullLeaf->getDigest (); |
| 60 | } |
| 61 | m_digest->finalize (); |
| 62 | } |
| 63 | else |
| 64 | { |
| 65 | std::istringstream is ("00"); //zero state |
| 66 | is >> *m_digest; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | return m_digest; |
| 71 | } |
| 72 | |
| 73 | // from State |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame] | 74 | tuple<bool/*inserted*/, bool/*updated*/, SeqNo/*oldSeqNo*/> |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 75 | FullState::update (NameInfoConstPtr info, const SeqNo &seq) |
| 76 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 77 | m_lastUpdated = ndn::time::system_clock::now(); |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 78 | |
| 79 | |
| 80 | m_digest.reset (); |
| 81 | |
| 82 | LeafContainer::iterator item = m_leaves.find (info); |
| 83 | if (item == m_leaves.end ()) |
| 84 | { |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame] | 85 | m_leaves.insert (make_shared<FullLeaf> (info, seq)); |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 86 | return make_tuple (true, false, SeqNo ()); |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | if ((*item)->getSeq () == seq || seq < (*item)->getSeq ()) |
| 91 | { |
| 92 | return make_tuple (false, false, SeqNo ()); |
| 93 | } |
| 94 | |
| 95 | SeqNo old = (*item)->getSeq (); |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame] | 96 | m_leaves.modify (item, [&seq](LeafPtr data){ data->setSeq(seq); }); |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 97 | return make_tuple (false, true, old); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | bool |
| 102 | FullState::remove (NameInfoConstPtr info) |
| 103 | { |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 104 | m_lastUpdated = ndn::time::system_clock::now(); |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 105 | |
| 106 | m_digest.reset (); |
| 107 | |
| 108 | LeafContainer::iterator item = m_leaves.find (info); |
| 109 | if (item != m_leaves.end ()) |
| 110 | { |
| 111 | m_leaves.erase (item); |
| 112 | return true; |
| 113 | } |
| 114 | else |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | } // Sync |