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 | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 30 | #include <utility> |
| 31 | |
| 32 | using namespace std; |
| 33 | using namespace boost; |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 34 | |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 35 | namespace Sync { |
| 36 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 37 | |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 38 | NameInfoConstPtr |
Alexander Afanasyev | de505f2 | 2012-03-02 11:45:45 -0800 | [diff] [blame] | 39 | Ns3NameInfo::FindOrCreate (ns3::Ptr<const ns3::CcnxNameComponents> name) |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 40 | { |
| 41 | string key = lexical_cast<string> (*name); |
| 42 | |
Alexander Afanasyev | de505f2 | 2012-03-02 11:45:45 -0800 | [diff] [blame] | 43 | NameInfoPtr value = NameInfoPtr (new Ns3NameInfo (name)); |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 44 | pair<NameMap::iterator,bool> item = |
| 45 | m_names.insert (make_pair (key, value)); |
| 46 | |
| 47 | return item.first->second; |
| 48 | } |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 49 | |
Alexander Afanasyev | de505f2 | 2012-03-02 11:45:45 -0800 | [diff] [blame] | 50 | Ns3NameInfo::Ns3NameInfo (ns3::Ptr<const ns3::CcnxNameComponents> name) |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 51 | : m_name (name) |
| 52 | { |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 53 | m_id = m_ids ++; // set ID for a newly inserted element |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 54 | m_digest << *name; |
| 55 | m_digest.getHash (); // finalize digest |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 58 | string |
Alexander Afanasyev | de505f2 | 2012-03-02 11:45:45 -0800 | [diff] [blame] | 59 | Ns3NameInfo::toString () const |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 60 | { |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 61 | return lexical_cast<std::string> (*m_name); |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 64 | bool |
Alexander Afanasyev | de505f2 | 2012-03-02 11:45:45 -0800 | [diff] [blame] | 65 | Ns3NameInfo::operator == (const NameInfo &info) const |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 66 | { |
| 67 | try |
| 68 | { |
Alexander Afanasyev | de505f2 | 2012-03-02 11:45:45 -0800 | [diff] [blame] | 69 | return *m_name == *dynamic_cast<const Ns3NameInfo&> (info).m_name; |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 70 | } |
| 71 | catch (...) |
| 72 | { |
| 73 | return false; |
| 74 | } |
| 75 | } |
| 76 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 77 | Digest & |
| 78 | operator << (Digest &digest, const ns3::CcnxNameComponents &name) |
| 79 | { |
| 80 | BOOST_FOREACH (const std::string &component, name.GetComponents ()) |
| 81 | { |
| 82 | Digest subhash; |
| 83 | subhash << component; |
| 84 | subhash.getHash (); // finalize hash |
| 85 | |
| 86 | digest << subhash; |
| 87 | } |
| 88 | |
| 89 | return digest; |
| 90 | } |
| 91 | |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 92 | |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 93 | } // Sync |
Alexander Afanasyev | 146a51b | 2012-03-05 10:47:35 -0800 | [diff] [blame] | 94 | |
| 95 | #endif |
| 96 | |