24 #include "wire-ccnb.h"
28 #include "ccnb-parser/common.h"
29 #include "ccnb-parser/visitors/void-depth-first-visitor.h"
30 #include "ccnb-parser/visitors/name-visitor.h"
31 #include "ccnb-parser/visitors/non-negative-integer-visitor.h"
32 #include "ccnb-parser/visitors/timestamp-visitor.h"
33 #include "ccnb-parser/visitors/string-visitor.h"
34 #include "ccnb-parser/visitors/uint32t-blob-visitor.h"
35 #include "ccnb-parser/visitors/content-type-visitor.h"
37 #include "ccnb-parser/syntax-tree/block.h"
38 #include "ccnb-parser/syntax-tree/dtag.h"
40 #include <boost/foreach.hpp>
42 NS_LOG_COMPONENT_DEFINE (
"ndn.wire.Ccnb.Data");
58 static TypeId GetTypeId ()
60 static TypeId tid = TypeId (
"ns3::ndn::Data::Ccnb::Closer")
62 .SetParent<Trailer> ()
63 .AddConstructor<DataTrailer> ()
68 virtual TypeId GetInstanceTypeId (
void)
const
73 virtual void Print (std::ostream &os)
const
77 virtual uint32_t GetSerializedSize (
void)
const
82 virtual void Serialize (Buffer::Iterator end)
const
84 Buffer::Iterator i = end;
91 virtual uint32_t Deserialize (Buffer::Iterator end)
93 Buffer::Iterator i = end;
96 uint8_t closing_tag_content = i.ReadU8 ();
97 NS_ASSERT_MSG (closing_tag_content==0,
"Should be a closing tag </Content> (0x00)");
99 uint8_t closing_tag_content_object = i.ReadU8 ();
100 NS_ASSERT_MSG (closing_tag_content_object==0,
"Should be a closing tag </Data> (0x00)");
106 NS_OBJECT_ENSURE_REGISTERED (
Data);
110 Data::GetTypeId (
void)
112 static TypeId tid = TypeId (
"ns3::ndn::Data::Ccnb")
113 .SetGroupName (
"Ndn")
114 .SetParent<Header> ()
115 .AddConstructor<Data> ()
121 Data::GetInstanceTypeId (
void)
const
127 : m_data (Create<ndn::
Data> ())
131 Data::Data (Ptr<ndn::Data> data)
143 Data::ToWire (Ptr<const ndn::Data> data)
145 static DataTrailer trailer;
147 Ptr<const Packet> p = data->GetWire ();
150 Ptr<Packet> packet = Create<Packet> (*data->GetPayload ());
151 Data wireEncoding (ConstCast<ndn::Data> (data));
152 packet->AddHeader (wireEncoding);
153 packet->AddTrailer (trailer);
154 data->SetWire (packet);
163 Data::FromWire (Ptr<Packet> packet)
165 static DataTrailer trailer;
167 Ptr<ndn::Data> data = Create<ndn::Data> ();
168 Ptr<Packet> wire = packet->Copy ();
170 Data wireEncoding (data);
171 packet->RemoveHeader (wireEncoding);
172 packet->RemoveTrailer (trailer);
174 data->SetPayload (packet);
175 data->SetWire (wire);
181 Data::Serialize (Buffer::Iterator start)
const
226 if (m_data->GetFreshness () > Seconds(0))
232 if (m_data->GetKeyLocator () != 0)
251 uint32_t payloadSize = m_data->GetPayload ()->GetSize ();
260 Data::GetSerializedSize ()
const
304 if (m_data->GetFreshness () > Seconds(0))
311 if (m_data->GetKeyLocator () != 0)
330 uint32_t payloadSize = m_data->GetPayload ()->GetSize ();
357 case CcnbParser::CCN_DTAG_Data:
359 BOOST_FOREACH (Ptr<CcnbParser::Block> block, n.
m_nestedTags)
361 block->accept (*
this, param);
364 case CcnbParser::CCN_DTAG_Name:
367 Ptr<Name> name = Create<Name> ();
368 n.
accept (nameVisitor, GetPointer (name));
373 case CcnbParser::CCN_DTAG_Signature:
375 BOOST_FOREACH (Ptr<CcnbParser::Block> block, n.
m_nestedTags)
377 block->accept (*
this, param);
391 case CcnbParser::CCN_DTAG_SignatureBits:
392 NS_LOG_DEBUG (
"SignatureBits");
397 (boost::any_cast<uint32_t> ((*n.
m_nestedTags.begin())->accept
398 (uint32tBlobVisitor)));
401 case CcnbParser::CCN_DTAG_SignedInfo:
403 BOOST_FOREACH (Ptr<CcnbParser::Block> block, n.
m_nestedTags)
405 block->accept (*
this, param);
419 case CcnbParser::CCN_DTAG_Timestamp:
420 NS_LOG_DEBUG (
"Timestamp");
425 (boost::any_cast<Time> ((*n.
m_nestedTags.begin())->accept
426 (timestampVisitor)));
440 case CcnbParser::CCN_DTAG_FreshnessSeconds:
441 NS_LOG_DEBUG (
"FreshnessSeconds");
448 (boost::any_cast<uint32_t> ((*n.
m_nestedTags.begin())->accept
449 (nonNegativeIntegerVisitor))));
452 case CcnbParser::CCN_DTAG_KeyLocator:
454 BOOST_FOREACH (Ptr<CcnbParser::Block> block, n.
m_nestedTags)
456 block->accept (*
this, param);
460 case CcnbParser::CCN_DTAG_KeyName:
466 Ptr<Name> name = Create<Name> ();
467 n.
accept (nameVisitor, GetPointer (name));
472 case CcnbParser::CCN_DTAG_Content:
474 NS_ASSERT_MSG (n.
m_nestedTags.size() == 0,
"Parser should have stopped just after processing <Content> tag");
484 Data::Deserialize (Buffer::Iterator start)
486 static DataVisitor contentObjectVisitor;
488 Buffer::Iterator i = start;
490 root->accept (contentObjectVisitor, GetPointer (m_data));
492 return i.GetDistanceFrom (start);
496 Data::Print (std::ostream &os)
const
498 os <<
"D: " << m_data->GetName ();
Depth-first visitor that takes one argument and returns nothing.
Visitor to obtain nonce value from BLOB block.
static size_t EstimateBlockHeader(size_t value)
Estimate size of the CCNB block header.
static size_t EstimateNumber(uint32_t number)
Estimate size of the number in CCNB encoding.
Routines to serialize/deserialize NDN Data packet in ccnb format.
std::list< Ptr< Block > > m_nestedTags
List of nested tags.
static size_t AppendTaggedBlobWithPadding(Buffer::Iterator &start, uint32_t dtag, uint32_t length, const uint8_t *data, size_t size)
Append a tagged BLOB, adding 0-byte padding if necessary.
static size_t SerializeName(Buffer::Iterator &start, const Name &name)
Append Name in CCNB encoding.
Exception thrown if there is a parsing error.
static size_t EstimateString(uint32_t dtag, const std::string &string)
Estimate size of the string in CCNB encoding.
static size_t EstimateTimestampBlob(const Time &time)
Estimate size of a binary timestamp as a BLOB using CCNB enconding.
virtual void visit(CcnbParser::Dtag &n, boost::any param)
Method accepting DTAG block.
Class to represent DTAG ccnb-encoded node.
static size_t AppendTimestampBlob(Buffer::Iterator &start, const Time &time)
Append a binary timestamp as a BLOB using the ccn binary Timestamp representation (12-bit fraction)...
static size_t AppendBlockHeader(Buffer::Iterator &start, size_t value, uint32_t block_type)
Append CCNB block header.
void SetTimestamp(const Time ×tamp)
Set content object timestamp.
static size_t EstimateTaggedBlob(uint32_t dtag, size_t size)
Estimate size of a tagged BLOB in CCNB enconding.
static size_t AppendNumber(Buffer::Iterator &start, uint32_t number)
Add number in CCNB encoding.
static size_t AppendString(Buffer::Iterator &start, uint32_t dtag, const std::string &string)
Append a tagged string (should be a valid UTF-8 coded string)
void SetKeyLocator(Ptr< Name > keyLocator)
Set key locator.
uint32_t m_dtag
Dictionary code for DTAG.
void SetFreshness(const Time &freshness)
Set freshness of the content object.
Visitor to obtain fill CcnxName object with name components.
Visitor to obtain non-negative integer value from UDATA block.
Visitor to obtain string value from UDATA block.
static size_t AppendCloser(Buffer::Iterator &start)
Append CCNB closer tag (estimated size is 1)
static Ptr< Block > ParseBlock(Buffer::Iterator &start, bool dontParseBlock=false)
Parsing stream (recursively) and creating a parsed BLOCK object.
Visitor to obtain timestamp value from BLOB block.
static size_t SerializedSizeName(const Name &name)
Estimate size of Name in CCNB encoding.
Visitor to obtain nonce value from BLOB block.
void SetName(Ptr< Name > name)
Set content object name.
void SetSignature(uint32_t signature)
Set "fake" signature on the content object.
virtual void accept(VoidNoArguVisitor &v)
Accept visitor void(*)()