Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [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> |
Chaoyi Bian | 3e1eb16 | 2012-04-03 16:59:32 -0700 | [diff] [blame] | 19 | * Chaoyi Bian <bcy@pku.edu.cn> |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 20 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 21 | */ |
| 22 | |
| 23 | #include "sync-full-state.h" |
| 24 | |
Zhenkai Zhu | ea02698 | 2012-06-01 16:00:25 -0700 | [diff] [blame] | 25 | #ifdef NS3_MODULE |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 26 | #include "ns3/simulator.h" |
Zhenkai Zhu | ea02698 | 2012-06-01 16:00:25 -0700 | [diff] [blame] | 27 | #endif // NS3_MODULE |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 28 | |
| 29 | #include <boost/make_shared.hpp> |
| 30 | #include <boost/lambda/lambda.hpp> |
| 31 | #include <boost/lambda/bind.hpp> |
Alexander Afanasyev | d94542d | 2012-03-05 08:41:46 -0800 | [diff] [blame] | 32 | #include <boost/foreach.hpp> |
| 33 | #include <boost/assert.hpp> |
| 34 | |
| 35 | #include "sync-full-leaf.h" |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 36 | |
| 37 | using namespace boost; |
| 38 | namespace ll = boost::lambda; |
| 39 | |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 40 | namespace Sync { |
| 41 | |
| 42 | |
| 43 | FullState::FullState () |
Zhenkai Zhu | ea02698 | 2012-06-01 16:00:25 -0700 | [diff] [blame] | 44 | // 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] | 45 | { |
| 46 | } |
| 47 | |
| 48 | FullState::~FullState () |
| 49 | { |
| 50 | } |
| 51 | |
Alexander Afanasyev | 146a51b | 2012-03-05 10:47:35 -0800 | [diff] [blame] | 52 | TimeDurationType |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 53 | FullState::getTimeFromLastUpdate () const |
| 54 | { |
Zhenkai Zhu | ea02698 | 2012-06-01 16:00:25 -0700 | [diff] [blame] | 55 | #ifdef NS3_MODULE |
Alexander Afanasyev | 017784c | 2012-03-02 11:44:13 -0800 | [diff] [blame] | 56 | return ns3::Simulator::Now () - m_lastUpdated; |
Alexander Afanasyev | 146a51b | 2012-03-05 10:47:35 -0800 | [diff] [blame] | 57 | #else |
| 58 | return boost::posix_time::second_clock::universal_time () - m_lastUpdated; |
Zhenkai Zhu | ea02698 | 2012-06-01 16:00:25 -0700 | [diff] [blame] | 59 | #endif // NS3_MODULE |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 60 | } |
Alexander Afanasyev | d94542d | 2012-03-05 08:41:46 -0800 | [diff] [blame] | 61 | |
| 62 | DigestConstPtr |
| 63 | FullState::getDigest () |
| 64 | { |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 65 | if (!m_digest) |
Alexander Afanasyev | d94542d | 2012-03-05 08:41:46 -0800 | [diff] [blame] | 66 | { |
| 67 | m_digest = make_shared<Digest> (); |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 68 | if (m_leaves.get<ordered> ().size () > 0) |
Alexander Afanasyev | d94542d | 2012-03-05 08:41:46 -0800 | [diff] [blame] | 69 | { |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 70 | 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 Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 75 | } |
| 76 | m_digest->finalize (); |
Alexander Afanasyev | d94542d | 2012-03-05 08:41:46 -0800 | [diff] [blame] | 77 | } |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 78 | else |
| 79 | { |
| 80 | std::istringstream is ("00"); //zero state |
| 81 | is >> *m_digest; |
| 82 | } |
Alexander Afanasyev | d94542d | 2012-03-05 08:41:46 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | return m_digest; |
| 86 | } |
| 87 | |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 88 | // from State |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 89 | boost::tuple<bool/*inserted*/, bool/*updated*/, SeqNo/*oldSeqNo*/> |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 90 | FullState::update (NameInfoConstPtr info, const SeqNo &seq) |
| 91 | { |
Zhenkai Zhu | ea02698 | 2012-06-01 16:00:25 -0700 | [diff] [blame] | 92 | #ifdef NS3_MODULE |
Alexander Afanasyev | 017784c | 2012-03-02 11:44:13 -0800 | [diff] [blame] | 93 | m_lastUpdated = ns3::Simulator::Now (); |
Alexander Afanasyev | 146a51b | 2012-03-05 10:47:35 -0800 | [diff] [blame] | 94 | #else |
| 95 | m_lastUpdated = boost::posix_time::second_clock::universal_time (); |
Zhenkai Zhu | ea02698 | 2012-06-01 16:00:25 -0700 | [diff] [blame] | 96 | #endif // NS3_MODULE |
Alexander Afanasyev | 146a51b | 2012-03-05 10:47:35 -0800 | [diff] [blame] | 97 | |
Alexander Afanasyev | d94542d | 2012-03-05 08:41:46 -0800 | [diff] [blame] | 98 | m_digest.reset (); |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 99 | |
Alexander Afanasyev | e4e2bf7 | 2012-03-12 12:44:54 -0700 | [diff] [blame] | 100 | LeafContainer::iterator item = m_leaves.find (info); |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 101 | if (item == m_leaves.end ()) |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 102 | { |
Alexander Afanasyev | 58c77b0 | 2012-03-05 21:52:25 -0800 | [diff] [blame] | 103 | m_leaves.insert (make_shared<FullLeaf> (info, cref (seq))); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 104 | return make_tuple (true, false, SeqNo ()); |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 105 | } |
| 106 | else |
| 107 | { |
Alexander Afanasyev | e4e2bf7 | 2012-03-12 12:44:54 -0700 | [diff] [blame] | 108 | if ((*item)->getSeq () == seq || seq < (*item)->getSeq ()) |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 109 | { |
| 110 | return make_tuple (false, false, SeqNo ()); |
| 111 | } |
| 112 | |
| 113 | SeqNo old = (*item)->getSeq (); |
Alexander Afanasyev | e4e2bf7 | 2012-03-12 12:44:54 -0700 | [diff] [blame] | 114 | m_leaves.modify (item, |
| 115 | ll::bind (&Leaf::setSeq, *ll::_1, seq)); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 116 | return make_tuple (false, true, old); |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
Alexander Afanasyev | e4e2bf7 | 2012-03-12 12:44:54 -0700 | [diff] [blame] | 120 | bool |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 121 | FullState::remove (NameInfoConstPtr info) |
| 122 | { |
Zhenkai Zhu | ea02698 | 2012-06-01 16:00:25 -0700 | [diff] [blame] | 123 | #ifdef NS3_MODULE |
Alexander Afanasyev | 017784c | 2012-03-02 11:44:13 -0800 | [diff] [blame] | 124 | m_lastUpdated = ns3::Simulator::Now (); |
Alexander Afanasyev | 146a51b | 2012-03-05 10:47:35 -0800 | [diff] [blame] | 125 | #else |
| 126 | m_lastUpdated = boost::posix_time::second_clock::universal_time (); |
Zhenkai Zhu | ea02698 | 2012-06-01 16:00:25 -0700 | [diff] [blame] | 127 | #endif // NS3_MODULE |
Alexander Afanasyev | 146a51b | 2012-03-05 10:47:35 -0800 | [diff] [blame] | 128 | |
Alexander Afanasyev | d94542d | 2012-03-05 08:41:46 -0800 | [diff] [blame] | 129 | m_digest.reset (); |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 130 | |
Alexander Afanasyev | e4e2bf7 | 2012-03-12 12:44:54 -0700 | [diff] [blame] | 131 | LeafContainer::iterator item = m_leaves.find (info); |
| 132 | if (item != m_leaves.end ()) |
| 133 | { |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 134 | m_leaves.erase (item); |
Alexander Afanasyev | e4e2bf7 | 2012-03-12 12:44:54 -0700 | [diff] [blame] | 135 | return true; |
| 136 | } |
| 137 | else |
| 138 | return false; |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 141 | } // Sync |