blob: 85df6b987567c3f3c43963c416b87c6f818fdcf7 [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
77Source::Source()
78 : m_nModules(1) // source per se is counted as one module
79{
80}
81
82void
83Source::pump()
84{
85 doPump();
86}
87
88Source&
89Source::operator>>(unique_ptr<Transform> transform)
90{
91 transform->setIndex(m_nModules);
92 m_nModules++;
93 this->appendChain(std::move(transform));
94
95 return *this;
96}
97
98void
99Source::operator>>(unique_ptr<Sink> sink)
100{
101 sink->setIndex(m_nModules);
102 m_nModules++;
103 this->appendChain(std::move(sink));
104
105 this->pump();
106}
107
108} // namespace transform
109} // namespace security
110} // namespace ndn