blob: b6ffd649743a57ed32adc973478fe93debe961ba [file] [log] [blame]
akmhoque66e66182014-02-21 17:56:03 -06001/* -*- 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 Lehman0a7da612014-10-29 14:39:29 -050020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
akmhoque66e66182014-02-21 17:56:03 -060021 */
22
23#include "sync-full-state.h"
24
akmhoque66e66182014-02-21 17:56:03 -060025#include <boost/assert.hpp>
26
27#include "sync-full-leaf.h"
28
akmhoque66e66182014-02-21 17:56:03 -060029namespace Sync {
30
31
32FullState::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
37FullState::~FullState ()
38{
39}
40
akmhoque05d5fcf2014-04-15 14:58:45 -050041ndn::time::system_clock::Duration
akmhoque66e66182014-02-21 17:56:03 -060042FullState::getTimeFromLastUpdate () const
43{
akmhoque05d5fcf2014-04-15 14:58:45 -050044 return ndn::time::system_clock::now() - m_lastUpdated;
akmhoque66e66182014-02-21 17:56:03 -060045}
46
47DigestConstPtr
48FullState::getDigest ()
49{
50 if (!m_digest)
51 {
52 m_digest = make_shared<Digest> ();
53 if (m_leaves.get<ordered> ().size () > 0)
54 {
Vince Lehman0a7da612014-10-29 14:39:29 -050055 for (LeafConstPtr leaf : m_leaves.get<ordered> ())
akmhoque66e66182014-02-21 17:56:03 -060056 {
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 Lehman0a7da612014-10-29 14:39:29 -050074tuple<bool/*inserted*/, bool/*updated*/, SeqNo/*oldSeqNo*/>
akmhoque66e66182014-02-21 17:56:03 -060075FullState::update (NameInfoConstPtr info, const SeqNo &seq)
76{
akmhoque05d5fcf2014-04-15 14:58:45 -050077 m_lastUpdated = ndn::time::system_clock::now();
akmhoque66e66182014-02-21 17:56:03 -060078
79
80 m_digest.reset ();
81
82 LeafContainer::iterator item = m_leaves.find (info);
83 if (item == m_leaves.end ())
84 {
Vince Lehman0a7da612014-10-29 14:39:29 -050085 m_leaves.insert (make_shared<FullLeaf> (info, seq));
akmhoque66e66182014-02-21 17:56:03 -060086 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 Lehman0a7da612014-10-29 14:39:29 -050096 m_leaves.modify (item, [&seq](LeafPtr data){ data->setSeq(seq); });
akmhoque66e66182014-02-21 17:56:03 -060097 return make_tuple (false, true, old);
98 }
99}
100
101bool
102FullState::remove (NameInfoConstPtr info)
103{
akmhoque05d5fcf2014-04-15 14:58:45 -0500104 m_lastUpdated = ndn::time::system_clock::now();
akmhoque66e66182014-02-21 17:56:03 -0600105
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