blob: 192cae5e5fc106499b5b568eaeb9a81c2c5b991f [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 Afanasyev4aac5572012-08-09 10:49:55 -070033NS_LOG_COMPONENT_DEFINE ("NdnInterestHeader");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070034
35namespace ns3
36{
37
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070038NS_OBJECT_ENSURE_REGISTERED (NdnInterestHeader);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070039
40TypeId
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070041NdnInterestHeader::GetTypeId (void)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070042{
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070043 static TypeId tid = TypeId ("ns3::NdnInterestHeader")
44 .SetGroupName ("Ndn")
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070045 .SetParent<Header> ()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070046 .AddConstructor<NdnInterestHeader> ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070047 ;
48 return tid;
49}
50
51
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070052NdnInterestHeader::NdnInterestHeader ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070053 : m_minSuffixComponents (-1)
54 , m_maxSuffixComponents (-1)
55 , m_childSelector (false)
56 , m_answerOriginKind (false)
57 , m_scope (-1)
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070058 , m_interestLifetime (Seconds (0))
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070059 , m_nonce (0)
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080060 , m_nackType (NORMAL_INTEREST)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070061{
62}
63
64void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070065NdnInterestHeader::SetName (const Ptr<NdnNameComponents> &name)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070066{
67 m_name = name;
68}
69
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070070const NdnNameComponents&
71NdnInterestHeader::GetName () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070072{
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070073 if (m_name==0) throw NdnInterestHeaderException();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070074 return *m_name;
75}
76
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070077Ptr<const NdnNameComponents>
78NdnInterestHeader::GetNamePtr () const
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070079{
80 return m_name;
81}
82
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070083void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070084NdnInterestHeader::SetMinSuffixComponents (int32_t value)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070085{
86 m_minSuffixComponents = value;
87}
88
89int32_t
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070090NdnInterestHeader::GetMinSuffixComponents () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070091{
92 return m_minSuffixComponents;
93}
94
95void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070096NdnInterestHeader::SetMaxSuffixComponents (int32_t value)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070097{
98 m_maxSuffixComponents = value;
99}
100
101int32_t
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700102NdnInterestHeader::GetMaxSuffixComponents () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700103{
104 return m_maxSuffixComponents;
105}
106
107void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700108NdnInterestHeader::SetExclude (const Ptr<NdnNameComponents> &exclude)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700109{
110 m_exclude = exclude;
111}
112
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700113bool
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700114NdnInterestHeader::IsEnabledExclude () const
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700115{
116 return m_exclude!=0;
117}
118
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700119const NdnNameComponents&
120NdnInterestHeader::GetExclude () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700121{
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700122 if (m_exclude==0) throw NdnInterestHeaderException();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700123 return *m_exclude;
124}
125
126void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700127NdnInterestHeader::SetChildSelector (bool value)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700128{
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700129 m_childSelector = value;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700130}
131
132bool
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700133NdnInterestHeader::IsEnabledChildSelector () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700134{
135 return m_childSelector;
136}
137
138void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700139NdnInterestHeader::SetAnswerOriginKind (bool value)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700140{
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700141 m_answerOriginKind = value;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700142}
143
144bool
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700145NdnInterestHeader::IsEnabledAnswerOriginKind () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700146{
147 return m_answerOriginKind;
148}
149
150void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700151NdnInterestHeader::SetScope (int8_t scope)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700152{
153 m_scope = scope;
154}
155
156int8_t
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700157NdnInterestHeader::GetScope () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700158{
159 return m_scope;
160}
161
162void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700163NdnInterestHeader::SetInterestLifetime (Time lifetime)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700164{
165 m_interestLifetime = lifetime;
166}
167
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700168Time
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700169NdnInterestHeader::GetInterestLifetime () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700170{
171 return m_interestLifetime;
172}
173
174void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700175NdnInterestHeader::SetNonce (uint32_t nonce)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700176{
177 m_nonce = nonce;
178}
179
180uint32_t
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700181NdnInterestHeader::GetNonce () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700182{
183 return m_nonce;
184}
Ilya Moiseenko75d9bf52011-10-28 13:18:32 -0700185
186void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700187NdnInterestHeader::SetNack (uint32_t nackType)
Ilya Moiseenko75d9bf52011-10-28 13:18:32 -0700188{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800189 m_nackType = nackType;
Ilya Moiseenko75d9bf52011-10-28 13:18:32 -0700190}
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800191
192uint32_t
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700193NdnInterestHeader::GetNack () const
Ilya Moiseenko75d9bf52011-10-28 13:18:32 -0700194{
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800195 return m_nackType;
Ilya Moiseenko75d9bf52011-10-28 13:18:32 -0700196}
Ilya Moiseenko75d9bf52011-10-28 13:18:32 -0700197
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700198uint32_t
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700199NdnInterestHeader::GetSerializedSize (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700200{
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700201 // unfortunately, we don't know exact header size in advance
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700202 return NdnEncodingHelper::GetSerializedSize (*this);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700203}
204
205void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700206NdnInterestHeader::Serialize (Buffer::Iterator start) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700207{
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700208 size_t size = NdnEncodingHelper::Serialize (start, *this);
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700209 NS_UNUSED (size);
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700210 NS_LOG_INFO ("Serialize size = " << size);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700211}
212
213uint32_t
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700214NdnInterestHeader::Deserialize (Buffer::Iterator start)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700215{
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700216 return NdnDecodingHelper::Deserialize (start, *this); // \todo Debugging is necessary
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700217}
218
219TypeId
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700220NdnInterestHeader::GetInstanceTypeId (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700221{
222 return GetTypeId ();
223}
224
225void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700226NdnInterestHeader::Print (std::ostream &os) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700227{
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700228 os << "I: " << GetName ();
229
230 return;
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700231 os << "<Interest>\n <Name>" << GetName () << "</Name>\n";
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800232 if (GetNack ()>0)
233 {
234 os << " <NACK>";
235 switch (GetNack ())
236 {
237 case NACK_LOOP:
238 os << "loop";
239 break;
240 case NACK_CONGESTION:
241 os << "congestion";
242 break;
243 default:
244 os << "unknown";
245 break;
246 }
247 os << "</NACK>\n";
248 }
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700249 if (GetMinSuffixComponents () >= 0)
250 os << " <MinSuffixComponents>" << GetMinSuffixComponents () << "</MinSuffixComponents>\n";
251 if (GetMaxSuffixComponents () >= 0)
252 os << " <MaxSuffixComponents>" << m_maxSuffixComponents << "</MaxSuffixComponents>\n";
253 if (IsEnabledExclude () && GetExclude ().size()>0)
254 os << " <Exclude>" << GetExclude () << "</Exclude>\n";
255 if (IsEnabledChildSelector ())
256 os << " <ChildSelector />\n";
257 if (IsEnabledAnswerOriginKind ())
258 os << " <AnswerOriginKind />\n";
259 if (GetScope () >= 0)
260 os << " <Scope>" << GetScope () << "</Scope>\n";
261 if ( !GetInterestLifetime ().IsZero() )
262 os << " <InterestLifetime>" << GetInterestLifetime () << "</InterestLifetime>\n";
263 if (GetNonce ()>0)
264 os << " <Nonce>" << GetNonce () << "</Nonce>\n";
265 os << "</Interest>";
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700266}
267
268}