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