blob: 052cd4a6b1eedf500a17b91527e1d2520abc9f2a [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 Afanasyeve6bf47e2013-06-24 11:51:52 -0700117 uint32_t size = 1 + 1 + 2 +
118 ((2 + 2) + (m_name->GetSerializedSize ()) + (2 + 2 + 4 + 2 + 2 + (2 + 0)));
Alexander Afanasyevd6e5c5f2013-03-30 19:09:56 -0700119 if (m_signature != 0)
120 size += 4;
121
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800122 NS_LOG_INFO ("Serialize size = " << size);
123 return size;
124}
Alexander Afanasyev9568f952012-04-05 16:09:14 -0700125
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700126void
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700127ContentObject::Serialize (Buffer::Iterator start) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700128{
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800129 start.WriteU8 (0x80); // version
130 start.WriteU8 (0x01); // packet type
Alexander Afanasyeve6bf47e2013-06-24 11:51:52 -0700131 start.WriteU16 (GetSerializedSize () - 4); // length
132
Alexander Afanasyevd6e5c5f2013-03-30 19:09:56 -0700133 if (m_signature != 0)
134 {
135 start.WriteU16 (6); // signature length
136 start.WriteU16 (0xFF00); // "fake" simulator signature
137 start.WriteU32 (m_signature);
138 }
139 else
140 {
141 start.WriteU16 (2); // signature length
142 start.WriteU16 (0); // empty signature
143 }
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700144
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800145 // name
146 uint32_t offset = m_name->Serialize (start);
Alexander Afanasyev11fdadd2012-11-19 15:43:17 -0800147 NS_LOG_DEBUG ("Offset: " << offset);
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800148 start.Next (offset);
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700149
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800150 // content
151 // for now assume that contentdata length is zero
152 start.WriteU16 (2 + 4 + 2 + 2 + (2 + 0));
153 start.WriteU16 (4 + 2 + 2 + (2 + 0));
154 start.WriteU32 (static_cast<uint32_t> (m_timestamp.ToInteger (Time::S)));
155 start.WriteU16 (static_cast<uint16_t> (m_freshness.ToInteger (Time::S)));
156 start.WriteU16 (0); // reserved
157 start.WriteU16 (0); // Length (ContentInfoOptions)
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700158
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800159 // that's it folks
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700160}
161
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700162
163uint32_t
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700164ContentObject::Deserialize (Buffer::Iterator start)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700165{
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700166 Buffer::Iterator i = start;
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700167
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800168 if (i.ReadU8 () != 0x80)
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700169 throw new ContentObjectException ();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800170
171 if (i.ReadU8 () != 0x01)
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700172 throw new ContentObjectException ();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800173
Alexander Afanasyeve6bf47e2013-06-24 11:51:52 -0700174 i.ReadU16 (); // length
175
Alexander Afanasyevd6e5c5f2013-03-30 19:09:56 -0700176 uint32_t signatureLength = i.ReadU16 ();
177 if (signatureLength == 6)
178 {
179 if (i.ReadU16 () != 0xFF00) // signature type
180 throw new ContentObjectException ();
181 m_signature = i.ReadU32 ();
182 }
183 else if (signatureLength == 2)
184 {
185 if (i.ReadU16 () != 0) // signature type
186 throw new ContentObjectException ();
187 m_signature = 0;
188 }
189 else
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700190 throw new ContentObjectException ();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800191
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700192 m_name = Create<Name> ();
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -0800193 uint32_t offset = m_name->Deserialize (i);
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800194 i.Next (offset);
195
196 if (i.ReadU16 () != (2 + 4 + 2 + 2 + (2 + 0))) // content length
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700197 throw new ContentObjectException ();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800198
199 if (i.ReadU16 () != (4 + 2 + 2 + (2 + 0))) // Length (content Info)
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700200 throw new ContentObjectException ();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800201
202 m_timestamp = Seconds (i.ReadU32 ());
203 m_freshness = Seconds (i.ReadU16 ());
204
205 if (i.ReadU16 () != 0) // Reserved
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700206 throw new ContentObjectException ();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800207 if (i.ReadU16 () != 0) // Length (ContentInfoOptions)
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700208 throw new ContentObjectException ();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800209
Alexander Afanasyev11fdadd2012-11-19 15:43:17 -0800210 NS_ASSERT_MSG (i.GetDistanceFrom (start) == GetSerializedSize (),
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700211 "Something wrong with ContentObject::Deserialize");
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800212
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700213 return i.GetDistanceFrom (start);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700214}
215
216TypeId
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700217ContentObject::GetInstanceTypeId (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700218{
219 return GetTypeId ();
220}
221
222void
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700223ContentObject::Print (std::ostream &os) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700224{
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700225 os << "D: " << GetName ();
226 // os << "<ContentObject><Name>" << GetName () << "</Name><Content>";
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700227}
228
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700229////////////////////////////////////////////////////////////////////////////////////////////////////
230
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700231ContentObjectTail::ContentObjectTail ()
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700232{
233}
234
235TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700236ContentObjectTail::GetTypeId (void)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700237{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700238 static TypeId tid = TypeId ("ns3::ndn::ContentObjectTail")
Alexander Afanasyeve91ab752011-08-31 19:13:40 -0700239 .SetParent<Trailer> ()
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700240 .AddConstructor<ContentObjectTail> ()
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700241 ;
242 return tid;
243}
244
245TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700246ContentObjectTail::GetInstanceTypeId (void) const
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700247{
248 return GetTypeId ();
249}
250
251void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700252ContentObjectTail::Print (std::ostream &os) const
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700253{
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800254 // os << "</Content></ContentObject>";
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700255}
256
257uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700258ContentObjectTail::GetSerializedSize (void) const
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700259{
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800260 return 0;
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700261}
262
263void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700264ContentObjectTail::Serialize (Buffer::Iterator start) const
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700265{
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700266}
267
268uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700269ContentObjectTail::Deserialize (Buffer::Iterator start)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700270{
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800271 return 0;
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700272}
273
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700274} // namespace ndn
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700275} // namespace ns3