NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
transform-base.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2019 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
23 
24 namespace ndn {
25 namespace security {
26 namespace transform {
27 
28 Error::Error(size_t index, const std::string& what)
29  : std::runtime_error("Error in module " + to_string(index) + ": " + what)
30  , m_index(index)
31 {
32 }
33 
35  : m_isEnd(false)
36  , m_index(0)
37 {
38 }
39 
40 size_t
41 Downstream::write(const uint8_t* buf, size_t size)
42 {
43  if (m_isEnd)
44  NDN_THROW(Error(getIndex(), "Module is closed, no more input"));
45 
46  size_t nBytesWritten = doWrite(buf, size);
47  BOOST_ASSERT(nBytesWritten <= size);
48  return nBytesWritten;
49 }
50 
51 void
53 {
54  if (m_isEnd)
55  return;
56 
57  m_isEnd = true;
58  return doEnd();
59 }
60 
62  : m_next(nullptr)
63 {
64 }
65 
66 void
67 Upstream::appendChain(unique_ptr<Downstream> tail)
68 {
69  if (m_next == nullptr) {
70  m_next = std::move(tail);
71  }
72  else {
73  BOOST_ASSERT(dynamic_cast<Transform*>(m_next.get()) != nullptr);
74  static_cast<Transform*>(m_next.get())->appendChain(std::move(tail));
75  }
76 }
77 
79  : m_oBuffer(nullptr)
80  , m_outputOffset(0)
81 {
82 }
83 
84 void
86 {
87  if (isOutputBufferEmpty())
88  return;
89 
90  size_t nWritten = m_next->write(&(*m_oBuffer)[m_outputOffset],
91  m_oBuffer->size() - m_outputOffset);
92  m_outputOffset += nWritten;
93 }
94 
95 void
97 {
98  while (!isOutputBufferEmpty()) {
100  }
101 }
102 
103 void
104 Transform::setOutputBuffer(unique_ptr<OBuffer> buffer)
105 {
106  BOOST_ASSERT(isOutputBufferEmpty());
107  m_oBuffer = std::move(buffer);
108  m_outputOffset = 0;
109 }
110 
111 bool
113 {
114  return (m_oBuffer == nullptr || m_oBuffer->size() == m_outputOffset);
115 }
116 
117 size_t
118 Transform::doWrite(const uint8_t* data, size_t dataLen)
119 {
121  if (!isOutputBufferEmpty())
122  return 0;
123 
124  preTransform();
126  if (!isOutputBufferEmpty())
127  return 0;
128 
129  size_t nConverted = convert(data, dataLen);
130 
132 
133  return nConverted;
134 }
135 
136 void
137 Transform::doEnd()
138 {
139  finalize();
140  m_next->end();
141 }
142 
143 void
144 Transform::preTransform()
145 {
146 }
147 
148 void
149 Transform::finalize()
150 {
151  flushAllOutput();
152 }
153 
155  : m_nModules(1) // source per se is counted as one module
156 {
157 }
158 
159 void
161 {
162  doPump();
163 }
164 
165 Source&
166 Source::operator>>(unique_ptr<Transform> transform)
167 {
168  transform->setIndex(m_nModules);
169  m_nModules++;
170  this->appendChain(std::move(transform));
171 
172  return *this;
173 }
174 
175 void
176 Source::operator>>(unique_ptr<Sink> sink)
177 {
178  sink->setIndex(m_nModules);
179  m_nModules++;
180  this->appendChain(std::move(sink));
181 
182  this->pump();
183 }
184 
185 } // namespace transform
186 } // namespace security
187 } // namespace ndn
buf
const uint8_t * buf
Definition: verification-helpers.cpp:47
transform
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:421
ndn::security::transform::Upstream::Upstream
Upstream()
Definition: transform-base.cpp:61
ndn::security::transform::Transform::flushAllOutput
void flushAllOutput()
Read the all the content from output buffer and write it into next module.
Definition: transform-base.cpp:96
ndn::security::transform::Transform::flushOutputBuffer
void flushOutputBuffer()
Read the content from output buffer and write it into next module.
Definition: transform-base.cpp:85
ndn::security::transform::Source::operator>>
Source & operator>>(unique_ptr< Transform > transform)
Connect to an intermediate transformation module.
Definition: transform-base.cpp:166
ndn::security::transform::Error
Base class of transformation error.
Definition: transform-base.hpp:49
ndn::security::transform::Downstream::write
size_t write(const uint8_t *buf, size_t size)
Accept input data and perform transformation.
Definition: transform-base.cpp:41
ndn::security::transform::Upstream::m_next
unique_ptr< Downstream > m_next
Definition: transform-base.hpp:180
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
ndn::security::transform::Downstream::end
void end()
Close the input interface of a module.
Definition: transform-base.cpp:52
ndn::security::transform::Transform::setOutputBuffer
void setOutputBuffer(unique_ptr< OBuffer > buffer)
Set output buffer to buffer.
Definition: transform-base.cpp:104
ndn::security::transform::Upstream::appendChain
void appendChain(unique_ptr< Downstream > tail)
connect to next transformation module
Definition: transform-base.cpp:67
ndn::security::transform::Source
Abstraction of the transformation source module.
Definition: transform-base.hpp:291
ndn::security::transform::Source::Source
Source()
Definition: transform-base.cpp:154
ndn::security::transform::Transform
Abstraction of an intermediate transformation module.
Definition: transform-base.hpp:189
ndn::security::transform::Source::pump
void pump()
Pump all data into next transformation module.
Definition: transform-base.cpp:160
ndn::security::transform::Downstream::Downstream
Downstream()
Definition: transform-base.cpp:34
ndn::security::transform::Transform::isOutputBufferEmpty
bool isOutputBufferEmpty() const
Check if output buffer is empty.
Definition: transform-base.cpp:112
ndn::security::transform::Transform::Transform
Transform()
Definition: transform-base.cpp:78
ndn::security::transform::Downstream::getIndex
size_t getIndex() const
Get the module index.
Definition: transform-base.hpp:126
ndn::to_string
std::string to_string(const T &val)
Definition: backports.hpp:102
transform-base.hpp
ndn::security::transform::Error::Error
Error(size_t index, const std::string &what)
Definition: transform-base.cpp:28
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34