repo: ndnputfile and ndngetfile
Change-Id: I0369f74eaaf1c7cb5921419182994eed822d2ebc
task #1887
diff --git a/tools/ndngetfile.cpp b/tools/ndngetfile.cpp
index fd9dc6a..50f8ab3 100644
--- a/tools/ndngetfile.cpp
+++ b/tools/ndngetfile.cpp
@@ -25,12 +25,14 @@
using namespace ndn;
+static const int MAX_RETRY = 3;
+
void
Consumer::fetchData(const Name& name)
{
Interest interest(name);
interest.setInterestLifetime(m_interestLifetime);
-
+ //std::cout<<"interest name = "<<interest.getName()<<std::endl;
if (m_hasVersion)
{
interest.setMustBeFresh(m_mustBeFresh);
@@ -42,7 +44,10 @@
}
m_face.expressInterest(interest,
- bind(&Consumer::onData, this, _1, _2),
+ m_hasVersion ?
+ bind(&Consumer::onVersionedData, this, _1, _2)
+ :
+ bind(&Consumer::onUnversionedData, this, _1, _2),
bind(&Consumer::onTimeout, this, _1));
}
@@ -51,10 +56,8 @@
{
// Send the first Interest
Name name(m_dataName);
- if (m_hasVersion)
- // If '-u' is specified, we already have version number in the name
- name.appendSegment(m_nextSegment++);
+ m_nextSegment++;
fetchData(name);
// processEvents will block until the requested data received or timeout occurs
@@ -62,71 +65,121 @@
}
void
-Consumer::onData(const Interest& interest, Data& data)
+Consumer::onVersionedData(const Interest& interest, Data& data)
{
const Name& name = data.getName();
- if (name.size() != m_dataName.size() + (m_hasVersion ? 1 : 2))
- throw std::invalid_argument("unexpected data name size.");
- uint64_t segment = name[-1].toSegment();
-
- if (!m_hasVersion)
- {
- // Assume the second to last component is the version number
- // and the last component is the segment number
- m_dataName.append(name[-2]);
- m_hasVersion = true;
- if (m_verbose)
- {
- std::cerr << "LOG: version number is " << name[-2].toVersion() << std::endl;
- }
-
- if (segment != 0)
- {
- // Discard this segment and fetch the first segment
- fetchData(Name(m_dataName).appendSegment(m_nextSegment++));
+ // the received data name may have segment number or not
+ if (name.size() == m_dataName.size()) {
+ if (!m_isSingle) {
+ Name fetchName = name;
+ fetchName.appendSegment(0);
+ fetchData(fetchName);
+ }
+ }
+ else if (name.size() == m_dataName.size() + 1) {
+ if (!m_isSingle) {
+ if (m_isFirst) {
+ uint64_t segment = name[-1].toSegment();
+ if (segment != 0) {
+ fetchData(Name(m_dataName).appendSegment(0));
+ m_isFirst = false;
return;
}
-
- m_nextSegment++;
+ m_isFirst = false;
+ }
+ fetchNextData(name, data);
}
+ else {
+ std::cerr << "ERROR: Data is not stored in a single packet" << std::endl;
+ return;
+ }
+ }
+ else {
+ std::cerr << "ERROR: Name size does not match" << std::endl;
+ return;
+ }
+ readData(data);
+}
- BOOST_ASSERT(segment == (m_nextSegment - 1));
+void
+Consumer::onUnversionedData(const Interest& interest, Data& data)
+{
+ const Name& name = data.getName();
+ //std::cout<<"recevied data name = "<<name<<std::endl;
+ if (name.size() == m_dataName.size() + 1) {
+ if (!m_isSingle) {
+ Name fetchName = name;
+ fetchName.append(name[-1]).appendSegment(0);
+ fetchData(fetchName);
+ }
+ }
+ else if (name.size() == m_dataName.size() + 2) {
+ if (!m_isSingle) {
+ if (m_isFirst) {
+ uint64_t segment = name[-1].toSegment();
+ if (segment != 0) {
+ fetchData(Name(m_dataName).append(name[-2]).appendSegment(0));
+ m_isFirst = false;
+ return;
+ }
+ m_isFirst = false;
+ }
+ fetchNextData(name, data);
+ }
+ else {
+ std::cerr << "ERROR: Data is not stored in a single packet" << std::endl;
+ return;
+ }
+ }
+ else {
+ std::cerr << "ERROR: Name size does not match" << std::endl;
+ return;
+ }
+ readData(data);
+}
- // Output the current segment
+void
+Consumer::readData(const Data& data)
+{
const Block& content = data.getContent();
m_os.write(reinterpret_cast<const char*>(content.value()), content.value_size());
m_totalSize += content.value_size();
if (m_verbose)
- {
- std::cerr << "LOG: received segment #" << segment << std::endl;
- }
-
- // Check final block id
- const name::Component& finalBlockId = data.getMetaInfo().getFinalBlockId();
- if (finalBlockId == name[-1])
- {
- // Reach EOF
- std::cerr << "INFO: End of file is reached." << std::endl;
- std::cerr << "INFO: Total # of segments received: " << (m_nextSegment - 1) << std::endl;
- std::cerr << "INFO: Total # bytes of content received: " << m_totalSize << std::endl;
- }
- else
- {
- // Reset retry counter
- m_retryCount = 0;
-
- // Fetch next segment
- fetchData(Name(m_dataName).appendSegment(m_nextSegment++));
- }
+ {
+ std::cerr << "LOG: received data = " << data.getName() << std::endl;
+ }
+ if (m_isFinished || m_isSingle) {
+ std::cerr << "INFO: End of file is reached." << std::endl;
+ std::cerr << "INFO: Total # of segments received: " << m_nextSegment << std::endl;
+ std::cerr << "INFO: Total # bytes of content received: " << m_totalSize << std::endl;
+ }
}
-const int Consumer::MAX_RETRY = 3;
+void
+Consumer::fetchNextData(const Name& name, const Data& data)
+{
+ uint64_t segment = name[-1].toSegment();
+ BOOST_ASSERT(segment == (m_nextSegment - 1));
+ const name::Component& finalBlockId = data.getMetaInfo().getFinalBlockId();
+ if (finalBlockId == name[-1]) {
+ m_isFinished = true;
+ }
+ else
+ {
+ // Reset retry counter
+ m_retryCount = 0;
+ if (m_hasVersion)
+ fetchData(Name(m_dataName).appendSegment(m_nextSegment++));
+ else
+ fetchData(Name(m_dataName).append(name[-2]).appendSegment(m_nextSegment++));
+ }
+}
void
Consumer::onTimeout(const Interest& interest)
{
- if (m_retryCount++ < Consumer::MAX_RETRY)
+ if (m_retryCount++ < MAX_RETRY)
{
// Retransmit the interest
fetchData(interest.getName());
@@ -138,7 +191,7 @@
else
{
std::cerr << "TIMEOUT: last interest sent for segment #" << (m_nextSegment - 1) << std::endl;
- std::cerr << "TIMEOUT: abort fetching after " << Consumer::MAX_RETRY
+ std::cerr << "TIMEOUT: abort fetching after " << MAX_RETRY
<< " times of retry" << std::endl;
}
}
@@ -148,14 +201,15 @@
usage(const std::string& filename)
{
std::cerr << "Usage: \n "
- << filename << " [-v] [-u] [-l lifetime] [-w timeout] [-o filename] ndn-name\n\n"
+ << filename << " [-v] [-s] [-u] [-l lifetime] [-w timeout] [-o filename] ndn-name\n\n"
<< "-v: be verbose\n"
- << "-u: unversioned: do not try to find the latest version; "
- "ndn-name contains version component\n"
- << "ndn-name: NDN Name prefix for Data to be read\n"
+ << "-s: only get single data packet\n"
+ << "-u: versioned: ndn-name contains version component\n"
+ << " if -u is not specified, this command will return the rightmost child for the prefix\n"
<< "-l: InterestLifetime in milliseconds\n"
<< "-w: timeout in milliseconds for whole process (default unlimited)\n"
- << "-o: write to local file name instead of stdout\n";
+ << "-o: write to local file name instead of stdout\n"
+ << "ndn-name: NDN Name prefix for Data to be read\n";
return 1;
}
@@ -165,19 +219,22 @@
{
std::string name;
const char* outputFile = 0;
- bool verbose = false, unversioned = false;
+ bool verbose = false, versioned = false, single = false;
int interestLifetime = 4000; // in milliseconds
int timeout = 0; // in milliseconds
int opt;
- while ((opt = getopt(argc, argv, "vul:w:o:")) != -1)
+ while ((opt = getopt(argc, argv, "vsul:w:o:")) != -1)
{
switch (opt) {
case 'v':
verbose = true;
break;
+ case 's':
+ single = true;
+ break;
case 'u':
- unversioned = true;
+ versioned = true;
break;
case 'l':
try
@@ -241,7 +298,7 @@
std::ostream os(buf);
- Consumer consumer(name, os, verbose, unversioned,
+ Consumer consumer(name, os, verbose, versioned, single,
interestLifetime, timeout);
try
diff --git a/tools/ndngetfile.hpp b/tools/ndngetfile.hpp
index 26a4956..8e84440 100644
--- a/tools/ndngetfile.hpp
+++ b/tools/ndngetfile.hpp
@@ -28,13 +28,16 @@
{
public:
Consumer(const std::string& dataName, std::ostream& os,
- bool verbose, bool unversioned,
+ bool verbose, bool versioned, bool single,
int interestLifetime, int timeout,
bool mustBeFresh = false)
: m_dataName(dataName)
, m_os(os)
, m_verbose(verbose)
- , m_hasVersion(unversioned)
+ , m_hasVersion(versioned)
+ , m_isSingle(single)
+ , m_isFinished(false)
+ , m_isFirst(true)
, m_interestLifetime(interestLifetime)
, m_timeout(timeout)
, m_nextSegment(0)
@@ -52,19 +55,30 @@
fetchData(const ndn::Name& name);
void
- onData(const ndn::Interest& interest, ndn::Data& data);
+ onVersionedData(const ndn::Interest& interest, ndn::Data& data);
+
+ void
+ onUnversionedData(const ndn::Interest& interest, ndn::Data& data);
void
onTimeout(const ndn::Interest& interest);
+
+ void
+ readData(const ndn::Data& data);
+
+ void
+ fetchNextData(const ndn::Name& name, const ndn::Data& data);
private:
- static const int MAX_RETRY;
ndn::Face m_face;
ndn::Name m_dataName;
std::ostream& m_os;
bool m_verbose;
bool m_hasVersion;
+ bool m_isSingle;
+ bool m_isFinished;
+ bool m_isFirst;
ndn::time::milliseconds m_interestLifetime;
ndn::time::milliseconds m_timeout;
uint64_t m_nextSegment;
diff --git a/tools/ndnputfile.cpp b/tools/ndnputfile.cpp
index 2103eb1..ffe5519 100644
--- a/tools/ndnputfile.cpp
+++ b/tools/ndnputfile.cpp
@@ -42,7 +42,7 @@
static const uint64_t DEFAULT_INTEREST_LIFETIME = 4000;
static const uint64_t DEFAULT_FRESHNESS_PERIOD = 10000;
static const uint64_t DEFAULT_CHECK_PERIOD = 1000;
-static const size_t PRE_SIGN_DATA_COUNT = 10;
+static const size_t PRE_SIGN_DATA_COUNT = 11;
class NdnPutFile : ndn::noncopyable
{
@@ -190,7 +190,7 @@
.appendSegment(m_currentSegmentNo));
if (insertStream->peek() == std::istream::traits_type::eof()) {
- data->setFinalBlockId(ndn::name::Component::fromNumber(m_currentSegmentNo));
+ data->setFinalBlockId(ndn::name::Component::fromSegment(m_currentSegmentNo));
m_isFinished = true;
}
@@ -221,6 +221,7 @@
ndn::bind(&NdnPutFile::onRegisterSuccess, this, _1),
ndn::bind(&NdnPutFile::onRegisterFailed, this, _1, _2));
+
if (hasTimeout)
m_scheduler.scheduleEvent(timeout, ndn::bind(&NdnPutFile::stopProcess, this));
@@ -303,6 +304,11 @@
return;
}
+ if (m_isFinished) {
+ uint64_t final = m_currentSegmentNo - 1;
+ item->second->setFinalBlockId(ndn::name::Component::fromSegment(final));
+
+ }
m_face.put(*item->second);
}
@@ -330,11 +336,11 @@
throw Error("Input data does not fit into one Data packet");
}
- ndn::Data data(m_dataPrefix);
- data.setContent(buffer, readSize);
- data.setFreshnessPeriod(freshnessPeriod);
- signData(data);
- m_face.put(data);
+ ndn::shared_ptr<ndn::Data> data = ndn::make_shared<ndn::Data>(m_dataPrefix);
+ data->setContent(buffer, readSize);
+ data->setFreshnessPeriod(freshnessPeriod);
+ signData(*data);
+ m_face.put(*data);
m_isFinished = true;
}