blob: 3928ae5e7713b7d76a7af318f32f433c612ec2a5 [file] [log] [blame]
hilata198cadb2014-02-15 23:46:19 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Syed Obaid4ae0ce32014-06-17 13:59:20 -05003 * Copyright (c) 2014, 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
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Syed Obaid4ae0ce32014-06-17 13:59:20 -050024 */
hilata198cadb2014-02-15 23:46:19 -060025
26#ifndef NFD_TOOLS_NFDC_HPP
27#define NFD_TOOLS_NFDC_HPP
28
Alexander Afanasyev4a771362014-04-24 21:29:33 -070029#include <ndn-cxx/face.hpp>
Junxiao Shi8e273ca2014-11-12 00:42:29 -070030#include <ndn-cxx/security/key-chain.hpp>
Syed Obaid4ae0ce32014-06-17 13:59:20 -050031#include <ndn-cxx/util/time.hpp>
Alexander Afanasyev4a771362014-04-24 21:29:33 -070032#include <ndn-cxx/management/nfd-controller.hpp>
Chengyu Fandae25302014-10-16 11:40:11 -060033#include <ndn-cxx/util/face-uri.hpp>
Muktadir R Chowdhury6c4a67a2015-09-07 22:13:01 -050034#include <ndn-cxx/security/validator-null.hpp>
Chengyu Fan27be0b02014-11-24 20:52:53 -070035#include <memory>
hilata198cadb2014-02-15 23:46:19 -060036
37namespace nfdc {
Alexander Afanasyev352e14e2014-03-27 16:02:12 -070038
39using namespace ndn::nfd;
40
hilata54e4eaf2014-04-10 23:38:33 -050041class Nfdc : boost::noncopyable
hilata198cadb2014-02-15 23:46:19 -060042{
43public:
Syed Obaid4ae0ce32014-06-17 13:59:20 -050044
45 static const ndn::time::milliseconds DEFAULT_EXPIRATION_PERIOD;
46 static const uint64_t DEFAULT_COST;
47
Alexander Afanasyev352e14e2014-03-27 16:02:12 -070048 class Error : public std::runtime_error
hilata198cadb2014-02-15 23:46:19 -060049 {
Alexander Afanasyev352e14e2014-03-27 16:02:12 -070050 public:
51 explicit
52 Error(const std::string& what)
53 : std::runtime_error(what)
54 {
55 }
hilata198cadb2014-02-15 23:46:19 -060056 };
Alexander Afanasyev352e14e2014-03-27 16:02:12 -070057
Chengyu Fan27be0b02014-11-24 20:52:53 -070058 class FaceIdFetcher
59 {
60 public:
61 typedef std::function<void(uint32_t)> SuccessCallback;
62 typedef std::function<void(const std::string&)> FailureCallback;
63
64 /** \brief obtain FaceId from input
65 * \param face Reference to the Face that should be used to fetch data
66 * \param controller Reference to the controller that should be used to sign the Interest
67 * \param input User input, either FaceId or FaceUri
68 * \param allowCreate Whether creating face is allowed
69 * \param onSucceed Callback to be fired when faceId is obtained
70 * \param onFail Callback to be fired when an error occurs
71 */
72 static void
73 start(ndn::Face& face,
74 Controller& controller,
75 const std::string& input,
76 bool allowCreate,
77 const SuccessCallback& onSucceed,
78 const FailureCallback& onFail);
79
80 private:
81 FaceIdFetcher(ndn::Face& face,
82 Controller& controller,
83 bool allowCreate,
84 const SuccessCallback& onSucceed,
85 const FailureCallback& onFail);
86
87 void
88 onQuerySuccess(const ndn::ConstBufferPtr& data,
89 const ndn::util::FaceUri& canonicalUri);
90
91 void
92 onQueryFailure(uint32_t errorCode,
93 const ndn::util::FaceUri& canonicalUri);
94
95 void
96 onCanonizeSuccess(const ndn::util::FaceUri& canonicalUri);
97
98 void
99 onCanonizeFailure(const std::string& reason);
100
101 void
102 startGetFaceId(const ndn::util::FaceUri& faceUri);
103
104 void
105 startFaceCreate(const ndn::util::FaceUri& canonicalUri);
106
107 void
108 onFaceCreateError(uint32_t code,
109 const std::string& error,
110 const std::string& message);
111
112 void
113 succeed(uint32_t faceId);
114
115 void
116 fail(const std::string& reason);
117
118 private:
119 ndn::Face& m_face;
120 Controller& m_controller;
121 bool m_allowCreate;
122 SuccessCallback m_onSucceed;
123 FailureCallback m_onFail;
Muktadir R Chowdhury6c4a67a2015-09-07 22:13:01 -0500124 ndn::ValidatorNull m_validator;
Chengyu Fan27be0b02014-11-24 20:52:53 -0700125 };
126
hilata198cadb2014-02-15 23:46:19 -0600127 explicit
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700128 Nfdc(ndn::Face& face);
129
130 ~Nfdc();
131
hilata198cadb2014-02-15 23:46:19 -0600132 bool
hilata54e4eaf2014-04-10 23:38:33 -0500133 dispatch(const std::string& cmd);
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700134
hilata198cadb2014-02-15 23:46:19 -0600135 /**
Obaiddca50792014-04-24 18:38:40 -0500136 * \brief Adds a nexthop to a FIB entry
hilatadc947ec2014-03-10 12:48:31 -0500137 *
138 * If the FIB entry does not exist, it is inserted automatically
hilata198cadb2014-02-15 23:46:19 -0600139 *
140 * cmd format:
Obaiddca50792014-04-24 18:38:40 -0500141 * [-c cost] name faceId|faceUri
hilata198cadb2014-02-15 23:46:19 -0600142 *
hilata198cadb2014-02-15 23:46:19 -0600143 */
144 void
Obaiddca50792014-04-24 18:38:40 -0500145 fibAddNextHop();
146
147 /**
hilatadc947ec2014-03-10 12:48:31 -0500148 * \brief Removes a nexthop from an existing FIB entry
hilata198cadb2014-02-15 23:46:19 -0600149 *
hilatadc947ec2014-03-10 12:48:31 -0500150 * If the last nexthop record in a FIB entry is removed, the FIB entry is also deleted
hilata198cadb2014-02-15 23:46:19 -0600151 *
152 * cmd format:
Obaiddca50792014-04-24 18:38:40 -0500153 * name faceId
hilata198cadb2014-02-15 23:46:19 -0600154 *
hilata198cadb2014-02-15 23:46:19 -0600155 */
156 void
hilata54e4eaf2014-04-10 23:38:33 -0500157 fibRemoveNextHop();
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700158
hilata198cadb2014-02-15 23:46:19 -0600159 /**
Obaiddca50792014-04-24 18:38:40 -0500160 * \brief Registers name to the given faceId or faceUri
hilata198cadb2014-02-15 23:46:19 -0600161 *
162 * cmd format:
Obaiddca50792014-04-24 18:38:40 -0500163 * [-I] [-C] [-c cost] name faceId|faceUri
164 */
165 void
166 ribRegisterPrefix();
167
168 /**
Chengyu Fan27be0b02014-11-24 20:52:53 -0700169 * \brief Unregisters name from the given faceId/faceUri
Obaiddca50792014-04-24 18:38:40 -0500170 *
171 * cmd format:
Chengyu Fan27be0b02014-11-24 20:52:53 -0700172 * name faceId/faceUri
Obaiddca50792014-04-24 18:38:40 -0500173 */
174 void
175 ribUnregisterPrefix();
176
177 /**
178 * \brief Creates new face
179 *
180 * This command allows creation of UDP unicast and TCP faces only
181 *
182 * cmd format:
183 * uri
hilata198cadb2014-02-15 23:46:19 -0600184 *
hilata198cadb2014-02-15 23:46:19 -0600185 */
186 void
hilata54e4eaf2014-04-10 23:38:33 -0500187 faceCreate();
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700188
hilata198cadb2014-02-15 23:46:19 -0600189 /**
Obaiddca50792014-04-24 18:38:40 -0500190 * \brief Destroys face
hilata198cadb2014-02-15 23:46:19 -0600191 *
192 * cmd format:
Obaiddca50792014-04-24 18:38:40 -0500193 * faceId|faceUri
hilata198cadb2014-02-15 23:46:19 -0600194 *
hilata198cadb2014-02-15 23:46:19 -0600195 */
196 void
hilata54e4eaf2014-04-10 23:38:33 -0500197 faceDestroy();
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700198
hilata141eaae2014-03-13 19:54:47 -0500199 /**
Obaiddca50792014-04-24 18:38:40 -0500200 * \brief Sets the strategy for a namespace
201 *
202 * cmd format:
203 * name strategy
hilata141eaae2014-03-13 19:54:47 -0500204 *
hilata141eaae2014-03-13 19:54:47 -0500205 */
206 void
hilata54e4eaf2014-04-10 23:38:33 -0500207 strategyChoiceSet();
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700208
hilata141eaae2014-03-13 19:54:47 -0500209 /**
210 * \brief Unset the strategy for a namespace
211 *
hilata141eaae2014-03-13 19:54:47 -0500212 * cmd format:
Obaiddca50792014-04-24 18:38:40 -0500213 * name strategy
hilata141eaae2014-03-13 19:54:47 -0500214 *
hilata141eaae2014-03-13 19:54:47 -0500215 */
216 void
hilata54e4eaf2014-04-10 23:38:33 -0500217 strategyChoiceUnset();
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700218
hilata198cadb2014-02-15 23:46:19 -0600219private:
hilata54e4eaf2014-04-10 23:38:33 -0500220
hilata198cadb2014-02-15 23:46:19 -0600221 void
Obaiddca50792014-04-24 18:38:40 -0500222 onSuccess(const ControlParameters& commandSuccessResult,
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700223 const std::string& message);
hilata198cadb2014-02-15 23:46:19 -0600224
225 void
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700226 onError(uint32_t code, const std::string& error, const std::string& message);
227
Chengyu Fandae25302014-10-16 11:40:11 -0600228 void
229 onCanonizeFailure(const std::string& reason);
230
231 void
232 startFaceCreate(const ndn::util::FaceUri& canonicalUri);
233
234 void
Chengyu Fan27be0b02014-11-24 20:52:53 -0700235 onObtainFaceIdFailure(const std::string& message);
Chengyu Fandae25302014-10-16 11:40:11 -0600236
hilata198cadb2014-02-15 23:46:19 -0600237public:
238 const char* m_programName;
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700239
hilata54e4eaf2014-04-10 23:38:33 -0500240 // command parameters without leading 'cmd' component
Junxiao Shi22295032014-04-29 22:57:40 -0700241 const char* const* m_commandLineArguments;
hilata54e4eaf2014-04-10 23:38:33 -0500242 int m_nOptions;
Obaiddca50792014-04-24 18:38:40 -0500243 uint64_t m_flags;
244 uint64_t m_cost;
245 uint64_t m_faceId;
Syed Obaid4ae0ce32014-06-17 13:59:20 -0500246 uint64_t m_origin;
247 ndn::time::milliseconds m_expires;
Obaiddca50792014-04-24 18:38:40 -0500248 std::string m_name;
Joao Pereirac6184cd2015-09-03 15:27:29 -0400249 FacePersistency m_facePersistency;
hilata54e4eaf2014-04-10 23:38:33 -0500250
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700251private:
Junxiao Shi8e273ca2014-11-12 00:42:29 -0700252 ndn::KeyChain m_keyChain;
Chengyu Fan27be0b02014-11-24 20:52:53 -0700253 ndn::Face& m_face;
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700254 Controller m_controller;
Chengyu Fandae25302014-10-16 11:40:11 -0600255 boost::asio::io_service& m_ioService;
hilata198cadb2014-02-15 23:46:19 -0600256};
257
hilata141eaae2014-03-13 19:54:47 -0500258} // namespace nfdc
hilata198cadb2014-02-15 23:46:19 -0600259
260#endif // NFD_TOOLS_NFDC_HPP