33 #include <ndn-cxx/management/nfd-face-event-notification.hpp>
34 #include <ndn-cxx/management/nfd-face-query-filter.hpp>
40 const Name FaceManager::COMMAND_PREFIX(
"/localhost/nfd/faces");
42 const size_t FaceManager::COMMAND_UNSIGNED_NCOMPS =
43 FaceManager::COMMAND_PREFIX.size() +
47 const size_t FaceManager::COMMAND_SIGNED_NCOMPS =
48 FaceManager::COMMAND_UNSIGNED_NCOMPS +
51 const FaceManager::SignedVerbAndProcessor FaceManager::SIGNED_COMMAND_VERBS[] =
53 SignedVerbAndProcessor(
54 Name::Component(
"create"),
55 &FaceManager::createFace
58 SignedVerbAndProcessor(
59 Name::Component(
"destroy"),
60 &FaceManager::destroyFace
63 SignedVerbAndProcessor(
64 Name::Component(
"enable-local-control"),
65 &FaceManager::enableLocalControl
68 SignedVerbAndProcessor(
69 Name::Component(
"disable-local-control"),
70 &FaceManager::disableLocalControl
74 const FaceManager::UnsignedVerbAndProcessor FaceManager::UNSIGNED_COMMAND_VERBS[] =
76 UnsignedVerbAndProcessor(
77 Name::Component(
"list"),
78 &FaceManager::listFaces
81 UnsignedVerbAndProcessor(
82 Name::Component(
"events"),
83 &FaceManager::ignoreUnsignedVerb
86 UnsignedVerbAndProcessor(
87 Name::Component(
"channels"),
88 &FaceManager::listChannels
91 UnsignedVerbAndProcessor(
92 Name::Component(
"query"),
93 &FaceManager::listQueriedFaces
97 const Name FaceManager::FACES_LIST_DATASET_PREFIX(
"/localhost/nfd/faces/list");
98 const size_t FaceManager::FACES_LIST_DATASET_NCOMPS = FACES_LIST_DATASET_PREFIX.size();
100 const Name FaceManager::FACE_EVENTS_PREFIX(
"/localhost/nfd/faces/events");
102 const Name FaceManager::CHANNELS_LIST_DATASET_PREFIX(
"/localhost/nfd/faces/channels");
103 const size_t FaceManager::CHANNELS_LIST_DATASET_NCOMPS = CHANNELS_LIST_DATASET_PREFIX.size();
105 const Name FaceManager::FACES_QUERY_DATASET_PREFIX(
"/localhost/nfd/faces/query");
106 const size_t FaceManager::FACES_QUERY_DATASET_NCOMPS = FACES_QUERY_DATASET_PREFIX.size() + 1;
109 shared_ptr<InternalFace> face,
110 ndn::KeyChain& keyChain)
112 , m_faceTable(faceTable)
113 , m_faceAddConn(m_faceTable.onAdd.connect(bind(&
FaceManager::onAddFace, this, _1)))
114 , m_faceRemoveConn(m_faceTable.onRemove.connect(bind(&
FaceManager::onRemoveFace, this, _1)))
115 , m_faceStatusPublisher(m_faceTable, *m_face, FACES_LIST_DATASET_PREFIX, keyChain)
116 , m_channelStatusPublisher(m_factories, *m_face, CHANNELS_LIST_DATASET_PREFIX, keyChain)
117 , m_notificationStream(*m_face, FACE_EVENTS_PREFIX, keyChain)
118 , m_signedVerbDispatch(SIGNED_COMMAND_VERBS,
119 SIGNED_COMMAND_VERBS +
120 (sizeof(SIGNED_COMMAND_VERBS) / sizeof(SignedVerbAndProcessor)))
121 , m_unsignedVerbDispatch(UNSIGNED_COMMAND_VERBS,
122 UNSIGNED_COMMAND_VERBS +
123 (sizeof(UNSIGNED_COMMAND_VERBS) / sizeof(UnsignedVerbAndProcessor)))
126 face->setInterestFilter(
"/localhost/nfd/faces",
139 bind(&FaceManager::onConfig,
this, _1, _2, _3));
146 const std::string& filename)
148 throw Error(
"Not supported");
154 const Name& command = request.getName();
155 const size_t commandNComps = command.size();
157 if (commandNComps <= COMMAND_PREFIX.size())
165 const Name::Component& verb = command.at(COMMAND_PREFIX.size());
167 const auto unsignedVerbProcessor = m_unsignedVerbDispatch.find(verb);
168 if (unsignedVerbProcessor != m_unsignedVerbDispatch.end())
171 (unsignedVerbProcessor->second)(
this, request);
173 else if (COMMAND_UNSIGNED_NCOMPS <= commandNComps &&
174 commandNComps < COMMAND_SIGNED_NCOMPS)
179 else if (commandNComps < COMMAND_SIGNED_NCOMPS ||
180 !COMMAND_PREFIX.isPrefixOf(command))
188 bind(&FaceManager::onValidatedFaceRequest,
this, _1),
194 FaceManager::onValidatedFaceRequest(
const shared_ptr<const Interest>& request)
196 const Name& command = request->getName();
197 const Name::Component& verb = command[COMMAND_PREFIX.size()];
198 const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1];
200 SignedVerbDispatchTable::const_iterator signedVerbProcessor = m_signedVerbDispatch.find(verb);
201 if (signedVerbProcessor != m_signedVerbDispatch.end())
203 ControlParameters parameters;
211 (signedVerbProcessor->second)(
this, *request, parameters);
222 FaceManager::addCreatedFaceToForwarder(
const shared_ptr<Face>& newFace)
224 m_faceTable.
add(newFace);
230 FaceManager::onCreated(
const Name& requestName,
231 ControlParameters& parameters,
232 const shared_ptr<Face>& newFace)
234 addCreatedFaceToForwarder(newFace);
235 parameters.setFaceId(newFace->getId());
236 parameters.setUri(newFace->getRemoteUri().toString());
238 sendResponse(requestName, 200,
"Success", parameters.wireEncode());
242 FaceManager::onConnectFailed(
const Name& requestName,
const std::string& reason)
249 FaceManager::createFace(
const Interest& request,
250 ControlParameters& parameters)
252 const Name& requestName = request.getName();
253 ndn::nfd::FaceCreateCommand command;
263 if (!uri.parse(parameters.getUri()))
270 if (!uri.isCanonical())
277 FactoryMap::iterator factory = m_factories.find(uri.getScheme());
278 if (factory == m_factories.end())
286 factory->second->createFace(uri,
287 bind(&FaceManager::onCreated,
288 this, requestName, parameters, _1),
289 bind(&FaceManager::onConnectFailed,
290 this, requestName, _1));
292 catch (
const std::runtime_error& error)
294 std::string errorMessage =
"NFD error: ";
295 errorMessage += error.what();
300 catch (
const std::logic_error& error)
302 std::string errorMessage =
"NFD error: ";
303 errorMessage += error.what();
312 FaceManager::destroyFace(
const Interest& request,
313 ControlParameters& parameters)
315 const Name& requestName = request.getName();
316 ndn::nfd::FaceDestroyCommand command;
324 shared_ptr<Face> target = m_faceTable.
get(parameters.getFaceId());
325 if (static_cast<bool>(target))
330 sendResponse(requestName, 200,
"Success", parameters.wireEncode());
335 FaceManager::onAddFace(shared_ptr<Face> face)
337 ndn::nfd::FaceEventNotification notification;
338 notification.setKind(ndn::nfd::FACE_EVENT_CREATED);
339 face->copyStatusTo(notification);
341 m_notificationStream.postNotification(notification);
345 FaceManager::onRemoveFace(shared_ptr<Face> face)
347 ndn::nfd::FaceEventNotification notification;
348 notification.setKind(ndn::nfd::FACE_EVENT_DESTROYED);
349 face->copyStatusTo(notification);
351 m_notificationStream.postNotification(notification);
355 FaceManager::extractLocalControlParameters(
const Interest& request,
356 ControlParameters& parameters,
357 ControlCommand& command,
358 shared_ptr<LocalFace>& outFace,
359 LocalControlFeature& outFeature)
363 sendResponse(request.getName(), 400,
"Malformed command");
367 shared_ptr<Face> face = m_faceTable.
get(request.getIncomingFaceId());
369 if (!static_cast<bool>(face))
371 NFD_LOG_DEBUG(
"command result: faceid " << request.getIncomingFaceId() <<
" not found");
375 else if (!face->isLocal())
377 NFD_LOG_DEBUG(
"command result: cannot enable local control on non-local faceid " <<
379 sendResponse(request.getName(), 412,
"Face is non-local");
383 outFace = dynamic_pointer_cast<LocalFace>(face);
384 outFeature =
static_cast<LocalControlFeature
>(parameters.getLocalControlFeature());
390 FaceManager::enableLocalControl(
const Interest& request,
391 ControlParameters& parameters)
393 ndn::nfd::FaceEnableLocalControlCommand command;
396 shared_ptr<LocalFace> face;
397 LocalControlFeature feature;
399 if (extractLocalControlParameters(request, parameters, command, face, feature))
401 face->setLocalControlHeaderFeature(feature,
true);
402 sendResponse(request.getName(), 200,
"Success", parameters.wireEncode());
407 FaceManager::disableLocalControl(
const Interest& request,
408 ControlParameters& parameters)
410 ndn::nfd::FaceDisableLocalControlCommand command;
411 shared_ptr<LocalFace> face;
412 LocalControlFeature feature;
414 if (extractLocalControlParameters(request, parameters, command, face, feature))
416 face->setLocalControlHeaderFeature(feature,
false);
417 sendResponse(request.getName(), 200,
"Success", parameters.wireEncode());
422 FaceManager::listFaces(
const Interest& request)
424 const Name& command = request.getName();
425 const size_t commandNComps = command.size();
427 if (commandNComps < FACES_LIST_DATASET_NCOMPS ||
428 !FACES_LIST_DATASET_PREFIX.isPrefixOf(command))
435 m_faceStatusPublisher.
publish();
439 FaceManager::listChannels(
const Interest& request)
442 const Name& command = request.getName();
443 const size_t commandNComps = command.size();
445 if (commandNComps < CHANNELS_LIST_DATASET_NCOMPS ||
446 !CHANNELS_LIST_DATASET_PREFIX.isPrefixOf(command))
454 m_channelStatusPublisher.
publish();
458 FaceManager::listQueriedFaces(
const Interest& request)
461 const Name& query = request.getName();
462 const size_t queryNComps = query.size();
464 if (queryNComps < FACES_QUERY_DATASET_NCOMPS ||
465 !FACES_QUERY_DATASET_PREFIX.isPrefixOf(query))
472 ndn::nfd::FaceQueryFilter faceFilter;
475 faceFilter.wireDecode(query[-1].blockFromValue());
485 faceQueryStatusPublisher(m_faceTable, *
m_face, query, faceFilter,
m_keyChain);
487 faceQueryStatusPublisher.publish();
490 shared_ptr<ProtocolFactory>
491 FaceManager::findFactory(
const std::string& protocol)
493 FactoryMap::iterator factory = m_factories.find(protocol);
494 if (factory != m_factories.end())
495 return factory->second;
497 return shared_ptr<ProtocolFactory>();
shared_ptr< InternalFace > m_face
FaceManager(FaceTable &faceTable, shared_ptr< InternalFace > face, ndn::KeyChain &keyChain)
void setConfigFile(ConfigFile &configFile)
Subscribe to a face management section(s) for the config file.
#define NFD_LOG_DEBUG(expression)
ndn::KeyChain & m_keyChain
void add(shared_ptr< Face > face)
void sendResponse(const Name &name, const ControlResponse &response)
#define NFD_LOG_ERROR(expression)
static bool extractParameters(const Name::Component ¶meterComponent, ControlParameters &extractedParameters)
void addSectionHandler(const std::string §ionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
void onFaceRequest(const Interest &request)
const std::string FACE_MANAGER_PRIVILEGE
void onCommandValidationFailed(const shared_ptr< const Interest > &command, const std::string &error)
boost::property_tree::ptree ConfigSection
void sendNack(const Name &name)
#define NFD_LOG_INIT(name)
#define NFD_LOG_TRACE(expression)
virtual bool validateParameters(const ControlCommand &command, ControlParameters ¶meters)
shared_ptr< Face > get(FaceId id) const
void validate(const Interest &interest, const ndn::OnInterestValidated &onValidated, const ndn::OnInterestValidationFailed &onValidationFailed)