31 #include <ndn-cxx/lp/tags.hpp> 32 #include <ndn-cxx/mgmt/nfd/channel-status.hpp> 41 , m_faceSystem(faceSystem)
42 , m_faceTable(faceSystem.getFaceTable())
45 registerCommandHandler<ndn::nfd::FaceCreateCommand>(
"create",
46 bind(&FaceManager::createFace,
this, _2, _3, _4, _5));
48 registerCommandHandler<ndn::nfd::FaceUpdateCommand>(
"update",
49 bind(&FaceManager::updateFace,
this, _2, _3, _4, _5));
51 registerCommandHandler<ndn::nfd::FaceDestroyCommand>(
"destroy",
52 bind(&FaceManager::destroyFace,
this, _2, _3, _4, _5));
61 m_faceAddConn = m_faceTable.
afterAdd.connect([
this] (
const Face& face) {
62 connectFaceStateChangeSignal(face);
63 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_CREATED);
65 m_faceRemoveConn = m_faceTable.
beforeRemove.connect([
this] (
const Face& face) {
66 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_DESTROYED);
73 m_faceSystem.setConfigFile(configFile);
77 FaceManager::createFace(
const Name& topPrefix,
const Interest& interest,
78 const ControlParameters& parameters,
79 const ndn::mgmt::CommandContinuation& done)
82 if (!remoteUri.parse(parameters.getUri())) {
83 NFD_LOG_TRACE(
"failed to parse remote URI: " << parameters.getUri());
84 done(ControlResponse(400,
"Malformed command"));
88 if (!remoteUri.isCanonical()) {
89 NFD_LOG_TRACE(
"received non-canonical remote URI: " << remoteUri.toString());
90 done(ControlResponse(400,
"Non-canonical remote URI"));
94 ndn::optional<FaceUri> localUri;
95 if (parameters.hasLocalUri()) {
98 if (!localUri->parse(parameters.getLocalUri())) {
99 NFD_LOG_TRACE(
"failed to parse local URI: " << parameters.getLocalUri());
100 done(ControlResponse(400,
"Malformed command"));
104 if (!localUri->isCanonical()) {
105 NFD_LOG_TRACE(
"received non-canonical local URI: " << localUri->toString());
106 done(ControlResponse(400,
"Non-canonical local URI"));
112 if (factory ==
nullptr) {
113 NFD_LOG_TRACE(
"received create request for unsupported protocol: " << remoteUri.getScheme());
114 done(ControlResponse(406,
"Unsupported protocol"));
119 factory->
createFace({remoteUri, localUri, parameters.getFacePersistency(),
120 parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
121 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED),
122 parameters.hasFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED) &&
123 parameters.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)},
124 bind(&FaceManager::afterCreateFaceSuccess,
this, parameters, _1, done),
125 bind(&FaceManager::afterCreateFaceFailure,
this, _1, _2, done));
127 catch (
const std::runtime_error& error) {
129 done(ControlResponse(500,
"Face creation failed due to internal error"));
132 catch (
const std::logic_error& error) {
134 done(ControlResponse(500,
"Face creation failed due to internal error"));
140 FaceManager::afterCreateFaceSuccess(
const ControlParameters& parameters,
141 const shared_ptr<Face>& face,
142 const ndn::mgmt::CommandContinuation& done)
145 NFD_LOG_TRACE(
"Attempted to create duplicate face of " << face->getId());
147 ControlParameters response = collectFaceProperties(*face,
true);
148 done(ControlResponse(409,
"Face with remote URI already exists").setBody(response.wireEncode()));
154 BOOST_ASSERT(face->getScope() == ndn::nfd::FACE_SCOPE_LOCAL ||
155 !parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) ||
156 (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
157 !parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)));
159 m_faceTable.
add(face);
161 ControlParameters response = collectFaceProperties(*face,
true);
162 done(ControlResponse(200,
"OK").setBody(response.wireEncode()));
166 FaceManager::afterCreateFaceFailure(uint32_t status,
167 const std::string& reason,
168 const ndn::mgmt::CommandContinuation& done)
172 done(ControlResponse(status, reason));
176 FaceManager::updateFace(
const Name& topPrefix,
const Interest& interest,
177 const ControlParameters& parameters,
178 const ndn::mgmt::CommandContinuation& done)
180 FaceId faceId = parameters.getFaceId();
183 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = interest.getTag<lp::IncomingFaceIdTag>();
184 if (incomingFaceIdTag ==
nullptr) {
186 done(ControlResponse(404,
"No FaceId specified and IncomingFaceId not available"));
189 faceId = *incomingFaceIdTag;
192 Face* face = m_faceTable.
get(faceId);
194 if (face ==
nullptr) {
196 done(ControlResponse(404,
"Specified face does not exist"));
201 ControlParameters response;
202 bool areParamsValid =
true;
204 if (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
205 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
206 face->getScope() != ndn::nfd::FACE_SCOPE_LOCAL) {
207 NFD_LOG_TRACE(
"received request to enable local fields on non-local face");
208 areParamsValid =
false;
209 response.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED,
210 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED));
214 if (parameters.hasFacePersistency()) {
215 auto persistency = parameters.getFacePersistency();
216 if (!face->getTransport()->canChangePersistencyTo(persistency)) {
217 NFD_LOG_TRACE(
"cannot change face persistency to " << persistency);
218 areParamsValid =
false;
219 response.setFacePersistency(persistency);
223 if (!areParamsValid) {
224 done(ControlResponse(409,
"Invalid properties specified").setBody(response.wireEncode()));
229 if (parameters.hasFacePersistency()) {
230 face->setPersistency(parameters.getFacePersistency());
232 setLinkServiceOptions(*face, parameters);
235 response = collectFaceProperties(*face,
false);
237 done(ControlResponse(200,
"OK").setBody(response.wireEncode()));
241 FaceManager::destroyFace(
const Name& topPrefix,
const Interest& interest,
242 const ControlParameters& parameters,
243 const ndn::mgmt::CommandContinuation& done)
245 Face* face = m_faceTable.
get(parameters.getFaceId());
246 if (face !=
nullptr) {
250 done(ControlResponse(200,
"OK").setBody(parameters.wireEncode()));
254 FaceManager::setLinkServiceOptions(Face& face,
255 const ControlParameters& parameters)
258 BOOST_ASSERT(linkService !=
nullptr);
260 auto options = linkService->getOptions();
261 if (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
262 face.getScope() == ndn::nfd::FACE_SCOPE_LOCAL) {
263 options.allowLocalFields = parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED);
265 if (parameters.hasFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)) {
266 options.reliabilityOptions.isEnabled = parameters.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED);
268 linkService->setOptions(options);
272 FaceManager::collectFaceProperties(
const Face& face,
bool wantUris)
275 BOOST_ASSERT(linkService !=
nullptr);
276 auto options = linkService->getOptions();
278 ControlParameters params;
279 params.setFaceId(face.getId())
280 .setFacePersistency(face.getPersistency())
281 .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, options.allowLocalFields,
false)
282 .setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, options.reliabilityOptions.isEnabled,
false);
284 params.setUri(face.getRemoteUri().toString())
285 .setLocalUri(face.getLocalUri().toString());
291 FaceManager::listFaces(
const Name& topPrefix,
const Interest& interest,
292 ndn::mgmt::StatusDatasetContext& context)
294 auto now = time::steady_clock::now();
295 for (
const Face& face : m_faceTable) {
296 ndn::nfd::FaceStatus status = collectFaceStatus(face, now);
297 context.append(status.wireEncode());
303 FaceManager::listChannels(
const Name& topPrefix,
const Interest& interest,
304 ndn::mgmt::StatusDatasetContext& context)
306 std::set<const face::ProtocolFactory*> factories = m_faceSystem.listProtocolFactories();
307 for (
const auto* factory : factories) {
308 for (
const auto& channel : factory->getChannels()) {
309 ndn::nfd::ChannelStatus entry;
310 entry.setLocalUri(channel->getUri().toString());
311 context.append(entry.wireEncode());
318 FaceManager::queryFaces(
const Name& topPrefix,
const Interest& interest,
319 ndn::mgmt::StatusDatasetContext& context)
321 ndn::nfd::FaceQueryFilter faceFilter;
322 const Name& query = interest.getName();
324 faceFilter.wireDecode(query[-1].blockFromValue());
326 catch (
const tlv::Error& e) {
328 return context.reject(ControlResponse(400,
"Malformed filter"));
331 auto now = time::steady_clock::now();
332 for (
const Face& face : m_faceTable) {
333 if (!matchFilter(faceFilter, face)) {
336 ndn::nfd::FaceStatus status = collectFaceStatus(face, now);
337 context.append(status.wireEncode());
344 FaceManager::matchFilter(
const ndn::nfd::FaceQueryFilter& filter,
const Face& face)
346 if (filter.hasFaceId() &&
347 filter.getFaceId() !=
static_cast<uint64_t
>(face.getId())) {
351 if (filter.hasUriScheme() &&
352 filter.getUriScheme() != face.getRemoteUri().getScheme() &&
353 filter.getUriScheme() != face.getLocalUri().getScheme()) {
357 if (filter.hasRemoteUri() &&
358 filter.getRemoteUri() != face.getRemoteUri().toString()) {
362 if (filter.hasLocalUri() &&
363 filter.getLocalUri() != face.getLocalUri().toString()) {
367 if (filter.hasFaceScope() &&
368 filter.getFaceScope() != face.getScope()) {
372 if (filter.hasFacePersistency() &&
373 filter.getFacePersistency() != face.getPersistency()) {
377 if (filter.hasLinkType() &&
378 filter.getLinkType() != face.getLinkType()) {
386 FaceManager::collectFaceStatus(
const Face& face,
const time::steady_clock::TimePoint& now)
388 ndn::nfd::FaceStatus status;
390 collectFaceProperties(face, status);
392 time::steady_clock::TimePoint expirationTime = face.getExpirationTime();
393 if (expirationTime != time::steady_clock::TimePoint::max()) {
394 status.setExpirationPeriod(std::max(time::milliseconds(0),
395 time::duration_cast<time::milliseconds>(expirationTime - now)));
411 template<
typename FaceTraits>
413 FaceManager::collectFaceProperties(
const Face& face, FaceTraits& traits)
415 traits.setFaceId(face.getId())
416 .setRemoteUri(face.getRemoteUri().toString())
417 .setLocalUri(face.getLocalUri().toString())
418 .setFaceScope(face.getScope())
419 .setFacePersistency(face.getPersistency())
420 .setLinkType(face.getLinkType());
424 if (linkService !=
nullptr) {
425 auto linkServiceOptions = linkService->
getOptions();
426 traits.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, linkServiceOptions.allowLocalFields);
427 traits.setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED,
428 linkServiceOptions.reliabilityOptions.isEnabled);
433 FaceManager::notifyFaceEvent(
const Face& face, ndn::nfd::FaceEventKind kind)
435 ndn::nfd::FaceEventNotification notification;
436 notification.setKind(kind);
437 collectFaceProperties(face, notification);
439 m_postNotification(notification.wireEncode());
443 FaceManager::connectFaceStateChangeSignal(
const Face& face)
445 FaceId faceId = face.getId();
446 m_faceStateChangeConn[faceId] = face.afterStateChange.connect(
448 const Face& face = *m_faceTable.
get(faceId);
451 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_UP);
454 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_DOWN);
457 m_faceStateChangeConn.erase(faceId);
void registerStatusDatasetHandler(const std::string &verb, const ndn::mgmt::StatusDatasetHandler &handler)
#define NFD_LOG_DEBUG(expression)
const PacketCounter & nOutInterests
configuration file parsing utility
TransportState
indicates the state of a transport
const Options & getOptions() const
get Options used by GenericLinkService
const PacketCounter & nOutData
GenericLinkService is a LinkService that implements the NDNLPv2 protocol.
Face * get(FaceId id) const
get face by FaceId
#define NFD_LOG_ERROR(expression)
FaceManager(FaceSystem &faceSystem, Dispatcher &dispatcher, CommandAuthenticator &authenticator)
provides ControlCommand authorization according to NFD configuration file
const ByteCounter & nInBytes
void add(shared_ptr< Face > face)
add a face
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
signal::Signal< FaceTable, Face & > beforeRemove
fires before a face is removed
the transport is closed, and can be safely deallocated
ndn::mgmt::PostNotification registerNotificationStream(const std::string &verb)
a collection of common functions shared by all NFD managers, such as communicating with the dispatche...
signal::Signal< FaceTable, Face & > afterAdd
fires after a face is added
const PacketCounter & nOutNacks
gives access to counters provided by Face
const PacketCounter & nInInterests
void setConfigFile(ConfigFile &configFile)
Subscribe to face_system section for the config file.
const ByteCounter & nOutBytes
Provides support for an underlying protocol.
#define NFD_LOG_INIT(name)
#define NFD_LOG_TRACE(expression)
the transport is up and can transmit packets
uint64_t FaceId
identifies a face
virtual void createFace(const CreateFaceParams ¶ms, const FaceCreatedCallback &onCreated, const FaceCreationFailedCallback &onFailure)=0
Try to create face using the supplied parameters.
const FaceId INVALID_FACEID
indicates an invalid FaceId
const PacketCounter & nInData
const PacketCounter & nInNacks
the transport is temporarily down, and is being recovered