blob: 1ec824963753acbe568f54da2ebff052226891da [file] [log] [blame]
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -08001/* -*- 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>
20 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
22
23#include "sync-full-state.h"
24
25#include "ns3/simulator.h"
26
27#include <boost/make_shared.hpp>
28#include <boost/lambda/lambda.hpp>
29#include <boost/lambda/bind.hpp>
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080030#include <boost/foreach.hpp>
31#include <boost/assert.hpp>
32
33#include "sync-full-leaf.h"
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080034
35using namespace boost;
36namespace ll = boost::lambda;
37
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080038namespace Sync {
39
40
41FullState::FullState ()
42 : m_lastUpdated (0)
43{
44}
45
46FullState::~FullState ()
47{
48}
49
Alexander Afanasyev017784c2012-03-02 11:44:13 -080050ns3::Time
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080051FullState::getTimeFromLastUpdate () const
52{
Alexander Afanasyev017784c2012-03-02 11:44:13 -080053 return ns3::Simulator::Now () - m_lastUpdated;
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080054}
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080055
56DigestConstPtr
57FullState::getDigest ()
58{
59 if (m_digest == 0)
60 {
61 m_digest = make_shared<Digest> ();
62 BOOST_FOREACH (LeafConstPtr leaf, m_leaves)
63 {
64 FullLeafConstPtr fullLeaf = dynamic_pointer_cast<const FullLeaf> (leaf);
65 BOOST_ASSERT (fullLeaf != 0);
66 *m_digest << fullLeaf->getDigest ();
67 }
68 }
69
70 return m_digest;
71}
72
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080073// from State
74void
75FullState::update (NameInfoConstPtr info, const SeqNo &seq)
76{
Alexander Afanasyev017784c2012-03-02 11:44:13 -080077 m_lastUpdated = ns3::Simulator::Now ();
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080078 m_digest.reset ();
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080079
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080080 LeafContainer::iterator item = m_leaves.find (*info);
81 if (item == m_leaves.end ())
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080082 {
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080083 m_leaves.insert (make_shared<Leaf> (info, cref (seq)));
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080084 }
85 else
86 {
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080087 m_leaves.modify (item, ll::bind (&Leaf::setSeq, *ll::_1, seq));
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080088 }
89}
90
91void
92FullState::remove (NameInfoConstPtr info)
93{
Alexander Afanasyev017784c2012-03-02 11:44:13 -080094 m_lastUpdated = ns3::Simulator::Now ();
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080095 m_digest.reset ();
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080096
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080097 m_leaves.erase (*info);
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080098}
99
100
101} // Sync