Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -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 | /* |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 3 | * Copyright (c) 2011-2013 University of California, Los Angeles |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 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 | |
Alexander Afanasyev | bd9c18e | 2012-11-19 15:23:41 -0800 | [diff] [blame] | 22 | #include "ndn-content-object.h" |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 23 | |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 24 | #include "ns3/log.h" |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 25 | |
| 26 | #include <boost/foreach.hpp> |
| 27 | |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 28 | NS_LOG_COMPONENT_DEFINE ("ndn.ContentObject"); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 29 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 30 | namespace ns3 { |
| 31 | namespace ndn { |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 32 | |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 33 | NS_OBJECT_ENSURE_REGISTERED (ContentObject); |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 34 | NS_OBJECT_ENSURE_REGISTERED (ContentObjectTail); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 35 | |
| 36 | TypeId |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 37 | ContentObject::GetTypeId (void) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 38 | { |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 39 | static TypeId tid = TypeId ("ns3::ndn::ContentObject") |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 40 | .SetGroupName ("Ndn") |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 41 | .SetParent<Header> () |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 42 | .AddConstructor<ContentObject> () |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 43 | ; |
| 44 | return tid; |
| 45 | } |
| 46 | |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 47 | ContentObject::ContentObject () |
Alexander Afanasyev | d6e5c5f | 2013-03-30 19:09:56 -0700 | [diff] [blame] | 48 | : m_signature (0) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 49 | { |
| 50 | } |
| 51 | |
| 52 | void |
Alexander Afanasyev | cc50d98 | 2013-03-30 19:09:10 -0700 | [diff] [blame] | 53 | ContentObject::SetName (Ptr<Name> name) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 54 | { |
| 55 | m_name = name; |
| 56 | } |
| 57 | |
Alexander Afanasyev | cc50d98 | 2013-03-30 19:09:10 -0700 | [diff] [blame] | 58 | void |
| 59 | ContentObject::SetName (const Name &name) |
| 60 | { |
| 61 | m_name = Create<Name> (name); |
| 62 | } |
| 63 | |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 64 | const Name& |
| 65 | ContentObject::GetName () const |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 66 | { |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 67 | if (m_name==0) throw ContentObjectException(); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 68 | return *m_name; |
| 69 | } |
| 70 | |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 71 | Ptr<const Name> |
| 72 | ContentObject::GetNamePtr () const |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 73 | { |
| 74 | return m_name; |
| 75 | } |
| 76 | |
Alexander Afanasyev | 11fdadd | 2012-11-19 15:43:17 -0800 | [diff] [blame] | 77 | |
| 78 | void |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 79 | ContentObject::SetTimestamp (const Time ×tamp) |
Alexander Afanasyev | 11fdadd | 2012-11-19 15:43:17 -0800 | [diff] [blame] | 80 | { |
| 81 | m_timestamp = timestamp; |
| 82 | } |
| 83 | |
| 84 | Time |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 85 | ContentObject::GetTimestamp () const |
Alexander Afanasyev | 11fdadd | 2012-11-19 15:43:17 -0800 | [diff] [blame] | 86 | { |
| 87 | return m_timestamp; |
| 88 | } |
| 89 | |
| 90 | void |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 91 | ContentObject::SetFreshness (const Time &freshness) |
Alexander Afanasyev | 11fdadd | 2012-11-19 15:43:17 -0800 | [diff] [blame] | 92 | { |
| 93 | m_freshness = freshness; |
| 94 | } |
| 95 | |
| 96 | Time |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 97 | ContentObject::GetFreshness () const |
Alexander Afanasyev | 11fdadd | 2012-11-19 15:43:17 -0800 | [diff] [blame] | 98 | { |
| 99 | return m_freshness; |
| 100 | } |
| 101 | |
Alexander Afanasyev | d6e5c5f | 2013-03-30 19:09:56 -0700 | [diff] [blame] | 102 | void |
| 103 | ContentObject::SetSignature (uint32_t signature) |
| 104 | { |
| 105 | m_signature = signature; |
| 106 | } |
| 107 | |
| 108 | uint32_t |
| 109 | ContentObject::GetSignature () const |
| 110 | { |
| 111 | return m_signature; |
| 112 | } |
Alexander Afanasyev | 11fdadd | 2012-11-19 15:43:17 -0800 | [diff] [blame] | 113 | |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 114 | uint32_t |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 115 | ContentObject::GetSerializedSize () const |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 116 | { |
Alexander Afanasyev | 11fdadd | 2012-11-19 15:43:17 -0800 | [diff] [blame] | 117 | uint32_t size = 2 + ((2 + 2) + (m_name->GetSerializedSize ()) + (2 + 2 + 4 + 2 + 2 + (2 + 0))); |
Alexander Afanasyev | d6e5c5f | 2013-03-30 19:09:56 -0700 | [diff] [blame] | 118 | if (m_signature != 0) |
| 119 | size += 4; |
| 120 | |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 121 | NS_LOG_INFO ("Serialize size = " << size); |
| 122 | return size; |
| 123 | } |
Alexander Afanasyev | 9568f95 | 2012-04-05 16:09:14 -0700 | [diff] [blame] | 124 | |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 125 | void |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 126 | ContentObject::Serialize (Buffer::Iterator start) const |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 127 | { |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 128 | start.WriteU8 (0x80); // version |
| 129 | start.WriteU8 (0x01); // packet type |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 130 | |
Alexander Afanasyev | d6e5c5f | 2013-03-30 19:09:56 -0700 | [diff] [blame] | 131 | if (m_signature != 0) |
| 132 | { |
| 133 | start.WriteU16 (6); // signature length |
| 134 | start.WriteU16 (0xFF00); // "fake" simulator signature |
| 135 | start.WriteU32 (m_signature); |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | start.WriteU16 (2); // signature length |
| 140 | start.WriteU16 (0); // empty signature |
| 141 | } |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 142 | |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 143 | // name |
| 144 | uint32_t offset = m_name->Serialize (start); |
Alexander Afanasyev | 11fdadd | 2012-11-19 15:43:17 -0800 | [diff] [blame] | 145 | NS_LOG_DEBUG ("Offset: " << offset); |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 146 | start.Next (offset); |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 147 | |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 148 | // content |
| 149 | // for now assume that contentdata length is zero |
| 150 | start.WriteU16 (2 + 4 + 2 + 2 + (2 + 0)); |
| 151 | start.WriteU16 (4 + 2 + 2 + (2 + 0)); |
| 152 | start.WriteU32 (static_cast<uint32_t> (m_timestamp.ToInteger (Time::S))); |
| 153 | start.WriteU16 (static_cast<uint16_t> (m_freshness.ToInteger (Time::S))); |
| 154 | start.WriteU16 (0); // reserved |
| 155 | start.WriteU16 (0); // Length (ContentInfoOptions) |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 156 | |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 157 | // that's it folks |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 160 | |
| 161 | uint32_t |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 162 | ContentObject::Deserialize (Buffer::Iterator start) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 163 | { |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 164 | Buffer::Iterator i = start; |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 165 | |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 166 | if (i.ReadU8 () != 0x80) |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 167 | throw new ContentObjectException (); |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 168 | |
| 169 | if (i.ReadU8 () != 0x01) |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 170 | throw new ContentObjectException (); |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 171 | |
Alexander Afanasyev | d6e5c5f | 2013-03-30 19:09:56 -0700 | [diff] [blame] | 172 | uint32_t signatureLength = i.ReadU16 (); |
| 173 | if (signatureLength == 6) |
| 174 | { |
| 175 | if (i.ReadU16 () != 0xFF00) // signature type |
| 176 | throw new ContentObjectException (); |
| 177 | m_signature = i.ReadU32 (); |
| 178 | } |
| 179 | else if (signatureLength == 2) |
| 180 | { |
| 181 | if (i.ReadU16 () != 0) // signature type |
| 182 | throw new ContentObjectException (); |
| 183 | m_signature = 0; |
| 184 | } |
| 185 | else |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 186 | throw new ContentObjectException (); |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 187 | |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 188 | m_name = Create<Name> (); |
Alexander Afanasyev | bd9c18e | 2012-11-19 15:23:41 -0800 | [diff] [blame] | 189 | uint32_t offset = m_name->Deserialize (i); |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 190 | i.Next (offset); |
| 191 | |
| 192 | if (i.ReadU16 () != (2 + 4 + 2 + 2 + (2 + 0))) // content length |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 193 | throw new ContentObjectException (); |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 194 | |
| 195 | if (i.ReadU16 () != (4 + 2 + 2 + (2 + 0))) // Length (content Info) |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 196 | throw new ContentObjectException (); |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 197 | |
| 198 | m_timestamp = Seconds (i.ReadU32 ()); |
| 199 | m_freshness = Seconds (i.ReadU16 ()); |
| 200 | |
| 201 | if (i.ReadU16 () != 0) // Reserved |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 202 | throw new ContentObjectException (); |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 203 | if (i.ReadU16 () != 0) // Length (ContentInfoOptions) |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 204 | throw new ContentObjectException (); |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 205 | |
Alexander Afanasyev | 11fdadd | 2012-11-19 15:43:17 -0800 | [diff] [blame] | 206 | NS_ASSERT_MSG (i.GetDistanceFrom (start) == GetSerializedSize (), |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 207 | "Something wrong with ContentObject::Deserialize"); |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 208 | |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 209 | return i.GetDistanceFrom (start); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | TypeId |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 213 | ContentObject::GetInstanceTypeId (void) const |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 214 | { |
| 215 | return GetTypeId (); |
| 216 | } |
| 217 | |
| 218 | void |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 219 | ContentObject::Print (std::ostream &os) const |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 220 | { |
Alexander Afanasyev | 7f3e49e | 2012-04-30 00:17:07 -0700 | [diff] [blame] | 221 | os << "D: " << GetName (); |
| 222 | // os << "<ContentObject><Name>" << GetName () << "</Name><Content>"; |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 225 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 226 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 227 | ContentObjectTail::ContentObjectTail () |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 228 | { |
| 229 | } |
| 230 | |
| 231 | TypeId |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 232 | ContentObjectTail::GetTypeId (void) |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 233 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 234 | static TypeId tid = TypeId ("ns3::ndn::ContentObjectTail") |
Alexander Afanasyev | e91ab75 | 2011-08-31 19:13:40 -0700 | [diff] [blame] | 235 | .SetParent<Trailer> () |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 236 | .AddConstructor<ContentObjectTail> () |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 237 | ; |
| 238 | return tid; |
| 239 | } |
| 240 | |
| 241 | TypeId |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 242 | ContentObjectTail::GetInstanceTypeId (void) const |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 243 | { |
| 244 | return GetTypeId (); |
| 245 | } |
| 246 | |
| 247 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 248 | ContentObjectTail::Print (std::ostream &os) const |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 249 | { |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 250 | // os << "</Content></ContentObject>"; |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | uint32_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 254 | ContentObjectTail::GetSerializedSize (void) const |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 255 | { |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 256 | return 0; |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 260 | ContentObjectTail::Serialize (Buffer::Iterator start) const |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 261 | { |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | uint32_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 265 | ContentObjectTail::Deserialize (Buffer::Iterator start) |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 266 | { |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 267 | return 0; |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 270 | } // namespace ndn |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 271 | } // namespace ns3 |