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 | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 30 | NS_LOG_COMPONENT_DEFINE ("ndn.NameComponents"); |
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 | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 35 | ATTRIBUTE_HELPER_CPP (NameComponents); |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 36 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 37 | NameComponents::NameComponents (/* root */) |
Alexander Afanasyev | 90d66ce | 2011-08-25 20:30:17 -0700 | [diff] [blame] | 38 | { |
| 39 | } |
| 40 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 41 | NameComponents::NameComponents (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 | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 49 | NameComponents::NameComponents (const std::string &prefix) |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 50 | { |
| 51 | istringstream is (prefix); |
| 52 | is >> *this; |
| 53 | } |
| 54 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 55 | NameComponents::NameComponents (const char *prefix) |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 56 | { |
| 57 | NS_ASSERT (prefix != 0); |
| 58 | |
| 59 | istringstream is (prefix); |
| 60 | is >> *this; |
| 61 | } |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 62 | |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 63 | const std::list<std::string> & |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 64 | NameComponents::GetComponents () const |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 65 | { |
| 66 | return m_prefix; |
| 67 | } |
| 68 | |
Alexander Afanasyev | 3183b5a | 2011-12-23 20:48:20 -0800 | [diff] [blame] | 69 | std::string |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 70 | NameComponents::GetLastComponent () const |
Alexander Afanasyev | 3183b5a | 2011-12-23 20:48:20 -0800 | [diff] [blame] | 71 | { |
| 72 | if (m_prefix.size () == 0) |
| 73 | { |
| 74 | return ""; |
| 75 | } |
| 76 | |
| 77 | return m_prefix.back (); |
| 78 | } |
| 79 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 80 | std::list<boost::reference_wrapper<const std::string> > |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 81 | NameComponents::GetSubComponents (size_t num) const |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 82 | { |
Alexander Afanasyev | 4a5c2c1 | 2011-12-12 18:50:57 -0800 | [diff] [blame] | 83 | NS_ASSERT_MSG (0<=num && num<=m_prefix.size (), "Invalid number of subcomponents requested"); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 84 | |
| 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 Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 94 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 95 | NameComponents |
| 96 | NameComponents::cut (size_t minusComponents) const |
Alexander Afanasyev | 59dedfd | 2012-07-26 17:55:29 -0700 | [diff] [blame] | 97 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 98 | NameComponents retval; |
Alexander Afanasyev | 59dedfd | 2012-07-26 17:55:29 -0700 | [diff] [blame] | 99 | 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 Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame^] | 108 | size_t |
| 109 | NameComponents::GetSerializedSize () const |
| 110 | { |
| 111 | size_t nameSerializedSize = 2; |
| 112 | for (std::list<std::string>::const_iterator i = this->begin (); |
| 113 | i != this->end (); |
| 114 | i++) |
| 115 | { |
| 116 | nameSerializedSize += 2 + i->size (); |
| 117 | } |
| 118 | NS_ASSERT_MSG (nameSerializedSize < 30000, "Name is too long (> 30kbytes)"); |
| 119 | |
| 120 | return nameSerializedSize; |
| 121 | } |
| 122 | |
| 123 | uint32_t |
| 124 | NameComponents::Serialize (Buffer::Iterator start) const |
| 125 | { |
| 126 | Buffer::Iterator i = start; |
| 127 | |
| 128 | i.WriteU16 (static_cast<uint16_t> (this->GetSerializedSize ()-2)); |
| 129 | |
| 130 | for (std::list<std::string>::const_iterator item = this->begin (); |
| 131 | item != this->end (); |
| 132 | item++) |
| 133 | { |
| 134 | i.WriteU16 (static_cast<uint16_t> (item->size ())); |
| 135 | i.Write (reinterpret_cast<const uint8_t*> (item->c_str ()), item->size ()); |
| 136 | } |
| 137 | |
| 138 | return i.GetDistanceFrom (start); |
| 139 | } |
| 140 | |
| 141 | uint32_t |
| 142 | NameComponents::Deserialize (Buffer::Iterator start) |
| 143 | { |
| 144 | Buffer::Iterator i = start; |
| 145 | |
| 146 | uint16_t nameLength = i.ReadU16 (); |
| 147 | while (nameLength > 0) |
| 148 | { |
| 149 | uint16_t length = i.ReadU16 (); |
| 150 | nameLength = nameLength - 2 - length; |
| 151 | |
| 152 | uint8_t tmp[length]; |
| 153 | i.Read (tmp, length); |
| 154 | |
| 155 | this->Add (string (reinterpret_cast<const char*> (tmp), length)); |
| 156 | } |
| 157 | |
| 158 | return i.GetDistanceFrom (start); |
| 159 | } |
| 160 | |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 161 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 162 | NameComponents::Print (std::ostream &os) const |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 163 | { |
| 164 | for (const_iterator i=m_prefix.begin(); i!=m_prefix.end(); i++) |
| 165 | { |
| 166 | os << "/" << *i; |
| 167 | } |
Alexander Afanasyev | 0782718 | 2011-12-13 01:07:32 -0800 | [diff] [blame] | 168 | if (m_prefix.size ()==0) os << "/"; |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | std::ostream & |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 172 | operator << (std::ostream &os, const NameComponents &components) |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 173 | { |
| 174 | components.Print (os); |
| 175 | return os; |
| 176 | } |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 177 | |
| 178 | std::istream & |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 179 | operator >> (std::istream &is, NameComponents &components) |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 180 | { |
| 181 | istream_iterator<char> eos; // end of stream |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 182 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 183 | std::string component = ""; |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 184 | istream_iterator<char> it (is); |
| 185 | for (; it != eos; it++) |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 186 | { |
| 187 | if (*it == '/') |
| 188 | { |
| 189 | if (component != "") |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 190 | components.Add (component); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 191 | component = ""; |
| 192 | } |
| 193 | else |
| 194 | component.push_back (*it); |
| 195 | } |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 196 | if (component != "") |
| 197 | components.Add (component); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 198 | |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 199 | is.clear (); |
| 200 | // NS_LOG_ERROR (components << ", bad: " << is.bad () <<", fail: " << is.fail ()); |
| 201 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 202 | return is; |
| 203 | } |
| 204 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 205 | } // ndn |
| 206 | } // ns3 |