blob: 3e4c0e6930b6a80ccd58e4fc7c71fa28a14929b3 [file] [log] [blame]
Junxiao Shid243d712016-08-19 06:45:31 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2016, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#include "legacy-nfdc.hpp"
27#include "face-id-fetcher.hpp"
28
29#include <boost/regex.hpp>
30
31namespace nfd {
32namespace tools {
33namespace nfdc {
34
35using ndn::nfd::ControlParameters;
36
37const time::milliseconds LegacyNfdc::DEFAULT_EXPIRATION_PERIOD = time::milliseconds::max();
38const uint64_t LegacyNfdc::DEFAULT_COST = 0;
39
40LegacyNfdc::LegacyNfdc(ndn::Face& face)
41 : m_flags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT)
42 , m_cost(DEFAULT_COST)
43 , m_origin(ndn::nfd::ROUTE_ORIGIN_STATIC)
44 , m_expires(DEFAULT_EXPIRATION_PERIOD)
45 , m_facePersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT)
46 , m_face(face)
47 , m_controller(face, m_keyChain)
48{
49}
50
51bool
52LegacyNfdc::dispatch(const std::string& command)
53{
54 if (command == "add-nexthop") {
55 if (m_nOptions != 2)
56 return false;
57 fibAddNextHop();
58 }
59 else if (command == "remove-nexthop") {
60 if (m_nOptions != 2)
61 return false;
62 fibRemoveNextHop();
63 }
64 else if (command == "register") {
65 if (m_nOptions != 2)
66 return false;
67 ribRegisterPrefix();
68 }
69 else if (command == "unregister") {
70 if (m_nOptions != 2)
71 return false;
72 ribUnregisterPrefix();
73 }
74 else if (command == "create") {
75 if (m_nOptions != 1)
76 return false;
77 faceCreate();
78 }
79 else if (command == "destroy") {
80 if (m_nOptions != 1)
81 return false;
82 faceDestroy();
83 }
84 else if (command == "set-strategy") {
85 if (m_nOptions != 2)
86 return false;
87 strategyChoiceSet();
88 }
89 else if (command == "unset-strategy") {
90 if (m_nOptions != 1)
91 return false;
92 strategyChoiceUnset();
93 }
94 else
95 return false;
96
97 return true;
98}
99
100void
101LegacyNfdc::fibAddNextHop()
102{
103 m_name = m_commandLineArguments[0];
104 const std::string& faceName = m_commandLineArguments[1];
105
106 FaceIdFetcher::start(m_face, m_controller, faceName, true,
107 [this] (const uint32_t faceId) {
108 ControlParameters parameters;
109 parameters
110 .setName(m_name)
111 .setCost(m_cost)
112 .setFaceId(faceId);
113
114 m_controller.start<ndn::nfd::FibAddNextHopCommand>(parameters,
115 bind(&LegacyNfdc::onSuccess, this, _1, "Nexthop insertion succeeded"),
Junxiao Shi29b41282016-08-22 03:47:02 +0000116 bind(&LegacyNfdc::onError, this, _1, "Nexthop insertion failed"));
Junxiao Shid243d712016-08-19 06:45:31 +0000117 },
118 bind(&LegacyNfdc::onObtainFaceIdFailure, this, _1));
119}
120
121void
122LegacyNfdc::fibRemoveNextHop()
123{
124 m_name = m_commandLineArguments[0];
125 const std::string& faceName = m_commandLineArguments[1];
126
127 FaceIdFetcher::start(m_face, m_controller, faceName, false,
128 [this] (const uint32_t faceId) {
129 ControlParameters parameters;
130 parameters
131 .setName(m_name)
132 .setFaceId(faceId);
133
134 m_controller.start<ndn::nfd::FibRemoveNextHopCommand>(parameters,
135 bind(&LegacyNfdc::onSuccess, this, _1, "Nexthop removal succeeded"),
Junxiao Shi29b41282016-08-22 03:47:02 +0000136 bind(&LegacyNfdc::onError, this, _1, "Nexthop removal failed"));
Junxiao Shid243d712016-08-19 06:45:31 +0000137 },
138 bind(&LegacyNfdc::onObtainFaceIdFailure, this, _1));
139}
140
141void
142LegacyNfdc::ribRegisterPrefix()
143{
144 m_name = m_commandLineArguments[0];
145 const std::string& faceName = m_commandLineArguments[1];
146
147 FaceIdFetcher::start(m_face, m_controller, faceName, true,
148 [this] (const uint32_t faceId) {
149 ControlParameters parameters;
150 parameters
151 .setName(m_name)
152 .setCost(m_cost)
153 .setFlags(m_flags)
154 .setOrigin(m_origin)
155 .setFaceId(faceId);
156
157 if (m_expires != DEFAULT_EXPIRATION_PERIOD)
158 parameters.setExpirationPeriod(m_expires);
159
160 m_controller.start<ndn::nfd::RibRegisterCommand>(parameters,
161 bind(&LegacyNfdc::onSuccess, this, _1, "Successful in name registration"),
Junxiao Shi29b41282016-08-22 03:47:02 +0000162 bind(&LegacyNfdc::onError, this, _1, "Failed in name registration"));
Junxiao Shid243d712016-08-19 06:45:31 +0000163 },
164 bind(&LegacyNfdc::onObtainFaceIdFailure, this, _1));
165}
166
167void
168LegacyNfdc::ribUnregisterPrefix()
169{
170 m_name = m_commandLineArguments[0];
171 const std::string& faceName = m_commandLineArguments[1];
172
173 FaceIdFetcher::start(m_face, m_controller, faceName, false,
174 [this] (const uint32_t faceId) {
175 ControlParameters parameters;
176 parameters
177 .setName(m_name)
178 .setFaceId(faceId)
179 .setOrigin(m_origin);
180
181 m_controller.start<ndn::nfd::RibUnregisterCommand>(parameters,
182 bind(&LegacyNfdc::onSuccess, this, _1, "Successful in unregistering name"),
Junxiao Shi29b41282016-08-22 03:47:02 +0000183 bind(&LegacyNfdc::onError, this, _1, "Failed in unregistering name"));
Junxiao Shid243d712016-08-19 06:45:31 +0000184 },
185 bind(&LegacyNfdc::onObtainFaceIdFailure, this, _1));
186}
187
188void
189LegacyNfdc::onCanonizeFailure(const std::string& reason)
190{
191 BOOST_THROW_EXCEPTION(Error(reason));
192}
193
194void
195LegacyNfdc::onObtainFaceIdFailure(const std::string& message)
196{
197 BOOST_THROW_EXCEPTION(Error(message));
198}
199
200void
201LegacyNfdc::faceCreate()
202{
203 boost::regex e("^[a-z0-9]+\\:.*");
204 if (!boost::regex_match(m_commandLineArguments[0], e))
205 BOOST_THROW_EXCEPTION(Error("invalid uri format"));
206
207 FaceUri faceUri;
208 faceUri.parse(m_commandLineArguments[0]);
209
210 faceUri.canonize(bind(&LegacyNfdc::startFaceCreate, this, _1),
211 bind(&LegacyNfdc::onCanonizeFailure, this, _1),
212 m_face.getIoService(), time::seconds(4));
213}
214
215void
216LegacyNfdc::startFaceCreate(const FaceUri& canonicalUri)
217{
218 ControlParameters parameters;
219 parameters.setUri(canonicalUri.toString());
220 parameters.setFacePersistency(m_facePersistency);
221
222 m_controller.start<ndn::nfd::FaceCreateCommand>(parameters,
223 bind(&LegacyNfdc::onSuccess, this, _1, "Face creation succeeded"),
Junxiao Shi29b41282016-08-22 03:47:02 +0000224 bind(&LegacyNfdc::onError, this, _1, "Face creation failed"));
Junxiao Shid243d712016-08-19 06:45:31 +0000225}
226
227void
228LegacyNfdc::faceDestroy()
229{
230 ControlParameters parameters;
231 const std::string& faceName = m_commandLineArguments[0];
232
233 FaceIdFetcher::start(m_face, m_controller, faceName, false,
234 [this] (const uint32_t faceId) {
235 ControlParameters faceParameters;
236 faceParameters.setFaceId(faceId);
237
238 m_controller.start<ndn::nfd::FaceDestroyCommand>(faceParameters,
239 bind(&LegacyNfdc::onSuccess, this, _1, "Face destroy succeeded"),
Junxiao Shi29b41282016-08-22 03:47:02 +0000240 bind(&LegacyNfdc::onError, this, _1, "Face destroy failed"));
Junxiao Shid243d712016-08-19 06:45:31 +0000241 },
242 bind(&LegacyNfdc::onObtainFaceIdFailure, this, _1));
243}
244
245void
246LegacyNfdc::strategyChoiceSet()
247{
248 const std::string& name = m_commandLineArguments[0];
249 const std::string& strategy = m_commandLineArguments[1];
250
251 ControlParameters parameters;
252 parameters
253 .setName(name)
254 .setStrategy(strategy);
255
256 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
257 bind(&LegacyNfdc::onSuccess, this, _1, "Successfully set strategy choice"),
Junxiao Shi29b41282016-08-22 03:47:02 +0000258 bind(&LegacyNfdc::onError, this, _1, "Failed to set strategy choice"));
Junxiao Shid243d712016-08-19 06:45:31 +0000259}
260
261void
262LegacyNfdc::strategyChoiceUnset()
263{
264 const std::string& name = m_commandLineArguments[0];
265
266 ControlParameters parameters;
267 parameters.setName(name);
268
269 m_controller.start<ndn::nfd::StrategyChoiceUnsetCommand>(parameters,
270 bind(&LegacyNfdc::onSuccess, this, _1, "Successfully unset strategy choice"),
Junxiao Shi29b41282016-08-22 03:47:02 +0000271 bind(&LegacyNfdc::onError, this, _1, "Failed to unset strategy choice"));
Junxiao Shid243d712016-08-19 06:45:31 +0000272}
273
274void
275LegacyNfdc::onSuccess(const ControlParameters& commandSuccessResult, const std::string& message)
276{
277 std::cout << message << ": " << commandSuccessResult << std::endl;
278}
279
280void
Junxiao Shi29b41282016-08-22 03:47:02 +0000281LegacyNfdc::onError(const ndn::nfd::ControlResponse& response, const std::string& message)
Junxiao Shid243d712016-08-19 06:45:31 +0000282{
283 std::ostringstream os;
Junxiao Shi29b41282016-08-22 03:47:02 +0000284 os << message << ": " << response.getText() << " (code: " << response.getCode() << ")";
Junxiao Shid243d712016-08-19 06:45:31 +0000285 BOOST_THROW_EXCEPTION(Error(os.str()));
286}
287
288} // namespace nfdc
289} // namespace tools
290} // namespace nfd