blob: b1e934f75d323341647fe16d9656e6dbe151a204 [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"
Davide Pesaventoa3148082018-04-12 18:21:54 -040027
28#include "core/logger.hpp"
Junxiao Shi40cb61c2015-09-23 18:36:45 -070029#include "face/generic-link-service.hpp"
Junxiao Shib8590312016-12-29 21:22:25 +000030#include "face/protocol-factory.hpp"
Yukai Tu0a49d342015-09-13 12:54:22 +080031#include "fw/face-table.hpp"
Yanbiao Li73860e32015-08-19 16:30:16 -070032
Eric Newberry0c3e57b2018-01-25 20:54:46 -070033#include <boost/logic/tribool.hpp>
34
Junxiao Shicbc8e942016-09-06 03:17:45 +000035#include <ndn-cxx/lp/tags.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000036#include <ndn-cxx/mgmt/nfd/channel-status.hpp>
Yanbiao Li73860e32015-08-19 16:30:16 -070037
Yanbiao Li73860e32015-08-19 16:30:16 -070038namespace nfd {
39
Davide Pesaventoa3148082018-04-12 18:21:54 -040040NFD_LOG_INIT(FaceManager);
Yanbiao Li73860e32015-08-19 16:30:16 -070041
Junxiao Shiea47bde2017-01-26 17:49:16 +000042FaceManager::FaceManager(FaceSystem& faceSystem,
Davide Pesaventocfb1a312018-03-01 01:30:56 -050043 Dispatcher& dispatcher,
44 CommandAuthenticator& authenticator)
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000045 : NfdManagerBase(dispatcher, authenticator, "faces")
Junxiao Shiea47bde2017-01-26 17:49:16 +000046 , m_faceSystem(faceSystem)
47 , m_faceTable(faceSystem.getFaceTable())
Yanbiao Li73860e32015-08-19 16:30:16 -070048{
Yanbiao Li58ba3f92017-02-15 14:27:18 +000049 // register handlers for ControlCommand
Yanbiao Li73860e32015-08-19 16:30:16 -070050 registerCommandHandler<ndn::nfd::FaceCreateCommand>("create",
51 bind(&FaceManager::createFace, this, _2, _3, _4, _5));
52
Eric Newberryb5aa7f52016-09-03 20:36:12 -070053 registerCommandHandler<ndn::nfd::FaceUpdateCommand>("update",
54 bind(&FaceManager::updateFace, this, _2, _3, _4, _5));
55
Yanbiao Li73860e32015-08-19 16:30:16 -070056 registerCommandHandler<ndn::nfd::FaceDestroyCommand>("destroy",
57 bind(&FaceManager::destroyFace, this, _2, _3, _4, _5));
58
Yanbiao Li58ba3f92017-02-15 14:27:18 +000059 // register handlers for StatusDataset
Yanbiao Li73860e32015-08-19 16:30:16 -070060 registerStatusDatasetHandler("list", bind(&FaceManager::listFaces, this, _1, _2, _3));
61 registerStatusDatasetHandler("channels", bind(&FaceManager::listChannels, this, _1, _2, _3));
62 registerStatusDatasetHandler("query", bind(&FaceManager::queryFaces, this, _1, _2, _3));
63
Yanbiao Li58ba3f92017-02-15 14:27:18 +000064 // register notification stream
Junxiao Shiae04d342016-07-19 13:20:22 +000065 m_postNotification = registerNotificationStream("events");
Eric Newberry1b4ba052016-10-07 23:04:07 -070066 m_faceAddConn = m_faceTable.afterAdd.connect([this] (const Face& face) {
67 connectFaceStateChangeSignal(face);
68 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_CREATED);
69 });
70 m_faceRemoveConn = m_faceTable.beforeRemove.connect([this] (const Face& face) {
71 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_DESTROYED);
72 });
Yanbiao Li73860e32015-08-19 16:30:16 -070073}
74
75void
Yanbiao Li73860e32015-08-19 16:30:16 -070076FaceManager::createFace(const Name& topPrefix, const Interest& interest,
77 const ControlParameters& parameters,
78 const ndn::mgmt::CommandContinuation& done)
79{
Eric Newberry78e32b02017-04-01 14:34:44 +000080 FaceUri remoteUri;
81 if (!remoteUri.parse(parameters.getUri())) {
82 NFD_LOG_TRACE("failed to parse remote URI: " << parameters.getUri());
Eric Newberry42602412016-08-27 09:33:18 -070083 done(ControlResponse(400, "Malformed command"));
84 return;
Yanbiao Li73860e32015-08-19 16:30:16 -070085 }
86
Eric Newberry78e32b02017-04-01 14:34:44 +000087 if (!remoteUri.isCanonical()) {
88 NFD_LOG_TRACE("received non-canonical remote URI: " << remoteUri.toString());
89 done(ControlResponse(400, "Non-canonical remote URI"));
Eric Newberry42602412016-08-27 09:33:18 -070090 return;
Yanbiao Li73860e32015-08-19 16:30:16 -070091 }
92
Davide Pesavento87fc0f82018-04-11 23:43:51 -040093 optional<FaceUri> localUri;
Eric Newberry78e32b02017-04-01 14:34:44 +000094 if (parameters.hasLocalUri()) {
95 localUri = FaceUri{};
96
97 if (!localUri->parse(parameters.getLocalUri())) {
98 NFD_LOG_TRACE("failed to parse local URI: " << parameters.getLocalUri());
99 done(ControlResponse(400, "Malformed command"));
100 return;
101 }
102
103 if (!localUri->isCanonical()) {
104 NFD_LOG_TRACE("received non-canonical local URI: " << localUri->toString());
105 done(ControlResponse(400, "Non-canonical local URI"));
106 return;
107 }
108 }
109
Davide Pesaventoe5eebad2017-04-06 20:23:26 -0400110 face::ProtocolFactory* factory = m_faceSystem.getFactoryByScheme(remoteUri.getScheme());
Junxiao Shib8590312016-12-29 21:22:25 +0000111 if (factory == nullptr) {
Eric Newberry78e32b02017-04-01 14:34:44 +0000112 NFD_LOG_TRACE("received create request for unsupported protocol: " << remoteUri.getScheme());
Eric Newberry42602412016-08-27 09:33:18 -0700113 done(ControlResponse(406, "Unsupported protocol"));
114 return;
Yanbiao Li73860e32015-08-19 16:30:16 -0700115 }
116
Davide Pesavento15b55052018-01-27 19:09:28 -0500117 face::FaceParams faceParams;
118 faceParams.persistency = parameters.getFacePersistency();
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700119 if (parameters.hasBaseCongestionMarkingInterval()) {
120 faceParams.baseCongestionMarkingInterval = parameters.getBaseCongestionMarkingInterval();
121 }
122 if (parameters.hasDefaultCongestionThreshold()) {
123 faceParams.defaultCongestionThreshold = parameters.getDefaultCongestionThreshold();
124 }
Eric Newberry812d6152018-06-06 15:06:01 -0700125 if (parameters.hasMtu()) {
126 faceParams.mtu = parameters.getMtu();
127 }
Davide Pesavento15b55052018-01-27 19:09:28 -0500128 faceParams.wantLocalFields = parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
129 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED);
130 faceParams.wantLpReliability = parameters.hasFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED) &&
131 parameters.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED);
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700132 if (parameters.hasFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) {
133 faceParams.wantCongestionMarking = parameters.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED);
134 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700135 try {
Davide Pesavento15b55052018-01-27 19:09:28 -0500136 factory->createFace({remoteUri, localUri, faceParams},
137 bind(&FaceManager::afterCreateFaceSuccess, this, parameters, _1, done),
138 bind(&FaceManager::afterCreateFaceFailure, this, _1, _2, done));
Yanbiao Li73860e32015-08-19 16:30:16 -0700139 }
140 catch (const std::runtime_error& error) {
Eric Newberry42602412016-08-27 09:33:18 -0700141 NFD_LOG_ERROR("Face creation failed: " << error.what());
142 done(ControlResponse(500, "Face creation failed due to internal error"));
143 return;
Yanbiao Li73860e32015-08-19 16:30:16 -0700144 }
145 catch (const std::logic_error& error) {
Eric Newberry42602412016-08-27 09:33:18 -0700146 NFD_LOG_ERROR("Face creation failed: " << error.what());
147 done(ControlResponse(500, "Face creation failed due to internal error"));
148 return;
Yanbiao Li73860e32015-08-19 16:30:16 -0700149 }
150}
151
152void
Eric Newberry42602412016-08-27 09:33:18 -0700153FaceManager::afterCreateFaceSuccess(const ControlParameters& parameters,
Eric Newberryf40551a2016-09-05 15:41:16 -0700154 const shared_ptr<Face>& face,
Yanbiao Li73860e32015-08-19 16:30:16 -0700155 const ndn::mgmt::CommandContinuation& done)
156{
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000157 if (face->getId() != face::INVALID_FACEID) {// Face already exists
158 NFD_LOG_TRACE("Attempted to create duplicate face of " << face->getId());
159
Junxiao Shi1cce2a32017-05-02 02:39:55 +0000160 ControlParameters response = collectFaceProperties(*face, true);
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000161 done(ControlResponse(409, "Face with remote URI already exists").setBody(response.wireEncode()));
162 return;
163 }
164
Eric Newberryf40551a2016-09-05 15:41:16 -0700165 // If scope non-local and flags set to enable local fields, request shouldn't
166 // have made it this far
167 BOOST_ASSERT(face->getScope() == ndn::nfd::FACE_SCOPE_LOCAL ||
168 !parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) ||
169 (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
170 !parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)));
171
172 m_faceTable.add(face);
173
Junxiao Shi1cce2a32017-05-02 02:39:55 +0000174 ControlParameters response = collectFaceProperties(*face, true);
Eric Newberry42602412016-08-27 09:33:18 -0700175 done(ControlResponse(200, "OK").setBody(response.wireEncode()));
176}
177
178void
179FaceManager::afterCreateFaceFailure(uint32_t status,
180 const std::string& reason,
181 const ndn::mgmt::CommandContinuation& done)
182{
183 NFD_LOG_DEBUG("Face creation failed: " << reason);
184
185 done(ControlResponse(status, reason));
Yanbiao Li73860e32015-08-19 16:30:16 -0700186}
187
188void
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700189FaceManager::updateFace(const Name& topPrefix, const Interest& interest,
190 const ControlParameters& parameters,
191 const ndn::mgmt::CommandContinuation& done)
192{
193 FaceId faceId = parameters.getFaceId();
194 if (faceId == 0) {
195 // Self-updating
196 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = interest.getTag<lp::IncomingFaceIdTag>();
197 if (incomingFaceIdTag == nullptr) {
198 NFD_LOG_TRACE("unable to determine face for self-update");
199 done(ControlResponse(404, "No FaceId specified and IncomingFaceId not available"));
200 return;
201 }
202 faceId = *incomingFaceIdTag;
203 }
204
205 Face* face = m_faceTable.get(faceId);
206
207 if (face == nullptr) {
208 NFD_LOG_TRACE("invalid face specified");
209 done(ControlResponse(404, "Specified face does not exist"));
210 return;
211 }
212
213 // Verify validity of requested changes
214 ControlParameters response;
215 bool areParamsValid = true;
216
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700217 if (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
218 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
219 face->getScope() != ndn::nfd::FACE_SCOPE_LOCAL) {
220 NFD_LOG_TRACE("received request to enable local fields on non-local face");
221 areParamsValid = false;
222 response.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED,
223 parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED));
224 }
225
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000226 // check whether the requested FacePersistency change is valid if it's present
227 if (parameters.hasFacePersistency()) {
228 auto persistency = parameters.getFacePersistency();
229 if (!face->getTransport()->canChangePersistencyTo(persistency)) {
230 NFD_LOG_TRACE("cannot change face persistency to " << persistency);
231 areParamsValid = false;
232 response.setFacePersistency(persistency);
233 }
234 }
235
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700236 if (!areParamsValid) {
237 done(ControlResponse(409, "Invalid properties specified").setBody(response.wireEncode()));
238 return;
239 }
240
241 // All specified properties are valid, so make changes
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000242 if (parameters.hasFacePersistency()) {
243 face->setPersistency(parameters.getFacePersistency());
244 }
Eric Newberry2642cd22017-07-13 21:34:53 -0400245 setLinkServiceOptions(*face, parameters);
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700246
Eric Newberryf40551a2016-09-05 15:41:16 -0700247 // Set ControlResponse fields
Junxiao Shi1cce2a32017-05-02 02:39:55 +0000248 response = collectFaceProperties(*face, false);
Eric Newberry812d6152018-06-06 15:06:01 -0700249 response.unsetMtu(); // This parameter is only included with the response to faces/create and FaceStatus
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700250
251 done(ControlResponse(200, "OK").setBody(response.wireEncode()));
252}
253
254void
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700255FaceManager::destroyFace(const Name& topPrefix, const Interest& interest,
256 const ControlParameters& parameters,
257 const ndn::mgmt::CommandContinuation& done)
258{
Junxiao Shi5b43f9a2016-07-19 13:15:56 +0000259 Face* face = m_faceTable.get(parameters.getFaceId());
260 if (face != nullptr) {
261 face->close();
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700262 }
263
264 done(ControlResponse(200, "OK").setBody(parameters.wireEncode()));
265}
266
267void
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700268FaceManager::setLinkServiceOptions(Face& face,
Eric Newberry2642cd22017-07-13 21:34:53 -0400269 const ControlParameters& parameters)
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700270{
271 auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService());
272 BOOST_ASSERT(linkService != nullptr);
273
274 auto options = linkService->getOptions();
275 if (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) &&
276 face.getScope() == ndn::nfd::FACE_SCOPE_LOCAL) {
277 options.allowLocalFields = parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED);
278 }
Eric Newberry2642cd22017-07-13 21:34:53 -0400279 if (parameters.hasFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)) {
280 options.reliabilityOptions.isEnabled = parameters.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED);
281 }
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700282 if (parameters.hasFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) {
283 options.allowCongestionMarking = parameters.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED);
284 }
285 if (parameters.hasBaseCongestionMarkingInterval()) {
286 options.baseCongestionMarkingInterval = parameters.getBaseCongestionMarkingInterval();
287 }
288 if (parameters.hasDefaultCongestionThreshold()) {
289 options.defaultCongestionThreshold = parameters.getDefaultCongestionThreshold();
290 }
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700291 linkService->setOptions(options);
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700292}
293
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000294ControlParameters
Junxiao Shi1cce2a32017-05-02 02:39:55 +0000295FaceManager::collectFaceProperties(const Face& face, bool wantUris)
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000296{
297 auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService());
298 BOOST_ASSERT(linkService != nullptr);
299 auto options = linkService->getOptions();
300
Eric Newberry812d6152018-06-06 15:06:01 -0700301 auto transport = face.getTransport();
302 BOOST_ASSERT(transport != nullptr);
303
Junxiao Shi1cce2a32017-05-02 02:39:55 +0000304 ControlParameters params;
305 params.setFaceId(face.getId())
306 .setFacePersistency(face.getPersistency())
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700307 .setBaseCongestionMarkingInterval(options.baseCongestionMarkingInterval)
308 .setDefaultCongestionThreshold(options.defaultCongestionThreshold)
Eric Newberry2642cd22017-07-13 21:34:53 -0400309 .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, options.allowLocalFields, false)
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700310 .setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, options.reliabilityOptions.isEnabled, false)
311 .setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED, options.allowCongestionMarking, false);
Eric Newberry812d6152018-06-06 15:06:01 -0700312
313 if (transport->getMtu() > 0) {
314 params.setMtu(std::min<uint64_t>(transport->getMtu(), ndn::MAX_NDN_PACKET_SIZE));
315 }
316 else if (transport->getMtu() == face::MTU_UNLIMITED) {
317 params.setMtu(ndn::MAX_NDN_PACKET_SIZE);
318 }
319
Junxiao Shi1cce2a32017-05-02 02:39:55 +0000320 if (wantUris) {
321 params.setUri(face.getRemoteUri().toString())
322 .setLocalUri(face.getLocalUri().toString());
323 }
324 return params;
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000325}
326
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700327void
Yanbiao Li73860e32015-08-19 16:30:16 -0700328FaceManager::listFaces(const Name& topPrefix, const Interest& interest,
329 ndn::mgmt::StatusDatasetContext& context)
330{
Eric Newberryc64d30a2015-12-26 11:07:27 -0700331 auto now = time::steady_clock::now();
Junxiao Shib84e6742016-07-19 13:16:22 +0000332 for (const Face& face : m_faceTable) {
333 ndn::nfd::FaceStatus status = collectFaceStatus(face, now);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700334 context.append(status.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700335 }
336 context.end();
337}
338
339void
340FaceManager::listChannels(const Name& topPrefix, const Interest& interest,
341 ndn::mgmt::StatusDatasetContext& context)
342{
Davide Pesaventoe5eebad2017-04-06 20:23:26 -0400343 std::set<const face::ProtocolFactory*> factories = m_faceSystem.listProtocolFactories();
344 for (const auto* factory : factories) {
Junxiao Shib8590312016-12-29 21:22:25 +0000345 for (const auto& channel : factory->getChannels()) {
346 ndn::nfd::ChannelStatus entry;
347 entry.setLocalUri(channel->getUri().toString());
348 context.append(entry.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700349 }
350 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700351 context.end();
352}
353
354void
355FaceManager::queryFaces(const Name& topPrefix, const Interest& interest,
356 ndn::mgmt::StatusDatasetContext& context)
357{
358 ndn::nfd::FaceQueryFilter faceFilter;
359 const Name& query = interest.getName();
360 try {
361 faceFilter.wireDecode(query[-1].blockFromValue());
362 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200363 catch (const tlv::Error& e) {
364 NFD_LOG_DEBUG("Malformed query filter: " << e.what());
365 return context.reject(ControlResponse(400, "Malformed filter"));
Yanbiao Li73860e32015-08-19 16:30:16 -0700366 }
367
Eric Newberryc64d30a2015-12-26 11:07:27 -0700368 auto now = time::steady_clock::now();
Junxiao Shib84e6742016-07-19 13:16:22 +0000369 for (const Face& face : m_faceTable) {
370 if (!matchFilter(faceFilter, face)) {
Junxiao Shida93f1f2015-11-11 06:13:16 -0700371 continue;
Yanbiao Li73860e32015-08-19 16:30:16 -0700372 }
Junxiao Shib84e6742016-07-19 13:16:22 +0000373 ndn::nfd::FaceStatus status = collectFaceStatus(face, now);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700374 context.append(status.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700375 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200376
Yanbiao Li73860e32015-08-19 16:30:16 -0700377 context.end();
378}
379
380bool
Junxiao Shib84e6742016-07-19 13:16:22 +0000381FaceManager::matchFilter(const ndn::nfd::FaceQueryFilter& filter, const Face& face)
Yanbiao Li73860e32015-08-19 16:30:16 -0700382{
383 if (filter.hasFaceId() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000384 filter.getFaceId() != static_cast<uint64_t>(face.getId())) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700385 return false;
386 }
387
388 if (filter.hasUriScheme() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000389 filter.getUriScheme() != face.getRemoteUri().getScheme() &&
390 filter.getUriScheme() != face.getLocalUri().getScheme()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700391 return false;
392 }
393
394 if (filter.hasRemoteUri() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000395 filter.getRemoteUri() != face.getRemoteUri().toString()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700396 return false;
397 }
398
399 if (filter.hasLocalUri() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000400 filter.getLocalUri() != face.getLocalUri().toString()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700401 return false;
402 }
403
404 if (filter.hasFaceScope() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000405 filter.getFaceScope() != face.getScope()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700406 return false;
407 }
408
409 if (filter.hasFacePersistency() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000410 filter.getFacePersistency() != face.getPersistency()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700411 return false;
412 }
413
414 if (filter.hasLinkType() &&
Junxiao Shib84e6742016-07-19 13:16:22 +0000415 filter.getLinkType() != face.getLinkType()) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700416 return false;
417 }
418
419 return true;
420}
421
Eric Newberryc64d30a2015-12-26 11:07:27 -0700422ndn::nfd::FaceStatus
423FaceManager::collectFaceStatus(const Face& face, const time::steady_clock::TimePoint& now)
424{
425 ndn::nfd::FaceStatus status;
426
427 collectFaceProperties(face, status);
428
429 time::steady_clock::TimePoint expirationTime = face.getExpirationTime();
430 if (expirationTime != time::steady_clock::TimePoint::max()) {
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700431 status.setExpirationPeriod(std::max(0_ms,
Eric Newberryc64d30a2015-12-26 11:07:27 -0700432 time::duration_cast<time::milliseconds>(expirationTime - now)));
433 }
434
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700435 // Get LinkService options
436 auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService());
437 if (linkService != nullptr) {
438 auto linkServiceOptions = linkService->getOptions();
439 status.setBaseCongestionMarkingInterval(linkServiceOptions.baseCongestionMarkingInterval);
440 status.setDefaultCongestionThreshold(linkServiceOptions.defaultCongestionThreshold);
441 }
442
Eric Newberryc64d30a2015-12-26 11:07:27 -0700443 const face::FaceCounters& counters = face.getCounters();
444 status.setNInInterests(counters.nInInterests)
445 .setNOutInterests(counters.nOutInterests)
Junxiao Shif03d4792017-04-06 16:41:22 +0000446 .setNInData(counters.nInData)
447 .setNOutData(counters.nOutData)
Eric Newberryc64d30a2015-12-26 11:07:27 -0700448 .setNInNacks(counters.nInNacks)
449 .setNOutNacks(counters.nOutNacks)
450 .setNInBytes(counters.nInBytes)
451 .setNOutBytes(counters.nOutBytes);
452
453 return status;
454}
455
Junxiao Shida93f1f2015-11-11 06:13:16 -0700456template<typename FaceTraits>
Junxiao Shicde37ad2015-12-24 01:02:05 -0700457void
458FaceManager::collectFaceProperties(const Face& face, FaceTraits& traits)
Junxiao Shida93f1f2015-11-11 06:13:16 -0700459{
460 traits.setFaceId(face.getId())
461 .setRemoteUri(face.getRemoteUri().toString())
462 .setLocalUri(face.getLocalUri().toString())
463 .setFaceScope(face.getScope())
464 .setFacePersistency(face.getPersistency())
465 .setLinkType(face.getLinkType());
Eric Newberry1b4ba052016-10-07 23:04:07 -0700466
467 // Set Flag bits
468 auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService());
469 if (linkService != nullptr) {
470 auto linkServiceOptions = linkService->getOptions();
471 traits.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, linkServiceOptions.allowLocalFields);
Eric Newberry84d3adc2017-08-09 23:31:40 -0400472 traits.setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED,
473 linkServiceOptions.reliabilityOptions.isEnabled);
Eric Newberryde332452018-01-30 11:45:32 -0700474 traits.setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED,
475 linkServiceOptions.allowCongestionMarking);
Eric Newberry1b4ba052016-10-07 23:04:07 -0700476 }
Junxiao Shida93f1f2015-11-11 06:13:16 -0700477}
478
479void
Eric Newberry1b4ba052016-10-07 23:04:07 -0700480FaceManager::notifyFaceEvent(const Face& face, ndn::nfd::FaceEventKind kind)
Yanbiao Li73860e32015-08-19 16:30:16 -0700481{
482 ndn::nfd::FaceEventNotification notification;
Eric Newberry1b4ba052016-10-07 23:04:07 -0700483 notification.setKind(kind);
Junxiao Shiae04d342016-07-19 13:20:22 +0000484 collectFaceProperties(face, notification);
Yanbiao Li73860e32015-08-19 16:30:16 -0700485
Junxiao Shiae04d342016-07-19 13:20:22 +0000486 m_postNotification(notification.wireEncode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700487}
488
489void
Eric Newberry1b4ba052016-10-07 23:04:07 -0700490FaceManager::connectFaceStateChangeSignal(const Face& face)
Yanbiao Li73860e32015-08-19 16:30:16 -0700491{
Davide Pesavento3cf75dc2018-03-17 00:38:03 -0400492 using face::FaceState;
493
Eric Newberry1b4ba052016-10-07 23:04:07 -0700494 FaceId faceId = face.getId();
495 m_faceStateChangeConn[faceId] = face.afterStateChange.connect(
Davide Pesavento3cf75dc2018-03-17 00:38:03 -0400496 [this, faceId, &face] (FaceState oldState, FaceState newState) {
497 if (newState == FaceState::UP) {
Eric Newberry1b4ba052016-10-07 23:04:07 -0700498 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_UP);
499 }
Davide Pesavento3cf75dc2018-03-17 00:38:03 -0400500 else if (newState == FaceState::DOWN) {
Eric Newberry1b4ba052016-10-07 23:04:07 -0700501 notifyFaceEvent(face, ndn::nfd::FACE_EVENT_DOWN);
502 }
Davide Pesavento3cf75dc2018-03-17 00:38:03 -0400503 else if (newState == FaceState::CLOSED) {
504 // cannot use face.getId() because it may already be reset to INVALID_FACEID
Eric Newberry1b4ba052016-10-07 23:04:07 -0700505 m_faceStateChangeConn.erase(faceId);
506 }
507 });
Yanbiao Li73860e32015-08-19 16:30:16 -0700508}
509
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200510} // namespace nfd