blob: 30a19deee9b54316034d7e4df62599517e655c33 [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 Afanasyev5d79e682012-11-19 14:12:23 -080070uint32_t
71ContentObjectHeader::GetSerializedSize () const
72{
73 uint32_t size = 2 + ((2 + 1) + (m_name->GetSerializedSize ()) + (2 + 2 + 4 + 2 + 2 + (2 + 0)));
74 NS_LOG_INFO ("Serialize size = " << size);
75 return size;
76}
Alexander Afanasyev9568f952012-04-05 16:09:14 -070077
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070078void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070079ContentObjectHeader::Serialize (Buffer::Iterator start) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070080{
Alexander Afanasyev5d79e682012-11-19 14:12:23 -080081 start.WriteU8 (0x80); // version
82 start.WriteU8 (0x01); // packet type
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -070083
Alexander Afanasyev5d79e682012-11-19 14:12:23 -080084 start.WriteU16 (2); // signature length
85 start.WriteU16 (0); // empty signature
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -070086
Alexander Afanasyev5d79e682012-11-19 14:12:23 -080087 // name
88 uint32_t offset = m_name->Serialize (start);
89 start.Next (offset);
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -070090
Alexander Afanasyev5d79e682012-11-19 14:12:23 -080091 // content
92 // for now assume that contentdata length is zero
93 start.WriteU16 (2 + 4 + 2 + 2 + (2 + 0));
94 start.WriteU16 (4 + 2 + 2 + (2 + 0));
95 start.WriteU32 (static_cast<uint32_t> (m_timestamp.ToInteger (Time::S)));
96 start.WriteU16 (static_cast<uint16_t> (m_freshness.ToInteger (Time::S)));
97 start.WriteU16 (0); // reserved
98 start.WriteU16 (0); // Length (ContentInfoOptions)
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -070099
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800100 // that's it folks
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700101}
102
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700103
104uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700105ContentObjectHeader::Deserialize (Buffer::Iterator start)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700106{
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700107 Buffer::Iterator i = start;
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700108
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800109 if (i.ReadU8 () != 0x80)
110 throw new ContentObjectHeaderException ();
111
112 if (i.ReadU8 () != 0x01)
113 throw new ContentObjectHeaderException ();
114
115 if (i.ReadU16 () != 2) // signature length
116 throw new ContentObjectHeaderException ();
117
118 if (i.ReadU16 () != 0) // signature type
119 throw new ContentObjectHeaderException ();
120
121 m_name = Create<NameComponents> ();
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -0800122 uint32_t offset = m_name->Deserialize (i);
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800123 i.Next (offset);
124
125 if (i.ReadU16 () != (2 + 4 + 2 + 2 + (2 + 0))) // content length
126 throw new ContentObjectHeaderException ();
127
128 if (i.ReadU16 () != (4 + 2 + 2 + (2 + 0))) // Length (content Info)
129 throw new ContentObjectHeaderException ();
130
131 m_timestamp = Seconds (i.ReadU32 ());
132 m_freshness = Seconds (i.ReadU16 ());
133
134 if (i.ReadU16 () != 0) // Reserved
135 throw new ContentObjectHeaderException ();
136 if (i.ReadU16 () != 0) // Length (ContentInfoOptions)
137 throw new ContentObjectHeaderException ();
138
139 NS_ASSERT_MSG (i.GetDistanceFrom (start) != GetSerializedSize (),
140 "Something wrong with ContentObjectHeader::Deserialize");
141
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700142 return i.GetDistanceFrom (start);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700143}
144
145TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700146ContentObjectHeader::GetInstanceTypeId (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700147{
148 return GetTypeId ();
149}
150
151void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700152ContentObjectHeader::Print (std::ostream &os) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700153{
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700154 os << "D: " << GetName ();
155 // os << "<ContentObject><Name>" << GetName () << "</Name><Content>";
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700156}
157
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700158////////////////////////////////////////////////////////////////////////////////////////////////////
159
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700160ContentObjectTail::ContentObjectTail ()
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700161{
162}
163
164TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700165ContentObjectTail::GetTypeId (void)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700166{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700167 static TypeId tid = TypeId ("ns3::ndn::ContentObjectTail")
Alexander Afanasyeve91ab752011-08-31 19:13:40 -0700168 .SetParent<Trailer> ()
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700169 .AddConstructor<ContentObjectTail> ()
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700170 ;
171 return tid;
172}
173
174TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700175ContentObjectTail::GetInstanceTypeId (void) const
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700176{
177 return GetTypeId ();
178}
179
180void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700181ContentObjectTail::Print (std::ostream &os) const
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700182{
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800183 // os << "</Content></ContentObject>";
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700184}
185
186uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700187ContentObjectTail::GetSerializedSize (void) const
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700188{
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800189 return 0;
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700190}
191
192void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700193ContentObjectTail::Serialize (Buffer::Iterator start) const
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700194{
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700195}
196
197uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700198ContentObjectTail::Deserialize (Buffer::Iterator start)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700199{
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800200 return 0;
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700201}
202
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700203} // namespace ndn
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700204} // namespace ns3