blob: dfd61791692b739a7ee9a8f00b02ad55627ffe5d [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>
Chaoyi Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
22
Alexander Afanasyev146a51b2012-03-05 10:47:35 -080023#ifndef STANDALONE
24
Alexander Afanasyevde505f22012-03-02 11:45:45 -080025#include "sync-ns3-name-info.h"
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080026#include "ns3/ccnx-name-components.h"
27
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080028#include <boost/foreach.hpp>
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080029#include <boost/lexical_cast.hpp>
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080030#include <boost/make_shared.hpp>
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080031#include <utility>
32
33using namespace std;
34using namespace boost;
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080035
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080036namespace Sync {
37
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080038NameInfoConstPtr
Alexander Afanasyevde505f22012-03-02 11:45:45 -080039Ns3NameInfo::FindOrCreate (ns3::Ptr<const ns3::CcnxNameComponents> name)
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080040{
Alexander Afanasyev0ad7c212012-04-05 13:31:14 -070041 mutex::scoped_lock namesLock (m_namesMutex);
42
43 NameInfoConstPtr ret;
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080044 string key = lexical_cast<string> (*name);
Alexander Afanasyev0ad7c212012-04-05 13:31:14 -070045
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080046 NameMap::iterator item = m_names.find (key);
Alexander Afanasyev0ad7c212012-04-05 13:31:14 -070047 if (item != m_names.end ())
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080048 {
Alexander Afanasyev0ad7c212012-04-05 13:31:14 -070049 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 Afanasyeva4ce9cf2012-03-06 14:29:58 -080056 pair<NameMap::iterator,bool> inserted =
57 m_names.insert (make_pair (key, value));
Alexander Afanasyev0ad7c212012-04-05 13:31:14 -070058
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080059 BOOST_ASSERT (inserted.second); // previous call has to insert value
60 item = inserted.first;
61 }
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080062
Alexander Afanasyev0ad7c212012-04-05 13:31:14 -070063 return ret;
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080064}
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080065
Alexander Afanasyev0ad7c212012-04-05 13:31:14 -070066
Alexander Afanasyevde505f22012-03-02 11:45:45 -080067Ns3NameInfo::Ns3NameInfo (ns3::Ptr<const ns3::CcnxNameComponents> name)
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080068 : m_name (name)
69{
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080070 m_id = m_ids ++; // set ID for a newly inserted element
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080071 m_digest << *name;
72 m_digest.getHash (); // finalize digest
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080073}
74
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080075string
Alexander Afanasyevde505f22012-03-02 11:45:45 -080076Ns3NameInfo::toString () const
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080077{
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080078 return lexical_cast<std::string> (*m_name);
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080079}
80
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080081bool
Alexander Afanasyevde505f22012-03-02 11:45:45 -080082Ns3NameInfo::operator == (const NameInfo &info) const
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080083{
84 try
85 {
Alexander Afanasyevde505f22012-03-02 11:45:45 -080086 return *m_name == *dynamic_cast<const Ns3NameInfo&> (info).m_name;
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080087 }
88 catch (...)
89 {
90 return false;
91 }
92}
93
Alexander Afanasyev0ad7c212012-04-05 13:31:14 -070094bool
95Ns3NameInfo::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 Afanasyeve00ffbe2012-03-05 00:01:36 -0800107Digest &
108operator << (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 Afanasyevb5547e32012-03-01 21:59:38 -0800122
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800123} // Sync
Alexander Afanasyev146a51b2012-03-05 10:47:35 -0800124
125#endif
126