NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
ndnlp-partial-message-store.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
27 #include "core/logger.hpp"
28 
29 namespace nfd {
30 namespace ndnlp {
31 
32 NFD_LOG_INIT("NdnlpPartialMessageStore");
33 
35  : m_fragCount(0)
36  , m_received(0)
37  , m_totalLength(0)
38 {
39 }
40 
41 bool
42 PartialMessage::add(uint16_t fragIndex, uint16_t fragCount, const Block& payload)
43 {
44  if (m_received == 0) { // first packet
45  m_fragCount = fragCount;
46  m_payloads.resize(fragCount);
47  }
48 
49  if (m_fragCount != fragCount || fragIndex >= m_fragCount) {
50  return false;
51  }
52 
53  if (!m_payloads[fragIndex].empty()) { // duplicate
54  return false;
55  }
56 
57  m_payloads[fragIndex] = payload;
58  ++m_received;
59  m_totalLength += payload.value_size();
60  return true;
61 }
62 
63 bool
65 {
66  return m_received == m_fragCount;
67 }
68 
69 std::tuple<bool, Block>
71 {
72  BOOST_ASSERT(this->isComplete());
73 
74  ndn::BufferPtr buffer = make_shared<ndn::Buffer>(m_totalLength);
75  ndn::Buffer::iterator buf = buffer->begin();
76  for (const Block& payload : m_payloads) {
77  buf = std::copy(payload.value_begin(), payload.value_end(), buf);
78  }
79  BOOST_ASSERT(buf == buffer->end());
80 
81  return Block::fromBuffer(buffer, 0);
82 }
83 
84 std::tuple<bool, Block>
86 {
87  BOOST_ASSERT(fragment.fragCount == 1);
88 
89  try {
90  return std::make_tuple(true, fragment.payload.blockFromValue());
91  }
92  catch (tlv::Error&) {
93  return std::make_tuple(false, Block());
94  }
95 }
96 
97 PartialMessageStore::PartialMessageStore(const time::nanoseconds& idleDuration)
98  : m_idleDuration(idleDuration)
99 {
100 }
101 
102 void
104 {
105  bool isReassembled = false;
106  Block reassembled;
107 
108  if (pkt.fragCount == 1) { // single fragment
109  std::tie(isReassembled, reassembled) = PartialMessage::reassembleSingle(pkt);
110  }
111  else {
112  uint64_t messageIdentifier = pkt.seq - pkt.fragIndex;
113  PartialMessage& pm = m_partialMessages[messageIdentifier];
114  this->scheduleCleanup(messageIdentifier, pm);
115 
116  pm.add(pkt.fragIndex, pkt.fragCount, pkt.payload);
117 
118  if (pm.isComplete()) {
119  std::tie(isReassembled, reassembled) = pm.reassemble();
120  m_partialMessages.erase(messageIdentifier);
121  }
122  else {
123  return;
124  }
125  }
126 
127  if (!isReassembled) {
128  NFD_LOG_TRACE(pkt.seq << " reassemble error");
129  return;
130  }
131 
132  NFD_LOG_TRACE(pkt.seq << " deliver");
133  this->onReceive(reassembled);
134 }
135 
136 void
137 PartialMessageStore::scheduleCleanup(uint64_t messageIdentifier,
138  PartialMessage& partialMessage)
139 {
140  partialMessage.expiry = scheduler::schedule(m_idleDuration,
141  bind(&PartialMessageStore::cleanup, this, messageIdentifier));
142 }
143 
144 void
145 PartialMessageStore::cleanup(uint64_t messageIdentifier)
146 {
147  NFD_LOG_TRACE(messageIdentifier << " cleanup");
148  m_partialMessages.erase(messageIdentifier);
149 }
150 
151 } // namespace ndnlp
152 } // namespace nfd
static std::tuple< bool, Block > fromBuffer(ConstBufferPtr buffer, size_t offset)
Try to construct block from Buffer.
Definition: block.cpp:253
bool add(uint16_t fragIndex, uint16_t fragCount, const Block &payload)
shared_ptr< Buffer > BufferPtr
Definition: buffer.hpp:35
static std::tuple< bool, Block > reassembleSingle(const NdnlpData &fragment)
reassemble network layer packet from a single fragment
represents a NdnlpData packet
Definition: ndnlp-data.hpp:43
signal::Signal< PartialMessageStore, Block > onReceive
fires when network layer packet is received
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
represents a partially received message
Table::const_iterator iterator
Definition: cs-internal.hpp:41
Block blockFromValue() const
Definition: block.cpp:437
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:38
EventId schedule(const time::nanoseconds &after, const std::function< void()> &event)
schedule an event
Definition: scheduler.cpp:50
size_t value_size() const
Definition: block.cpp:529
std::tuple< bool, Block > reassemble()
reassemble network layer packet
PartialMessageStore(const time::nanoseconds &idleDuration=time::milliseconds(100))
#define NFD_LOG_INIT(name)
Definition: logger.hpp:33
void receive(const NdnlpData &pkt)
receive a NdnlpData packet
#define NFD_LOG_TRACE(expression)
Definition: logger.hpp:35
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50