blob: 91d890d42dae1e32114f3fcc704e1fede1d61854 [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 Afanasyev08d984e2011-08-13 19:20:22 -070048{
49}
50
51void
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070052ContentObject::SetName (const Ptr<Name> &name)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070053{
54 m_name = name;
55}
56
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070057const Name&
58ContentObject::GetName () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070059{
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070060 if (m_name==0) throw ContentObjectException();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070061 return *m_name;
62}
63
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070064Ptr<const Name>
65ContentObject::GetNamePtr () const
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070066{
67 return m_name;
68}
69
Alexander Afanasyev11fdadd2012-11-19 15:43:17 -080070
71void
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070072ContentObject::SetTimestamp (const Time &timestamp)
Alexander Afanasyev11fdadd2012-11-19 15:43:17 -080073{
74 m_timestamp = timestamp;
75}
76
77Time
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070078ContentObject::GetTimestamp () const
Alexander Afanasyev11fdadd2012-11-19 15:43:17 -080079{
80 return m_timestamp;
81}
82
83void
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070084ContentObject::SetFreshness (const Time &freshness)
Alexander Afanasyev11fdadd2012-11-19 15:43:17 -080085{
86 m_freshness = freshness;
87}
88
89Time
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070090ContentObject::GetFreshness () const
Alexander Afanasyev11fdadd2012-11-19 15:43:17 -080091{
92 return m_freshness;
93}
94
95
Alexander Afanasyev5d79e682012-11-19 14:12:23 -080096uint32_t
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070097ContentObject::GetSerializedSize () const
Alexander Afanasyev5d79e682012-11-19 14:12:23 -080098{
Alexander Afanasyev11fdadd2012-11-19 15:43:17 -080099 uint32_t size = 2 + ((2 + 2) + (m_name->GetSerializedSize ()) + (2 + 2 + 4 + 2 + 2 + (2 + 0)));
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800100 NS_LOG_INFO ("Serialize size = " << size);
101 return size;
102}
Alexander Afanasyev9568f952012-04-05 16:09:14 -0700103
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700104void
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700105ContentObject::Serialize (Buffer::Iterator start) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700106{
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800107 start.WriteU8 (0x80); // version
108 start.WriteU8 (0x01); // packet type
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700109
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800110 start.WriteU16 (2); // signature length
111 start.WriteU16 (0); // empty signature
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700112
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800113 // name
114 uint32_t offset = m_name->Serialize (start);
Alexander Afanasyev11fdadd2012-11-19 15:43:17 -0800115 NS_LOG_DEBUG ("Offset: " << offset);
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800116 start.Next (offset);
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700117
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800118 // 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 Afanasyev8c5046a2012-06-05 16:22:14 -0700126
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800127 // that's it folks
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700128}
129
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700130
131uint32_t
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700132ContentObject::Deserialize (Buffer::Iterator start)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700133{
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700134 Buffer::Iterator i = start;
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700135
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800136 if (i.ReadU8 () != 0x80)
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700137 throw new ContentObjectException ();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800138
139 if (i.ReadU8 () != 0x01)
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700140 throw new ContentObjectException ();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800141
142 if (i.ReadU16 () != 2) // signature length
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700143 throw new ContentObjectException ();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800144
145 if (i.ReadU16 () != 0) // signature type
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700146 throw new ContentObjectException ();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800147
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700148 m_name = Create<Name> ();
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -0800149 uint32_t offset = m_name->Deserialize (i);
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800150 i.Next (offset);
151
152 if (i.ReadU16 () != (2 + 4 + 2 + 2 + (2 + 0))) // content length
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700153 throw new ContentObjectException ();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800154
155 if (i.ReadU16 () != (4 + 2 + 2 + (2 + 0))) // Length (content Info)
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700156 throw new ContentObjectException ();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800157
158 m_timestamp = Seconds (i.ReadU32 ());
159 m_freshness = Seconds (i.ReadU16 ());
160
161 if (i.ReadU16 () != 0) // Reserved
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700162 throw new ContentObjectException ();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800163 if (i.ReadU16 () != 0) // Length (ContentInfoOptions)
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700164 throw new ContentObjectException ();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800165
Alexander Afanasyev11fdadd2012-11-19 15:43:17 -0800166 NS_ASSERT_MSG (i.GetDistanceFrom (start) == GetSerializedSize (),
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700167 "Something wrong with ContentObject::Deserialize");
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800168
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700169 return i.GetDistanceFrom (start);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700170}
171
172TypeId
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700173ContentObject::GetInstanceTypeId (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700174{
175 return GetTypeId ();
176}
177
178void
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700179ContentObject::Print (std::ostream &os) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700180{
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700181 os << "D: " << GetName ();
182 // os << "<ContentObject><Name>" << GetName () << "</Name><Content>";
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700183}
184
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700185////////////////////////////////////////////////////////////////////////////////////////////////////
186
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700187ContentObjectTail::ContentObjectTail ()
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700188{
189}
190
191TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700192ContentObjectTail::GetTypeId (void)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700193{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700194 static TypeId tid = TypeId ("ns3::ndn::ContentObjectTail")
Alexander Afanasyeve91ab752011-08-31 19:13:40 -0700195 .SetParent<Trailer> ()
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700196 .AddConstructor<ContentObjectTail> ()
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700197 ;
198 return tid;
199}
200
201TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700202ContentObjectTail::GetInstanceTypeId (void) const
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700203{
204 return GetTypeId ();
205}
206
207void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700208ContentObjectTail::Print (std::ostream &os) const
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700209{
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800210 // os << "</Content></ContentObject>";
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700211}
212
213uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700214ContentObjectTail::GetSerializedSize (void) const
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700215{
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800216 return 0;
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700217}
218
219void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700220ContentObjectTail::Serialize (Buffer::Iterator start) const
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700221{
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700222}
223
224uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700225ContentObjectTail::Deserialize (Buffer::Iterator start)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700226{
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800227 return 0;
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700228}
229
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700230} // namespace ndn
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700231} // namespace ns3