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 | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 22 | #include "ndn-name-components.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); |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 36 | ATTRIBUTE_HELPER_CPP (NameComponents); |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 37 | |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 38 | Name::Name (/* root */) |
Alexander Afanasyev | 90d66ce | 2011-08-25 20:30:17 -0700 | [diff] [blame] | 39 | { |
| 40 | } |
| 41 | |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 42 | Name::Name (const std::list<boost::reference_wrapper<const std::string> > &components) |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 43 | { |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 44 | BOOST_FOREACH (const boost::reference_wrapper<const std::string> &component, components) |
| 45 | { |
| 46 | Add (component.get ()); |
| 47 | } |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 48 | } |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 49 | |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 50 | Name::Name (const std::string &prefix) |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 51 | { |
| 52 | istringstream is (prefix); |
| 53 | is >> *this; |
| 54 | } |
| 55 | |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 56 | Name::Name (const char *prefix) |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 57 | { |
| 58 | NS_ASSERT (prefix != 0); |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 59 | |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 60 | istringstream is (prefix); |
| 61 | is >> *this; |
| 62 | } |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 63 | |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 64 | const std::list<std::string> & |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 65 | Name::GetComponents () const |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 66 | { |
| 67 | return m_prefix; |
| 68 | } |
| 69 | |
Alexander Afanasyev | 3183b5a | 2011-12-23 20:48:20 -0800 | [diff] [blame] | 70 | std::string |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 71 | Name::GetLastComponent () const |
Alexander Afanasyev | 3183b5a | 2011-12-23 20:48:20 -0800 | [diff] [blame] | 72 | { |
| 73 | if (m_prefix.size () == 0) |
| 74 | { |
| 75 | return ""; |
| 76 | } |
| 77 | |
| 78 | return m_prefix.back (); |
| 79 | } |
| 80 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 81 | std::list<boost::reference_wrapper<const std::string> > |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 82 | Name::GetSubComponents (size_t num) const |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 83 | { |
Alexander Afanasyev | 4a5c2c1 | 2011-12-12 18:50:57 -0800 | [diff] [blame] | 84 | 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] | 85 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 86 | 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 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 | return subComponents; |
| 94 | } |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 95 | |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 96 | Name |
| 97 | Name::cut (size_t minusComponents) const |
Alexander Afanasyev | 59dedfd | 2012-07-26 17:55:29 -0700 | [diff] [blame] | 98 | { |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 99 | Name retval; |
| 100 | std::list<std::string>::const_iterator component = m_prefix.begin (); |
Alexander Afanasyev | 59dedfd | 2012-07-26 17:55:29 -0700 | [diff] [blame] | 101 | 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 Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 109 | size_t |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 110 | Name::GetSerializedSize () const |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 111 | { |
| 112 | size_t nameSerializedSize = 2; |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 113 | |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 114 | 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 | |
| 125 | uint32_t |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 126 | Name::Serialize (Buffer::Iterator start) const |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 127 | { |
| 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 Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 142 | |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 143 | uint32_t |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 144 | Name::Deserialize (Buffer::Iterator start) |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 145 | { |
| 146 | Buffer::Iterator i = start; |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 147 | |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 148 | 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 Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 163 | void |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 164 | Name::Print (std::ostream &os) const |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 165 | { |
| 166 | for (const_iterator i=m_prefix.begin(); i!=m_prefix.end(); i++) |
| 167 | { |
| 168 | os << "/" << *i; |
| 169 | } |
Alexander Afanasyev | 0782718 | 2011-12-13 01:07:32 -0800 | [diff] [blame] | 170 | if (m_prefix.size ()==0) os << "/"; |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 171 | } |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 172 | |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 173 | std::ostream & |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 174 | operator << (std::ostream &os, const Name &components) |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 175 | { |
| 176 | components.Print (os); |
| 177 | return os; |
| 178 | } |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 179 | |
| 180 | std::istream & |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 181 | operator >> (std::istream &is, Name &components) |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 182 | { |
| 183 | istream_iterator<char> eos; // end of stream |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 184 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 185 | std::string component = ""; |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 186 | istream_iterator<char> it (is); |
| 187 | for (; it != eos; it++) |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 188 | { |
| 189 | if (*it == '/') |
| 190 | { |
| 191 | if (component != "") |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 192 | components.Add (component); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 193 | component = ""; |
| 194 | } |
| 195 | else |
| 196 | component.push_back (*it); |
| 197 | } |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 198 | if (component != "") |
| 199 | components.Add (component); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 200 | |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 201 | is.clear (); |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 202 | // NS_LOG_ERROR (components << ", bad: " << is.bad () <<", fail: " << is.fail ()); |
Alexander Afanasyev | 32c0756 | 2013-02-01 12:58:43 -0800 | [diff] [blame] | 203 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 204 | return is; |
| 205 | } |
| 206 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 207 | } // ndn |
| 208 | } // ns3 |