blob: d9926ef65536813c8199d05f88c25f714ba5e3f9 [file] [log] [blame]
Alexander Afanasyev4aac5572012-08-09 10:49:55 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyev08d984e2011-08-13 19:20:22 -07002/*
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -07003 * Copyright (c) 2011-2013 University of California, Los Angeles
Alexander Afanasyev08d984e2011-08-13 19:20:22 -07004 *
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 Afanasyevbd9c18e2012-11-19 15:23:41 -080022#include "ndn-content-object.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070023
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070024#include "ns3/log.h"
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -070025
26#include <boost/foreach.hpp>
27
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070028NS_LOG_COMPONENT_DEFINE ("ndn.ContentObject");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070029
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070030namespace ns3 {
31namespace ndn {
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -070032
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070033NS_OBJECT_ENSURE_REGISTERED (ContentObject);
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070034NS_OBJECT_ENSURE_REGISTERED (ContentObjectTail);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070035
36TypeId
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070037ContentObject::GetTypeId (void)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070038{
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070039 static TypeId tid = TypeId ("ns3::ndn::ContentObject")
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070040 .SetGroupName ("Ndn")
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070041 .SetParent<Header> ()
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070042 .AddConstructor<ContentObject> ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070043 ;
44 return tid;
45}
46
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070047ContentObject::ContentObject ()
Alexander Afanasyevd6e5c5f2013-03-30 19:09:56 -070048 : m_signature (0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070049{
50}
51
52void
Alexander Afanasyevcc50d982013-03-30 19:09:10 -070053ContentObject::SetName (Ptr<Name> name)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070054{
55 m_name = name;
56}
57
Alexander Afanasyevcc50d982013-03-30 19:09:10 -070058void
59ContentObject::SetName (const Name &name)
60{
61 m_name = Create<Name> (name);
62}
63
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070064const Name&
65ContentObject::GetName () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070066{
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070067 if (m_name==0) throw ContentObjectException();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070068 return *m_name;
69}
70
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070071Ptr<const Name>
72ContentObject::GetNamePtr () const
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070073{
74 return m_name;
75}
76
Alexander Afanasyev11fdadd2012-11-19 15:43:17 -080077
78void
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070079ContentObject::SetTimestamp (const Time &timestamp)
Alexander Afanasyev11fdadd2012-11-19 15:43:17 -080080{
81 m_timestamp = timestamp;
82}
83
84Time
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070085ContentObject::GetTimestamp () const
Alexander Afanasyev11fdadd2012-11-19 15:43:17 -080086{
87 return m_timestamp;
88}
89
90void
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070091ContentObject::SetFreshness (const Time &freshness)
Alexander Afanasyev11fdadd2012-11-19 15:43:17 -080092{
93 m_freshness = freshness;
94}
95
96Time
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070097ContentObject::GetFreshness () const
Alexander Afanasyev11fdadd2012-11-19 15:43:17 -080098{
99 return m_freshness;
100}
101
Alexander Afanasyevd6e5c5f2013-03-30 19:09:56 -0700102void
103ContentObject::SetSignature (uint32_t signature)
104{
105 m_signature = signature;
106}
107
108uint32_t
109ContentObject::GetSignature () const
110{
111 return m_signature;
112}
Alexander Afanasyev11fdadd2012-11-19 15:43:17 -0800113
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800114uint32_t
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700115ContentObject::GetSerializedSize () const
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800116{
Alexander Afanasyev11fdadd2012-11-19 15:43:17 -0800117 uint32_t size = 2 + ((2 + 2) + (m_name->GetSerializedSize ()) + (2 + 2 + 4 + 2 + 2 + (2 + 0)));
Alexander Afanasyevd6e5c5f2013-03-30 19:09:56 -0700118 if (m_signature != 0)
119 size += 4;
120
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800121 NS_LOG_INFO ("Serialize size = " << size);
122 return size;
123}
Alexander Afanasyev9568f952012-04-05 16:09:14 -0700124
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700125void
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700126ContentObject::Serialize (Buffer::Iterator start) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700127{
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800128 start.WriteU8 (0x80); // version
129 start.WriteU8 (0x01); // packet type
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700130
Alexander Afanasyevd6e5c5f2013-03-30 19:09:56 -0700131 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 Afanasyev8c5046a2012-06-05 16:22:14 -0700142
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800143 // name
144 uint32_t offset = m_name->Serialize (start);
Alexander Afanasyev11fdadd2012-11-19 15:43:17 -0800145 NS_LOG_DEBUG ("Offset: " << offset);
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800146 start.Next (offset);
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700147
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800148 // 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 Afanasyev8c5046a2012-06-05 16:22:14 -0700156
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800157 // that's it folks
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700158}
159
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700160
161uint32_t
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700162ContentObject::Deserialize (Buffer::Iterator start)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700163{
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700164 Buffer::Iterator i = start;
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700165
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800166 if (i.ReadU8 () != 0x80)
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700167 throw new ContentObjectException ();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800168
169 if (i.ReadU8 () != 0x01)
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700170 throw new ContentObjectException ();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800171
Alexander Afanasyevd6e5c5f2013-03-30 19:09:56 -0700172 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 Afanasyevcfdc14f2013-03-15 14:38:44 -0700186 throw new ContentObjectException ();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800187
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700188 m_name = Create<Name> ();
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -0800189 uint32_t offset = m_name->Deserialize (i);
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800190 i.Next (offset);
191
192 if (i.ReadU16 () != (2 + 4 + 2 + 2 + (2 + 0))) // content length
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700193 throw new ContentObjectException ();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800194
195 if (i.ReadU16 () != (4 + 2 + 2 + (2 + 0))) // Length (content Info)
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700196 throw new ContentObjectException ();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800197
198 m_timestamp = Seconds (i.ReadU32 ());
199 m_freshness = Seconds (i.ReadU16 ());
200
201 if (i.ReadU16 () != 0) // Reserved
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700202 throw new ContentObjectException ();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800203 if (i.ReadU16 () != 0) // Length (ContentInfoOptions)
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700204 throw new ContentObjectException ();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800205
Alexander Afanasyev11fdadd2012-11-19 15:43:17 -0800206 NS_ASSERT_MSG (i.GetDistanceFrom (start) == GetSerializedSize (),
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700207 "Something wrong with ContentObject::Deserialize");
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800208
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700209 return i.GetDistanceFrom (start);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700210}
211
212TypeId
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700213ContentObject::GetInstanceTypeId (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700214{
215 return GetTypeId ();
216}
217
218void
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700219ContentObject::Print (std::ostream &os) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700220{
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700221 os << "D: " << GetName ();
222 // os << "<ContentObject><Name>" << GetName () << "</Name><Content>";
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700223}
224
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700225////////////////////////////////////////////////////////////////////////////////////////////////////
226
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700227ContentObjectTail::ContentObjectTail ()
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700228{
229}
230
231TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700232ContentObjectTail::GetTypeId (void)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700233{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700234 static TypeId tid = TypeId ("ns3::ndn::ContentObjectTail")
Alexander Afanasyeve91ab752011-08-31 19:13:40 -0700235 .SetParent<Trailer> ()
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700236 .AddConstructor<ContentObjectTail> ()
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700237 ;
238 return tid;
239}
240
241TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700242ContentObjectTail::GetInstanceTypeId (void) const
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700243{
244 return GetTypeId ();
245}
246
247void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700248ContentObjectTail::Print (std::ostream &os) const
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700249{
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800250 // os << "</Content></ContentObject>";
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700251}
252
253uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700254ContentObjectTail::GetSerializedSize (void) const
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700255{
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800256 return 0;
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700257}
258
259void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700260ContentObjectTail::Serialize (Buffer::Iterator start) const
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700261{
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700262}
263
264uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700265ContentObjectTail::Deserialize (Buffer::Iterator start)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700266{
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800267 return 0;
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700268}
269
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700270} // namespace ndn
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700271} // namespace ns3