blob: f8895b54d71028fabe3befe20eb8866b0e26310b [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>
Chengyu Fan27be0b02014-11-24 20:52:53 -070034#include <memory>
hilata198cadb2014-02-15 23:46:19 -060035
36namespace nfdc {
Alexander Afanasyev352e14e2014-03-27 16:02:12 -070037
38using namespace ndn::nfd;
39
hilata54e4eaf2014-04-10 23:38:33 -050040class Nfdc : boost::noncopyable
hilata198cadb2014-02-15 23:46:19 -060041{
42public:
Syed Obaid4ae0ce32014-06-17 13:59:20 -050043
44 static const ndn::time::milliseconds DEFAULT_EXPIRATION_PERIOD;
45 static const uint64_t DEFAULT_COST;
46
Alexander Afanasyev352e14e2014-03-27 16:02:12 -070047 class Error : public std::runtime_error
hilata198cadb2014-02-15 23:46:19 -060048 {
Alexander Afanasyev352e14e2014-03-27 16:02:12 -070049 public:
50 explicit
51 Error(const std::string& what)
52 : std::runtime_error(what)
53 {
54 }
hilata198cadb2014-02-15 23:46:19 -060055 };
Alexander Afanasyev352e14e2014-03-27 16:02:12 -070056
Chengyu Fan27be0b02014-11-24 20:52:53 -070057 class FaceIdFetcher
58 {
59 public:
60 typedef std::function<void(uint32_t)> SuccessCallback;
61 typedef std::function<void(const std::string&)> FailureCallback;
62
63 /** \brief obtain FaceId from input
64 * \param face Reference to the Face that should be used to fetch data
65 * \param controller Reference to the controller that should be used to sign the Interest
66 * \param input User input, either FaceId or FaceUri
67 * \param allowCreate Whether creating face is allowed
68 * \param onSucceed Callback to be fired when faceId is obtained
69 * \param onFail Callback to be fired when an error occurs
70 */
71 static void
72 start(ndn::Face& face,
73 Controller& controller,
74 const std::string& input,
75 bool allowCreate,
76 const SuccessCallback& onSucceed,
77 const FailureCallback& onFail);
78
79 private:
80 FaceIdFetcher(ndn::Face& face,
81 Controller& controller,
82 bool allowCreate,
83 const SuccessCallback& onSucceed,
84 const FailureCallback& onFail);
85
86 void
87 onQuerySuccess(const ndn::ConstBufferPtr& data,
88 const ndn::util::FaceUri& canonicalUri);
89
90 void
91 onQueryFailure(uint32_t errorCode,
92 const ndn::util::FaceUri& canonicalUri);
93
94 void
95 onCanonizeSuccess(const ndn::util::FaceUri& canonicalUri);
96
97 void
98 onCanonizeFailure(const std::string& reason);
99
100 void
101 startGetFaceId(const ndn::util::FaceUri& faceUri);
102
103 void
104 startFaceCreate(const ndn::util::FaceUri& canonicalUri);
105
106 void
107 onFaceCreateError(uint32_t code,
108 const std::string& error,
109 const std::string& message);
110
111 void
112 succeed(uint32_t faceId);
113
114 void
115 fail(const std::string& reason);
116
117 private:
118 ndn::Face& m_face;
119 Controller& m_controller;
120 bool m_allowCreate;
121 SuccessCallback m_onSucceed;
122 FailureCallback m_onFail;
123 };
124
hilata198cadb2014-02-15 23:46:19 -0600125 explicit
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700126 Nfdc(ndn::Face& face);
127
128 ~Nfdc();
129
hilata198cadb2014-02-15 23:46:19 -0600130 bool
hilata54e4eaf2014-04-10 23:38:33 -0500131 dispatch(const std::string& cmd);
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700132
hilata198cadb2014-02-15 23:46:19 -0600133 /**
Obaiddca50792014-04-24 18:38:40 -0500134 * \brief Adds a nexthop to a FIB entry
hilatadc947ec2014-03-10 12:48:31 -0500135 *
136 * If the FIB entry does not exist, it is inserted automatically
hilata198cadb2014-02-15 23:46:19 -0600137 *
138 * cmd format:
Obaiddca50792014-04-24 18:38:40 -0500139 * [-c cost] name faceId|faceUri
hilata198cadb2014-02-15 23:46:19 -0600140 *
hilata198cadb2014-02-15 23:46:19 -0600141 */
142 void
Obaiddca50792014-04-24 18:38:40 -0500143 fibAddNextHop();
144
145 /**
hilatadc947ec2014-03-10 12:48:31 -0500146 * \brief Removes a nexthop from an existing FIB entry
hilata198cadb2014-02-15 23:46:19 -0600147 *
hilatadc947ec2014-03-10 12:48:31 -0500148 * If the last nexthop record in a FIB entry is removed, the FIB entry is also deleted
hilata198cadb2014-02-15 23:46:19 -0600149 *
150 * cmd format:
Obaiddca50792014-04-24 18:38:40 -0500151 * name faceId
hilata198cadb2014-02-15 23:46:19 -0600152 *
hilata198cadb2014-02-15 23:46:19 -0600153 */
154 void
hilata54e4eaf2014-04-10 23:38:33 -0500155 fibRemoveNextHop();
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700156
hilata198cadb2014-02-15 23:46:19 -0600157 /**
Obaiddca50792014-04-24 18:38:40 -0500158 * \brief Registers name to the given faceId or faceUri
hilata198cadb2014-02-15 23:46:19 -0600159 *
160 * cmd format:
Obaiddca50792014-04-24 18:38:40 -0500161 * [-I] [-C] [-c cost] name faceId|faceUri
162 */
163 void
164 ribRegisterPrefix();
165
166 /**
Chengyu Fan27be0b02014-11-24 20:52:53 -0700167 * \brief Unregisters name from the given faceId/faceUri
Obaiddca50792014-04-24 18:38:40 -0500168 *
169 * cmd format:
Chengyu Fan27be0b02014-11-24 20:52:53 -0700170 * name faceId/faceUri
Obaiddca50792014-04-24 18:38:40 -0500171 */
172 void
173 ribUnregisterPrefix();
174
175 /**
176 * \brief Creates new face
177 *
178 * This command allows creation of UDP unicast and TCP faces only
179 *
180 * cmd format:
181 * uri
hilata198cadb2014-02-15 23:46:19 -0600182 *
hilata198cadb2014-02-15 23:46:19 -0600183 */
184 void
hilata54e4eaf2014-04-10 23:38:33 -0500185 faceCreate();
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700186
hilata198cadb2014-02-15 23:46:19 -0600187 /**
Obaiddca50792014-04-24 18:38:40 -0500188 * \brief Destroys face
hilata198cadb2014-02-15 23:46:19 -0600189 *
190 * cmd format:
Obaiddca50792014-04-24 18:38:40 -0500191 * faceId|faceUri
hilata198cadb2014-02-15 23:46:19 -0600192 *
hilata198cadb2014-02-15 23:46:19 -0600193 */
194 void
hilata54e4eaf2014-04-10 23:38:33 -0500195 faceDestroy();
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700196
hilata141eaae2014-03-13 19:54:47 -0500197 /**
Obaiddca50792014-04-24 18:38:40 -0500198 * \brief Sets the strategy for a namespace
199 *
200 * cmd format:
201 * name strategy
hilata141eaae2014-03-13 19:54:47 -0500202 *
hilata141eaae2014-03-13 19:54:47 -0500203 */
204 void
hilata54e4eaf2014-04-10 23:38:33 -0500205 strategyChoiceSet();
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700206
hilata141eaae2014-03-13 19:54:47 -0500207 /**
208 * \brief Unset the strategy for a namespace
209 *
hilata141eaae2014-03-13 19:54:47 -0500210 * cmd format:
Obaiddca50792014-04-24 18:38:40 -0500211 * name strategy
hilata141eaae2014-03-13 19:54:47 -0500212 *
hilata141eaae2014-03-13 19:54:47 -0500213 */
214 void
hilata54e4eaf2014-04-10 23:38:33 -0500215 strategyChoiceUnset();
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700216
hilata198cadb2014-02-15 23:46:19 -0600217private:
hilata54e4eaf2014-04-10 23:38:33 -0500218
hilata198cadb2014-02-15 23:46:19 -0600219 void
Obaiddca50792014-04-24 18:38:40 -0500220 onSuccess(const ControlParameters& commandSuccessResult,
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700221 const std::string& message);
hilata198cadb2014-02-15 23:46:19 -0600222
223 void
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700224 onError(uint32_t code, const std::string& error, const std::string& message);
225
Chengyu Fandae25302014-10-16 11:40:11 -0600226 void
227 onCanonizeFailure(const std::string& reason);
228
229 void
230 startFaceCreate(const ndn::util::FaceUri& canonicalUri);
231
232 void
Chengyu Fan27be0b02014-11-24 20:52:53 -0700233 onObtainFaceIdFailure(const std::string& message);
Chengyu Fandae25302014-10-16 11:40:11 -0600234
hilata198cadb2014-02-15 23:46:19 -0600235public:
236 const char* m_programName;
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700237
hilata54e4eaf2014-04-10 23:38:33 -0500238 // command parameters without leading 'cmd' component
Junxiao Shi22295032014-04-29 22:57:40 -0700239 const char* const* m_commandLineArguments;
hilata54e4eaf2014-04-10 23:38:33 -0500240 int m_nOptions;
Obaiddca50792014-04-24 18:38:40 -0500241 uint64_t m_flags;
242 uint64_t m_cost;
243 uint64_t m_faceId;
Syed Obaid4ae0ce32014-06-17 13:59:20 -0500244 uint64_t m_origin;
245 ndn::time::milliseconds m_expires;
Obaiddca50792014-04-24 18:38:40 -0500246 std::string m_name;
hilata54e4eaf2014-04-10 23:38:33 -0500247
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700248private:
Junxiao Shi8e273ca2014-11-12 00:42:29 -0700249 ndn::KeyChain m_keyChain;
Chengyu Fan27be0b02014-11-24 20:52:53 -0700250 ndn::Face& m_face;
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700251 Controller m_controller;
Chengyu Fandae25302014-10-16 11:40:11 -0600252 boost::asio::io_service& m_ioService;
hilata198cadb2014-02-15 23:46:19 -0600253};
254
hilata141eaae2014-03-13 19:54:47 -0500255} // namespace nfdc
hilata198cadb2014-02-15 23:46:19 -0600256
257#endif // NFD_TOOLS_NFDC_HPP