blob: e03387c0767c9efa346de1c15fd8a4ea28cd8019 [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/*
3 * Copyright (c) 2011 University of California, Los Angeles
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 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 Afanasyev2b4c9472012-08-09 15:00:38 -070028NS_LOG_COMPONENT_DEFINE ("ndn.ContentObjectHeader");
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 Afanasyev2b4c9472012-08-09 15:00:38 -070033NS_OBJECT_ENSURE_REGISTERED (ContentObjectHeader);
34NS_OBJECT_ENSURE_REGISTERED (ContentObjectTail);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070035
36TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070037ContentObjectHeader::GetTypeId (void)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070038{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070039 static TypeId tid = TypeId ("ns3::ndn::ContentObjectHeader")
40 .SetGroupName ("Ndn")
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070041 .SetParent<Header> ()
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070042 .AddConstructor<ContentObjectHeader> ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070043 ;
44 return tid;
45}
46
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070047ContentObjectHeader::ContentObjectHeader ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070048{
49}
50
51void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070052ContentObjectHeader::SetName (const Ptr<NameComponents> &name)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070053{
54 m_name = name;
55}
56
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070057const NameComponents&
58ContentObjectHeader::GetName () const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070059{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070060 if (m_name==0) throw ContentObjectHeaderException();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070061 return *m_name;
62}
63
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070064Ptr<const NameComponents>
65ContentObjectHeader::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
72ContentObjectHeader::SetTimestamp (const Time &timestamp)
73{
74 m_timestamp = timestamp;
75}
76
77Time
78ContentObjectHeader::GetTimestamp () const
79{
80 return m_timestamp;
81}
82
83void
84ContentObjectHeader::SetFreshness (const Time &freshness)
85{
86 m_freshness = freshness;
87}
88
89Time
90ContentObjectHeader::GetFreshness () const
91{
92 return m_freshness;
93}
94
95
Alexander Afanasyev5d79e682012-11-19 14:12:23 -080096uint32_t
97ContentObjectHeader::GetSerializedSize () const
98{
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 Afanasyev2b4c9472012-08-09 15:00:38 -0700105ContentObjectHeader::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 Afanasyev2b4c9472012-08-09 15:00:38 -0700132ContentObjectHeader::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)
137 throw new ContentObjectHeaderException ();
138
139 if (i.ReadU8 () != 0x01)
140 throw new ContentObjectHeaderException ();
141
142 if (i.ReadU16 () != 2) // signature length
143 throw new ContentObjectHeaderException ();
144
145 if (i.ReadU16 () != 0) // signature type
146 throw new ContentObjectHeaderException ();
147
148 m_name = Create<NameComponents> ();
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
153 throw new ContentObjectHeaderException ();
154
155 if (i.ReadU16 () != (4 + 2 + 2 + (2 + 0))) // Length (content Info)
156 throw new ContentObjectHeaderException ();
157
158 m_timestamp = Seconds (i.ReadU32 ());
159 m_freshness = Seconds (i.ReadU16 ());
160
161 if (i.ReadU16 () != 0) // Reserved
162 throw new ContentObjectHeaderException ();
163 if (i.ReadU16 () != 0) // Length (ContentInfoOptions)
164 throw new ContentObjectHeaderException ();
165
Alexander Afanasyev11fdadd2012-11-19 15:43:17 -0800166 NS_ASSERT_MSG (i.GetDistanceFrom (start) == GetSerializedSize (),
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800167 "Something wrong with ContentObjectHeader::Deserialize");
168
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700169 return i.GetDistanceFrom (start);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700170}
171
172TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700173ContentObjectHeader::GetInstanceTypeId (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700174{
175 return GetTypeId ();
176}
177
178void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700179ContentObjectHeader::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