blob: c877eaaee33e922b0f0d0fe5942b9b586c83b49b [file] [log] [blame]
Andrea Tosatto672b9a72016-01-05 16:18:20 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2016, Regents of the University of California,
4 * Colorado State University,
5 * University Pierre & Marie Curie, Sorbonne University.
6 *
7 * This file is part of ndn-tools (Named Data Networking Essential Tools).
8 * See AUTHORS.md for complete list of ndn-tools authors and contributors.
9 *
10 * ndn-tools is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
20 *
21 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
22 *
23 * @author Wentao Shang
24 * @author Steve DiBenedetto
25 * @author Andrea Tosatto
Weiwei Liue4765012016-06-01 00:10:29 -070026 * @author Davide Pesavento
27 * @author Weiwei Liu
Andrea Tosatto672b9a72016-01-05 16:18:20 +010028 */
29
30#include "core/version.hpp"
31#include "options.hpp"
32#include "consumer.hpp"
33#include "discover-version-fixed.hpp"
34#include "discover-version-iterative.hpp"
Weiwei Liue4765012016-06-01 00:10:29 -070035#include "pipeline-interests-fixed-window.hpp"
Andrea Tosatto672b9a72016-01-05 16:18:20 +010036
37#include <ndn-cxx/security/validator-null.hpp>
38
39namespace ndn {
40namespace chunks {
41
42static int
43main(int argc, char** argv)
44{
45 std::string programName(argv[0]);
46 Options options;
47 std::string discoverType("fixed");
Weiwei Liue4765012016-06-01 00:10:29 -070048 std::string pipelineType("fixed");
Andrea Tosatto672b9a72016-01-05 16:18:20 +010049 size_t maxPipelineSize(1);
50 int maxRetriesAfterVersionFound(1);
51 std::string uri;
52
53 namespace po = boost::program_options;
54 po::options_description visibleDesc("Options");
55 visibleDesc.add_options()
56 ("help,h", "print this help message and exit")
57 ("discover-version,d", po::value<std::string>(&discoverType)->default_value(discoverType),
58 "version discovery algorithm to use; valid values are: 'fixed', 'iterative'")
59 ("fresh,f", po::bool_switch(&options.mustBeFresh), "only return fresh content")
60 ("lifetime,l", po::value<uint64_t>()->default_value(options.interestLifetime.count()),
61 "lifetime of expressed Interests, in milliseconds")
62 ("pipeline,p", po::value<size_t>(&maxPipelineSize)->default_value(maxPipelineSize),
63 "maximum size of the Interest pipeline")
64 ("retries,r", po::value<int>(&options.maxRetriesOnTimeoutOrNack)->default_value(options.maxRetriesOnTimeoutOrNack),
65 "maximum number of retries in case of Nack or timeout (-1 = no limit)")
66 ("retries-iterative,i", po::value<int>(&maxRetriesAfterVersionFound)->default_value(maxRetriesAfterVersionFound),
67 "number of timeouts that have to occur in order to confirm a discovered Data "
68 "version as the latest one (only applicable to 'iterative' version discovery)")
69 ("verbose,v", po::bool_switch(&options.isVerbose), "turn on verbose output")
70 ("version,V", "print program version and exit")
71 ;
72
73 po::options_description hiddenDesc("Hidden options");
74 hiddenDesc.add_options()
75 ("ndn-name,n", po::value<std::string>(&uri), "NDN name of the requested content");
76
77 po::positional_options_description p;
78 p.add("ndn-name", -1);
79
80 po::options_description optDesc("Allowed options");
81 optDesc.add(visibleDesc).add(hiddenDesc);
82
83 po::variables_map vm;
84 try {
85 po::store(po::command_line_parser(argc, argv).options(optDesc).positional(p).run(), vm);
86 po::notify(vm);
87 }
88 catch (const po::error& e) {
89 std::cerr << "ERROR: " << e.what() << std::endl;
90 return 2;
91 }
92 catch (const boost::bad_any_cast& e) {
93 std::cerr << "ERROR: " << e.what() << std::endl;
94 return 2;
95 }
96
97 if (vm.count("help") > 0) {
98 std::cout << "Usage: " << programName << " [options] ndn:/name" << std::endl;
99 std::cout << visibleDesc;
100 return 0;
101 }
102
103 if (vm.count("version") > 0) {
104 std::cout << "ndncatchunks " << tools::VERSION << std::endl;
105 return 0;
106 }
107
108 if (vm.count("ndn-name") == 0) {
109 std::cerr << "Usage: " << programName << " [options] ndn:/name" << std::endl;
110 std::cerr << visibleDesc;
111 return 2;
112 }
113
114 Name prefix(uri);
115 if (discoverType == "fixed" && (prefix.empty() || !prefix[-1].isVersion())) {
116 std::cerr << "ERROR: The specified name must contain a version component when using "
117 "fixed version discovery" << std::endl;
118 return 2;
119 }
120
121 if (maxPipelineSize < 1 || maxPipelineSize > 1024) {
122 std::cerr << "ERROR: pipeline size must be between 1 and 1024" << std::endl;
123 return 2;
124 }
125
126 if (options.maxRetriesOnTimeoutOrNack < -1 || options.maxRetriesOnTimeoutOrNack > 1024) {
127 std::cerr << "ERROR: retries value must be between -1 and 1024" << std::endl;
128 return 2;
129 }
130
131 if (maxRetriesAfterVersionFound < 0 || maxRetriesAfterVersionFound > 1024) {
132 std::cerr << "ERROR: retries iterative value must be between 0 and 1024" << std::endl;
133 return 2;
134 }
135
136 options.interestLifetime = time::milliseconds(vm["lifetime"].as<uint64_t>());
137
138 try {
139 Face face;
140
141 unique_ptr<DiscoverVersion> discover;
142 if (discoverType == "fixed") {
143 discover = make_unique<DiscoverVersionFixed>(prefix, face, options);
144 }
145 else if (discoverType == "iterative") {
146 DiscoverVersionIterative::Options optionsIterative(options);
147 optionsIterative.maxRetriesAfterVersionFound = maxRetriesAfterVersionFound;
148 discover = make_unique<DiscoverVersionIterative>(prefix, face, optionsIterative);
149 }
150 else {
151 std::cerr << "ERROR: discover version type not valid" << std::endl;
152 return 2;
153 }
154
Weiwei Liue4765012016-06-01 00:10:29 -0700155 unique_ptr<PipelineInterests> pipeline;
156 if (pipelineType == "fixed") {
157 PipelineInterestsFixedWindow::Options optionsPipeline(options);
158 optionsPipeline.maxPipelineSize = maxPipelineSize;
159 pipeline = make_unique<PipelineInterestsFixedWindow>(face, optionsPipeline);
160 }
161 else {
162 std::cerr << "ERROR: Interest pipeline type not valid" << std::endl;
163 return 2;
164 }
165
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100166 ValidatorNull validator;
Weiwei Liu05d92092016-07-19 17:34:33 -0700167 Consumer consumer(validator, options.isVerbose);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100168
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100169 BOOST_ASSERT(discover != nullptr);
Weiwei Liue4765012016-06-01 00:10:29 -0700170 BOOST_ASSERT(pipeline != nullptr);
171 consumer.run(std::move(discover), std::move(pipeline));
Weiwei Liu05d92092016-07-19 17:34:33 -0700172 face.processEvents();
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100173 }
174 catch (const Consumer::ApplicationNackError& e) {
175 std::cerr << "ERROR: " << e.what() << std::endl;
176 return 3;
177 }
178 catch (const std::exception& e) {
179 std::cerr << "ERROR: " << e.what() << std::endl;
180 return 1;
181 }
182
183 return 0;
184}
185
186} // namespace chunks
187} // namespace ndn
188
189int
190main(int argc, char** argv)
191{
192 return ndn::chunks::main(argc, argv);
193}