blob: 4a7965d82f2a5e00e34d52e6ab810e8cb40f2fa8 [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 Afanasyev2b4c9472012-08-09 15:00:38 -070030NS_LOG_COMPONENT_DEFINE ("ndn.NameComponents");
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 Afanasyev2b4c9472012-08-09 15:00:38 -070035ATTRIBUTE_HELPER_CPP (NameComponents);
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070036
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070037NameComponents::NameComponents (/* root */)
Alexander Afanasyev90d66ce2011-08-25 20:30:17 -070038{
39}
40
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070041NameComponents::NameComponents (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 Afanasyev2b4c9472012-08-09 15:00:38 -070049NameComponents::NameComponents (const std::string &prefix)
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070050{
51 istringstream is (prefix);
52 is >> *this;
53}
54
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070055NameComponents::NameComponents (const char *prefix)
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070056{
57 NS_ASSERT (prefix != 0);
58
59 istringstream is (prefix);
60 is >> *this;
61}
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070062
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070063const std::list<std::string> &
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070064NameComponents::GetComponents () const
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070065{
66 return m_prefix;
67}
68
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -080069std::string
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070070NameComponents::GetLastComponent () const
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -080071{
72 if (m_prefix.size () == 0)
73 {
74 return "";
75 }
76
77 return m_prefix.back ();
78}
79
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070080std::list<boost::reference_wrapper<const std::string> >
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070081NameComponents::GetSubComponents (size_t num) const
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070082{
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -080083 NS_ASSERT_MSG (0<=num && num<=m_prefix.size (), "Invalid number of subcomponents requested");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070084
85 std::list<boost::reference_wrapper<const std::string> > subComponents;
86 std::list<std::string>::const_iterator component = m_prefix.begin();
87 for (size_t i=0; i<num; i++, component++)
88 {
89 subComponents.push_back (boost::ref (*component));
90 }
91
92 return subComponents;
93}
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070094
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070095NameComponents
96NameComponents::cut (size_t minusComponents) const
Alexander Afanasyev59dedfd2012-07-26 17:55:29 -070097{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070098 NameComponents retval;
Alexander Afanasyev59dedfd2012-07-26 17:55:29 -070099 std::list<std::string>::const_iterator component = m_prefix.begin ();
100 for (uint32_t i = 0; i < m_prefix.size () - minusComponents; i++, component++)
101 {
102 retval.Add (*component);
103 }
104
105 return retval;
106}
107
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800108size_t
109NameComponents::GetSerializedSize () const
110{
111 size_t nameSerializedSize = 2;
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -0800112
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800113 for (std::list<std::string>::const_iterator i = this->begin ();
114 i != this->end ();
115 i++)
116 {
117 nameSerializedSize += 2 + i->size ();
118 }
119 NS_ASSERT_MSG (nameSerializedSize < 30000, "Name is too long (> 30kbytes)");
120
121 return nameSerializedSize;
122}
123
124uint32_t
125NameComponents::Serialize (Buffer::Iterator start) const
126{
127 Buffer::Iterator i = start;
128
129 i.WriteU16 (static_cast<uint16_t> (this->GetSerializedSize ()-2));
130
131 for (std::list<std::string>::const_iterator item = this->begin ();
132 item != this->end ();
133 item++)
134 {
135 i.WriteU16 (static_cast<uint16_t> (item->size ()));
136 i.Write (reinterpret_cast<const uint8_t*> (item->c_str ()), item->size ());
137 }
138
139 return i.GetDistanceFrom (start);
140}
141
142uint32_t
143NameComponents::Deserialize (Buffer::Iterator start)
144{
145 Buffer::Iterator i = start;
146
147 uint16_t nameLength = i.ReadU16 ();
148 while (nameLength > 0)
149 {
150 uint16_t length = i.ReadU16 ();
151 nameLength = nameLength - 2 - length;
152
153 uint8_t tmp[length];
154 i.Read (tmp, length);
155
156 this->Add (string (reinterpret_cast<const char*> (tmp), length));
157 }
158
159 return i.GetDistanceFrom (start);
160}
161
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700162void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700163NameComponents::Print (std::ostream &os) const
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700164{
165 for (const_iterator i=m_prefix.begin(); i!=m_prefix.end(); i++)
166 {
167 os << "/" << *i;
168 }
Alexander Afanasyev07827182011-12-13 01:07:32 -0800169 if (m_prefix.size ()==0) os << "/";
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700170}
171
172std::ostream &
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700173operator << (std::ostream &os, const NameComponents &components)
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700174{
175 components.Print (os);
176 return os;
177}
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700178
179std::istream &
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700180operator >> (std::istream &is, NameComponents &components)
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700181{
182 istream_iterator<char> eos; // end of stream
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700183
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700184 std::string component = "";
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800185 istream_iterator<char> it (is);
186 for (; it != eos; it++)
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700187 {
188 if (*it == '/')
189 {
190 if (component != "")
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700191 components.Add (component);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700192 component = "";
193 }
194 else
195 component.push_back (*it);
196 }
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700197 if (component != "")
198 components.Add (component);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700199
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800200 is.clear ();
201 // NS_LOG_ERROR (components << ", bad: " << is.bad () <<", fail: " << is.fail ());
202
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700203 return is;
204}
205
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700206} // ndn
207} // ns3