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
Error(size_t index, const std::string &what)
void appendChain(unique_ptr< Downstream > tail)
connect to next transformation module
There are three types of module in a transformation chain: Source, Transform, and Sink...
Definition: data.cpp:26
void pump()
Pump all data into next transformation module.
Source & operator>>(unique_ptr< Transform > transform)
Connect to an intermediate transformation module.
STL namespace.
void flushAllOutput()
Read the all the content from output buffer and write it into next module.
#define NDN_THROW(e)
Definition: exception.hpp:61
size_t write(const uint8_t *buf, size_t size)
Accept input data and perform transformation.
void setOutputBuffer(unique_ptr< OBuffer > buffer)
Set output buffer to buffer.
bool isOutputBufferEmpty() const
Check if output buffer is empty.
void end()
Close the input interface of a module.
unique_ptr< Downstream > m_next
Base class of transformation error.
Abstraction of an intermediate transformation module.
void flushOutputBuffer()
Read the content from output buffer and write it into next module.
std::string to_string(const V &v)
Definition: backports.hpp:67
Abstraction of the transformation source module.
size_t getIndex() const
Get the module index.