blob: 936e0aecf88ef638d9000f8927b9b9dbca0177bc [file] [log] [blame]
Yingdi Yu3168c302015-07-04 16:45:40 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016 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
22#include "transform-base.hpp"
23
24namespace ndn {
25namespace security {
26namespace transform {
27
28Error::Error(size_t index, const std::string& what)
29 : std::runtime_error("Error in module " + std::to_string(index) + ": " + what)
30 , m_index(index)
31{
32}
33
34Downstream::Downstream()
35 : m_isEnd(false)
36{
37}
38
39size_t
40Downstream::write(const uint8_t* buf, size_t size)
41{
42 if (m_isEnd)
43 BOOST_THROW_EXCEPTION(Error(getIndex(), "Module is closed, no more input"));
44
45 size_t nBytesWritten = doWrite(buf, size);
46 BOOST_ASSERT(nBytesWritten <= size);
47 return nBytesWritten;
48}
49
50void
51Downstream::end()
52{
53 if (m_isEnd)
54 return;
55
56 m_isEnd = true;
57 return doEnd();
58}
59
60Upstream::Upstream()
61 : m_next(nullptr)
62{
63}
64
65void
66Upstream::appendChain(unique_ptr<Downstream> tail)
67{
68 if (m_next == nullptr) {
69 m_next = std::move(tail);
70 }
71 else {
72 BOOST_ASSERT(dynamic_cast<Transform*>(m_next.get()) != nullptr);
73 static_cast<Transform*>(m_next.get())->appendChain(std::move(tail));
74 }
75}
76
Yingdi Yu38317e52015-07-22 13:58:02 -070077Transform::Transform()
78 : m_oBuffer(nullptr)
79 , m_outputOffset(0)
80{
81}
82
83void
84Transform::flushOutputBuffer()
85{
86 if (isOutputBufferEmpty())
87 return;
88
89 size_t nWritten = m_next->write(&(*m_oBuffer)[m_outputOffset],
90 m_oBuffer->size() - m_outputOffset);
91 m_outputOffset += nWritten;
92}
93
94void
95Transform::setOutputBuffer(unique_ptr<OBuffer> buffer)
96{
97 BOOST_ASSERT(isOutputBufferEmpty());
98 m_oBuffer = std::move(buffer);
99 m_outputOffset = 0;
100}
101
102bool
103Transform::isOutputBufferEmpty() const
104{
105 return (m_oBuffer == nullptr || m_oBuffer->size() == m_outputOffset);
106}
107
108size_t
109Transform::doWrite(const uint8_t* data, size_t dataLen)
110{
111 flushOutputBuffer();
112 if (!isOutputBufferEmpty())
113 return 0;
114
115 preTransform();
116 flushOutputBuffer();
117 if (!isOutputBufferEmpty())
118 return 0;
119
120 size_t nConverted = convert(data, dataLen);
121
122 flushOutputBuffer();
123
124 return nConverted;
125}
126
127void
128Transform::doEnd()
129{
130 finalize();
131 m_next->end();
132}
133
134void
135Transform::preTransform()
136{
137}
138
139void
140Transform::finalize()
141{
142 while (!isOutputBufferEmpty()) {
143 flushOutputBuffer();
144 }
145}
146
Yingdi Yu3168c302015-07-04 16:45:40 -0700147Source::Source()
148 : m_nModules(1) // source per se is counted as one module
149{
150}
151
152void
153Source::pump()
154{
155 doPump();
156}
157
158Source&
159Source::operator>>(unique_ptr<Transform> transform)
160{
161 transform->setIndex(m_nModules);
162 m_nModules++;
163 this->appendChain(std::move(transform));
164
165 return *this;
166}
167
168void
169Source::operator>>(unique_ptr<Sink> sink)
170{
171 sink->setIndex(m_nModules);
172 m_nModules++;
173 this->appendChain(std::move(sink));
174
175 this->pump();
176}
177
178} // namespace transform
179} // namespace security
180} // namespace ndn