NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
ndnlp-slicer.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "ndnlp-slicer.hpp"
27 
28 #include <ndn-cxx/encoding/encoding-buffer.hpp>
29 
30 namespace nfd {
31 namespace ndnlp {
32 
33 Slicer::Slicer(size_t mtu)
34  : m_mtu(mtu)
35 {
36  this->estimateOverhead();
37 }
38 
40 {
41 }
42 
43 template<bool T>
44 size_t
45 Slicer::encodeFragment(ndn::EncodingImpl<T>& blk,
46  uint64_t seq, uint16_t fragIndex, uint16_t fragCount,
47  const uint8_t* payload, size_t payloadSize)
48 {
49  size_t totalLength = 0;
50 
51  // NdnlpPayload
52  size_t payloadLength = blk.prependByteArray(payload, payloadSize);
53  totalLength += payloadLength;
54  totalLength += blk.prependVarNumber(payloadLength);
55  totalLength += blk.prependVarNumber(tlv::NdnlpPayload);
56 
57  bool needFragIndexAndCount = fragCount > 1;
58  if (needFragIndexAndCount) {
59  // NdnlpFragCount
60  size_t fragCountLength = blk.prependNonNegativeInteger(fragCount);
61  totalLength += fragCountLength;
62  totalLength += blk.prependVarNumber(fragCountLength);
63  totalLength += blk.prependVarNumber(tlv::NdnlpFragCount);
64 
65  // NdnlpFragIndex
66  size_t fragIndexLength = blk.prependNonNegativeInteger(fragIndex);
67  totalLength += fragIndexLength;
68  totalLength += blk.prependVarNumber(fragIndexLength);
69  totalLength += blk.prependVarNumber(tlv::NdnlpFragIndex);
70  }
71 
72  // NdnlpSequence
73  uint64_t sequenceBE = htobe64(seq);
74  size_t sequenceLength = blk.prependByteArray(
75  reinterpret_cast<uint8_t*>(&sequenceBE), sizeof(sequenceBE));
76  totalLength += sequenceLength;
77  totalLength += blk.prependVarNumber(sequenceLength);
78  totalLength += blk.prependVarNumber(tlv::NdnlpSequence);
79 
80  // NdnlpData
81  totalLength += blk.prependVarNumber(totalLength);
82  totalLength += blk.prependVarNumber(tlv::NdnlpData);
83 
84  return totalLength;
85 }
86 
87 void
88 Slicer::estimateOverhead()
89 {
90  // estimate fragment size with all header fields at largest possible size
91  ndn::EncodingEstimator estimator;
92  size_t estimatedSize = this->encodeFragment(estimator,
93  std::numeric_limits<uint64_t>::max(),
94  std::numeric_limits<uint16_t>::max() - 1,
95  std::numeric_limits<uint16_t>::max(),
96  nullptr, m_mtu);
97 
98  size_t overhead = estimatedSize - m_mtu; // minus payload length in estimation
99  m_maxPayload = m_mtu - overhead;
100 }
101 
103 Slicer::slice(const Block& block)
104 {
105  BOOST_ASSERT(block.hasWire());
106  const uint8_t* networkPacket = block.wire();
107  size_t networkPacketSize = block.size();
108 
109  uint16_t fragCount = static_cast<uint16_t>(
110  (networkPacketSize / m_maxPayload) +
111  (networkPacketSize % m_maxPayload == 0 ? 0 : 1)
112  );
113  PacketArray pa = make_shared<std::vector<Block>>();
114  pa->reserve(fragCount);
115  SequenceBlock seqBlock = m_seqgen.nextBlock(fragCount);
116 
117  for (uint16_t fragIndex = 0; fragIndex < fragCount; ++fragIndex) {
118  size_t payloadOffset = fragIndex * m_maxPayload;
119  const uint8_t* payload = networkPacket + payloadOffset;
120  size_t payloadSize = std::min(m_maxPayload, networkPacketSize - payloadOffset);
121 
122  ndn::EncodingBuffer buffer(m_mtu, 0);
123  size_t pktSize = this->encodeFragment(buffer,
124  seqBlock[fragIndex], fragIndex, fragCount, payload, payloadSize);
125 
126  BOOST_VERIFY(pktSize <= m_mtu);
127 
128  pa->push_back(buffer.block());
129  }
130 
131  return pa;
132 }
133 
134 } // namespace ndnlp
135 } // namespace nfd
PacketArray slice(const Block &block)
EncodingImpl< EstimatorTag > EncodingEstimator
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
size_t size() const
Definition: block.cpp:504
EncodingImpl< EncoderTag > EncodingBuffer
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:38
represents a block of sequence numbers
shared_ptr< std::vector< Block > > PacketArray
SequenceBlock nextBlock(size_t count)
generates a block of consecutive sequence numbers
const uint8_t * wire() const
Definition: block.cpp:495
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:471
Slicer(size_t mtu)