blob: dc3b5615130ba3b3e5f114cf224ba4c6fee70584 [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
Alexander Afanasyevde505f22012-03-02 11:45:45 -080023#include "sync-ns3-name-info.h"
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080024#include "ns3/ccnx-name-components.h"
25
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080026#include <boost/foreach.hpp>
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080027#include <boost/lexical_cast.hpp>
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080028#include <utility>
29
30using namespace std;
31using namespace boost;
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080032
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080033namespace Sync {
34
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080035
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080036NameInfoConstPtr
Alexander Afanasyevde505f22012-03-02 11:45:45 -080037Ns3NameInfo::FindOrCreate (ns3::Ptr<const ns3::CcnxNameComponents> name)
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080038{
39 string key = lexical_cast<string> (*name);
40
Alexander Afanasyevde505f22012-03-02 11:45:45 -080041 NameInfoPtr value = NameInfoPtr (new Ns3NameInfo (name));
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080042 pair<NameMap::iterator,bool> item =
43 m_names.insert (make_pair (key, value));
44
45 return item.first->second;
46}
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080047
Alexander Afanasyevde505f22012-03-02 11:45:45 -080048Ns3NameInfo::Ns3NameInfo (ns3::Ptr<const ns3::CcnxNameComponents> name)
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080049 : m_name (name)
50{
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080051 m_id = m_ids ++; // set ID for a newly inserted element
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080052 m_digest << *name;
53 m_digest.getHash (); // finalize digest
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080054}
55
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080056string
Alexander Afanasyevde505f22012-03-02 11:45:45 -080057Ns3NameInfo::toString () const
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080058{
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080059 return lexical_cast<std::string> (*m_name);
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080060}
61
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080062bool
Alexander Afanasyevde505f22012-03-02 11:45:45 -080063Ns3NameInfo::operator == (const NameInfo &info) const
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080064{
65 try
66 {
Alexander Afanasyevde505f22012-03-02 11:45:45 -080067 return *m_name == *dynamic_cast<const Ns3NameInfo&> (info).m_name;
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080068 }
69 catch (...)
70 {
71 return false;
72 }
73}
74
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080075Digest &
76operator << (Digest &digest, const ns3::CcnxNameComponents &name)
77{
78 BOOST_FOREACH (const std::string &component, name.GetComponents ())
79 {
80 Digest subhash;
81 subhash << component;
82 subhash.getHash (); // finalize hash
83
84 digest << subhash;
85 }
86
87 return digest;
88}
89
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080090
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080091} // Sync