Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 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 | * |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 19 | */ |
| 20 | |
Alexander Afanasyev | bd9c18e | 2012-11-19 15:23:41 -0800 | [diff] [blame] | 21 | #include "ndn-interest.h" |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 22 | |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 23 | #include "ns3/log.h" |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 24 | #include "ns3/unused.h" |
Alexander Afanasyev | 663d63f | 2012-09-09 11:55:36 -0700 | [diff] [blame] | 25 | #include "ns3/packet.h" |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 26 | |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 27 | NS_LOG_COMPONENT_DEFINE ("ndn.Interest"); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 28 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 29 | namespace ns3 { |
| 30 | namespace ndn { |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 31 | |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 32 | NS_OBJECT_ENSURE_REGISTERED (Interest); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 33 | |
| 34 | TypeId |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 35 | Interest::GetTypeId (void) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 36 | { |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 37 | static TypeId tid = TypeId ("ns3::ndn::Interest") |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 38 | .SetGroupName ("Ndn") |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 39 | .SetParent<Header> () |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 40 | .AddConstructor<Interest> () |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 41 | ; |
| 42 | return tid; |
| 43 | } |
| 44 | |
| 45 | |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 46 | Interest::Interest () |
Alexander Afanasyev | 31cb469 | 2012-08-17 13:08:20 -0700 | [diff] [blame] | 47 | : m_name () |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 48 | , m_scope (0xFF) |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 49 | , m_interestLifetime (Seconds (0)) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 50 | , m_nonce (0) |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 51 | , m_nackType (NORMAL_INTEREST) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 52 | { |
| 53 | } |
| 54 | |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 55 | Interest::Interest (const Interest &interest) |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 56 | : m_name (Create<Name> (interest.GetName ())) |
Alexander Afanasyev | 31cb469 | 2012-08-17 13:08:20 -0700 | [diff] [blame] | 57 | , 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 Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 64 | Ptr<Interest> |
| 65 | Interest::GetInterest (Ptr<Packet> packet) |
Alexander Afanasyev | 663d63f | 2012-09-09 11:55:36 -0700 | [diff] [blame] | 66 | { |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 67 | Ptr<Interest> interest = Create<Interest> (); |
Alexander Afanasyev | 663d63f | 2012-09-09 11:55:36 -0700 | [diff] [blame] | 68 | packet->RemoveHeader (*interest); |
| 69 | |
| 70 | return interest; |
| 71 | } |
| 72 | |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 73 | void |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 74 | Interest::SetName (Ptr<Name> name) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 75 | { |
| 76 | m_name = name; |
| 77 | } |
| 78 | |
Alexander Afanasyev | cc50d98 | 2013-03-30 19:09:10 -0700 | [diff] [blame] | 79 | void |
| 80 | Interest::SetName (const Name &name) |
| 81 | { |
| 82 | m_name = Create<Name> (name); |
| 83 | } |
| 84 | |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 85 | const Name& |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 86 | Interest::GetName () const |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 87 | { |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 88 | if (m_name==0) throw InterestException(); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 89 | return *m_name; |
| 90 | } |
| 91 | |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 92 | Ptr<const Name> |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 93 | Interest::GetNamePtr () const |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 94 | { |
| 95 | return m_name; |
| 96 | } |
| 97 | |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 98 | void |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 99 | Interest::SetScope (int8_t scope) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 100 | { |
| 101 | m_scope = scope; |
| 102 | } |
| 103 | |
| 104 | int8_t |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 105 | Interest::GetScope () const |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 106 | { |
| 107 | return m_scope; |
| 108 | } |
| 109 | |
| 110 | void |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 111 | Interest::SetInterestLifetime (Time lifetime) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 112 | { |
| 113 | m_interestLifetime = lifetime; |
| 114 | } |
| 115 | |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 116 | Time |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 117 | Interest::GetInterestLifetime () const |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 118 | { |
| 119 | return m_interestLifetime; |
| 120 | } |
| 121 | |
| 122 | void |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 123 | Interest::SetNonce (uint32_t nonce) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 124 | { |
| 125 | m_nonce = nonce; |
| 126 | } |
| 127 | |
| 128 | uint32_t |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 129 | Interest::GetNonce () const |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 130 | { |
| 131 | return m_nonce; |
| 132 | } |
Ilya Moiseenko | 75d9bf5 | 2011-10-28 13:18:32 -0700 | [diff] [blame] | 133 | |
| 134 | void |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 135 | Interest::SetNack (uint8_t nackType) |
Ilya Moiseenko | 75d9bf5 | 2011-10-28 13:18:32 -0700 | [diff] [blame] | 136 | { |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 137 | m_nackType = nackType; |
Ilya Moiseenko | 75d9bf5 | 2011-10-28 13:18:32 -0700 | [diff] [blame] | 138 | } |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 139 | |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 140 | uint8_t |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 141 | Interest::GetNack () const |
Ilya Moiseenko | 75d9bf5 | 2011-10-28 13:18:32 -0700 | [diff] [blame] | 142 | { |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 143 | return m_nackType; |
Ilya Moiseenko | 75d9bf5 | 2011-10-28 13:18:32 -0700 | [diff] [blame] | 144 | } |
Ilya Moiseenko | 75d9bf5 | 2011-10-28 13:18:32 -0700 | [diff] [blame] | 145 | |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 146 | uint32_t |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 147 | Interest::GetSerializedSize (void) const |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 148 | { |
Alexander Afanasyev | e6bf47e | 2013-06-24 11:51:52 -0700 | [diff] [blame] | 149 | 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 Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 155 | NS_LOG_INFO ("Serialize size = " << size); |
| 156 | |
| 157 | return size; |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 161 | Interest::Serialize (Buffer::Iterator start) const |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 162 | { |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 163 | start.WriteU8 (0x80); // version |
| 164 | start.WriteU8 (0x00); // packet type |
| 165 | |
Alexander Afanasyev | e6bf47e | 2013-06-24 11:51:52 -0700 | [diff] [blame] | 166 | start.WriteU16 (GetSerializedSize () - 4); |
| 167 | |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 168 | 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 Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | uint32_t |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 186 | Interest::Deserialize (Buffer::Iterator start) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 187 | { |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 188 | Buffer::Iterator i = start; |
| 189 | |
| 190 | if (i.ReadU8 () != 0x80) |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 191 | throw new InterestException (); |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 192 | |
| 193 | if (i.ReadU8 () != 0x00) |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 194 | throw new InterestException (); |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 195 | |
Alexander Afanasyev | e6bf47e | 2013-06-24 11:51:52 -0700 | [diff] [blame] | 196 | i.ReadU16 (); // length, don't need it right now |
| 197 | |
Alexander Afanasyev | bd9c18e | 2012-11-19 15:23:41 -0800 | [diff] [blame] | 198 | m_nonce = i.ReadU32 (); |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 199 | m_scope = i.ReadU8 (); |
| 200 | m_nackType = i.ReadU8 (); |
Alexander Afanasyev | bd9c18e | 2012-11-19 15:23:41 -0800 | [diff] [blame] | 201 | |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 202 | m_interestLifetime = Seconds (i.ReadU16 ()); |
| 203 | |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 204 | m_name = Create<Name> (); |
Alexander Afanasyev | bd9c18e | 2012-11-19 15:23:41 -0800 | [diff] [blame] | 205 | uint32_t offset = m_name->Deserialize (i); |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 206 | 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 Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | TypeId |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 217 | Interest::GetInstanceTypeId (void) const |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 218 | { |
| 219 | return GetTypeId (); |
| 220 | } |
| 221 | |
| 222 | void |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 223 | Interest::Print (std::ostream &os) const |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 224 | { |
Alexander Afanasyev | 7f3e49e | 2012-04-30 00:17:07 -0700 | [diff] [blame] | 225 | os << "I: " << GetName (); |
| 226 | |
| 227 | return; |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 228 | os << "<Interest>\n <Name>" << GetName () << "</Name>\n"; |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 229 | 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 Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 246 | os << " <Scope>" << GetScope () << "</Scope>\n"; |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 247 | if ( !GetInterestLifetime ().IsZero() ) |
| 248 | os << " <InterestLifetime>" << GetInterestLifetime () << "</InterestLifetime>\n"; |
| 249 | if (GetNonce ()>0) |
| 250 | os << " <Nonce>" << GetNonce () << "</Nonce>\n"; |
| 251 | os << "</Interest>"; |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 254 | } // namespace ndn |
| 255 | } // namespace ns3 |
| 256 | |