blob: 8e7a87290490f7db68cc633d7921951423837a5a [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 Afanasyev4aac5572012-08-09 10:49:55 -070022#include "ndn-name-components.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);
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070036ATTRIBUTE_HELPER_CPP (NameComponents);
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070037
Alexander Afanasyev32c07562013-02-01 12:58:43 -080038Name::Name (/* root */)
Alexander Afanasyev90d66ce2011-08-25 20:30:17 -070039{
40}
41
Alexander Afanasyev32c07562013-02-01 12:58:43 -080042Name::Name (const std::list<boost::reference_wrapper<const std::string> > &components)
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070043{
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070044 BOOST_FOREACH (const boost::reference_wrapper<const std::string> &component, components)
45 {
46 Add (component.get ());
47 }
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070048}
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070049
Alexander Afanasyev32c07562013-02-01 12:58:43 -080050Name::Name (const std::string &prefix)
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070051{
52 istringstream is (prefix);
53 is >> *this;
54}
55
Alexander Afanasyev32c07562013-02-01 12:58:43 -080056Name::Name (const char *prefix)
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070057{
58 NS_ASSERT (prefix != 0);
Alexander Afanasyev32c07562013-02-01 12:58:43 -080059
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070060 istringstream is (prefix);
61 is >> *this;
62}
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070063
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070064const std::list<std::string> &
Alexander Afanasyev32c07562013-02-01 12:58:43 -080065Name::GetComponents () const
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070066{
67 return m_prefix;
68}
69
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -080070std::string
Alexander Afanasyev32c07562013-02-01 12:58:43 -080071Name::GetLastComponent () const
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -080072{
73 if (m_prefix.size () == 0)
74 {
75 return "";
76 }
77
78 return m_prefix.back ();
79}
80
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070081std::list<boost::reference_wrapper<const std::string> >
Alexander Afanasyev32c07562013-02-01 12:58:43 -080082Name::GetSubComponents (size_t num) const
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070083{
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -080084 NS_ASSERT_MSG (0<=num && num<=m_prefix.size (), "Invalid number of subcomponents requested");
Alexander Afanasyev32c07562013-02-01 12:58:43 -080085
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070086 std::list<boost::reference_wrapper<const std::string> > subComponents;
87 std::list<std::string>::const_iterator component = m_prefix.begin();
88 for (size_t i=0; i<num; i++, component++)
89 {
90 subComponents.push_back (boost::ref (*component));
91 }
Alexander Afanasyev32c07562013-02-01 12:58:43 -080092
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070093 return subComponents;
94}
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070095
Alexander Afanasyev32c07562013-02-01 12:58:43 -080096Name
97Name::cut (size_t minusComponents) const
Alexander Afanasyev59dedfd2012-07-26 17:55:29 -070098{
Alexander Afanasyev32c07562013-02-01 12:58:43 -080099 Name retval;
100 std::list<std::string>::const_iterator component = m_prefix.begin ();
Alexander Afanasyev59dedfd2012-07-26 17:55:29 -0700101 for (uint32_t i = 0; i < m_prefix.size () - minusComponents; i++, component++)
102 {
103 retval.Add (*component);
104 }
105
106 return retval;
107}
108
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800109size_t
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800110Name::GetSerializedSize () const
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800111{
112 size_t nameSerializedSize = 2;
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800113
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800114 for (std::list<std::string>::const_iterator i = this->begin ();
115 i != this->end ();
116 i++)
117 {
118 nameSerializedSize += 2 + i->size ();
119 }
120 NS_ASSERT_MSG (nameSerializedSize < 30000, "Name is too long (> 30kbytes)");
121
122 return nameSerializedSize;
123}
124
125uint32_t
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800126Name::Serialize (Buffer::Iterator start) const
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800127{
128 Buffer::Iterator i = start;
129
130 i.WriteU16 (static_cast<uint16_t> (this->GetSerializedSize ()-2));
131
132 for (std::list<std::string>::const_iterator item = this->begin ();
133 item != this->end ();
134 item++)
135 {
136 i.WriteU16 (static_cast<uint16_t> (item->size ()));
137 i.Write (reinterpret_cast<const uint8_t*> (item->c_str ()), item->size ());
138 }
139
140 return i.GetDistanceFrom (start);
141}
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800142
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800143uint32_t
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800144Name::Deserialize (Buffer::Iterator start)
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800145{
146 Buffer::Iterator i = start;
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800147
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800148 uint16_t nameLength = i.ReadU16 ();
149 while (nameLength > 0)
150 {
151 uint16_t length = i.ReadU16 ();
152 nameLength = nameLength - 2 - length;
153
154 uint8_t tmp[length];
155 i.Read (tmp, length);
156
157 this->Add (string (reinterpret_cast<const char*> (tmp), length));
158 }
159
160 return i.GetDistanceFrom (start);
161}
162
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700163void
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800164Name::Print (std::ostream &os) const
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700165{
166 for (const_iterator i=m_prefix.begin(); i!=m_prefix.end(); i++)
167 {
168 os << "/" << *i;
169 }
Alexander Afanasyev07827182011-12-13 01:07:32 -0800170 if (m_prefix.size ()==0) os << "/";
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700171}
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800172
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700173std::ostream &
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800174operator << (std::ostream &os, const Name &components)
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700175{
176 components.Print (os);
177 return os;
178}
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700179
180std::istream &
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800181operator >> (std::istream &is, Name &components)
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700182{
183 istream_iterator<char> eos; // end of stream
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800184
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700185 std::string component = "";
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800186 istream_iterator<char> it (is);
187 for (; it != eos; it++)
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700188 {
189 if (*it == '/')
190 {
191 if (component != "")
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700192 components.Add (component);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700193 component = "";
194 }
195 else
196 component.push_back (*it);
197 }
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700198 if (component != "")
199 components.Add (component);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700200
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800201 is.clear ();
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800202 // NS_LOG_ERROR (components << ", bad: " << is.bad () <<", fail: " << is.fail ());
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800203
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700204 return is;
205}
206
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700207} // ndn
208} // ns3