NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
stream-source.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2017 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 "stream-source.hpp"
23 
24 #include <istream>
25 #include <vector>
26 
27 namespace ndn {
28 namespace security {
29 namespace transform {
30 
31 const std::size_t StreamSource::DEFAULT_BUFFER_LEN = 1024;
32 
33 StreamSource::StreamSource(std::istream& is, size_t bufferSize)
34  : Source()
35  , m_is(is)
36  , m_bufferSize(bufferSize)
37 {
38  BOOST_ASSERT(bufferSize > 0);
39 }
40 
41 void
42 StreamSource::doPump()
43 {
44  BOOST_ASSERT(m_next != nullptr);
45 
46  std::vector<uint8_t> buffer(m_bufferSize);
47  size_t dataOffset = 0;
48  size_t dataLen = 0;
49 
50  while (dataLen > 0 || !m_is.eof()) {
51  if (dataLen > 0) {
52  // we have some leftover, handle them first
53  size_t nBytesWritten = m_next->write(&buffer[dataOffset], dataLen);
54 
55  dataOffset += nBytesWritten;
56  dataLen -= nBytesWritten;
57  }
58  else if (!m_is) {
59  BOOST_THROW_EXCEPTION(Error(getIndex(), "Input stream in bad state"));
60  }
61  else if (m_is.good()) {
62  m_is.read(reinterpret_cast<char*>(&buffer.front()), buffer.size());
63  dataOffset = 0;
64  dataLen = m_is.gcount();
65  }
66  }
67  m_next->end();
68 }
69 
70 } // namespace transform
71 } // namespace security
72 } // namespace ndn
Copyright (c) 2011-2015 Regents of the University of California.
size_t getIndex() const
Get the source module index (should always be 0).
Catch-all error for security policy errors that don&#39;t fit in other categories.
Definition: base.hpp:79
StreamSource(std::istream &is, size_t bufLen=DEFAULT_BUFFER_LEN)
Construst a source using is as input.
unique_ptr< Downstream > m_next
Base class of transformation error.
Abstraction of the transformation source module.
static const std::size_t DEFAULT_BUFFER_LEN