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> |
| 19 | * 卞超轶 Chaoyi Bian <bcy@pku.edu.cn> |
| 20 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 21 | */ |
| 22 | |
Alexander Afanasyev | 146a51b | 2012-03-05 10:47:35 -0800 | [diff] [blame] | 23 | #ifndef STANDALONE |
| 24 | |
Alexander Afanasyev | de505f2 | 2012-03-02 11:45:45 -0800 | [diff] [blame] | 25 | #include "sync-ns3-name-info.h" |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 26 | #include "ns3/ccnx-name-components.h" |
| 27 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 28 | #include <boost/foreach.hpp> |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 29 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 30 | #include <boost/make_shared.hpp> |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 31 | #include <utility> |
| 32 | |
| 33 | using namespace std; |
| 34 | using namespace boost; |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 35 | |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 36 | namespace Sync { |
| 37 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 38 | |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 39 | NameInfoConstPtr |
Alexander Afanasyev | de505f2 | 2012-03-02 11:45:45 -0800 | [diff] [blame] | 40 | Ns3NameInfo::FindOrCreate (ns3::Ptr<const ns3::CcnxNameComponents> name) |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 41 | { |
| 42 | string key = lexical_cast<string> (*name); |
| 43 | |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 44 | NameMap::iterator item = m_names.find (key); |
| 45 | if (item == m_names.end ()) |
| 46 | { |
| 47 | NameInfoPtr value = NameInfoPtr (new Ns3NameInfo (name)); |
| 48 | pair<NameMap::iterator,bool> inserted = |
| 49 | m_names.insert (make_pair (key, value)); |
| 50 | BOOST_ASSERT (inserted.second); // previous call has to insert value |
| 51 | item = inserted.first; |
| 52 | } |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 53 | |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 54 | return item->second; |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 55 | } |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 56 | |
Alexander Afanasyev | de505f2 | 2012-03-02 11:45:45 -0800 | [diff] [blame] | 57 | Ns3NameInfo::Ns3NameInfo (ns3::Ptr<const ns3::CcnxNameComponents> name) |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 58 | : m_name (name) |
| 59 | { |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 60 | m_id = m_ids ++; // set ID for a newly inserted element |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 61 | m_digest << *name; |
| 62 | m_digest.getHash (); // finalize digest |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 65 | string |
Alexander Afanasyev | de505f2 | 2012-03-02 11:45:45 -0800 | [diff] [blame] | 66 | Ns3NameInfo::toString () const |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 67 | { |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 68 | return lexical_cast<std::string> (*m_name); |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 71 | bool |
Alexander Afanasyev | de505f2 | 2012-03-02 11:45:45 -0800 | [diff] [blame] | 72 | Ns3NameInfo::operator == (const NameInfo &info) const |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 73 | { |
| 74 | try |
| 75 | { |
Alexander Afanasyev | de505f2 | 2012-03-02 11:45:45 -0800 | [diff] [blame] | 76 | return *m_name == *dynamic_cast<const Ns3NameInfo&> (info).m_name; |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 77 | } |
| 78 | catch (...) |
| 79 | { |
| 80 | return false; |
| 81 | } |
| 82 | } |
| 83 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 84 | Digest & |
| 85 | operator << (Digest &digest, const ns3::CcnxNameComponents &name) |
| 86 | { |
| 87 | BOOST_FOREACH (const std::string &component, name.GetComponents ()) |
| 88 | { |
| 89 | Digest subhash; |
| 90 | subhash << component; |
| 91 | subhash.getHash (); // finalize hash |
| 92 | |
| 93 | digest << subhash; |
| 94 | } |
| 95 | |
| 96 | return digest; |
| 97 | } |
| 98 | |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 99 | |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 100 | } // Sync |
Alexander Afanasyev | 146a51b | 2012-03-05 10:47:35 -0800 | [diff] [blame] | 101 | |
| 102 | #endif |
| 103 | |