blob: 7ddf6eaf7b06d01382bb9a455350426e5482c11e [file] [log] [blame]
Yanbiao Li73860e32015-08-19 16:30:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberry944f38b2017-07-20 20:54:22 -04002/*
Davide Pesavento15b55052018-01-27 19:09:28 -05003 * Copyright (c) 2014-2018, Regents of the University of California,
Yanbiao Li73860e32015-08-19 16:30:16 -07004 * 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 "face-manager.hpp"
Junxiao Shi40cb61c2015-09-23 18:36:45 -070027#include "face/generic-link-service.hpp"
Junxiao Shib8590312016-12-29 21:22:25 +000028#include "face/protocol-factory.hpp"
Yukai Tu0a49d342015-09-13 12:54:22 +080029#include "fw/face-table.hpp"
Yanbiao Li73860e32015-08-19 16:30:16 -070030
Junxiao Shicbc8e942016-09-06 03:17:45 +000031#include <ndn-cxx/lp/tags.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000032#include <ndn-cxx/mgmt/nfd/channel-status.hpp>
Yanbiao Li73860e32015-08-19 16:30:16 -070033
Yanbiao Li73860e32015-08-19 16:30:16 -070034namespace nfd {
35
36NFD_LOG_INIT("FaceManager");
37
Junxiao Shiea47bde2017-01-26 17:49:16 +000038FaceManager::FaceManager(FaceSystem& faceSystem,
39 Dispatcher& dispatcher, CommandAuthenticator& authenticator)
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000040 : NfdManagerBase(dispatcher, authenticator, "faces")
Junxiao Shiea47bde2017-01-26 17:49:16 +000041 , m_faceSystem(faceSystem)
42 , m_faceTable(faceSystem.getFaceTable())
Yanbiao Li73860e32015-08-19 16:30:16 -070043{
Yanbiao Li58ba3f92017-02-15 14:27:18 +000044 // register handlers for ControlCommand
Yanbiao Li73860e32015-08-19 16:30:16 -070045 registerCommandHandler<ndn::nfd::FaceCreateCommand>("create",
46 bind(&FaceManager::createFace, this, _2, _3, _4, _5));
47
Eric Newberryb5aa7f52016-09-03 20:36:12 -070048 registerCommandHandler<ndn::nfd::FaceUpdateCommand>("update",
49 bind(&FaceManager::updateFace, this, _2, _3, _4, _5));
50
Yanbiao Li73860e32015-08-19 16:30:16 -070051 registerCommandHandler<ndn::nfd::FaceDestroyCommand>("destroy",
52 bind(&FaceManager::destroyFace, this, _2, _3, _4, _5));
53
Yanbiao Li58ba3f92017-02-15 14:27:18 +000054 // register handlers for StatusDataset
Yanbiao Li73860e32015-08-19 16:30:16 -070055 registerStatusDatasetHandler("list", bind(&FaceManager::listFaces, this, _1, _2, _3));
56 registerStatusDatasetHandler("channels", bind(&FaceManager::listChannels, this, _1, _2, _3));
57 registerStatusDatasetHandler("query", bind(&FaceManager::queryFaces, this, _1, _2, _3));
58
Yanbiao Li58ba3f92017-02-15 14:27:18 +000059 // register notification stream
Junxiao Shiae04d342016-07-19 13:20:22 +000060 m_postNotification = registerNotificationStream("events");
Eric Newberry1b4ba052016-10-07 23:04:07 -070061 m_faceAddConn = m_faceTable.afterAdd.connect([this] (const Face& face) {
62 connectFaceStateChangeSignal(face);
63 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_CREATED);
64 });
65 m_faceRemoveConn = m_faceTable.beforeRemove.connect([this] (const Face& face) {
66 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_DESTROYED);
67 });
Yanbiao Li73860e32015-08-19 16:30:16 -070068}
69
70void
71FaceManager::setConfigFile(ConfigFile& configFile)
72{
Junxiao Shib8590312016-12-29 21:22:25 +000073 m_faceSystem.setConfigFile(configFile);
Yanbiao Li73860e32015-08-19 16:30:16 -070074}
75
76void
77FaceManager::createFace(const Name& topPrefix, const Interest& interest,
78 const ControlParameters& parameters,
79 const ndn::mgmt::CommandContinuation& done)
80{
Eric Newberry78e32b02017-04-01 14:34:44 +000081 FaceUri remoteUri;
82 if (!remoteUri.parse(parameters.getUri())) {
83 NFD_LOG_TRACE("failed to parse remote URI: " << parameters.getUri());
Eric Newberry42602412016-08-27 09:33:18 -070084 done(ControlResponse(400, "Malformed command"));
85 return;
Yanbiao Li73860e32015-08-19 16:30:16 -070086 }
87
Eric Newberry78e32b02017-04-01 14:34:44 +000088 if (!remoteUri.isCanonical()) {
89 NFD_LOG_TRACE("received non-canonical remote URI: " << remoteUri.toString());
90 done(ControlResponse(400, "Non-canonical remote URI"));
Eric Newberry42602412016-08-27 09:33:18 -070091 return;
Yanbiao Li73860e32015-08-19 16:30:16 -070092 }
93
Eric Newberry78e32b02017-04-01 14:34:44 +000094 ndn::optional<FaceUri> localUri;
95 if (parameters.hasLocalUri()) {
96 localUri = FaceUri{};
97
98 if (!localUri->parse(parameters.getLocalUri())) {
99 NFD_LOG_TRACE("failed to parse local URI: " << parameters.getLocalUri());
100 done(ControlResponse(400, "Malformed command"));
101 return;
102 }
103
104 if (!localUri->isCanonical()) {
105 NFD_LOG_TRACE("received non-canonical local URI: " << localUri->toString());
106 done(ControlResponse(400, "Non-canonical local URI"));
107 return;
108 }
109 }
110
Davide Pesaventoe5eebad2017-04-06 20:23:26 -0400111 face::ProtocolFactory* factory = m_faceSystem.getFactoryByScheme(remoteUri.getScheme());
Junxiao Shib8590312016-12-29 21:22:25 +0000112 if (factory == nullptr) {
Eric Newberry78e32b02017-04-01 14:34:44 +0000113 NFD_LOG_TRACE("received create request for unsupported protocol: " << remoteUri.getScheme());
Eric Newberry42602412016-08-27 09:33:18 -0700114 done(ControlResponse(406, "Unsupported protocol"));
115 return;
Yanbiao Li73860e32015-08-19 16:30:16 -0700116 }
117
Davide Pesavento15b55052018-01-27 19:09:28 -0500118 face::FaceParams faceParams;
119 faceParams.persistency = parameters.getFacePersistency();
120 faceParams.wantLocalFields = parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
121 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED);
122 faceParams.wantLpReliability = parameters.hasFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED) &&
123 parameters.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED);
Yanbiao Li73860e32015-08-19 16:30:16 -0700124 try {
Davide Pesavento15b55052018-01-27 19:09:28 -0500125 factory->createFace({remoteUri, localUri, faceParams},
126 bind(&FaceManager::afterCreateFaceSuccess, this, parameters, _1, done),
127 bind(&FaceManager::afterCreateFaceFailure, this, _1, _2, done));
Yanbiao Li73860e32015-08-19 16:30:16 -0700128 }
129 catch (const std::runtime_error& error) {
Eric Newberry42602412016-08-27 09:33:18 -0700130 NFD_LOG_ERROR("Face creation failed: " << error.what());
131 done(ControlResponse(500, "Face creation failed due to internal error"));
132 return;
Yanbiao Li73860e32015-08-19 16:30:16 -0700133 }
134 catch (const std::logic_error& error) {
Eric Newberry42602412016-08-27 09:33:18 -0700135 NFD_LOG_ERROR("Face creation failed: " << error.what());
136 done(ControlResponse(500, "Face creation failed due to internal error"));
137 return;
Yanbiao Li73860e32015-08-19 16:30:16 -0700138 }
139}
140
141void
Eric Newberry42602412016-08-27 09:33:18 -0700142FaceManager::afterCreateFaceSuccess(const ControlParameters& parameters,
Eric Newberryf40551a2016-09-05 15:41:16 -0700143 const shared_ptr<Face>& face,
Yanbiao Li73860e32015-08-19 16:30:16 -0700144 const ndn::mgmt::CommandContinuation& done)
145{
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000146 if (face->getId() != face::INVALID_FACEID) {// Face already exists
147 NFD_LOG_TRACE("Attempted to create duplicate face of " << face->getId());
148
Junxiao Shi1cce2a32017-05-02 02:39:55 +0000149 ControlParameters response = collectFaceProperties(*face, true);
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000150 done(ControlResponse(409, "Face with remote URI already exists").setBody(response.wireEncode()));
151 return;
152 }
153
Eric Newberryf40551a2016-09-05 15:41:16 -0700154 // If scope non-local and flags set to enable local fields, request shouldn't
155 // have made it this far
156 BOOST_ASSERT(face->getScope() == ndn::nfd::FACE_SCOPE_LOCAL ||
157 !parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) ||
158 (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
159 !parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)));
160
161 m_faceTable.add(face);
162
Junxiao Shi1cce2a32017-05-02 02:39:55 +0000163 ControlParameters response = collectFaceProperties(*face, true);
Eric Newberry42602412016-08-27 09:33:18 -0700164 done(ControlResponse(200, "OK").setBody(response.wireEncode()));
165}
166
167void
168FaceManager::afterCreateFaceFailure(uint32_t status,
169 const std::string& reason,
170 const ndn::mgmt::CommandContinuation& done)
171{
172 NFD_LOG_DEBUG("Face creation failed: " << reason);
173
174 done(ControlResponse(status, reason));
Yanbiao Li73860e32015-08-19 16:30:16 -0700175}
176
177void
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700178FaceManager::updateFace(const Name& topPrefix, const Interest& interest,
179 const ControlParameters& parameters,
180 const ndn::mgmt::CommandContinuation& done)
181{
182 FaceId faceId = parameters.getFaceId();
183 if (faceId == 0) {
184 // Self-updating
185 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = interest.getTag<lp::IncomingFaceIdTag>();
186 if (incomingFaceIdTag == nullptr) {
187 NFD_LOG_TRACE("unable to determine face for self-update");
188 done(ControlResponse(404, "No FaceId specified and IncomingFaceId not available"));
189 return;
190 }
191 faceId = *incomingFaceIdTag;
192 }
193
194 Face* face = m_faceTable.get(faceId);
195
196 if (face == nullptr) {
197 NFD_LOG_TRACE("invalid face specified");
198 done(ControlResponse(404, "Specified face does not exist"));
199 return;
200 }
201
202 // Verify validity of requested changes
203 ControlParameters response;
204 bool areParamsValid = true;
205
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700206 if (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
207 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
208 face->getScope() != ndn::nfd::FACE_SCOPE_LOCAL) {
209 NFD_LOG_TRACE("received request to enable local fields on non-local face");
210 areParamsValid = false;
211 response.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED,
212 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED));
213 }
214
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000215 // check whether the requested FacePersistency change is valid if it's present
216 if (parameters.hasFacePersistency()) {
217 auto persistency = parameters.getFacePersistency();
218 if (!face->getTransport()->canChangePersistencyTo(persistency)) {
219 NFD_LOG_TRACE("cannot change face persistency to " << persistency);
220 areParamsValid = false;
221 response.setFacePersistency(persistency);
222 }
223 }
224
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700225 if (!areParamsValid) {
226 done(ControlResponse(409, "Invalid properties specified").setBody(response.wireEncode()));
227 return;
228 }
229
230 // All specified properties are valid, so make changes
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000231 if (parameters.hasFacePersistency()) {
232 face->setPersistency(parameters.getFacePersistency());
233 }
Eric Newberry2642cd22017-07-13 21:34:53 -0400234 setLinkServiceOptions(*face, parameters);
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700235
Eric Newberryf40551a2016-09-05 15:41:16 -0700236 // Set ControlResponse fields
Junxiao Shi1cce2a32017-05-02 02:39:55 +0000237 response = collectFaceProperties(*face, false);
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700238
239 done(ControlResponse(200, "OK").setBody(response.wireEncode()));
240}
241
242void
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700243FaceManager::destroyFace(const Name& topPrefix, const Interest& interest,
244 const ControlParameters& parameters,
245 const ndn::mgmt::CommandContinuation& done)
246{
Junxiao Shi5b43f9a2016-07-19 13:15:56 +0000247 Face* face = m_faceTable.get(parameters.getFaceId());
248 if (face != nullptr) {
249 face->close();
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700250 }
251
252 done(ControlResponse(200, "OK").setBody(parameters.wireEncode()));
253}
254
255void
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700256FaceManager::setLinkServiceOptions(Face& face,
Eric Newberry2642cd22017-07-13 21:34:53 -0400257 const ControlParameters& parameters)
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700258{
259 auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService());
260 BOOST_ASSERT(linkService != nullptr);
261
262 auto options = linkService->getOptions();
263 if (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
264 face.getScope() == ndn::nfd::FACE_SCOPE_LOCAL) {
265 options.allowLocalFields = parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED);
266 }
Eric Newberry2642cd22017-07-13 21:34:53 -0400267 if (parameters.hasFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)) {
268 options.reliabilityOptions.isEnabled = parameters.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED);
269 }
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700270 linkService->setOptions(options);
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700271}
272
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000273ControlParameters
Junxiao Shi1cce2a32017-05-02 02:39:55 +0000274FaceManager::collectFaceProperties(const Face& face, bool wantUris)
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000275{
276 auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService());
277 BOOST_ASSERT(linkService != nullptr);
278 auto options = linkService->getOptions();
279
Junxiao Shi1cce2a32017-05-02 02:39:55 +0000280 ControlParameters params;
281 params.setFaceId(face.getId())
282 .setFacePersistency(face.getPersistency())
Eric Newberry2642cd22017-07-13 21:34:53 -0400283 .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, options.allowLocalFields, false)
284 .setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, options.reliabilityOptions.isEnabled, false);
Junxiao Shi1cce2a32017-05-02 02:39:55 +0000285 if (wantUris) {
286 params.setUri(face.getRemoteUri().toString())
287 .setLocalUri(face.getLocalUri().toString());
288 }
289 return params;
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000290}
291
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700292void
Yanbiao Li73860e32015-08-19 16:30:16 -0700293FaceManager::listFaces(const Name& topPrefix, const Interest& interest,
294 ndn::mgmt::StatusDatasetContext& context)
295{
Eric Newberryc64d30a2015-12-26 11:07:27 -0700296 auto now = time::steady_clock::now();
Junxiao Shib84e6742016-07-19 13:16:22 +0000297 for (const Face& face : m_faceTable) {
298 ndn::nfd::FaceStatus status = collectFaceStatus(face, now);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700299 context.append(status.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700300 }
301 context.end();
302}
303
304void
305FaceManager::listChannels(const Name& topPrefix, const Interest& interest,
306 ndn::mgmt::StatusDatasetContext& context)
307{
Davide Pesaventoe5eebad2017-04-06 20:23:26 -0400308 std::set<const face::ProtocolFactory*> factories = m_faceSystem.listProtocolFactories();
309 for (const auto* factory : factories) {
Junxiao Shib8590312016-12-29 21:22:25 +0000310 for (const auto& channel : factory->getChannels()) {
311 ndn::nfd::ChannelStatus entry;
312 entry.setLocalUri(channel->getUri().toString());
313 context.append(entry.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700314 }
315 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700316 context.end();
317}
318
319void
320FaceManager::queryFaces(const Name& topPrefix, const Interest& interest,
321 ndn::mgmt::StatusDatasetContext& context)
322{
323 ndn::nfd::FaceQueryFilter faceFilter;
324 const Name& query = interest.getName();
325 try {
326 faceFilter.wireDecode(query[-1].blockFromValue());
327 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200328 catch (const tlv::Error& e) {
329 NFD_LOG_DEBUG("Malformed query filter: " << e.what());
330 return context.reject(ControlResponse(400, "Malformed filter"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700331 }
332
Eric Newberryc64d30a2015-12-26 11:07:27 -0700333 auto now = time::steady_clock::now();
Junxiao Shib84e6742016-07-19 13:16:22 +0000334 for (const Face& face : m_faceTable) {
335 if (!matchFilter(faceFilter, face)) {
Junxiao Shida93f1f2015-11-11 06:13:16 -0700336 continue;
Yanbiao Li73860e32015-08-19 16:30:16 -0700337 }
Junxiao Shib84e6742016-07-19 13:16:22 +0000338 ndn::nfd::FaceStatus status = collectFaceStatus(face, now);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700339 context.append(status.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700340 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200341
Yanbiao Li73860e32015-08-19 16:30:16 -0700342 context.end();
343}
344
345bool
Junxiao Shib84e6742016-07-19 13:16:22 +0000346FaceManager::matchFilter(const ndn::nfd::FaceQueryFilter& filter, const Face& face)
Yanbiao Li73860e32015-08-19 16:30:16 -0700347{
348 if (filter.hasFaceId() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000349 filter.getFaceId() != static_cast<uint64_t>(face.getId())) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700350 return false;
351 }
352
353 if (filter.hasUriScheme() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000354 filter.getUriScheme() != face.getRemoteUri().getScheme() &&
355 filter.getUriScheme() != face.getLocalUri().getScheme()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700356 return false;
357 }
358
359 if (filter.hasRemoteUri() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000360 filter.getRemoteUri() != face.getRemoteUri().toString()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700361 return false;
362 }
363
364 if (filter.hasLocalUri() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000365 filter.getLocalUri() != face.getLocalUri().toString()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700366 return false;
367 }
368
369 if (filter.hasFaceScope() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000370 filter.getFaceScope() != face.getScope()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700371 return false;
372 }
373
374 if (filter.hasFacePersistency() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000375 filter.getFacePersistency() != face.getPersistency()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700376 return false;
377 }
378
379 if (filter.hasLinkType() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000380 filter.getLinkType() != face.getLinkType()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700381 return false;
382 }
383
384 return true;
385}
386
Eric Newberryc64d30a2015-12-26 11:07:27 -0700387ndn::nfd::FaceStatus
388FaceManager::collectFaceStatus(const Face& face, const time::steady_clock::TimePoint& now)
389{
390 ndn::nfd::FaceStatus status;
391
392 collectFaceProperties(face, status);
393
394 time::steady_clock::TimePoint expirationTime = face.getExpirationTime();
395 if (expirationTime != time::steady_clock::TimePoint::max()) {
396 status.setExpirationPeriod(std::max(time::milliseconds(0),
397 time::duration_cast<time::milliseconds>(expirationTime - now)));
398 }
399
400 const face::FaceCounters& counters = face.getCounters();
401 status.setNInInterests(counters.nInInterests)
402 .setNOutInterests(counters.nOutInterests)
Junxiao Shif03d4792017-04-06 16:41:22 +0000403 .setNInData(counters.nInData)
404 .setNOutData(counters.nOutData)
Eric Newberryc64d30a2015-12-26 11:07:27 -0700405 .setNInNacks(counters.nInNacks)
406 .setNOutNacks(counters.nOutNacks)
407 .setNInBytes(counters.nInBytes)
408 .setNOutBytes(counters.nOutBytes);
409
410 return status;
411}
412
Junxiao Shida93f1f2015-11-11 06:13:16 -0700413template<typename FaceTraits>
Junxiao Shicde37ad2015-12-24 01:02:05 -0700414void
415FaceManager::collectFaceProperties(const Face& face, FaceTraits& traits)
Junxiao Shida93f1f2015-11-11 06:13:16 -0700416{
417 traits.setFaceId(face.getId())
418 .setRemoteUri(face.getRemoteUri().toString())
419 .setLocalUri(face.getLocalUri().toString())
420 .setFaceScope(face.getScope())
421 .setFacePersistency(face.getPersistency())
422 .setLinkType(face.getLinkType());
Eric Newberry1b4ba052016-10-07 23:04:07 -0700423
424 // Set Flag bits
425 auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService());
426 if (linkService != nullptr) {
427 auto linkServiceOptions = linkService->getOptions();
428 traits.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, linkServiceOptions.allowLocalFields);
Eric Newberry84d3adc2017-08-09 23:31:40 -0400429 traits.setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED,
430 linkServiceOptions.reliabilityOptions.isEnabled);
Eric Newberry1b4ba052016-10-07 23:04:07 -0700431 }
Junxiao Shida93f1f2015-11-11 06:13:16 -0700432}
433
434void
Eric Newberry1b4ba052016-10-07 23:04:07 -0700435FaceManager::notifyFaceEvent(const Face& face, ndn::nfd::FaceEventKind kind)
Yanbiao Li73860e32015-08-19 16:30:16 -0700436{
437 ndn::nfd::FaceEventNotification notification;
Eric Newberry1b4ba052016-10-07 23:04:07 -0700438 notification.setKind(kind);
Junxiao Shiae04d342016-07-19 13:20:22 +0000439 collectFaceProperties(face, notification);
Yanbiao Li73860e32015-08-19 16:30:16 -0700440
Junxiao Shiae04d342016-07-19 13:20:22 +0000441 m_postNotification(notification.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700442}
443
444void
Eric Newberry1b4ba052016-10-07 23:04:07 -0700445FaceManager::connectFaceStateChangeSignal(const Face& face)
Yanbiao Li73860e32015-08-19 16:30:16 -0700446{
Eric Newberry1b4ba052016-10-07 23:04:07 -0700447 FaceId faceId = face.getId();
448 m_faceStateChangeConn[faceId] = face.afterStateChange.connect(
449 [this, faceId] (face::FaceState oldState, face::FaceState newState) {
450 const Face& face = *m_faceTable.get(faceId);
Yanbiao Li73860e32015-08-19 16:30:16 -0700451
Eric Newberry1b4ba052016-10-07 23:04:07 -0700452 if (newState == face::FaceState::UP) {
453 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_UP);
454 }
455 else if (newState == face::FaceState::DOWN) {
456 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_DOWN);
457 }
458 else if (newState == face::FaceState::CLOSED) {
459 m_faceStateChangeConn.erase(faceId);
460 }
461 });
Yanbiao Li73860e32015-08-19 16:30:16 -0700462}
463
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200464} // namespace nfd