Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | * Ilya Moiseenko <iliamo@cs.ucla.edu> |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 22 | #include "ndn-name.h" |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 23 | #include <boost/foreach.hpp> |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 24 | #include "ns3/log.h" |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 25 | |
| 26 | #include <iostream> |
| 27 | |
| 28 | using namespace std; |
| 29 | |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 30 | NS_LOG_COMPONENT_DEFINE ("ndn.Name"); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 31 | |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 32 | namespace ns3 { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 33 | namespace ndn { |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 34 | |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 35 | ATTRIBUTE_HELPER_CPP (Name); |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 36 | |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 37 | Name::Name (/* root */) |
Alexander Afanasyev | 90d66ce | 2011-08-25 20:30:17 -0700 | [diff] [blame] | 38 | { |
| 39 | } |
| 40 | |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 41 | Name::Name (const std::list<boost::reference_wrapper<const std::string> > &components) |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 42 | { |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 43 | BOOST_FOREACH (const boost::reference_wrapper<const std::string> &component, components) |
| 44 | { |
| 45 | Add (component.get ()); |
| 46 | } |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 47 | } |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 48 | |
Alexander Afanasyev | 8e436fa | 2013-03-17 14:16:32 -0700 | [diff] [blame] | 49 | Name::Name (const std::list<std::string> &components) |
| 50 | { |
| 51 | BOOST_FOREACH (const std::string &component, components) |
| 52 | { |
| 53 | Add (component); |
| 54 | } |
| 55 | } |
| 56 | |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 57 | Name::Name (const std::string &prefix) |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 58 | { |
| 59 | istringstream is (prefix); |
| 60 | is >> *this; |
| 61 | } |
| 62 | |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 63 | Name::Name (const char *prefix) |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 64 | { |
| 65 | NS_ASSERT (prefix != 0); |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 66 | |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 67 | istringstream is (prefix); |
| 68 | is >> *this; |
| 69 | } |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 70 | |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 71 | const std::list<std::string> & |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 72 | Name::GetComponents () const |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 73 | { |
| 74 | return m_prefix; |
| 75 | } |
| 76 | |
Alexander Afanasyev | 3183b5a | 2011-12-23 20:48:20 -0800 | [diff] [blame] | 77 | std::string |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 78 | Name::GetLastComponent () const |
Alexander Afanasyev | 3183b5a | 2011-12-23 20:48:20 -0800 | [diff] [blame] | 79 | { |
| 80 | if (m_prefix.size () == 0) |
| 81 | { |
| 82 | return ""; |
| 83 | } |
| 84 | |
| 85 | return m_prefix.back (); |
| 86 | } |
| 87 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 88 | std::list<boost::reference_wrapper<const std::string> > |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 89 | Name::GetSubComponents (size_t num) const |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 90 | { |
Alexander Afanasyev | 4a5c2c1 | 2011-12-12 18:50:57 -0800 | [diff] [blame] | 91 | NS_ASSERT_MSG (0<=num && num<=m_prefix.size (), "Invalid number of subcomponents requested"); |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 92 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 93 | 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 Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 99 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 100 | return subComponents; |
| 101 | } |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 102 | |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 103 | Name |
| 104 | Name::cut (size_t minusComponents) const |
Alexander Afanasyev | 59dedfd | 2012-07-26 17:55:29 -0700 | [diff] [blame] | 105 | { |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 106 | Name retval; |
| 107 | std::list<std::string>::const_iterator component = m_prefix.begin (); |
Alexander Afanasyev | 59dedfd | 2012-07-26 17:55:29 -0700 | [diff] [blame] | 108 | 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 Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 116 | size_t |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 117 | Name::GetSerializedSize () const |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 118 | { |
| 119 | size_t nameSerializedSize = 2; |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 120 | |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 121 | 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 | |
| 132 | uint32_t |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 133 | Name::Serialize (Buffer::Iterator start) const |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 134 | { |
| 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 Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 149 | |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 150 | uint32_t |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 151 | Name::Deserialize (Buffer::Iterator start) |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 152 | { |
| 153 | Buffer::Iterator i = start; |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 154 | |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 155 | 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 Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 170 | void |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 171 | Name::Print (std::ostream &os) const |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 172 | { |
| 173 | for (const_iterator i=m_prefix.begin(); i!=m_prefix.end(); i++) |
| 174 | { |
| 175 | os << "/" << *i; |
| 176 | } |
Alexander Afanasyev | 0782718 | 2011-12-13 01:07:32 -0800 | [diff] [blame] | 177 | if (m_prefix.size ()==0) os << "/"; |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 178 | } |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 179 | |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 180 | std::ostream & |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 181 | operator << (std::ostream &os, const Name &components) |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 182 | { |
| 183 | components.Print (os); |
| 184 | return os; |
| 185 | } |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 186 | |
| 187 | std::istream & |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 188 | operator >> (std::istream &is, Name &components) |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 189 | { |
| 190 | istream_iterator<char> eos; // end of stream |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 191 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 192 | std::string component = ""; |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 193 | istream_iterator<char> it (is); |
| 194 | for (; it != eos; it++) |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 195 | { |
| 196 | if (*it == '/') |
| 197 | { |
| 198 | if (component != "") |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 199 | components.Add (component); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 200 | component = ""; |
| 201 | } |
| 202 | else |
| 203 | component.push_back (*it); |
| 204 | } |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 205 | if (component != "") |
| 206 | components.Add (component); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 207 | |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 208 | is.clear (); |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 209 | // NS_LOG_ERROR (components << ", bad: " << is.bad () <<", fail: " << is.fail ()); |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 210 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 211 | return is; |
| 212 | } |
| 213 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 214 | } // ndn |
| 215 | } // ns3 |