peek: change --link-file option to expect a raw binary file

Change-Id: I952ebc55616307a939c4ecb5ae4019e27b7342bb
This commit is contained in:
Davide Pesavento committed 2019-07-29 20:40:10 -04:00
1 parent 0d4b182304
commit 50cf6db473
1 file changed
+30 -10
+30 -10
View File
@@ -31,6 +31,10 @@
#include <ndn-cxx/util/io.hpp>
#include <cerrno>
#include <cstring>
#include <fstream>
namespace ndn {
namespace peek {
@@ -45,6 +49,17 @@ usage(std::ostream& os, const std::string& program, const po::options_descriptio
<< options;
}
static std::ifstream
openBinaryFile(const std::string& filename)
{
std::ifstream file(filename, std::ios::binary);
if (!file) {
std::cerr << "ERROR: cannot open '" << filename << "' for reading: "
<< std::strerror(errno) << std::endl;
}
return file;
}
static int
main(int argc, char* argv[])
{
@@ -65,7 +80,7 @@ main(int argc, char* argv[])
interestOptDesc.add_options()
("prefix,P", po::bool_switch(&options.canBePrefix), "set CanBePrefix")
("fresh,f", po::bool_switch(&options.mustBeFresh), "set MustBeFresh")
("link-file", po::value<std::string>(), "set ForwardingHint from a file")
("link-file", po::value<std::string>(), "set ForwardingHint from a raw binary file")
("lifetime,l", po::value<time::milliseconds::rep>()->default_value(options.interestLifetime.count()),
"set InterestLifetime, in milliseconds")
;
@@ -117,12 +132,6 @@ main(int argc, char* argv[])
return 2;
}
options.interestLifetime = time::milliseconds(vm["lifetime"].as<time::milliseconds::rep>());
if (options.interestLifetime < 0_ms) {
std::cerr << "ERROR: lifetime cannot be negative" << std::endl;
return 2;
}
if (vm.count("timeout") > 0) {
options.timeout = time::milliseconds(vm["timeout"].as<time::milliseconds::rep>());
if (*options.timeout < 0_ms) {
@@ -132,11 +141,22 @@ main(int argc, char* argv[])
}
if (vm.count("link-file") > 0) {
options.link = io::load<Link>(vm["link-file"].as<std::string>());
if (options.link == nullptr) {
std::cerr << "ERROR: cannot read Link object from the specified file" << std::endl;
auto filename = vm["link-file"].as<std::string>();
std::ifstream linkFile = openBinaryFile(filename);
if (!linkFile) {
return 2;
}
options.link = io::load<Link>(linkFile, io::NO_ENCODING);
if (!options.link) {
std::cerr << "ERROR: cannot parse a valid Link object from file '" << filename << "'" << std::endl;
return 2;
}
}
options.interestLifetime = time::milliseconds(vm["lifetime"].as<time::milliseconds::rep>());
if (options.interestLifetime < 0_ms) {
std::cerr << "ERROR: lifetime cannot be negative" << std::endl;
return 2;
}
try {