blob: d0ebaab58a81877a43704f885c218d9b4104e2bb [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 Afanasyev663d63f2012-09-09 11:55:36 -070030#include "ns3/packet.h"
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070031#include "../helper/ndn-encoding-helper.h"
32#include "../helper/ndn-decoding-helper.h"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070033
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070034NS_LOG_COMPONENT_DEFINE ("ndn.InterestHeader");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070035
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070036namespace ns3 {
37namespace ndn {
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070038
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070039NS_OBJECT_ENSURE_REGISTERED (InterestHeader);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070040
41TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070042InterestHeader::GetTypeId (void)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070043{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070044 static TypeId tid = TypeId ("ns3::ndn::InterestHeader")
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070045 .SetGroupName ("Ndn")
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070046 .SetParent<Header> ()
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070047 .AddConstructor<InterestHeader> ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070048 ;
49 return tid;
50}
51
52
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070053InterestHeader::InterestHeader ()
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070054 : m_name ()
55 , m_minSuffixComponents (-1)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070056 , m_maxSuffixComponents (-1)
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070057 , m_exclude ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070058 , m_childSelector (false)
59 , m_answerOriginKind (false)
60 , m_scope (-1)
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070061 , m_interestLifetime (Seconds (0))
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070062 , m_nonce (0)
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080063 , m_nackType (NORMAL_INTEREST)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070064{
65}
66
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070067InterestHeader::InterestHeader (const InterestHeader &interest)
68 : m_name (Create<NameComponents> (interest.GetName ()))
69 , m_minSuffixComponents (interest.m_minSuffixComponents)
70 , m_maxSuffixComponents (interest.m_maxSuffixComponents)
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -070071 , m_exclude (interest.IsEnabledExclude () ? Create<NameComponents> (interest.GetExclude ()) : 0)
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070072 , m_childSelector (interest.m_childSelector)
73 , m_answerOriginKind (interest.m_answerOriginKind)
74 , m_scope (interest.m_scope)
75 , m_interestLifetime (interest.m_interestLifetime)
76 , m_nonce (interest.m_nonce)
77 , m_nackType (interest.m_nackType)
78{
79}
80
Alexander Afanasyev663d63f2012-09-09 11:55:36 -070081Ptr<InterestHeader>
82InterestHeader::GetInterest (Ptr<Packet> packet)
83{
84 Ptr<InterestHeader> interest = Create<InterestHeader> ();
85 packet->RemoveHeader (*interest);
86
87 return interest;
88}
89
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070090void
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070091InterestHeader::SetName (Ptr<NameComponents> name)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070092{
93 m_name = name;
94}
95
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070096const NameComponents&
97InterestHeader::GetName () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070098{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070099 if (m_name==0) throw InterestHeaderException();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700100 return *m_name;
101}
102
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700103Ptr<const NameComponents>
104InterestHeader::GetNamePtr () const
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700105{
106 return m_name;
107}
108
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700109void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700110InterestHeader::SetMinSuffixComponents (int32_t value)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700111{
112 m_minSuffixComponents = value;
113}
114
115int32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700116InterestHeader::GetMinSuffixComponents () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700117{
118 return m_minSuffixComponents;
119}
120
121void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700122InterestHeader::SetMaxSuffixComponents (int32_t value)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700123{
124 m_maxSuffixComponents = value;
125}
126
127int32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700128InterestHeader::GetMaxSuffixComponents () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700129{
130 return m_maxSuffixComponents;
131}
132
133void
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700134InterestHeader::SetExclude (Ptr<NameComponents> exclude)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700135{
136 m_exclude = exclude;
137}
138
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700139bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700140InterestHeader::IsEnabledExclude () const
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700141{
142 return m_exclude!=0;
143}
144
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700145const NameComponents&
146InterestHeader::GetExclude () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700147{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700148 if (m_exclude==0) throw InterestHeaderException();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700149 return *m_exclude;
150}
151
152void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700153InterestHeader::SetChildSelector (bool value)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700154{
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700155 m_childSelector = value;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700156}
157
158bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700159InterestHeader::IsEnabledChildSelector () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700160{
161 return m_childSelector;
162}
163
164void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700165InterestHeader::SetAnswerOriginKind (bool value)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700166{
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700167 m_answerOriginKind = value;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700168}
169
170bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700171InterestHeader::IsEnabledAnswerOriginKind () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700172{
173 return m_answerOriginKind;
174}
175
176void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700177InterestHeader::SetScope (int8_t scope)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700178{
179 m_scope = scope;
180}
181
182int8_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700183InterestHeader::GetScope () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700184{
185 return m_scope;
186}
187
188void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700189InterestHeader::SetInterestLifetime (Time lifetime)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700190{
191 m_interestLifetime = lifetime;
192}
193
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700194Time
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700195InterestHeader::GetInterestLifetime () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700196{
197 return m_interestLifetime;
198}
199
200void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700201InterestHeader::SetNonce (uint32_t nonce)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700202{
203 m_nonce = nonce;
204}
205
206uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700207InterestHeader::GetNonce () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700208{
209 return m_nonce;
210}
Ilya Moiseenko75d9bf52011-10-28 13:18:32 -0700211
212void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700213InterestHeader::SetNack (uint32_t nackType)
Ilya Moiseenko75d9bf52011-10-28 13:18:32 -0700214{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800215 m_nackType = nackType;
Ilya Moiseenko75d9bf52011-10-28 13:18:32 -0700216}
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800217
218uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700219InterestHeader::GetNack () const
Ilya Moiseenko75d9bf52011-10-28 13:18:32 -0700220{
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800221 return m_nackType;
Ilya Moiseenko75d9bf52011-10-28 13:18:32 -0700222}
Ilya Moiseenko75d9bf52011-10-28 13:18:32 -0700223
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700224uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700225InterestHeader::GetSerializedSize (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700226{
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700227 // unfortunately, we don't know exact header size in advance
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700228 return EncodingHelper::GetSerializedSize (*this);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700229}
230
231void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700232InterestHeader::Serialize (Buffer::Iterator start) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700233{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700234 size_t size = EncodingHelper::Serialize (start, *this);
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700235 NS_UNUSED (size);
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700236 NS_LOG_INFO ("Serialize size = " << size);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700237}
238
239uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700240InterestHeader::Deserialize (Buffer::Iterator start)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700241{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700242 return DecodingHelper::Deserialize (start, *this); // \todo Debugging is necessary
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700243}
244
245TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700246InterestHeader::GetInstanceTypeId (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700247{
248 return GetTypeId ();
249}
250
251void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700252InterestHeader::Print (std::ostream &os) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700253{
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700254 os << "I: " << GetName ();
255
256 return;
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700257 os << "<Interest>\n <Name>" << GetName () << "</Name>\n";
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800258 if (GetNack ()>0)
259 {
260 os << " <NACK>";
261 switch (GetNack ())
262 {
263 case NACK_LOOP:
264 os << "loop";
265 break;
266 case NACK_CONGESTION:
267 os << "congestion";
268 break;
269 default:
270 os << "unknown";
271 break;
272 }
273 os << "</NACK>\n";
274 }
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700275 if (GetMinSuffixComponents () >= 0)
276 os << " <MinSuffixComponents>" << GetMinSuffixComponents () << "</MinSuffixComponents>\n";
277 if (GetMaxSuffixComponents () >= 0)
278 os << " <MaxSuffixComponents>" << m_maxSuffixComponents << "</MaxSuffixComponents>\n";
279 if (IsEnabledExclude () && GetExclude ().size()>0)
280 os << " <Exclude>" << GetExclude () << "</Exclude>\n";
281 if (IsEnabledChildSelector ())
282 os << " <ChildSelector />\n";
283 if (IsEnabledAnswerOriginKind ())
284 os << " <AnswerOriginKind />\n";
285 if (GetScope () >= 0)
286 os << " <Scope>" << GetScope () << "</Scope>\n";
287 if ( !GetInterestLifetime ().IsZero() )
288 os << " <InterestLifetime>" << GetInterestLifetime () << "</InterestLifetime>\n";
289 if (GetNonce ()>0)
290 os << " <Nonce>" << GetNonce () << "</Nonce>\n";
291 os << "</Interest>";
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700292}
293
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700294} // namespace ndn
295} // namespace ns3
296