tools: ndncatchunks and ndnputchunks tool
Change-Id: I831064afaa6b7883a61f1f2091062ce184ca0950
diff --git a/.gitignore b/.gitignore
index 968fa6e..0fd0443 100644
--- a/.gitignore
+++ b/.gitignore
@@ -74,6 +74,8 @@
# Tools
tools/tlvdump
+tools/ndncatchunks
+tools/ndnputchunks
# Examples
examples/consumer
diff --git a/tools/Makefile.am b/tools/Makefile.am
index a1a0094..216bb82 100644
--- a/tools/Makefile.am
+++ b/tools/Makefile.am
@@ -1,6 +1,11 @@
-bin_PROGRAMS=tlvdump
+bin_PROGRAMS = tlvdump ndncatchunks ndnputchunks
tlvdump_SOURCES = tlvdump.cpp
-
tlvdump_LDADD = ../libndn-cpp.la @BOOST_SYSTEM_LIB@
+
+ndncatchunks_SOURCES = ndncatchunks.cpp
+ndncatchunks_LDADD = ../libndn-cpp.la @BOOST_SYSTEM_LIB@
+
+ndnputchunks_SOURCES = ndnputchunks.cpp
+ndnputchunks_LDADD = ../libndn-cpp.la @BOOST_SYSTEM_LIB@
diff --git a/tools/ndncatchunks.cpp b/tools/ndncatchunks.cpp
new file mode 100644
index 0000000..6bfb07a
--- /dev/null
+++ b/tools/ndncatchunks.cpp
@@ -0,0 +1,192 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Author: Wentao Shang <wentao@cs.ucla.edu>
+ */
+
+
+#include <ndn-cpp/face.hpp>
+#include <stdexcept>
+#include <iostream>
+
+#if NDN_CPP_HAVE_CXX11
+// In the std library, the placeholders are in a different namespace than boost.
+using namespace ndn::func_lib::placeholders;
+#endif
+
+class Consumer
+{
+public:
+ Consumer (const std::string &data_name, int pipe_size, int total_seg)
+ : m_data_name (data_name), m_pipe_size (pipe_size), m_total_seg (total_seg),
+ m_next_seg (0), m_total_size (0), m_output (false)
+ {
+ }
+
+ inline void
+ enable_output ()
+ {
+ m_output = true;
+ }
+
+ void
+ run ();
+
+private:
+ void
+ on_data (const ndn::ptr_lib::shared_ptr<const ndn::Interest> &interest,
+ const ndn::ptr_lib::shared_ptr<ndn::Data> &data);
+
+ void
+ on_timeout (const ndn::ptr_lib::shared_ptr<const ndn::Interest> &interest);
+
+ ndn::Face m_face;
+ ndn::Name m_data_name;
+ int m_pipe_size;
+ int m_total_seg;
+ int m_next_seg;
+ int m_total_size;
+ bool m_output; // set to false by default
+};
+
+void
+Consumer::run ()
+{
+ try
+ {
+ for (int i = 0; i < m_pipe_size; i++)
+ {
+ ndn::Interest inst (ndn::Name(m_data_name).appendSegment (m_next_seg++));
+ inst.setScope (1);
+ inst.setInterestLifetime (1000);
+ inst.setMustBeFresh (true);
+
+ m_face.expressInterest (inst,
+ ndn::func_lib::bind (&Consumer::on_data, this, _1, _2),
+ ndn::func_lib::bind (&Consumer::on_timeout, this, _1));
+ }
+
+ // processEvents will block until the requested data received or timeout occurs
+ m_face.processEvents ();
+ }
+ catch (std::exception& e)
+ {
+ std::cerr << "ERROR: " << e.what () << std::endl;
+ }
+}
+
+void
+Consumer::on_data (const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest, const ndn::ptr_lib::shared_ptr<ndn::Data>& data)
+{
+ const ndn::Block& content = data->getContent ();
+ const ndn::Name& name = data->getName ();
+
+ if (m_output)
+ {
+ std::cout.write(reinterpret_cast<const char*>(content.value()), content.value_size());
+ }
+
+ m_total_size += content.value_size ();
+
+ if ((int) (name.rbegin ()->toSegment ()) + 1 == m_total_seg)
+ {
+ std::cerr << "Last segment received." << std::endl;
+ std::cerr << "Total # bytes of content received: " << m_total_size << std::endl;
+ }
+ else
+ {
+ // Send interest for next segment
+ ndn::Interest inst (ndn::Name(m_data_name).appendSegment (m_next_seg++));
+ inst.setScope (1);
+ inst.setInterestLifetime (1000);
+ inst.setMustBeFresh (true);
+
+ m_face.expressInterest (inst,
+ ndn::func_lib::bind (&Consumer::on_data, this, _1, _2),
+ ndn::func_lib::bind (&Consumer::on_timeout, this, _1));
+ }
+}
+
+
+void
+Consumer::on_timeout (const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest)
+{
+ //XXX: currently no retrans
+ std::cerr << "TIMEOUT: last interest sent for segment #" << (m_next_seg - 1) << std::endl;
+}
+
+
+int
+usage(const std::string &filename)
+{
+ std::cerr << "Usage: \n " << filename << " [-p pipe_size] [-c total_segment] [-o] /ndn/name\n";
+ return 1;
+}
+
+
+int
+main (int argc, char **argv)
+{
+ std::string name;
+ int pipe_size = 1;
+ int total_seg = std::numeric_limits<int>::max();
+ bool output = false;
+
+ int opt;
+ while ((opt = getopt(argc, argv, "op:c:")) != -1)
+ {
+ switch (opt)
+ {
+ case 'p':
+ pipe_size = atoi (optarg);
+ if (pipe_size <= 0)
+ pipe_size = 1;
+ std::cerr << "main (): set pipe size = " << pipe_size << std::endl;
+ break;
+ case 'c':
+ total_seg = atoi (optarg);
+ if (total_seg <= 0)
+ total_seg = 1;
+ std::cerr << "main (): set total seg = " << total_seg << std::endl;
+ break;
+ case 'o':
+ output = true;
+ break;
+ default:
+ return usage(argv[0]);
+ }
+ }
+
+ if (optind < argc)
+ {
+ name = argv[optind];
+ }
+
+ if (name.empty())
+ {
+ return usage(argv[0]);
+ }
+
+ Consumer consumer (name, pipe_size, total_seg);
+
+ if (output)
+ consumer.enable_output ();
+
+ consumer.run ();
+
+ return 0;
+}
diff --git a/tools/ndnputchunks.cpp b/tools/ndnputchunks.cpp
new file mode 100644
index 0000000..68755a9
--- /dev/null
+++ b/tools/ndnputchunks.cpp
@@ -0,0 +1,131 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Author: Wentao Shang <wentao@cs.ucla.edu>
+ */
+
+#include <ndn-cpp/face.hpp>
+#include <ndn-cpp/security/key-chain.hpp>
+
+#if NDN_CPP_HAVE_CXX11
+// In the std library, the placeholders are in a different namespace than boost.
+using namespace ndn::func_lib::placeholders;
+#endif
+
+#define MAX_SEG_SIZE 4096
+
+class Producer
+{
+public:
+ Producer (const char* name)
+ : m_name (name)
+ , m_verbose (false)
+ {
+ int segnum = 0;
+ char* buf = new char[MAX_SEG_SIZE];
+ do
+ {
+ std::cin.read (buf, MAX_SEG_SIZE);
+ int got = std::cin.gcount ();
+
+ if (got > 0)
+ {
+ ndn::ptr_lib::shared_ptr<ndn::Data> data = ndn::ptr_lib::make_shared<ndn::Data> (ndn::Name(m_name).appendSegment (segnum));
+ data->setFreshnessPeriod (10000); // 10 sec
+ data->setContent (reinterpret_cast<const uint8_t*>(buf), got);
+
+ m_keychain.sign(*data);
+ m_store.push_back(data);
+ segnum++;
+ }
+ }
+ while (std::cin);
+
+ if (m_verbose)
+ std::cerr << "Created " << segnum << " chunks for prefix [" << m_name << "]" << std::endl;
+ }
+
+ void
+ onInterest (const ndn::ptr_lib::shared_ptr<const ndn::Name>& name, const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest)
+ {
+ if (m_verbose)
+ std::cerr << "<< I: " << *interest << std::endl;
+
+ size_t segnum = static_cast<size_t>(interest->getName ().rbegin ()->toSegment ());
+
+ if (segnum < m_store.size())
+ {
+ m_face.put (*m_store[segnum]);
+ }
+ }
+
+ void
+ onRegisterFailed (const ndn::ptr_lib::shared_ptr<const ndn::Name>&)
+ {
+ std::cerr << "ERROR: Failed to register prefix in local hub's daemon" << std::endl;
+ m_face.shutdown ();
+ }
+
+ void
+ run ()
+ {
+ if (m_store.empty())
+ {
+ std::cerr << "Nothing to serve. Exiting." << std::endl;
+ return;
+ }
+
+ m_face.setInterestFilter (m_name,
+ ndn::func_lib::bind (&Producer::onInterest, this, _1, _2),
+ ndn::func_lib::bind (&Producer::onRegisterFailed, this, _1));
+ m_face.processEvents ();
+ }
+
+private:
+ ndn::Name m_name;
+ ndn::Face m_face;
+ ndn::KeyChain m_keychain;
+
+ std::vector< ndn::ptr_lib::shared_ptr<ndn::Data> > m_store;
+
+ bool m_verbose;
+};
+
+int
+main (int argc, char *argv[])
+{
+ if (argc < 2)
+ {
+ std::cerr << "Usage: ./ndnputchunks [data_prefix]\n";
+ return -1;
+ }
+
+ try
+ {
+ ndn::MillisecondsSince1970 time = ndn::getNow();
+
+ std::cerr << "Preparing the input..." << std::endl;
+ Producer producer (argv[1]);
+ std::cerr << "Ready... (took " << ((ndn::getNow() - time)/1000) << " seconds)" << std::endl;
+ producer.run ();
+ }
+ catch (std::exception& e)
+ {
+ std::cerr << "ERROR: " << e.what () << std::endl;
+ }
+ return 0;
+}