chunks: segmented file transfer

refs #3071

Change-Id: I88e4fc1a8e33a0d61a95e2291cccc7b998647489
diff --git a/tools/chunks/putchunks/ndnputchunks.cpp b/tools/chunks/putchunks/ndnputchunks.cpp
new file mode 100644
index 0000000..202cd83
--- /dev/null
+++ b/tools/chunks/putchunks/ndnputchunks.cpp
@@ -0,0 +1,137 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2016,  Regents of the University of California,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University.
+ *
+ * This file is part of ndn-tools (Named Data Networking Essential Tools).
+ * See AUTHORS.md for complete list of ndn-tools authors and contributors.
+ *
+ * ndn-tools is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ndn-tools, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ *
+ * @author Wentao Shang
+ * @author Steve DiBenedetto
+ * @author Andrea Tosatto
+ */
+
+#include "core/version.hpp"
+#include "producer.hpp"
+
+namespace ndn {
+namespace chunks {
+
+static int
+main(int argc, char** argv)
+{
+  std::string programName = argv[0];
+  uint64_t freshnessPeriod = 10000;
+  bool printVersion = false;
+  size_t maxChunkSize = MAX_NDN_PACKET_SIZE >> 1;
+  std::string signingStr;
+  bool isVerbose = false;
+  std::string prefix;
+
+  namespace po = boost::program_options;
+  po::options_description visibleDesc("Options");
+  visibleDesc.add_options()
+    ("help,h",          "print this help message and exit")
+    ("freshness,f",     po::value<uint64_t>(&freshnessPeriod)->default_value(freshnessPeriod),
+                        "specify FreshnessPeriod, in milliseconds")
+    ("print-data-version,p",  po::bool_switch(&printVersion), "print Data version to the standard output")
+    ("size,s",          po::value<size_t>(&maxChunkSize)->default_value(maxChunkSize),
+                        "maximum chunk size, in bytes")
+    ("signing-info,S",  po::value<std::string>(&signingStr)->default_value(signingStr),
+                        "set signing information")
+    ("verbose,v",       po::bool_switch(&isVerbose), "turn on verbose output")
+    ("version,V",       "print program version and exit")
+    ;
+
+  po::options_description hiddenDesc("Hidden options");
+  hiddenDesc.add_options()
+    ("ndn-name,n", po::value<std::string>(&prefix), "NDN name for the served content");
+
+  po::positional_options_description p;
+  p.add("ndn-name", -1);
+
+  po::options_description optDesc("Allowed options");
+  optDesc.add(visibleDesc).add(hiddenDesc);
+
+  po::variables_map vm;
+  try {
+    po::store(po::command_line_parser(argc, argv).options(optDesc).positional(p).run(), vm);
+    po::notify(vm);
+  }
+  catch (const po::error& e) {
+    std::cerr << "ERROR: " << e.what() << std::endl;
+    return 2;
+  }
+  catch (const boost::bad_any_cast& e) {
+    std::cerr << "ERROR: " << e.what() << std::endl;
+    return 2;
+  }
+
+  if (vm.count("help") > 0) {
+    std::cout << "Usage: " << programName << " [options] ndn:/name" << std::endl;
+    std::cout << visibleDesc;
+    return 0;
+  }
+
+  if (vm.count("version") > 0) {
+    std::cout << "ndnputchunks " << tools::VERSION << std::endl;
+    return 0;
+  }
+
+  if (prefix.empty()) {
+    std::cerr << "Usage: " << programName << " [options] ndn:/name" << std::endl;
+    std::cerr << visibleDesc;
+    return 2;
+  }
+
+  if (maxChunkSize < 1 || maxChunkSize > MAX_NDN_PACKET_SIZE) {
+    std::cerr << "ERROR: Maximum chunk size must be between 1 and " << MAX_NDN_PACKET_SIZE << std::endl;
+    return 2;
+  }
+
+  security::SigningInfo signingInfo;
+  try {
+    signingInfo = security::SigningInfo(signingStr);
+  }
+  catch (const std::invalid_argument& e) {
+    std::cerr << "ERROR: " << e.what() << std::endl;
+    return 2;
+  }
+
+  try {
+    Face face;
+    KeyChain keyChain;
+    Producer producer(prefix, face, keyChain, signingInfo, time::milliseconds(freshnessPeriod),
+                      maxChunkSize, isVerbose, printVersion, std::cin);
+    producer.run();
+  }
+  catch (const std::exception& e) {
+    std::cerr << "ERROR: " << e.what() << std::endl;
+    return 1;
+  }
+
+  return 0;
+}
+
+} // namespace chunks
+} // namespace ndn
+
+int
+main(int argc, char** argv)
+{
+  return ndn::chunks::main(argc, argv);
+}
diff --git a/tools/chunks/putchunks/producer.cpp b/tools/chunks/putchunks/producer.cpp
new file mode 100644
index 0000000..141be5b
--- /dev/null
+++ b/tools/chunks/putchunks/producer.cpp
@@ -0,0 +1,157 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2016,  Regents of the University of California,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University.
+ *
+ * This file is part of ndn-tools (Named Data Networking Essential Tools).
+ * See AUTHORS.md for complete list of ndn-tools authors and contributors.
+ *
+ * ndn-tools is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ndn-tools, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ *
+ * @author Wentao Shang
+ * @author Steve DiBenedetto
+ * @author Andrea Tosatto
+ */
+
+#include "producer.hpp"
+
+namespace ndn {
+namespace chunks {
+
+Producer::Producer(const Name& prefix,
+                   Face& face,
+                   KeyChain& keyChain,
+                   const security::SigningInfo& signingInfo,
+                   time::milliseconds freshnessPeriod,
+                   size_t maxSegmentSize,
+                   bool isVerbose,
+                   bool needToPrintVersion,
+                   std::istream& is)
+  : m_face(face)
+  , m_keyChain(keyChain)
+  , m_signingInfo(signingInfo)
+  , m_freshnessPeriod(freshnessPeriod)
+  , m_maxSegmentSize(maxSegmentSize)
+  , m_isVerbose(isVerbose)
+{
+  if (prefix.size() > 0 && prefix[-1].isVersion()) {
+    m_prefix = prefix.getPrefix(-1);
+    m_versionedPrefix = prefix;
+  }
+  else {
+    m_prefix = prefix;
+    m_versionedPrefix = Name(m_prefix).appendVersion();
+  }
+
+  populateStore(is);
+
+  if (needToPrintVersion)
+    std::cout << m_versionedPrefix[-1] << std::endl;
+
+  m_face.setInterestFilter(m_prefix,
+                           bind(&Producer::onInterest, this, _2),
+                           RegisterPrefixSuccessCallback(),
+                           bind(&Producer::onRegisterFailed, this, _1, _2));
+
+  if (m_isVerbose)
+    std::cerr << "Data published with name: " << m_versionedPrefix << std::endl;
+}
+
+void
+Producer::run()
+{
+  m_face.processEvents();
+}
+
+void
+Producer::onInterest(const Interest& interest)
+{
+  BOOST_ASSERT(m_store.size() > 0);
+
+  if (m_isVerbose)
+    std::cerr << "Interest: " << interest << std::endl;
+
+  const Name& name = interest.getName();
+  shared_ptr<Data> data;
+
+  // is this a discovery Interest or a sequence retrieval?
+  if (name.size() == m_versionedPrefix.size() + 1 && m_versionedPrefix.isPrefixOf(name) &&
+      name[-1].isSegment()) {
+    const auto segmentNo = static_cast<size_t>(interest.getName()[-1].toSegment());
+    // specific segment retrieval
+    if (segmentNo < m_store.size()) {
+      data = m_store[segmentNo];
+    }
+  }
+  else if (interest.matchesData(*m_store[0])) {
+    // Interest has version and is looking for the first segment or has no version
+    data = m_store[0];
+  }
+
+  if (data != nullptr) {
+    if (m_isVerbose)
+      std::cerr << "Data: " << *data << std::endl;
+
+    m_face.put(*data);
+  }
+}
+
+void
+Producer::populateStore(std::istream& is)
+{
+  BOOST_ASSERT(m_store.size() == 0);
+
+  if (m_isVerbose)
+    std::cerr << "Loading input ..." << std::endl;
+
+  std::vector<uint8_t> buffer(m_maxSegmentSize);
+  while (is.good()) {
+    is.read(reinterpret_cast<char*>(buffer.data()), buffer.size());
+    const auto nCharsRead = is.gcount();
+    if (nCharsRead > 0) {
+      auto data = make_shared<Data>(Name(m_versionedPrefix).appendSegment(m_store.size()));
+      data->setFreshnessPeriod(m_freshnessPeriod);
+      data->setContent(&buffer[0], nCharsRead);
+
+      m_store.push_back(data);
+    }
+  }
+
+  if (m_store.empty()) {
+    auto data = make_shared<Data>(Name(m_versionedPrefix).appendSegment(0));
+    data->setFreshnessPeriod(m_freshnessPeriod);
+    m_store.push_back(data);
+  }
+
+  auto finalBlockId = name::Component::fromSegment(m_store.size() - 1);
+  for (const auto& data : m_store) {
+    data->setFinalBlockId(finalBlockId);
+    m_keyChain.sign(*data, m_signingInfo);
+  }
+
+  if (m_isVerbose)
+    std::cerr << "Created " << m_store.size() << " chunks for prefix " << m_prefix << std::endl;
+}
+
+void
+Producer::onRegisterFailed(const Name& prefix, const std::string& reason)
+{
+  std::cerr << "ERROR: Failed to register prefix '"
+            << prefix << "' (" << reason << ")" << std::endl;
+  m_face.shutdown();
+}
+
+} // namespace chunks
+} // namespace ndn
diff --git a/tools/chunks/putchunks/producer.hpp b/tools/chunks/putchunks/producer.hpp
new file mode 100644
index 0000000..061a2bf
--- /dev/null
+++ b/tools/chunks/putchunks/producer.hpp
@@ -0,0 +1,100 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2016,  Regents of the University of California,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University.
+ *
+ * This file is part of ndn-tools (Named Data Networking Essential Tools).
+ * See AUTHORS.md for complete list of ndn-tools authors and contributors.
+ *
+ * ndn-tools is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ndn-tools, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ *
+ * @author Wentao Shang
+ * @author Steve DiBenedetto
+ * @author Andrea Tosatto
+ */
+
+#ifndef NDN_TOOLS_CHUNKS_PUTCHUNKS_PRODUCER_HPP
+#define NDN_TOOLS_CHUNKS_PUTCHUNKS_PRODUCER_HPP
+
+#include "core/common.hpp"
+
+namespace ndn {
+namespace chunks {
+
+/**
+ * @brief Segmented version Producer
+ *
+ * Packetizes and publishes data from an input stream under /prefix/<version>/<segment number>.
+ * The current time is used as the version number. The store has always at least one element (also
+ * with empty input stream).
+ */
+class Producer : noncopyable
+{
+public:
+  /**
+   * @brief Create the Producer
+   *
+   * @prefix prefix used to publish data, if the last component of prefix is not a version number
+   *         the current time is used as version number.
+   */
+  Producer(const Name& prefix, Face& face, KeyChain& keyChain,
+           const security::SigningInfo& signingInfo, time::milliseconds freshnessPeriod,
+           size_t maxSegmentSize, bool isVerbose = false, bool needToPrintVersion = false,
+           std::istream& is = std::cin);
+
+  /**
+   * @brief Run the Producer
+   */
+  void
+  run();
+
+private:
+  void
+  onInterest(const Interest& interest);
+
+  /**
+   * @brief Split the input stream in data packets and save them to the store
+   *
+   * Create data packets reading all the characters from the input stream until EOF, or an
+   * error occurs. Each data packet has a maximum payload size of m_maxSegmentSize value and is
+   * stored inside the vector m_store. An empty data packet is created and stored if the input
+   * stream is empty.
+   *
+   * @return Number of data packets contained in the store after the operation
+   */
+  void
+  populateStore(std::istream& is);
+
+  void
+  onRegisterFailed(const Name& prefix, const std::string& reason);
+
+PUBLIC_WITH_TESTS_ELSE_PRIVATE:
+  std::vector<shared_ptr<Data>> m_store;
+
+private:
+  Name m_prefix;
+  Name m_versionedPrefix;
+  Face& m_face;
+  KeyChain& m_keyChain;
+  security::SigningInfo m_signingInfo;
+  time::milliseconds m_freshnessPeriod;
+  size_t m_maxSegmentSize;
+  bool m_isVerbose;
+};
+
+} // namespace chunks
+} // namespace ndn
+
+#endif // NDN_TOOLS_CHUNKS_PUTCHUNKS_PRODUCER_HPP