blob: 7f247841d28c249e9bb8ba2930b7192420eb2c6e [file] [log] [blame]
Alexander Afanasyev32c07562013-02-01 12:58:43 -08001/* -*- 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 *
18 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 * Ilya Moiseenko <iliamo@cs.ucla.edu>
20 */
21
22#ifndef _NDN_NAME_H_
23#define _NDN_NAME_H_
24
25#include "ns3/simple-ref-count.h"
26#include "ns3/attribute.h"
27#include "ns3/attribute-helper.h"
28
29#include <string>
30#include <algorithm>
31#include <list>
32#include "ns3/object.h"
33#include "ns3/buffer.h"
34
35#include <boost/ref.hpp>
36
37namespace ns3 {
38namespace ndn {
39
40/**
41 * \ingroup ndn
42 * \brief Hierarchical NDN name
43 * A Name element represents a hierarchical name for Ndn content.
44 * It simply contains a sequence of Component elements.
45 * Each Component element contains a sequence of zero or more bytes.
46 * There are no restrictions on what byte sequences may be used.
47 * The Name element in an Interest is often referred to with the term name prefix or simply prefix.
48 */
49class Name : public SimpleRefCount<Name>
50{
51public:
52 typedef std::list<std::string>::iterator iterator;
53 typedef std::list<std::string>::const_iterator const_iterator;
54
55 /**
56 * \brief Constructor
57 * Creates a prefix with zero components (can be looked as root "/")
58 */
59 Name ();
60
61 /**
62 * \brief Constructor
63 * Creates a prefix from a list of strings where every string represents a prefix component
64 * @param[in] components A list of strings
65 */
66 Name (const std::list<boost::reference_wrapper<const std::string> > &components);
67
68 /**
69 * @brief Constructor
70 * Creates a prefix from the string (string is parsed using operator>>)
71 * @param[in] prefix A string representation of a prefix
72 */
73 Name (const std::string &prefix);
74
75 /**
76 * @brief Constructor
77 * Creates a prefix from the string (string is parsed using operator>>)
78 * @param[in] prefix A string representation of a prefix
79 */
80 Name (const char *prefix);
81
82 /**
83 * \brief Generic Add method
84 * Appends object of type T to the list of components
85 * @param[in] value The object to be appended
86 */
87 template<class T>
88 inline void
89 Add (const T &value);
90
91 /**
92 * \brief Generic constructor operator
93 * The object of type T will be appended to the list of components
94 */
95 template<class T>
96 inline Name&
97 operator () (const T &value);
98
99 /**
100 * \brief Get a name
101 * Returns a list of components (strings)
102 */
103 const std::list<std::string> &
104 GetComponents () const;
105
106 /**
107 * @brief Helper call to get the last component of the name
108 */
109 std::string
110 GetLastComponent () const;
111
112 /**
113 * \brief Get subcomponents of the name, starting with first component
114 * @param[in] num Number of components to return. Valid value is in range [1, GetComponents ().size ()]
115 */
116 std::list<boost::reference_wrapper<const std::string> >
117 GetSubComponents (size_t num) const;
118
119 /**
120 * @brief Get prefix of the name, containing less minusComponents right components
121 */
122 Name
123 cut (size_t minusComponents) const;
124
125 /**
126 * \brief Print name
127 * @param[in] os Stream to print
128 */
129 void Print (std::ostream &os) const;
130
131 /**
132 * @brief Get serialized size for ndnSIM packet encoding
133 */
134 size_t
135 GetSerializedSize () const;
136
137 /**
138 * @brief Serialize Name in ndnSIM packet encoding
139 * @param[in] start buffer to contain serialized name
140 */
141 uint32_t
142 Serialize (Buffer::Iterator start) const;
143
144 /**
145 * \brief Deserialize Name in ndnSIM packet encoding
146 * @param[in] start buffer that contains serialized name
147 */
148 uint32_t
149 Deserialize (Buffer::Iterator start);
150
151 /**
152 * \brief Returns the size of Name
153 */
154 inline size_t
155 size () const;
156
157 /**
158 * @brief Get read-write begin() iterator
159 */
160 inline iterator
161 begin ();
162
163 /**
164 * @brief Get read-only begin() iterator
165 */
166 inline const_iterator
167 begin () const;
168
169 /**
170 * @brief Get read-write end() iterator
171 */
172 inline iterator
173 end ();
174
175 /**
176 * @brief Get read-only end() iterator
177 */
178 inline const_iterator
179 end () const;
180
181 /**
182 * \brief Equality operator for Name
183 */
184 inline bool
185 operator== (const Name &prefix) const;
186
187 /**
188 * \brief Less than operator for Name
189 */
190 inline bool
191 operator< (const Name &prefix) const;
192
193 typedef std::string partial_type;
194
195private:
196 std::list<std::string> m_prefix; ///< \brief a list of strings (components)
197};
198
199/**
200 * \brief Print out name components separated by slashes, e.g., /first/second/third
201 */
202std::ostream &
203operator << (std::ostream &os, const Name &components);
204
205/**
206 * \brief Read components from input and add them to components. Will read input stream till eof
207 * Substrings separated by slashes will become separate components
208 */
209std::istream &
210operator >> (std::istream &is, Name &components);
211
212/**
213 * \brief Returns the size of Name object
214 */
215size_t
216Name::size () const
217{
218 return m_prefix.size ();
219}
220
221Name::iterator
222Name::begin ()
223{
224 return m_prefix.begin ();
225}
226
227/**
228 * @brief Get read-only begin() iterator
229 */
230Name::const_iterator
231Name::begin () const
232{
233 return m_prefix.begin ();
234}
235
236/**
237 * @brief Get read-write end() iterator
238 */
239Name::iterator
240Name::end ()
241{
242 return m_prefix.end ();
243}
244
245/**
246 * @brief Get read-only end() iterator
247 */
248Name::const_iterator
249Name::end () const
250{
251 return m_prefix.end ();
252}
253
254
255/**
256 * \brief Generic constructor operator
257 * The object of type T will be appended to the list of components
258 */
259template<class T>
260Name&
261Name::operator () (const T &value)
262{
263 Add (value);
264 return *this;
265}
266
267/**
268 * \brief Generic Add method
269 * Appends object of type T to the list of components
270 * @param[in] value The object to be appended
271 */
272template<class T>
273void
274Name::Add (const T &value)
275{
276 std::ostringstream os;
277 os << value;
278 m_prefix.push_back (os.str ());
279}
280
281/**
282 * \brief Equality operator for Name
283 */
284bool
285Name::operator== (const Name &prefix) const
286{
287 if (m_prefix.size () != prefix.m_prefix.size ())
288 return false;
289
290 return std::equal (m_prefix.begin (), m_prefix.end (), prefix.m_prefix.begin ());
291}
292
293/**
294 * \brief Less than operator for Name
295 */
296bool
297Name::operator< (const Name &prefix) const
298{
299 return std::lexicographical_compare (m_prefix.begin (), m_prefix.end (),
300 prefix.m_prefix.begin (), prefix.m_prefix.end ());
301}
302
303ATTRIBUTE_HELPER_HEADER (Name);
304
305/**
306 * @ingroup Ndn
307 * @brief For backwards compatibility
308 */
309class NameComponents : public Name
310{
311public:
312 NameComponents () : Name () { }
313
314 NameComponents (const std::list<boost::reference_wrapper<const std::string> > &components) : Name (components) { }
315
316 NameComponents (const std::string &prefix) : Name (prefix) { }
317
318 NameComponents (const char *prefix) : Name (prefix) { }
319};
320
321ATTRIBUTE_HELPER_HEADER (NameComponents);
322
323} // namespace ndn
324} // namespace ns3
325
326#endif // _NDN_NAME_H_
327