blob: 46cbf254f14e5bbf833daa6ee659541e3087e2cc [file] [log] [blame]
Alexander Afanasyevc74a6022011-08-15 20:01:35 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyev08d984e2011-08-13 19:20:22 -07002/*
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: Ilya Moiseenko <iliamo@cs.ucla.edu>
19 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
20 */
21
22///< #CCN_PR_SCOPE0 (0x20) local scope,
23///< #CCN_PR_SCOPE1 (0x40) this host,
24///< #CCN_PR_SCOPE2 (0x80) immediate neighborhood
25
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070026#include "ndn-interest-header.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070027
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070028#include "ns3/log.h"
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -070029#include "ns3/unused.h"
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070030#include "../helper/ndn-encoding-helper.h"
31#include "../helper/ndn-decoding-helper.h"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070032
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070033NS_LOG_COMPONENT_DEFINE ("ndn.InterestHeader");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070034
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070035namespace ns3 {
36namespace ndn {
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070037
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070038NS_OBJECT_ENSURE_REGISTERED (InterestHeader);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070039
40TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070041InterestHeader::GetTypeId (void)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070042{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070043 static TypeId tid = TypeId ("ns3::ndn::InterestHeader")
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070044 .SetGroupName ("Ndn")
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070045 .SetParent<Header> ()
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070046 .AddConstructor<InterestHeader> ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070047 ;
48 return tid;
49}
50
51
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070052InterestHeader::InterestHeader ()
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070053 : m_name ()
54 , m_minSuffixComponents (-1)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070055 , m_maxSuffixComponents (-1)
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070056 , m_exclude ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070057 , m_childSelector (false)
58 , m_answerOriginKind (false)
59 , m_scope (-1)
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070060 , m_interestLifetime (Seconds (0))
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070061 , m_nonce (0)
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080062 , m_nackType (NORMAL_INTEREST)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070063{
64}
65
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070066InterestHeader::InterestHeader (const InterestHeader &interest)
67 : m_name (Create<NameComponents> (interest.GetName ()))
68 , m_minSuffixComponents (interest.m_minSuffixComponents)
69 , m_maxSuffixComponents (interest.m_maxSuffixComponents)
70 , m_exclude (Create<NameComponents> (interest.GetExclude ()))
71 , m_childSelector (interest.m_childSelector)
72 , m_answerOriginKind (interest.m_answerOriginKind)
73 , m_scope (interest.m_scope)
74 , m_interestLifetime (interest.m_interestLifetime)
75 , m_nonce (interest.m_nonce)
76 , m_nackType (interest.m_nackType)
77{
78}
79
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070080void
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070081InterestHeader::SetName (Ptr<NameComponents> name)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070082{
83 m_name = name;
84}
85
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070086const NameComponents&
87InterestHeader::GetName () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070088{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070089 if (m_name==0) throw InterestHeaderException();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070090 return *m_name;
91}
92
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070093Ptr<const NameComponents>
94InterestHeader::GetNamePtr () const
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070095{
96 return m_name;
97}
98
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070099void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700100InterestHeader::SetMinSuffixComponents (int32_t value)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700101{
102 m_minSuffixComponents = value;
103}
104
105int32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700106InterestHeader::GetMinSuffixComponents () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700107{
108 return m_minSuffixComponents;
109}
110
111void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700112InterestHeader::SetMaxSuffixComponents (int32_t value)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700113{
114 m_maxSuffixComponents = value;
115}
116
117int32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700118InterestHeader::GetMaxSuffixComponents () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700119{
120 return m_maxSuffixComponents;
121}
122
123void
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700124InterestHeader::SetExclude (Ptr<NameComponents> exclude)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700125{
126 m_exclude = exclude;
127}
128
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700129bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700130InterestHeader::IsEnabledExclude () const
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700131{
132 return m_exclude!=0;
133}
134
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700135const NameComponents&
136InterestHeader::GetExclude () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700137{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700138 if (m_exclude==0) throw InterestHeaderException();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700139 return *m_exclude;
140}
141
142void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700143InterestHeader::SetChildSelector (bool value)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700144{
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700145 m_childSelector = value;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700146}
147
148bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700149InterestHeader::IsEnabledChildSelector () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700150{
151 return m_childSelector;
152}
153
154void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700155InterestHeader::SetAnswerOriginKind (bool value)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700156{
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700157 m_answerOriginKind = value;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700158}
159
160bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700161InterestHeader::IsEnabledAnswerOriginKind () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700162{
163 return m_answerOriginKind;
164}
165
166void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700167InterestHeader::SetScope (int8_t scope)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700168{
169 m_scope = scope;
170}
171
172int8_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700173InterestHeader::GetScope () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700174{
175 return m_scope;
176}
177
178void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700179InterestHeader::SetInterestLifetime (Time lifetime)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700180{
181 m_interestLifetime = lifetime;
182}
183
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700184Time
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700185InterestHeader::GetInterestLifetime () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700186{
187 return m_interestLifetime;
188}
189
190void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700191InterestHeader::SetNonce (uint32_t nonce)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700192{
193 m_nonce = nonce;
194}
195
196uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700197InterestHeader::GetNonce () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700198{
199 return m_nonce;
200}
Ilya Moiseenko75d9bf52011-10-28 13:18:32 -0700201
202void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700203InterestHeader::SetNack (uint32_t nackType)
Ilya Moiseenko75d9bf52011-10-28 13:18:32 -0700204{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800205 m_nackType = nackType;
Ilya Moiseenko75d9bf52011-10-28 13:18:32 -0700206}
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800207
208uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700209InterestHeader::GetNack () const
Ilya Moiseenko75d9bf52011-10-28 13:18:32 -0700210{
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800211 return m_nackType;
Ilya Moiseenko75d9bf52011-10-28 13:18:32 -0700212}
Ilya Moiseenko75d9bf52011-10-28 13:18:32 -0700213
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700214uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700215InterestHeader::GetSerializedSize (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700216{
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700217 // unfortunately, we don't know exact header size in advance
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700218 return EncodingHelper::GetSerializedSize (*this);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700219}
220
221void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700222InterestHeader::Serialize (Buffer::Iterator start) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700223{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700224 size_t size = EncodingHelper::Serialize (start, *this);
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700225 NS_UNUSED (size);
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700226 NS_LOG_INFO ("Serialize size = " << size);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700227}
228
229uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700230InterestHeader::Deserialize (Buffer::Iterator start)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700231{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700232 return DecodingHelper::Deserialize (start, *this); // \todo Debugging is necessary
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700233}
234
235TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700236InterestHeader::GetInstanceTypeId (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700237{
238 return GetTypeId ();
239}
240
241void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700242InterestHeader::Print (std::ostream &os) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700243{
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700244 os << "I: " << GetName ();
245
246 return;
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700247 os << "<Interest>\n <Name>" << GetName () << "</Name>\n";
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800248 if (GetNack ()>0)
249 {
250 os << " <NACK>";
251 switch (GetNack ())
252 {
253 case NACK_LOOP:
254 os << "loop";
255 break;
256 case NACK_CONGESTION:
257 os << "congestion";
258 break;
259 default:
260 os << "unknown";
261 break;
262 }
263 os << "</NACK>\n";
264 }
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700265 if (GetMinSuffixComponents () >= 0)
266 os << " <MinSuffixComponents>" << GetMinSuffixComponents () << "</MinSuffixComponents>\n";
267 if (GetMaxSuffixComponents () >= 0)
268 os << " <MaxSuffixComponents>" << m_maxSuffixComponents << "</MaxSuffixComponents>\n";
269 if (IsEnabledExclude () && GetExclude ().size()>0)
270 os << " <Exclude>" << GetExclude () << "</Exclude>\n";
271 if (IsEnabledChildSelector ())
272 os << " <ChildSelector />\n";
273 if (IsEnabledAnswerOriginKind ())
274 os << " <AnswerOriginKind />\n";
275 if (GetScope () >= 0)
276 os << " <Scope>" << GetScope () << "</Scope>\n";
277 if ( !GetInterestLifetime ().IsZero() )
278 os << " <InterestLifetime>" << GetInterestLifetime () << "</InterestLifetime>\n";
279 if (GetNonce ()>0)
280 os << " <Nonce>" << GetNonce () << "</Nonce>\n";
281 os << "</Interest>";
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700282}
283
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700284} // namespace ndn
285} // namespace ns3
286