blob: 9b7c42c73fafa35e863b360e1319902b68d5ed75 [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>
Chaoyi Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
22
23#include "sync-full-state.h"
24
Zhenkai Zhuea026982012-06-01 16:00:25 -070025#ifdef NS3_MODULE
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080026#include "ns3/simulator.h"
Zhenkai Zhuea026982012-06-01 16:00:25 -070027#endif // NS3_MODULE
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080028
29#include <boost/make_shared.hpp>
30#include <boost/lambda/lambda.hpp>
31#include <boost/lambda/bind.hpp>
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080032#include <boost/foreach.hpp>
33#include <boost/assert.hpp>
34
35#include "sync-full-leaf.h"
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080036
37using namespace boost;
38namespace ll = boost::lambda;
39
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080040namespace Sync {
41
42
43FullState::FullState ()
Zhenkai Zhuea026982012-06-01 16:00:25 -070044// m_lastUpdated is initialized to "not_a_date_time" in normal lib mode and to "0" time in NS-3 mode
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080045{
46}
47
48FullState::~FullState ()
49{
50}
51
Alexander Afanasyev146a51b2012-03-05 10:47:35 -080052TimeDurationType
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080053FullState::getTimeFromLastUpdate () const
54{
Zhenkai Zhuea026982012-06-01 16:00:25 -070055#ifdef NS3_MODULE
Alexander Afanasyev017784c2012-03-02 11:44:13 -080056 return ns3::Simulator::Now () - m_lastUpdated;
Alexander Afanasyev146a51b2012-03-05 10:47:35 -080057#else
58 return boost::posix_time::second_clock::universal_time () - m_lastUpdated;
Zhenkai Zhuea026982012-06-01 16:00:25 -070059#endif // NS3_MODULE
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080060}
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080061
62DigestConstPtr
63FullState::getDigest ()
64{
65 if (m_digest == 0)
66 {
67 m_digest = make_shared<Digest> ();
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070068 if (m_leaves.get<ordered> ().size () > 0)
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080069 {
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070070 BOOST_FOREACH (LeafConstPtr leaf, m_leaves.get<ordered> ())
71 {
72 FullLeafConstPtr fullLeaf = dynamic_pointer_cast<const FullLeaf> (leaf);
73 BOOST_ASSERT (fullLeaf != 0);
74 *m_digest << fullLeaf->getDigest ();
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070075 }
76 m_digest->finalize ();
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080077 }
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070078 else
79 {
80 std::istringstream is ("00"); //zero state
81 is >> *m_digest;
82 }
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080083 }
84
85 return m_digest;
86}
87
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080088// from State
Alexander Afanasyev750d1872012-03-12 15:33:56 -070089boost::tuple<bool/*inserted*/, bool/*updated*/, SeqNo/*oldSeqNo*/>
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080090FullState::update (NameInfoConstPtr info, const SeqNo &seq)
91{
Zhenkai Zhuea026982012-06-01 16:00:25 -070092#ifdef NS3_MODULE
Alexander Afanasyev017784c2012-03-02 11:44:13 -080093 m_lastUpdated = ns3::Simulator::Now ();
Alexander Afanasyev146a51b2012-03-05 10:47:35 -080094#else
95 m_lastUpdated = boost::posix_time::second_clock::universal_time ();
Zhenkai Zhuea026982012-06-01 16:00:25 -070096#endif // NS3_MODULE
Alexander Afanasyev146a51b2012-03-05 10:47:35 -080097
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080098 m_digest.reset ();
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080099
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700100 LeafContainer::iterator item = m_leaves.find (info);
Alexander Afanasyevb5547e32012-03-01 21:59:38 -0800101 if (item == m_leaves.end ())
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800102 {
Alexander Afanasyev58c77b02012-03-05 21:52:25 -0800103 m_leaves.insert (make_shared<FullLeaf> (info, cref (seq)));
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700104 return make_tuple (true, false, SeqNo ());
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800105 }
106 else
107 {
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700108 if ((*item)->getSeq () == seq || seq < (*item)->getSeq ())
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700109 {
110 return make_tuple (false, false, SeqNo ());
111 }
112
113 SeqNo old = (*item)->getSeq ();
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700114 m_leaves.modify (item,
115 ll::bind (&Leaf::setSeq, *ll::_1, seq));
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700116 return make_tuple (false, true, old);
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800117 }
118}
119
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700120bool
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800121FullState::remove (NameInfoConstPtr info)
122{
Zhenkai Zhuea026982012-06-01 16:00:25 -0700123#ifdef NS3_MODULE
Alexander Afanasyev017784c2012-03-02 11:44:13 -0800124 m_lastUpdated = ns3::Simulator::Now ();
Alexander Afanasyev146a51b2012-03-05 10:47:35 -0800125#else
126 m_lastUpdated = boost::posix_time::second_clock::universal_time ();
Zhenkai Zhuea026982012-06-01 16:00:25 -0700127#endif // NS3_MODULE
Alexander Afanasyev146a51b2012-03-05 10:47:35 -0800128
Alexander Afanasyevd94542d2012-03-05 08:41:46 -0800129 m_digest.reset ();
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800130
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700131 LeafContainer::iterator item = m_leaves.find (info);
132 if (item != m_leaves.end ())
133 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700134 m_leaves.erase (item);
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700135 return true;
136 }
137 else
138 return false;
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800139}
140
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800141} // Sync