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 | |
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 | 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 | { |
Alexander Afanasyev | 0ad7c21 | 2012-04-05 13:31:14 -0700 | [diff] [blame] | 41 | mutex::scoped_lock namesLock (m_namesMutex); |
| 42 | |
| 43 | NameInfoConstPtr ret; |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 44 | string key = lexical_cast<string> (*name); |
Alexander Afanasyev | 0ad7c21 | 2012-04-05 13:31:14 -0700 | [diff] [blame] | 45 | |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 46 | NameMap::iterator item = m_names.find (key); |
Alexander Afanasyev | 0ad7c21 | 2012-04-05 13:31:14 -0700 | [diff] [blame] | 47 | if (item != m_names.end ()) |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 48 | { |
Alexander Afanasyev | 0ad7c21 | 2012-04-05 13:31:14 -0700 | [diff] [blame] | 49 | ret = item->second.lock (); |
| 50 | BOOST_ASSERT (ret != 0); |
| 51 | } |
| 52 | else |
| 53 | { |
| 54 | ret = NameInfoPtr (new Ns3NameInfo (name)); |
| 55 | weak_ptr<const NameInfo> value (ret); |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 56 | pair<NameMap::iterator,bool> inserted = |
| 57 | m_names.insert (make_pair (key, value)); |
Alexander Afanasyev | 0ad7c21 | 2012-04-05 13:31:14 -0700 | [diff] [blame] | 58 | |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 59 | BOOST_ASSERT (inserted.second); // previous call has to insert value |
| 60 | item = inserted.first; |
| 61 | } |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 62 | |
Alexander Afanasyev | 0ad7c21 | 2012-04-05 13:31:14 -0700 | [diff] [blame] | 63 | return ret; |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 64 | } |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 65 | |
Alexander Afanasyev | 0ad7c21 | 2012-04-05 13:31:14 -0700 | [diff] [blame] | 66 | |
Alexander Afanasyev | de505f2 | 2012-03-02 11:45:45 -0800 | [diff] [blame] | 67 | Ns3NameInfo::Ns3NameInfo (ns3::Ptr<const ns3::CcnxNameComponents> name) |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 68 | : m_name (name) |
| 69 | { |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 70 | m_id = m_ids ++; // set ID for a newly inserted element |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 71 | m_digest << *name; |
| 72 | m_digest.getHash (); // finalize digest |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 75 | string |
Alexander Afanasyev | de505f2 | 2012-03-02 11:45:45 -0800 | [diff] [blame] | 76 | Ns3NameInfo::toString () const |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 77 | { |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 78 | return lexical_cast<std::string> (*m_name); |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 81 | bool |
Alexander Afanasyev | de505f2 | 2012-03-02 11:45:45 -0800 | [diff] [blame] | 82 | Ns3NameInfo::operator == (const NameInfo &info) const |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 83 | { |
| 84 | try |
| 85 | { |
Alexander Afanasyev | de505f2 | 2012-03-02 11:45:45 -0800 | [diff] [blame] | 86 | return *m_name == *dynamic_cast<const Ns3NameInfo&> (info).m_name; |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 87 | } |
| 88 | catch (...) |
| 89 | { |
| 90 | return false; |
| 91 | } |
| 92 | } |
| 93 | |
Alexander Afanasyev | 0ad7c21 | 2012-04-05 13:31:14 -0700 | [diff] [blame] | 94 | bool |
| 95 | Ns3NameInfo::operator < (const NameInfo &info) const |
| 96 | { |
| 97 | try |
| 98 | { |
| 99 | return *m_name < *dynamic_cast<const Ns3NameInfo&> (info).m_name; |
| 100 | } |
| 101 | catch (...) |
| 102 | { |
| 103 | return false; |
| 104 | } |
| 105 | } |
| 106 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 107 | Digest & |
| 108 | operator << (Digest &digest, const ns3::CcnxNameComponents &name) |
| 109 | { |
| 110 | BOOST_FOREACH (const std::string &component, name.GetComponents ()) |
| 111 | { |
| 112 | Digest subhash; |
| 113 | subhash << component; |
| 114 | subhash.getHash (); // finalize hash |
| 115 | |
| 116 | digest << subhash; |
| 117 | } |
| 118 | |
| 119 | return digest; |
| 120 | } |
| 121 | |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 122 | |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 123 | } // Sync |
Alexander Afanasyev | 146a51b | 2012-03-05 10:47:35 -0800 | [diff] [blame] | 124 | |
| 125 | #endif |
| 126 | |