blob: 1c4e3d6d07ce011efa0066ce5d1fad3821f462c9 [file] [log] [blame]
Alexander Afanasyev5d79e682012-11-19 14:12:23 -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: 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
26#include "ndn-interest-header-ccnb.h"
27
28#include "ns3/log.h"
29#include "ns3/unused.h"
30#include "ns3/packet.h"
31#include "../helper/ndn-encoding-helper.h"
32#include "../helper/ndn-decoding-helper.h"
33
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070034NS_LOG_COMPONENT_DEFINE ("ndn.Interest");
Alexander Afanasyev5d79e682012-11-19 14:12:23 -080035
36namespace ns3 {
37namespace ndn {
38
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070039NS_OBJECT_ENSURE_REGISTERED (Interest);
Alexander Afanasyev5d79e682012-11-19 14:12:23 -080040
41TypeId
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070042Interest::GetTypeId (void)
Alexander Afanasyev5d79e682012-11-19 14:12:23 -080043{
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070044 static TypeId tid = TypeId ("ns3::ndn::Interest")
Alexander Afanasyev5d79e682012-11-19 14:12:23 -080045 .SetGroupName ("Ndn")
46 .SetParent<Header> ()
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070047 .AddConstructor<Interest> ()
Alexander Afanasyev5d79e682012-11-19 14:12:23 -080048 ;
49 return tid;
50}
51
52
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070053Interest::Interest ()
Alexander Afanasyev5d79e682012-11-19 14:12:23 -080054 : m_name ()
55 , m_minSuffixComponents (-1)
56 , m_maxSuffixComponents (-1)
57 , m_exclude ()
58 , m_childSelector (false)
59 , m_answerOriginKind (false)
60 , m_scope (-1)
61 , m_interestLifetime (Seconds (0))
62 , m_nonce (0)
63 , m_nackType (NORMAL_INTEREST)
64{
65}
66
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070067Interest::Interest (const Interest &interest)
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070068 : m_name (Create<Name> (interest.GetName ()))
Alexander Afanasyev5d79e682012-11-19 14:12:23 -080069 , m_minSuffixComponents (interest.m_minSuffixComponents)
70 , m_maxSuffixComponents (interest.m_maxSuffixComponents)
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070071 , m_exclude (interest.IsEnabledExclude () ? Create<Name> (interest.GetExclude ()) : 0)
Alexander Afanasyev5d79e682012-11-19 14:12:23 -080072 , 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 Afanasyeveae83ee2013-03-15 15:01:10 -070081Ptr<Interest>
82Interest::GetInterest (Ptr<Packet> packet)
Alexander Afanasyev5d79e682012-11-19 14:12:23 -080083{
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070084 Ptr<Interest> interest = Create<Interest> ();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -080085 packet->RemoveHeader (*interest);
86
87 return interest;
88}
89
90void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070091Interest::SetName (Ptr<Name> name)
Alexander Afanasyev5d79e682012-11-19 14:12:23 -080092{
93 m_name = name;
94}
95
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070096const Name&
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070097Interest::GetName () const
Alexander Afanasyev5d79e682012-11-19 14:12:23 -080098{
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070099 if (m_name==0) throw InterestException();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800100 return *m_name;
101}
102
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700103Ptr<const Name>
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700104Interest::GetNamePtr () const
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800105{
106 return m_name;
107}
108
109void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700110Interest::SetMinSuffixComponents (int32_t value)
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800111{
112 m_minSuffixComponents = value;
113}
114
115int32_t
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700116Interest::GetMinSuffixComponents () const
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800117{
118 return m_minSuffixComponents;
119}
120
121void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700122Interest::SetMaxSuffixComponents (int32_t value)
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800123{
124 m_maxSuffixComponents = value;
125}
126
127int32_t
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700128Interest::GetMaxSuffixComponents () const
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800129{
130 return m_maxSuffixComponents;
131}
132
133void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700134Interest::SetExclude (Ptr<Name> exclude)
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800135{
136 m_exclude = exclude;
137}
138
139bool
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700140Interest::IsEnabledExclude () const
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800141{
142 return m_exclude!=0;
143}
144
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700145const Name&
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700146Interest::GetExclude () const
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800147{
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700148 if (m_exclude==0) throw InterestException();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800149 return *m_exclude;
150}
151
152void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700153Interest::SetChildSelector (bool value)
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800154{
155 m_childSelector = value;
156}
157
158bool
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700159Interest::IsEnabledChildSelector () const
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800160{
161 return m_childSelector;
162}
163
164void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700165Interest::SetAnswerOriginKind (bool value)
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800166{
167 m_answerOriginKind = value;
168}
169
170bool
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700171Interest::IsEnabledAnswerOriginKind () const
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800172{
173 return m_answerOriginKind;
174}
175
176void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700177Interest::SetScope (int8_t scope)
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800178{
179 m_scope = scope;
180}
181
182int8_t
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700183Interest::GetScope () const
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800184{
185 return m_scope;
186}
187
188void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700189Interest::SetInterestLifetime (Time lifetime)
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800190{
191 m_interestLifetime = lifetime;
192}
193
194Time
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700195Interest::GetInterestLifetime () const
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800196{
197 return m_interestLifetime;
198}
199
200void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700201Interest::SetNonce (uint32_t nonce)
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800202{
203 m_nonce = nonce;
204}
205
206uint32_t
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700207Interest::GetNonce () const
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800208{
209 return m_nonce;
210}
211
212void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700213Interest::SetNack (uint32_t nackType)
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800214{
215 m_nackType = nackType;
216}
217
218uint32_t
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700219Interest::GetNack () const
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800220{
221 return m_nackType;
222}
223
224uint32_t
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700225Interest::GetSerializedSize (void) const
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800226{
227 // unfortunately, we don't know exact header size in advance
228 return EncodingHelper::GetSerializedSize (*this);
229}
230
231void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700232Interest::Serialize (Buffer::Iterator start) const
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800233{
234 size_t size = EncodingHelper::Serialize (start, *this);
235 NS_UNUSED (size);
236 NS_LOG_INFO ("Serialize size = " << size);
237}
238
239uint32_t
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700240Interest::Deserialize (Buffer::Iterator start)
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800241{
242 return DecodingHelper::Deserialize (start, *this); // \todo Debugging is necessary
243}
244
245TypeId
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700246Interest::GetInstanceTypeId (void) const
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800247{
248 return GetTypeId ();
249}
250
251void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700252Interest::Print (std::ostream &os) const
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800253{
254 os << "I: " << GetName ();
255
256 return;
257 os << "<Interest>\n <Name>" << GetName () << "</Name>\n";
258 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 }
275 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>";
292}
293
294} // namespace ndn
295} // namespace ns3
296