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; -*- */
22 #include "stream-source.hpp"
23 #include <vector>
24 
25 namespace ndn {
26 namespace security {
27 namespace transform {
28 
29 const std::size_t StreamSource::DEFAULT_BUFFER_LEN = 1024;
30 
31 StreamSource::StreamSource(std::istream& is, size_t bufferSize)
32  : Source()
33  , m_is(is)
34  , m_bufferSize(bufferSize)
35 {
36  BOOST_ASSERT(bufferSize > 0);
37 }
38 
39 void
40 StreamSource::doPump()
41 {
42  BOOST_ASSERT(m_next != nullptr);
43 
44  std::vector<uint8_t> buffer(m_bufferSize);
45  size_t dataOffset = 0;
46  size_t dataLen = 0;
47 
48  while (dataLen > 0 || !m_is.eof()) {
49  if (dataLen > 0) {
50  // we have some leftover, handle them first
51  size_t nBytesWritten = m_next->write(&buffer[dataOffset], dataLen);
52 
53  dataOffset += nBytesWritten;
54  dataLen -= nBytesWritten;
55  }
56  else if (!m_is) {
57  BOOST_THROW_EXCEPTION(Error(getIndex(), "Input stream in bad state"));
58  }
59  else if (m_is.good()) {
60  m_is.read(reinterpret_cast<char*>(&buffer.front()), buffer.size());
61  dataOffset = 0;
62  dataLen = m_is.gcount();
63  }
64  }
65  m_next->end();
66 }
67 
68 } // namespace transform
69 } // namespace security
70 } // namespace ndn
Copyright (c) 2011-2015 Regents of the University of California.
StreamSource(std::istream &is, size_t bufLen=DEFAULT_BUFFER_LEN)
Construst a source using is as input.
unique_ptr< Downstream > m_next
size_t getIndex() const
Get the source module index (should always be 0).
Base class of transformation error.
Abstraction of the transformation source module.
static const std::size_t DEFAULT_BUFFER_LEN