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; |
Alexander Afanasyev | bd9c18e | 2012-11-19 15:23:41 -0800 | [diff] [blame] | 112 | |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 113 | 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 | |
| 124 | uint32_t |
| 125 | NameComponents::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 | |
| 142 | uint32_t |
| 143 | NameComponents::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 Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 162 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 163 | NameComponents::Print (std::ostream &os) const |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 164 | { |
| 165 | for (const_iterator i=m_prefix.begin(); i!=m_prefix.end(); i++) |
| 166 | { |
| 167 | os << "/" << *i; |
| 168 | } |
Alexander Afanasyev | 0782718 | 2011-12-13 01:07:32 -0800 | [diff] [blame] | 169 | if (m_prefix.size ()==0) os << "/"; |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | std::ostream & |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 173 | operator << (std::ostream &os, const NameComponents &components) |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 174 | { |
| 175 | components.Print (os); |
| 176 | return os; |
| 177 | } |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 178 | |
| 179 | std::istream & |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 180 | operator >> (std::istream &is, NameComponents &components) |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 181 | { |
| 182 | istream_iterator<char> eos; // end of stream |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 183 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 184 | std::string component = ""; |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 185 | istream_iterator<char> it (is); |
| 186 | for (; it != eos; it++) |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 187 | { |
| 188 | if (*it == '/') |
| 189 | { |
| 190 | if (component != "") |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 191 | components.Add (component); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 192 | component = ""; |
| 193 | } |
| 194 | else |
| 195 | component.push_back (*it); |
| 196 | } |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 197 | if (component != "") |
| 198 | components.Add (component); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 199 | |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 200 | is.clear (); |
| 201 | // NS_LOG_ERROR (components << ", bad: " << is.bad () <<", fail: " << is.fail ()); |
| 202 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 203 | return is; |
| 204 | } |
| 205 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 206 | } // ndn |
| 207 | } // ns3 |