Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 2 | /* |
| 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 Afanasyev | bd9c18e | 2012-11-19 15:23:41 -0800 | [diff] [blame^] | 22 | #include "ndn-content-object.h" |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 23 | |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 24 | #include "ns3/log.h" |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 25 | |
| 26 | #include <boost/foreach.hpp> |
| 27 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 28 | NS_LOG_COMPONENT_DEFINE ("ndn.ContentObjectHeader"); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 29 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 30 | namespace ns3 { |
| 31 | namespace ndn { |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 32 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 33 | NS_OBJECT_ENSURE_REGISTERED (ContentObjectHeader); |
| 34 | NS_OBJECT_ENSURE_REGISTERED (ContentObjectTail); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 35 | |
| 36 | TypeId |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 37 | ContentObjectHeader::GetTypeId (void) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 38 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 39 | static TypeId tid = TypeId ("ns3::ndn::ContentObjectHeader") |
| 40 | .SetGroupName ("Ndn") |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 41 | .SetParent<Header> () |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 42 | .AddConstructor<ContentObjectHeader> () |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 43 | ; |
| 44 | return tid; |
| 45 | } |
| 46 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 47 | ContentObjectHeader::ContentObjectHeader () |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 48 | { |
| 49 | } |
| 50 | |
| 51 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 52 | ContentObjectHeader::SetName (const Ptr<NameComponents> &name) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 53 | { |
| 54 | m_name = name; |
| 55 | } |
| 56 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 57 | const NameComponents& |
| 58 | ContentObjectHeader::GetName () const |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 59 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 60 | if (m_name==0) throw ContentObjectHeaderException(); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 61 | return *m_name; |
| 62 | } |
| 63 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 64 | Ptr<const NameComponents> |
| 65 | ContentObjectHeader::GetNamePtr () const |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 66 | { |
| 67 | return m_name; |
| 68 | } |
| 69 | |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 70 | uint32_t |
| 71 | ContentObjectHeader::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 Afanasyev | 9568f95 | 2012-04-05 16:09:14 -0700 | [diff] [blame] | 77 | |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 78 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 79 | ContentObjectHeader::Serialize (Buffer::Iterator start) const |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 80 | { |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 81 | start.WriteU8 (0x80); // version |
| 82 | start.WriteU8 (0x01); // packet type |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 83 | |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 84 | start.WriteU16 (2); // signature length |
| 85 | start.WriteU16 (0); // empty signature |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 86 | |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 87 | // name |
| 88 | uint32_t offset = m_name->Serialize (start); |
| 89 | start.Next (offset); |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 90 | |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 91 | // 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 Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 99 | |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 100 | // that's it folks |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 103 | |
| 104 | uint32_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 105 | ContentObjectHeader::Deserialize (Buffer::Iterator start) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 106 | { |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 107 | Buffer::Iterator i = start; |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 108 | |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 109 | 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 Afanasyev | bd9c18e | 2012-11-19 15:23:41 -0800 | [diff] [blame^] | 122 | uint32_t offset = m_name->Deserialize (i); |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 123 | 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 Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 142 | return i.GetDistanceFrom (start); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | TypeId |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 146 | ContentObjectHeader::GetInstanceTypeId (void) const |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 147 | { |
| 148 | return GetTypeId (); |
| 149 | } |
| 150 | |
| 151 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 152 | ContentObjectHeader::Print (std::ostream &os) const |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 153 | { |
Alexander Afanasyev | 7f3e49e | 2012-04-30 00:17:07 -0700 | [diff] [blame] | 154 | os << "D: " << GetName (); |
| 155 | // os << "<ContentObject><Name>" << GetName () << "</Name><Content>"; |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 158 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 159 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 160 | ContentObjectTail::ContentObjectTail () |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 161 | { |
| 162 | } |
| 163 | |
| 164 | TypeId |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 165 | ContentObjectTail::GetTypeId (void) |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 166 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 167 | static TypeId tid = TypeId ("ns3::ndn::ContentObjectTail") |
Alexander Afanasyev | e91ab75 | 2011-08-31 19:13:40 -0700 | [diff] [blame] | 168 | .SetParent<Trailer> () |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 169 | .AddConstructor<ContentObjectTail> () |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 170 | ; |
| 171 | return tid; |
| 172 | } |
| 173 | |
| 174 | TypeId |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 175 | ContentObjectTail::GetInstanceTypeId (void) const |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 176 | { |
| 177 | return GetTypeId (); |
| 178 | } |
| 179 | |
| 180 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 181 | ContentObjectTail::Print (std::ostream &os) const |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 182 | { |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 183 | // os << "</Content></ContentObject>"; |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | uint32_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 187 | ContentObjectTail::GetSerializedSize (void) const |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 188 | { |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 189 | return 0; |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 193 | ContentObjectTail::Serialize (Buffer::Iterator start) const |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 194 | { |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | uint32_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 198 | ContentObjectTail::Deserialize (Buffer::Iterator start) |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 199 | { |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 200 | return 0; |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 203 | } // namespace ndn |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 204 | } // namespace ns3 |