blob: f53de39a0c3500bd26b5150efad8f700af509b2e [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
Alexander Afanasyev146a51b2012-03-05 10:47:35 -080025#ifndef STANDALONE
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080026#include "ns3/simulator.h"
Alexander Afanasyev146a51b2012-03-05 10:47:35 -080027#endif // STANDALONE
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 ()
Alexander Afanasyev146a51b2012-03-05 10:47:35 -080044// m_lastUpdated is initialized to "not_a_date_time" in STANDALONE 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{
Alexander Afanasyev146a51b2012-03-05 10:47:35 -080055#ifndef STANDALONE
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;
59#endif // STANDALONE
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 {
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080067 // std::cout << "getDigest: ";
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080068 m_digest = make_shared<Digest> ();
Alexander Afanasyeva5625322012-03-06 00:03:41 -080069 BOOST_FOREACH (LeafConstPtr leaf, m_leaves.get<ordered> ())
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080070 {
71 FullLeafConstPtr fullLeaf = dynamic_pointer_cast<const FullLeaf> (leaf);
72 BOOST_ASSERT (fullLeaf != 0);
73 *m_digest << fullLeaf->getDigest ();
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080074 // std::cout << *leaf << "[" << fullLeaf->getDigest () << "] ";
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080075 }
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080076 // std::cout << "\n";
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080077 }
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080078 m_digest->finalize ();
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080079
80 return m_digest;
81}
82
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080083// from State
84void
85FullState::update (NameInfoConstPtr info, const SeqNo &seq)
86{
Alexander Afanasyev146a51b2012-03-05 10:47:35 -080087#ifndef STANDALONE
Alexander Afanasyev017784c2012-03-02 11:44:13 -080088 m_lastUpdated = ns3::Simulator::Now ();
Alexander Afanasyev146a51b2012-03-05 10:47:35 -080089#else
90 m_lastUpdated = boost::posix_time::second_clock::universal_time ();
91#endif // STANDALONE
92
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080093 m_digest.reset ();
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080094
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080095 LeafContainer::iterator item = m_leaves.find (*info);
96 if (item == m_leaves.end ())
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080097 {
Alexander Afanasyev58c77b02012-03-05 21:52:25 -080098 m_leaves.insert (make_shared<FullLeaf> (info, cref (seq)));
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080099 }
100 else
101 {
Alexander Afanasyevb5547e32012-03-01 21:59:38 -0800102 m_leaves.modify (item, ll::bind (&Leaf::setSeq, *ll::_1, seq));
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800103 }
104}
105
106void
107FullState::remove (NameInfoConstPtr info)
108{
Alexander Afanasyev146a51b2012-03-05 10:47:35 -0800109#ifndef STANDALONE
Alexander Afanasyev017784c2012-03-02 11:44:13 -0800110 m_lastUpdated = ns3::Simulator::Now ();
Alexander Afanasyev146a51b2012-03-05 10:47:35 -0800111#else
112 m_lastUpdated = boost::posix_time::second_clock::universal_time ();
113#endif // STANDALONE
114
Alexander Afanasyevd94542d2012-03-05 08:41:46 -0800115 m_digest.reset ();
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800116
Alexander Afanasyevb5547e32012-03-01 21:59:38 -0800117 m_leaves.erase (*info);
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800118}
119
Alexander Afanasyev64d50692012-03-07 20:48:35 -0800120#ifdef _DEBUG
121#define DEBUG_ENDL os << "\n";
122#else
123#define DEBUG_ENDL
124#endif
125
126std::ostream &
127operator << (std::ostream &os, const FullState &state)
128{
129 os << "<state type=\"full\">"; DEBUG_ENDL;
130
131 BOOST_FOREACH (shared_ptr<const Leaf> leaf, state.getLeaves ())
132 {
133 os << "<item>"; DEBUG_ENDL;
134 os << "<name>" << leaf->getInfo () << "</name>"; DEBUG_ENDL;
135 os << "<seq>" << leaf->getSeq () << "</seq>"; DEBUG_ENDL;
136 os << "</item>"; DEBUG_ENDL;
137 }
138 os << "</state>";
139}
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800140
141} // Sync