blob: f0a4f29e97918059f110c8b6146aa8d4368f18c5 [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 *
Alexander Afanasyev5d79e682012-11-19 14:12:23 -080018 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070019 */
20
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -080021#include "ndn-interest.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070022
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070023#include "ns3/log.h"
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -070024#include "ns3/unused.h"
Alexander Afanasyev663d63f2012-09-09 11:55:36 -070025#include "ns3/packet.h"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070026
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070027NS_LOG_COMPONENT_DEFINE ("ndn.Interest");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070028
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070029namespace ns3 {
30namespace ndn {
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070031
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070032NS_OBJECT_ENSURE_REGISTERED (Interest);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070033
34TypeId
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070035Interest::GetTypeId (void)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070036{
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070037 static TypeId tid = TypeId ("ns3::ndn::Interest")
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070038 .SetGroupName ("Ndn")
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070039 .SetParent<Header> ()
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070040 .AddConstructor<Interest> ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070041 ;
42 return tid;
43}
44
45
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070046Interest::Interest ()
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070047 : m_name ()
Alexander Afanasyev5d79e682012-11-19 14:12:23 -080048 , m_scope (0xFF)
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070049 , m_interestLifetime (Seconds (0))
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070050 , m_nonce (0)
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080051 , m_nackType (NORMAL_INTEREST)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070052{
53}
54
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070055Interest::Interest (const Interest &interest)
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070056 : m_name (Create<Name> (interest.GetName ()))
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070057 , m_scope (interest.m_scope)
58 , m_interestLifetime (interest.m_interestLifetime)
59 , m_nonce (interest.m_nonce)
60 , m_nackType (interest.m_nackType)
61{
62}
63
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070064Ptr<Interest>
65Interest::GetInterest (Ptr<Packet> packet)
Alexander Afanasyev663d63f2012-09-09 11:55:36 -070066{
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070067 Ptr<Interest> interest = Create<Interest> ();
Alexander Afanasyev663d63f2012-09-09 11:55:36 -070068 packet->RemoveHeader (*interest);
69
70 return interest;
71}
72
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070073void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070074Interest::SetName (Ptr<Name> name)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070075{
76 m_name = name;
77}
78
Alexander Afanasyevcc50d982013-03-30 19:09:10 -070079void
80Interest::SetName (const Name &name)
81{
82 m_name = Create<Name> (name);
83}
84
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070085const Name&
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070086Interest::GetName () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070087{
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070088 if (m_name==0) throw InterestException();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070089 return *m_name;
90}
91
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070092Ptr<const Name>
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070093Interest::GetNamePtr () const
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070094{
95 return m_name;
96}
97
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070098void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070099Interest::SetScope (int8_t scope)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700100{
101 m_scope = scope;
102}
103
104int8_t
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700105Interest::GetScope () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700106{
107 return m_scope;
108}
109
110void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700111Interest::SetInterestLifetime (Time lifetime)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700112{
113 m_interestLifetime = lifetime;
114}
115
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700116Time
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700117Interest::GetInterestLifetime () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700118{
119 return m_interestLifetime;
120}
121
122void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700123Interest::SetNonce (uint32_t nonce)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700124{
125 m_nonce = nonce;
126}
127
128uint32_t
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700129Interest::GetNonce () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700130{
131 return m_nonce;
132}
Ilya Moiseenko75d9bf52011-10-28 13:18:32 -0700133
134void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700135Interest::SetNack (uint8_t nackType)
Ilya Moiseenko75d9bf52011-10-28 13:18:32 -0700136{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800137 m_nackType = nackType;
Ilya Moiseenko75d9bf52011-10-28 13:18:32 -0700138}
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800139
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800140uint8_t
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700141Interest::GetNack () const
Ilya Moiseenko75d9bf52011-10-28 13:18:32 -0700142{
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800143 return m_nackType;
Ilya Moiseenko75d9bf52011-10-28 13:18:32 -0700144}
Ilya Moiseenko75d9bf52011-10-28 13:18:32 -0700145
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700146uint32_t
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700147Interest::GetSerializedSize (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700148{
Alexander Afanasyeve6bf47e2013-06-24 11:51:52 -0700149 size_t size =
150 1/*version*/ + 1 /*type*/ + 2/*length*/ +
151 (4/*nonce*/ + 1/*scope*/ + 1/*nack type*/ + 2/*timestamp*/ +
152 (m_name->GetSerializedSize ()) +
153 (2 + 0)/* selectors */ +
154 (2 + 0)/* options */);
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800155 NS_LOG_INFO ("Serialize size = " << size);
156
157 return size;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700158}
159
160void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700161Interest::Serialize (Buffer::Iterator start) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700162{
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800163 start.WriteU8 (0x80); // version
164 start.WriteU8 (0x00); // packet type
165
Alexander Afanasyeve6bf47e2013-06-24 11:51:52 -0700166 start.WriteU16 (GetSerializedSize () - 4);
167
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800168 start.WriteU32 (m_nonce);
169 start.WriteU8 (m_scope);
170 start.WriteU8 (m_nackType);
171
172 NS_ASSERT_MSG (0 <= m_interestLifetime.ToInteger (Time::S) && m_interestLifetime.ToInteger (Time::S) < 65535,
173 "Incorrect InterestLifetime (should not be smaller than 0 and larger than 65535");
174
175 // rounding timestamp value to seconds
176 start.WriteU16 (static_cast<uint16_t> (m_interestLifetime.ToInteger (Time::S)));
177
178 uint32_t offset = m_name->Serialize (start);
179 start.Next (offset);
180
181 start.WriteU16 (0); // no selectors
182 start.WriteU16 (0); // no options
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700183}
184
185uint32_t
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700186Interest::Deserialize (Buffer::Iterator start)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700187{
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800188 Buffer::Iterator i = start;
189
190 if (i.ReadU8 () != 0x80)
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700191 throw new InterestException ();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800192
193 if (i.ReadU8 () != 0x00)
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700194 throw new InterestException ();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800195
Alexander Afanasyeve6bf47e2013-06-24 11:51:52 -0700196 i.ReadU16 (); // length, don't need it right now
197
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -0800198 m_nonce = i.ReadU32 ();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800199 m_scope = i.ReadU8 ();
200 m_nackType = i.ReadU8 ();
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -0800201
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800202 m_interestLifetime = Seconds (i.ReadU16 ());
203
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700204 m_name = Create<Name> ();
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -0800205 uint32_t offset = m_name->Deserialize (i);
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800206 i.Next (offset);
207
208 i.ReadU16 ();
209 i.ReadU16 ();
210
211 NS_ASSERT (GetSerializedSize () == (i.GetDistanceFrom (start)));
212
213 return i.GetDistanceFrom (start);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700214}
215
216TypeId
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700217Interest::GetInstanceTypeId (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700218{
219 return GetTypeId ();
220}
221
222void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700223Interest::Print (std::ostream &os) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700224{
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700225 os << "I: " << GetName ();
226
227 return;
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700228 os << "<Interest>\n <Name>" << GetName () << "</Name>\n";
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800229 if (GetNack ()>0)
230 {
231 os << " <NACK>";
232 switch (GetNack ())
233 {
234 case NACK_LOOP:
235 os << "loop";
236 break;
237 case NACK_CONGESTION:
238 os << "congestion";
239 break;
240 default:
241 os << "unknown";
242 break;
243 }
244 os << "</NACK>\n";
245 }
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800246 os << " <Scope>" << GetScope () << "</Scope>\n";
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700247 if ( !GetInterestLifetime ().IsZero() )
248 os << " <InterestLifetime>" << GetInterestLifetime () << "</InterestLifetime>\n";
249 if (GetNonce ()>0)
250 os << " <Nonce>" << GetNonce () << "</Nonce>\n";
251 os << "</Interest>";
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700252}
253
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700254} // namespace ndn
255} // namespace ns3
256