Remove dependency on Selectors and refactor codebase.
Change-Id: Ic3024b76ba0eea61f790c91c36090b4aa68702a3
Refs: #4522
diff --git a/tools/ndngetfile.cpp b/tools/ndngetfile.cpp
index 9764ca6..559541c 100644
--- a/tools/ndngetfile.cpp
+++ b/tools/ndngetfile.cpp
@@ -47,14 +47,15 @@
}
else {
interest.setMustBeFresh(true);
- interest.setChildSelector(1);
}
+ interest.setCanBePrefix(m_canBePrefix);
+
m_face.expressInterest(interest,
- m_hasVersion ? bind(&Consumer::onVersionedData, this, _1, _2)
- : bind(&Consumer::onUnversionedData, this, _1, _2),
- bind(&Consumer::onTimeout, this, _1), // Nack
- bind(&Consumer::onTimeout, this, _1));
+ m_hasVersion ? std::bind(&Consumer::onVersionedData, this, _1, _2)
+ : std::bind(&Consumer::onUnversionedData, this, _1, _2),
+ std::bind(&Consumer::onTimeout, this, _1), // Nack
+ std::bind(&Consumer::onTimeout, this, _1));
}
void
@@ -112,7 +113,6 @@
Consumer::onUnversionedData(const Interest& interest, const 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;
@@ -155,7 +155,7 @@
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: 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;
}
@@ -188,7 +188,7 @@
// Retransmit the interest
fetchData(interest.getName());
if (m_verbose) {
- std::cerr << "TIMEOUT: retransmit interest for " << interest.getName() << std::endl;
+ std::cerr << "TIMEOUT: retransmit interest for " << interest.getName() << std::endl;
}
}
else {
@@ -240,7 +240,7 @@
interestLifetime = boost::lexical_cast<int>(optarg);
}
catch (const boost::bad_lexical_cast&) {
- std::cerr << "ERROR: -l option should be an integer." << std::endl;
+ std::cerr << "ERROR: -l option should be an integer" << std::endl;
return 1;
}
interestLifetime = std::max(interestLifetime, 0);
@@ -250,7 +250,7 @@
timeout = boost::lexical_cast<int>(optarg);
}
catch (const boost::bad_lexical_cast&) {
- std::cerr << "ERROR: -w option should be an integer." << std::endl;
+ std::cerr << "ERROR: -w option should be an integer" << std::endl;
return 1;
}
timeout = std::max(timeout, 0);
diff --git a/tools/ndngetfile.hpp b/tools/ndngetfile.hpp
index e7e851b..92cf799 100644
--- a/tools/ndngetfile.hpp
+++ b/tools/ndngetfile.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014-2017, Regents of the University of California.
+ * Copyright (c) 2014-2018, Regents of the University of California.
*
* This file is part of NDN repo-ng (Next generation of NDN repository).
* See AUTHORS.md for complete list of repo-ng authors and contributors.
@@ -30,7 +30,8 @@
Consumer(const std::string& dataName, std::ostream& os,
bool verbose, bool versioned, bool single,
int interestLifetime, int timeout,
- bool mustBeFresh = false)
+ bool mustBeFresh = false,
+ bool canBePrefix = false)
: m_dataName(dataName)
, m_os(os)
, m_verbose(verbose)
@@ -44,6 +45,7 @@
, m_totalSize(0)
, m_retryCount(0)
, m_mustBeFresh(mustBeFresh)
+ , m_canBePrefix(canBePrefix)
{
}
@@ -85,6 +87,7 @@
int m_totalSize;
int m_retryCount;
bool m_mustBeFresh;
+ bool m_canBePrefix;
};
} // namespace repo
diff --git a/tools/ndnputfile.cpp b/tools/ndnputfile.cpp
index b4f40f4..97dbd77 100644
--- a/tools/ndnputfile.cpp
+++ b/tools/ndnputfile.cpp
@@ -33,9 +33,9 @@
#include <iostream>
#include <string>
+#include <boost/asio.hpp>
#include <boost/filesystem.hpp>
#include <boost/lexical_cast.hpp>
-#include <boost/asio.hpp>
#include <boost/iostreams/operations.hpp>
#include <boost/iostreams/read.hpp>
@@ -161,7 +161,7 @@
bool m_isFinished;
ndn::Name m_dataPrefix;
- typedef std::map<uint64_t, shared_ptr<ndn::Data> > DataContainer;
+ using DataContainer = std::map<uint64_t, shared_ptr<ndn::Data>>;
DataContainer m_data;
ndn::security::CommandInterestSigner m_cmdSigner;
};
@@ -265,7 +265,6 @@
NdnPutFile::onInsertCommandResponse(const ndn::Interest& interest, const ndn::Data& data)
{
RepoCommandResponse response(data.getContent().blockFromValue());
- //int statusCode = response.getStatusCode();
int statusCode = response.getCode();
if (statusCode >= 400) {
BOOST_THROW_EXCEPTION(Error("insert command failed with code " +
@@ -503,7 +502,7 @@
ndnPutFile.freshnessPeriod = milliseconds(boost::lexical_cast<uint64_t>(optarg));
}
catch (const boost::bad_lexical_cast&) {
- std::cerr << "-x option should be an integer.";
+ std::cerr << "-x option should be an integer" << std::endl;;
return 1;
}
break;
@@ -512,7 +511,7 @@
ndnPutFile.interestLifetime = milliseconds(boost::lexical_cast<uint64_t>(optarg));
}
catch (const boost::bad_lexical_cast&) {
- std::cerr << "-l option should be an integer.";
+ std::cerr << "-l option should be an integer" << std::endl;;
return 1;
}
break;
@@ -522,7 +521,7 @@
ndnPutFile.timeout = milliseconds(boost::lexical_cast<uint64_t>(optarg));
}
catch (const boost::bad_lexical_cast&) {
- std::cerr << "-w option should be an integer.";
+ std::cerr << "-w option should be an integer" << std::endl;;
return 1;
}
break;
@@ -543,10 +542,9 @@
if (argc != 3)
usage();
- ndnPutFile.repoPrefix = Name(argv[0]); std::cout << "Repo prefix: " << argv[0] << std::endl;
- ndnPutFile.ndnName = Name(argv[1]); std::cout << "NDN name: " << argv[1] << std::endl;
+ ndnPutFile.repoPrefix = Name(argv[0]);
+ ndnPutFile.ndnName = Name(argv[1]);
if (strcmp(argv[2], "-") == 0) {
-
ndnPutFile.insertStream = &std::cin;
ndnPutFile.run();
}
diff --git a/tools/ndnrepowatch.cpp b/tools/ndnrepowatch.cpp
deleted file mode 100644
index b844c00..0000000
--- a/tools/ndnrepowatch.cpp
+++ /dev/null
@@ -1,420 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/*
- * Copyright (c) 2014-2018, Regents of the University of California.
- *
- * This file is part of NDN repo-ng (Next generation of NDN repository).
- * See AUTHORS.md for complete list of repo-ng authors and contributors.
- *
- * repo-ng 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.
- *
- * repo-ng 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
- * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include "../src/repo-command-parameter.hpp"
-#include "../src/repo-command-response.hpp"
-
-#include <ndn-cxx/face.hpp>
-#include <ndn-cxx/security/command-interest-signer.hpp>
-#include <ndn-cxx/security/key-chain.hpp>
-#include <ndn-cxx/security/signing-helpers.hpp>
-
-#include <boost/asio/io_service.hpp>
-#include <ndn-cxx/util/scheduler.hpp>
-
-#include <stdint.h>
-#include <stdlib.h>
-
-#include <fstream>
-#include <iostream>
-#include <string>
-
-#include <boost/asio/io_service.hpp>
-#include <boost/lexical_cast.hpp>
-
-namespace repo {
-
-using namespace ndn::time;
-
-using std::shared_ptr;
-using std::bind;
-using std::placeholders::_1;
-using std::placeholders::_2;
-
-static const uint64_t DEFAULT_INTEREST_LIFETIME = 4000;
-static const uint64_t DEFAULT_FRESHNESS_PERIOD = 10000;
-static const uint64_t DEFAULT_CHECK_PERIOD = 1000;
-
-enum CommandType
-{
- START,
- CHECK,
- STOP
-};
-
-class NdnRepoWatch : boost::noncopyable
-{
-public:
- class Error : public std::runtime_error
- {
- public:
- explicit
- Error(const std::string& what)
- : std::runtime_error(what)
- {
- }
- };
-
- NdnRepoWatch()
- : freshnessPeriod(DEFAULT_FRESHNESS_PERIOD)
- , interestLifetime(DEFAULT_INTEREST_LIFETIME)
- , hasTimeout(false)
- , watchTimeout(0)
- , hasMaxInterestNum(false)
- , maxInterestNum(0)
- , status(START)
- , isVerbose(false)
- , m_scheduler(m_face.getIoService())
- , m_checkPeriod(DEFAULT_CHECK_PERIOD)
- , m_cmdSigner(m_keyChain)
- {
- }
-
- void
- run();
-
-private:
-
- void
- startWatchCommand();
-
- void
- onWatchCommandResponse(const ndn::Interest& interest, const ndn::Data& data);
-
- void
- onWatchCommandTimeout(const ndn::Interest& interest);
-
- void
- stopProcess();
-
- void
- signData(ndn::Data& data);
-
- void
- startCheckCommand();
-
- void
- onCheckCommandTimeout(const ndn::Interest& interest);
-
- void
- onStopCommandResponse(const ndn::Interest& interest, ndn::Data& data);
-
- void
- onStopCommandTimeout(const ndn::Interest& interest);
-
- ndn::Interest
- generateCommandInterest(const ndn::Name& commandPrefix, const std::string& command,
- const RepoCommandParameter& commandParameter);
-
-public:
- std::string identityForCommand;
- milliseconds freshnessPeriod;
- milliseconds interestLifetime;
- bool hasTimeout;
- milliseconds watchTimeout;
- bool hasMaxInterestNum;
- int64_t maxInterestNum;
- CommandType status;
- ndn::Name repoPrefix;
- ndn::Name ndnName;
- bool isVerbose;
-
-
-private:
- ndn::Face m_face;
- ndn::Scheduler m_scheduler;
- milliseconds m_checkPeriod;
-
- ndn::Name m_dataPrefix;
- ndn::KeyChain m_keyChain;
- typedef std::map<uint64_t, shared_ptr<ndn::Data> > DataContainer;
- ndn::security::CommandInterestSigner m_cmdSigner;
-};
-
-void
-NdnRepoWatch::run()
-{
- m_dataPrefix = ndnName;
- startWatchCommand();
-
- if (hasTimeout)
- m_scheduler.scheduleEvent(watchTimeout, bind(&NdnRepoWatch::stopProcess, this));
-
- m_face.processEvents();
-}
-
-void
-NdnRepoWatch::startWatchCommand()
-{
- RepoCommandParameter parameters;
- parameters.setName(m_dataPrefix);
-
- repoPrefix.append("watch");
- if (status == START) {
- if (hasMaxInterestNum) {
- parameters.setMaxInterestNum(maxInterestNum);
- }
- if (hasTimeout) {
- parameters.setWatchTimeout(watchTimeout);
- }
- ndn::Interest commandInterest = generateCommandInterest(repoPrefix, "start", parameters);
- m_face.expressInterest(commandInterest,
- bind(&NdnRepoWatch::onWatchCommandResponse, this,
- _1, _2),
- bind(&NdnRepoWatch::onWatchCommandTimeout, this, _1), // Nack
- bind(&NdnRepoWatch::onWatchCommandTimeout, this, _1));
- }
- else if (status == STOP){
- ndn::Interest commandInterest = generateCommandInterest(repoPrefix, "stop", parameters);
- m_face.expressInterest(commandInterest,
- bind(&NdnRepoWatch::onWatchCommandResponse, this,
- _1, _2),
- bind(&NdnRepoWatch::onWatchCommandTimeout, this, _1), // Nack
- bind(&NdnRepoWatch::onWatchCommandTimeout, this, _1));
- }
- else if (status == CHECK){
- ndn::Interest commandInterest = generateCommandInterest(repoPrefix, "check", parameters);
- m_face.expressInterest(commandInterest,
- bind(&NdnRepoWatch::onWatchCommandResponse, this,
- _1, _2),
- bind(&NdnRepoWatch::onWatchCommandTimeout, this, _1), // Nack
- bind(&NdnRepoWatch::onWatchCommandTimeout, this, _1));
- }
-
-}
-
-void
-NdnRepoWatch::onWatchCommandResponse(const ndn::Interest& interest, const ndn::Data& data)
-{
- RepoCommandResponse response(data.getContent().blockFromValue());
- int statusCode = response.getCode();
- if (statusCode >= 400) {
- BOOST_THROW_EXCEPTION(Error("Watch command failed with code " +
- boost::lexical_cast<std::string>(statusCode)));
- }
- else if (statusCode == 101) {
- std::cerr << "Watching prefix is stopped!" <<std::endl;
- m_face.getIoService().stop();
- return;
- }
- else if (statusCode == 300) {
- std::cerr << "Watching prefix is running!" <<std::endl;
- m_scheduler.scheduleEvent(m_checkPeriod,
- bind(&NdnRepoWatch::startCheckCommand, this));
- return;
- }
- else if (statusCode == 100) {
- std::cerr << "Watching prefix starts!" <<std::endl;
- m_scheduler.scheduleEvent(m_checkPeriod,
- bind(&NdnRepoWatch::startCheckCommand, this));
- return;
- }
- else {
- BOOST_THROW_EXCEPTION(Error("Unrecognized Status Code " +
- boost::lexical_cast<std::string>(statusCode)));
- }
-}
-
-void
-NdnRepoWatch::onWatchCommandTimeout(const ndn::Interest& interest)
-{
- BOOST_THROW_EXCEPTION(Error("command response timeout"));
-}
-
-void
-NdnRepoWatch::stopProcess()
-{
- m_face.getIoService().stop();
-}
-
-void
-NdnRepoWatch::startCheckCommand()
-{
- repoPrefix.append("watch");
- ndn::Interest checkInterest = generateCommandInterest(repoPrefix, "check",
- RepoCommandParameter()
- .setName(m_dataPrefix));
- m_face.expressInterest(checkInterest,
- bind(&NdnRepoWatch::onWatchCommandResponse, this, _1, _2),
- bind(&NdnRepoWatch::onCheckCommandTimeout, this, _1), // Nack
- bind(&NdnRepoWatch::onCheckCommandTimeout, this, _1));
-}
-
-void
-NdnRepoWatch::onCheckCommandTimeout(const ndn::Interest& interest)
-{
- BOOST_THROW_EXCEPTION(Error("check response timeout"));
-}
-
-void
-NdnRepoWatch::onStopCommandResponse(const ndn::Interest& interest, ndn::Data& data)
-{
- RepoCommandResponse response(data.getContent().blockFromValue());
- int statusCode = response.getCode();
- if (statusCode != 101) {
- BOOST_THROW_EXCEPTION(Error("Watch stop command failed with code: " +
- boost::lexical_cast<std::string>(statusCode)));
- }
- else {
- std::cerr << "Status code is 101. Watching prefix is stopped successfully!" << std::endl;
- m_face.getIoService().stop();
- return;
- }
-}
-
-void
-NdnRepoWatch::onStopCommandTimeout(const ndn::Interest& interest)
-{
- BOOST_THROW_EXCEPTION(Error("stop response timeout"));
-}
-
-ndn::Interest
-NdnRepoWatch::generateCommandInterest(const ndn::Name& commandPrefix, const std::string& command,
- const RepoCommandParameter& commandParameter)
-{
- Name cmd = commandPrefix;
- cmd
- .append(command)
- .append(commandParameter.wireEncode());
- ndn::Interest interest;
-
- if (identityForCommand.empty())
- interest = m_cmdSigner.makeCommandInterest(cmd);
- else {
- interest = m_cmdSigner.makeCommandInterest(cmd, ndn::signingByIdentity(identityForCommand));
- }
-
- interest.setInterestLifetime(interestLifetime);
- return interest;
-}
-
-static void
-usage()
-{
- fprintf(stderr,
- "NdnRepoWatch [-I identity]"
- " [-x freshness] [-l lifetime] [-w watchtimeout]"
- " [-n maxinterestnum][-s stop] [-c check]repo-prefix ndn-name\n"
- "\n"
- " Write a file into a repo.\n"
- " -I: specify identity used for signing commands\n"
- " -x: FreshnessPeriod in milliseconds\n"
- " -l: InterestLifetime in milliseconds for each command\n"
- " -w: timeout in milliseconds for whole process (default unlimited)\n"
- " -n: total number of interests to be sent for whole process (default unlimited)\n"
- " -s: stop the whole process\n"
- " -c: check the process\n"
- " repo-prefix: repo command prefix\n"
- " ndn-name: NDN Name prefix for written Data\n"
- );
- exit(1);
-}
-
-int
-main(int argc, char** argv)
-{
- NdnRepoWatch app;
- int opt;
- while ((opt = getopt(argc, argv, "x:l:w:n:scI:")) != -1) {
- switch (opt) {
- case 'x':
- try {
- app.freshnessPeriod = milliseconds(boost::lexical_cast<uint64_t>(optarg));
- }
- catch (const boost::bad_lexical_cast&) {
- std::cerr << "-x option should be an integer.";
- return 1;
- }
- break;
- case 'l':
- try {
- app.interestLifetime = milliseconds(boost::lexical_cast<uint64_t>(optarg));
- }
- catch (const boost::bad_lexical_cast&) {
- std::cerr << "-l option should be an integer.";
- return 1;
- }
- break;
- case 'w':
- app.hasTimeout = true;
- try {
- app.watchTimeout = milliseconds(boost::lexical_cast<int64_t>(optarg));
- }
- catch (const boost::bad_lexical_cast&) {
- std::cerr << "-w option should be an integer.";
- return 1;
- }
- break;
- case 'n':
- app.hasMaxInterestNum = true;
- try {
- app.maxInterestNum = boost::lexical_cast<int64_t>(optarg);
- }
- catch (const boost::bad_lexical_cast&) {
- std::cerr << "-n option should be an integer.";
- return 1;
- }
- break;
- case 's':
- app.status = STOP;
- break;
- case 'c':
- app.status = CHECK;
- break;
- case 'I':
- app.identityForCommand = std::string(optarg);
- break;
- case 'v':
- app.isVerbose = true;
- break;
- case 'h':
- usage();
- break;
- default:
- break;
- }
- }
-
- argc -= optind;
- argv += optind;
-
- if (argc != 2)
- usage();
-
- app.repoPrefix = Name(argv[0]);
- app.ndnName = Name(argv[1]);
-
- app.run();
-
- return 0;
-}
-
-} // namespace repo
-
-int
-main(int argc, char** argv)
-{
- try {
- return repo::main(argc, argv);
- }
- catch (const std::exception& e) {
- std::cerr << "ERROR: " << e.what() << std::endl;
- return 2;
- }
-}
diff --git a/tools/repo-ng-ls.cpp b/tools/repo-ng-ls.cpp
index 1ce0b28..637b03f 100644
--- a/tools/repo-ng-ls.cpp
+++ b/tools/repo-ng-ls.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2017, Regents of the University of California.
+ * Copyright (c) 2014-2018, Regents of the University of California.
*
* This file is part of NDN repo-ng (Next generation of NDN repository).
* See AUTHORS.md for complete list of repo-ng authors and contributors.
@@ -23,6 +23,8 @@
#include <iostream>
#include <string>
+#include <ndn-cxx/util/sqlite3-statement.hpp>
+
#include <boost/property_tree/info_parser.hpp>
#include <boost/property_tree/ptree.hpp>
@@ -30,8 +32,6 @@
namespace repo {
-using std::string;
-
static void
printUsage(const char* programName)
{
@@ -80,20 +80,19 @@
RepoEnumerator::RepoEnumerator(const std::string& configFile)
{
readConfig(configFile);
- char* errMsg = 0;
- int rc = sqlite3_open_v2(m_dbPath.c_str(), &m_db,
- SQLITE_OPEN_READONLY,
+ char* errMsg = nullptr;
+ int rc = sqlite3_open_v2(m_dbPath.c_str(), &m_db, SQLITE_OPEN_READONLY,
#ifdef DISABLE_SQLITE3_FS_LOCKING
- "unix-dotfile"
+ "unix-dotfile"
#else
- 0
+ nullptr
#endif
);
if (rc != SQLITE_OK) {
BOOST_THROW_EXCEPTION(Error("Database file open failure"));
}
- sqlite3_exec(m_db, "PRAGMA synchronous = OFF", 0, 0, &errMsg);
- sqlite3_exec(m_db, "PRAGMA journal_mode = WAL", 0, 0, &errMsg);
+ sqlite3_exec(m_db, "PRAGMA synchronous = OFF", nullptr, nullptr, &errMsg);
+ sqlite3_exec(m_db, "PRAGMA journal_mode = WAL", nullptr, nullptr, &errMsg);
}
void
@@ -123,49 +122,34 @@
uint64_t
RepoEnumerator::enumerate(bool showImplicitDigest)
{
- sqlite3_stmt* m_stmt = 0;
- int rc = SQLITE_DONE;
- string sql = string("SELECT id, name, keylocatorHash FROM NDN_REPO;");
- rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &m_stmt, 0);
- if (rc != SQLITE_OK)
- BOOST_THROW_EXCEPTION(Error("Initiation Read Entries from Database Prepare error"));
- uint64_t entryNumber = 0;
+ ndn::util::Sqlite3Statement stmt(m_db, "SELECT data FROM NDN_REPO_V2;");
+ uint64_t nEntries = 0;
while (true) {
- rc = sqlite3_step(m_stmt);
+ int rc = stmt.step();
if (rc == SQLITE_ROW) {
- Name name;
- name.wireDecode(Block(reinterpret_cast<const uint8_t*>(sqlite3_column_blob(m_stmt, 1)),
- sqlite3_column_bytes(m_stmt, 1)));
- try {
- if (showImplicitDigest) {
- std::cout << name << std::endl;
- }
- else {
- std::cout << name.getPrefix(-1) << std::endl;
- }
+ Data data(stmt.getBlock(0));
+ if (showImplicitDigest) {
+ std::cout << data.getFullName() << std::endl;
}
- catch (...){
- sqlite3_finalize(m_stmt);
- throw;
+ else {
+ std::cout << data.getName() << std::endl;
}
- entryNumber++;
+ nEntries++;
}
else if (rc == SQLITE_DONE) {
- sqlite3_finalize(m_stmt);
break;
}
else {
- sqlite3_finalize(m_stmt);
BOOST_THROW_EXCEPTION(Error("Initiation Read Entries error"));
}
}
- return entryNumber;
+ return nEntries;
}
int
main(int argc, char** argv)
{
- string configPath = DEFAULT_CONFIG_FILE;
+ std::string configPath = DEFAULT_CONFIG_FILE;
bool showImplicitDigest = true;
int opt;
while ((opt = getopt(argc, argv, "hc:n")) != -1) {
@@ -174,7 +158,7 @@
printUsage(argv[0]);
return 0;
case 'c':
- configPath = string(optarg);
+ configPath = std::string(optarg);
break;
case 'n':
showImplicitDigest = false;