blob: 1a3da802d12a5fa7ffe36138141b7124b572e9ec [file] [log] [blame]
Ilya Moiseenkod26e6822011-08-23 17:48:38 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2011 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 *
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070018 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 * Ilya Moiseenko <iliamo@cs.ucla.edu>
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070020 */
21
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070022#include "ndn-name.h"
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070023#include <boost/foreach.hpp>
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070024#include "ns3/log.h"
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070025
26#include <iostream>
27
28using namespace std;
29
Alexander Afanasyev32c07562013-02-01 12:58:43 -080030NS_LOG_COMPONENT_DEFINE ("ndn.Name");
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070031
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070032namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070033namespace ndn {
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070034
Alexander Afanasyev32c07562013-02-01 12:58:43 -080035ATTRIBUTE_HELPER_CPP (Name);
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070036
Alexander Afanasyev32c07562013-02-01 12:58:43 -080037Name::Name (/* root */)
Alexander Afanasyev90d66ce2011-08-25 20:30:17 -070038{
39}
40
Alexander Afanasyev32c07562013-02-01 12:58:43 -080041Name::Name (const std::list<boost::reference_wrapper<const std::string> > &components)
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070042{
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070043 BOOST_FOREACH (const boost::reference_wrapper<const std::string> &component, components)
44 {
45 Add (component.get ());
46 }
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070047}
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070048
Alexander Afanasyev8e436fa2013-03-17 14:16:32 -070049Name::Name (const std::list<std::string> &components)
50{
51 BOOST_FOREACH (const std::string &component, components)
52 {
53 Add (component);
54 }
55}
56
Alexander Afanasyev32c07562013-02-01 12:58:43 -080057Name::Name (const std::string &prefix)
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070058{
59 istringstream is (prefix);
60 is >> *this;
61}
62
Alexander Afanasyev32c07562013-02-01 12:58:43 -080063Name::Name (const char *prefix)
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070064{
65 NS_ASSERT (prefix != 0);
Alexander Afanasyev32c07562013-02-01 12:58:43 -080066
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070067 istringstream is (prefix);
68 is >> *this;
69}
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070070
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070071const std::list<std::string> &
Alexander Afanasyev32c07562013-02-01 12:58:43 -080072Name::GetComponents () const
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070073{
74 return m_prefix;
75}
76
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -080077std::string
Alexander Afanasyev32c07562013-02-01 12:58:43 -080078Name::GetLastComponent () const
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -080079{
80 if (m_prefix.size () == 0)
81 {
82 return "";
83 }
84
85 return m_prefix.back ();
86}
87
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070088std::list<boost::reference_wrapper<const std::string> >
Alexander Afanasyev32c07562013-02-01 12:58:43 -080089Name::GetSubComponents (size_t num) const
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070090{
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -080091 NS_ASSERT_MSG (0<=num && num<=m_prefix.size (), "Invalid number of subcomponents requested");
Alexander Afanasyev32c07562013-02-01 12:58:43 -080092
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070093 std::list<boost::reference_wrapper<const std::string> > subComponents;
94 std::list<std::string>::const_iterator component = m_prefix.begin();
95 for (size_t i=0; i<num; i++, component++)
96 {
97 subComponents.push_back (boost::ref (*component));
98 }
Alexander Afanasyev32c07562013-02-01 12:58:43 -080099
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700100 return subComponents;
101}
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700102
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800103Name
104Name::cut (size_t minusComponents) const
Alexander Afanasyev59dedfd2012-07-26 17:55:29 -0700105{
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800106 Name retval;
107 std::list<std::string>::const_iterator component = m_prefix.begin ();
Alexander Afanasyev59dedfd2012-07-26 17:55:29 -0700108 for (uint32_t i = 0; i < m_prefix.size () - minusComponents; i++, component++)
109 {
110 retval.Add (*component);
111 }
112
113 return retval;
114}
115
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800116size_t
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800117Name::GetSerializedSize () const
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800118{
119 size_t nameSerializedSize = 2;
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800120
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800121 for (std::list<std::string>::const_iterator i = this->begin ();
122 i != this->end ();
123 i++)
124 {
125 nameSerializedSize += 2 + i->size ();
126 }
127 NS_ASSERT_MSG (nameSerializedSize < 30000, "Name is too long (> 30kbytes)");
128
129 return nameSerializedSize;
130}
131
132uint32_t
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800133Name::Serialize (Buffer::Iterator start) const
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800134{
135 Buffer::Iterator i = start;
136
137 i.WriteU16 (static_cast<uint16_t> (this->GetSerializedSize ()-2));
138
139 for (std::list<std::string>::const_iterator item = this->begin ();
140 item != this->end ();
141 item++)
142 {
143 i.WriteU16 (static_cast<uint16_t> (item->size ()));
144 i.Write (reinterpret_cast<const uint8_t*> (item->c_str ()), item->size ());
145 }
146
147 return i.GetDistanceFrom (start);
148}
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800149
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800150uint32_t
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800151Name::Deserialize (Buffer::Iterator start)
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800152{
153 Buffer::Iterator i = start;
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800154
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800155 uint16_t nameLength = i.ReadU16 ();
156 while (nameLength > 0)
157 {
158 uint16_t length = i.ReadU16 ();
159 nameLength = nameLength - 2 - length;
160
161 uint8_t tmp[length];
162 i.Read (tmp, length);
163
164 this->Add (string (reinterpret_cast<const char*> (tmp), length));
165 }
166
167 return i.GetDistanceFrom (start);
168}
169
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700170void
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800171Name::Print (std::ostream &os) const
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700172{
173 for (const_iterator i=m_prefix.begin(); i!=m_prefix.end(); i++)
174 {
175 os << "/" << *i;
176 }
Alexander Afanasyev07827182011-12-13 01:07:32 -0800177 if (m_prefix.size ()==0) os << "/";
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700178}
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800179
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700180std::ostream &
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800181operator << (std::ostream &os, const Name &components)
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700182{
183 components.Print (os);
184 return os;
185}
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700186
187std::istream &
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800188operator >> (std::istream &is, Name &components)
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700189{
190 istream_iterator<char> eos; // end of stream
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800191
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700192 std::string component = "";
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800193 istream_iterator<char> it (is);
194 for (; it != eos; it++)
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700195 {
196 if (*it == '/')
197 {
198 if (component != "")
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700199 components.Add (component);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700200 component = "";
201 }
202 else
203 component.push_back (*it);
204 }
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700205 if (component != "")
206 components.Add (component);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700207
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800208 is.clear ();
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800209 // NS_LOG_ERROR (components << ", bad: " << is.bad () <<", fail: " << is.fail ());
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800210
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700211 return is;
212}
213
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700214} // ndn
215} // ns3