Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Eric Newberry | 944f38b | 2017-07-20 20:54:22 -0400 | [diff] [blame^] | 2 | /* |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 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. |
| 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 Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 27 | #include "face/generic-link-service.hpp" |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 28 | #include "face/protocol-factory.hpp" |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 29 | #include "fw/face-table.hpp" |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 30 | |
Junxiao Shi | cbc8e94 | 2016-09-06 03:17:45 +0000 | [diff] [blame] | 31 | #include <ndn-cxx/lp/tags.hpp> |
Junxiao Shi | 25c6ce4 | 2016-09-09 13:49:59 +0000 | [diff] [blame] | 32 | #include <ndn-cxx/mgmt/nfd/channel-status.hpp> |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 33 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 34 | namespace nfd { |
| 35 | |
| 36 | NFD_LOG_INIT("FaceManager"); |
| 37 | |
Junxiao Shi | ea47bde | 2017-01-26 17:49:16 +0000 | [diff] [blame] | 38 | FaceManager::FaceManager(FaceSystem& faceSystem, |
| 39 | Dispatcher& dispatcher, CommandAuthenticator& authenticator) |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 40 | : NfdManagerBase(dispatcher, authenticator, "faces") |
Junxiao Shi | ea47bde | 2017-01-26 17:49:16 +0000 | [diff] [blame] | 41 | , m_faceSystem(faceSystem) |
| 42 | , m_faceTable(faceSystem.getFaceTable()) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 43 | { |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 44 | // register handlers for ControlCommand |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 45 | registerCommandHandler<ndn::nfd::FaceCreateCommand>("create", |
| 46 | bind(&FaceManager::createFace, this, _2, _3, _4, _5)); |
| 47 | |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 48 | registerCommandHandler<ndn::nfd::FaceUpdateCommand>("update", |
| 49 | bind(&FaceManager::updateFace, this, _2, _3, _4, _5)); |
| 50 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 51 | registerCommandHandler<ndn::nfd::FaceDestroyCommand>("destroy", |
| 52 | bind(&FaceManager::destroyFace, this, _2, _3, _4, _5)); |
| 53 | |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 54 | // register handlers for StatusDataset |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 55 | 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 Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 59 | // register notification stream |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 60 | m_postNotification = registerNotificationStream("events"); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 61 | 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 Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void |
| 71 | FaceManager::setConfigFile(ConfigFile& configFile) |
| 72 | { |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 73 | m_faceSystem.setConfigFile(configFile); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void |
| 77 | FaceManager::createFace(const Name& topPrefix, const Interest& interest, |
| 78 | const ControlParameters& parameters, |
| 79 | const ndn::mgmt::CommandContinuation& done) |
| 80 | { |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 81 | FaceUri remoteUri; |
| 82 | if (!remoteUri.parse(parameters.getUri())) { |
| 83 | NFD_LOG_TRACE("failed to parse remote URI: " << parameters.getUri()); |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 84 | done(ControlResponse(400, "Malformed command")); |
| 85 | return; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 88 | if (!remoteUri.isCanonical()) { |
| 89 | NFD_LOG_TRACE("received non-canonical remote URI: " << remoteUri.toString()); |
| 90 | done(ControlResponse(400, "Non-canonical remote URI")); |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 91 | return; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 94 | 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 Pesavento | e5eebad | 2017-04-06 20:23:26 -0400 | [diff] [blame] | 111 | face::ProtocolFactory* factory = m_faceSystem.getFactoryByScheme(remoteUri.getScheme()); |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 112 | if (factory == nullptr) { |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 113 | NFD_LOG_TRACE("received create request for unsupported protocol: " << remoteUri.getScheme()); |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 114 | done(ControlResponse(406, "Unsupported protocol")); |
| 115 | return; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | try { |
Eric Newberry | 944f38b | 2017-07-20 20:54:22 -0400 | [diff] [blame^] | 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)}, |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 122 | bind(&FaceManager::afterCreateFaceSuccess, this, parameters, _1, done), |
| 123 | bind(&FaceManager::afterCreateFaceFailure, this, _1, _2, done)); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 124 | } |
| 125 | catch (const std::runtime_error& error) { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 126 | NFD_LOG_ERROR("Face creation failed: " << error.what()); |
| 127 | done(ControlResponse(500, "Face creation failed due to internal error")); |
| 128 | return; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 129 | } |
| 130 | catch (const std::logic_error& error) { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 131 | NFD_LOG_ERROR("Face creation failed: " << error.what()); |
| 132 | done(ControlResponse(500, "Face creation failed due to internal error")); |
| 133 | return; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | void |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 138 | FaceManager::afterCreateFaceSuccess(const ControlParameters& parameters, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 139 | const shared_ptr<Face>& face, |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 140 | const ndn::mgmt::CommandContinuation& done) |
| 141 | { |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 142 | if (face->getId() != face::INVALID_FACEID) {// Face already exists |
| 143 | NFD_LOG_TRACE("Attempted to create duplicate face of " << face->getId()); |
| 144 | |
Junxiao Shi | 1cce2a3 | 2017-05-02 02:39:55 +0000 | [diff] [blame] | 145 | ControlParameters response = collectFaceProperties(*face, true); |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 146 | done(ControlResponse(409, "Face with remote URI already exists").setBody(response.wireEncode())); |
| 147 | return; |
| 148 | } |
| 149 | |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 150 | // If scope non-local and flags set to enable local fields, request shouldn't |
| 151 | // have made it this far |
| 152 | BOOST_ASSERT(face->getScope() == ndn::nfd::FACE_SCOPE_LOCAL || |
| 153 | !parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) || |
| 154 | (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) && |
| 155 | !parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED))); |
| 156 | |
| 157 | m_faceTable.add(face); |
| 158 | |
Junxiao Shi | 1cce2a3 | 2017-05-02 02:39:55 +0000 | [diff] [blame] | 159 | ControlParameters response = collectFaceProperties(*face, true); |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 160 | done(ControlResponse(200, "OK").setBody(response.wireEncode())); |
| 161 | } |
| 162 | |
| 163 | void |
| 164 | FaceManager::afterCreateFaceFailure(uint32_t status, |
| 165 | const std::string& reason, |
| 166 | const ndn::mgmt::CommandContinuation& done) |
| 167 | { |
| 168 | NFD_LOG_DEBUG("Face creation failed: " << reason); |
| 169 | |
| 170 | done(ControlResponse(status, reason)); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 174 | FaceManager::updateFace(const Name& topPrefix, const Interest& interest, |
| 175 | const ControlParameters& parameters, |
| 176 | const ndn::mgmt::CommandContinuation& done) |
| 177 | { |
| 178 | FaceId faceId = parameters.getFaceId(); |
| 179 | if (faceId == 0) { |
| 180 | // Self-updating |
| 181 | shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = interest.getTag<lp::IncomingFaceIdTag>(); |
| 182 | if (incomingFaceIdTag == nullptr) { |
| 183 | NFD_LOG_TRACE("unable to determine face for self-update"); |
| 184 | done(ControlResponse(404, "No FaceId specified and IncomingFaceId not available")); |
| 185 | return; |
| 186 | } |
| 187 | faceId = *incomingFaceIdTag; |
| 188 | } |
| 189 | |
| 190 | Face* face = m_faceTable.get(faceId); |
| 191 | |
| 192 | if (face == nullptr) { |
| 193 | NFD_LOG_TRACE("invalid face specified"); |
| 194 | done(ControlResponse(404, "Specified face does not exist")); |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | // Verify validity of requested changes |
| 199 | ControlParameters response; |
| 200 | bool areParamsValid = true; |
| 201 | |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 202 | if (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) && |
| 203 | parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) && |
| 204 | face->getScope() != ndn::nfd::FACE_SCOPE_LOCAL) { |
| 205 | NFD_LOG_TRACE("received request to enable local fields on non-local face"); |
| 206 | areParamsValid = false; |
| 207 | response.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, |
| 208 | parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)); |
| 209 | } |
| 210 | |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 211 | // check whether the requested FacePersistency change is valid if it's present |
| 212 | if (parameters.hasFacePersistency()) { |
| 213 | auto persistency = parameters.getFacePersistency(); |
| 214 | if (!face->getTransport()->canChangePersistencyTo(persistency)) { |
| 215 | NFD_LOG_TRACE("cannot change face persistency to " << persistency); |
| 216 | areParamsValid = false; |
| 217 | response.setFacePersistency(persistency); |
| 218 | } |
| 219 | } |
| 220 | |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 221 | if (!areParamsValid) { |
| 222 | done(ControlResponse(409, "Invalid properties specified").setBody(response.wireEncode())); |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | // All specified properties are valid, so make changes |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 227 | if (parameters.hasFacePersistency()) { |
| 228 | face->setPersistency(parameters.getFacePersistency()); |
| 229 | } |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 230 | setLinkServiceOptions(*face, parameters, response); |
| 231 | |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 232 | // Set ControlResponse fields |
Junxiao Shi | 1cce2a3 | 2017-05-02 02:39:55 +0000 | [diff] [blame] | 233 | response = collectFaceProperties(*face, false); |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 234 | |
| 235 | done(ControlResponse(200, "OK").setBody(response.wireEncode())); |
| 236 | } |
| 237 | |
| 238 | void |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 239 | FaceManager::destroyFace(const Name& topPrefix, const Interest& interest, |
| 240 | const ControlParameters& parameters, |
| 241 | const ndn::mgmt::CommandContinuation& done) |
| 242 | { |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 243 | Face* face = m_faceTable.get(parameters.getFaceId()); |
| 244 | if (face != nullptr) { |
| 245 | face->close(); |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | done(ControlResponse(200, "OK").setBody(parameters.wireEncode())); |
| 249 | } |
| 250 | |
| 251 | void |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 252 | FaceManager::setLinkServiceOptions(Face& face, |
| 253 | const ControlParameters& parameters, |
| 254 | ControlParameters& response) |
| 255 | { |
| 256 | auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService()); |
| 257 | BOOST_ASSERT(linkService != nullptr); |
| 258 | |
| 259 | auto options = linkService->getOptions(); |
| 260 | if (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) && |
| 261 | face.getScope() == ndn::nfd::FACE_SCOPE_LOCAL) { |
| 262 | options.allowLocalFields = parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED); |
| 263 | } |
| 264 | linkService->setOptions(options); |
| 265 | |
| 266 | // Set Flags for ControlResponse |
| 267 | response.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, options.allowLocalFields, false); |
| 268 | } |
| 269 | |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 270 | ControlParameters |
Junxiao Shi | 1cce2a3 | 2017-05-02 02:39:55 +0000 | [diff] [blame] | 271 | FaceManager::collectFaceProperties(const Face& face, bool wantUris) |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 272 | { |
| 273 | auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService()); |
| 274 | BOOST_ASSERT(linkService != nullptr); |
| 275 | auto options = linkService->getOptions(); |
| 276 | |
Junxiao Shi | 1cce2a3 | 2017-05-02 02:39:55 +0000 | [diff] [blame] | 277 | ControlParameters params; |
| 278 | params.setFaceId(face.getId()) |
| 279 | .setFacePersistency(face.getPersistency()) |
| 280 | .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, options.allowLocalFields, false); |
| 281 | if (wantUris) { |
| 282 | params.setUri(face.getRemoteUri().toString()) |
| 283 | .setLocalUri(face.getLocalUri().toString()); |
| 284 | } |
| 285 | return params; |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 288 | void |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 289 | FaceManager::listFaces(const Name& topPrefix, const Interest& interest, |
| 290 | ndn::mgmt::StatusDatasetContext& context) |
| 291 | { |
Eric Newberry | c64d30a | 2015-12-26 11:07:27 -0700 | [diff] [blame] | 292 | auto now = time::steady_clock::now(); |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 293 | for (const Face& face : m_faceTable) { |
| 294 | ndn::nfd::FaceStatus status = collectFaceStatus(face, now); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 295 | context.append(status.wireEncode()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 296 | } |
| 297 | context.end(); |
| 298 | } |
| 299 | |
| 300 | void |
| 301 | FaceManager::listChannels(const Name& topPrefix, const Interest& interest, |
| 302 | ndn::mgmt::StatusDatasetContext& context) |
| 303 | { |
Davide Pesavento | e5eebad | 2017-04-06 20:23:26 -0400 | [diff] [blame] | 304 | std::set<const face::ProtocolFactory*> factories = m_faceSystem.listProtocolFactories(); |
| 305 | for (const auto* factory : factories) { |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 306 | for (const auto& channel : factory->getChannels()) { |
| 307 | ndn::nfd::ChannelStatus entry; |
| 308 | entry.setLocalUri(channel->getUri().toString()); |
| 309 | context.append(entry.wireEncode()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 312 | context.end(); |
| 313 | } |
| 314 | |
| 315 | void |
| 316 | FaceManager::queryFaces(const Name& topPrefix, const Interest& interest, |
| 317 | ndn::mgmt::StatusDatasetContext& context) |
| 318 | { |
| 319 | ndn::nfd::FaceQueryFilter faceFilter; |
| 320 | const Name& query = interest.getName(); |
| 321 | try { |
| 322 | faceFilter.wireDecode(query[-1].blockFromValue()); |
| 323 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 324 | catch (const tlv::Error& e) { |
| 325 | NFD_LOG_DEBUG("Malformed query filter: " << e.what()); |
| 326 | return context.reject(ControlResponse(400, "Malformed filter")); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Eric Newberry | c64d30a | 2015-12-26 11:07:27 -0700 | [diff] [blame] | 329 | auto now = time::steady_clock::now(); |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 330 | for (const Face& face : m_faceTable) { |
| 331 | if (!matchFilter(faceFilter, face)) { |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 332 | continue; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 333 | } |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 334 | ndn::nfd::FaceStatus status = collectFaceStatus(face, now); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 335 | context.append(status.wireEncode()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 336 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 337 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 338 | context.end(); |
| 339 | } |
| 340 | |
| 341 | bool |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 342 | FaceManager::matchFilter(const ndn::nfd::FaceQueryFilter& filter, const Face& face) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 343 | { |
| 344 | if (filter.hasFaceId() && |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 345 | filter.getFaceId() != static_cast<uint64_t>(face.getId())) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 346 | return false; |
| 347 | } |
| 348 | |
| 349 | if (filter.hasUriScheme() && |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 350 | filter.getUriScheme() != face.getRemoteUri().getScheme() && |
| 351 | filter.getUriScheme() != face.getLocalUri().getScheme()) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 352 | return false; |
| 353 | } |
| 354 | |
| 355 | if (filter.hasRemoteUri() && |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 356 | filter.getRemoteUri() != face.getRemoteUri().toString()) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 357 | return false; |
| 358 | } |
| 359 | |
| 360 | if (filter.hasLocalUri() && |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 361 | filter.getLocalUri() != face.getLocalUri().toString()) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 362 | return false; |
| 363 | } |
| 364 | |
| 365 | if (filter.hasFaceScope() && |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 366 | filter.getFaceScope() != face.getScope()) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 367 | return false; |
| 368 | } |
| 369 | |
| 370 | if (filter.hasFacePersistency() && |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 371 | filter.getFacePersistency() != face.getPersistency()) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 372 | return false; |
| 373 | } |
| 374 | |
| 375 | if (filter.hasLinkType() && |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 376 | filter.getLinkType() != face.getLinkType()) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 377 | return false; |
| 378 | } |
| 379 | |
| 380 | return true; |
| 381 | } |
| 382 | |
Eric Newberry | c64d30a | 2015-12-26 11:07:27 -0700 | [diff] [blame] | 383 | ndn::nfd::FaceStatus |
| 384 | FaceManager::collectFaceStatus(const Face& face, const time::steady_clock::TimePoint& now) |
| 385 | { |
| 386 | ndn::nfd::FaceStatus status; |
| 387 | |
| 388 | collectFaceProperties(face, status); |
| 389 | |
| 390 | time::steady_clock::TimePoint expirationTime = face.getExpirationTime(); |
| 391 | if (expirationTime != time::steady_clock::TimePoint::max()) { |
| 392 | status.setExpirationPeriod(std::max(time::milliseconds(0), |
| 393 | time::duration_cast<time::milliseconds>(expirationTime - now))); |
| 394 | } |
| 395 | |
| 396 | const face::FaceCounters& counters = face.getCounters(); |
| 397 | status.setNInInterests(counters.nInInterests) |
| 398 | .setNOutInterests(counters.nOutInterests) |
Junxiao Shi | f03d479 | 2017-04-06 16:41:22 +0000 | [diff] [blame] | 399 | .setNInData(counters.nInData) |
| 400 | .setNOutData(counters.nOutData) |
Eric Newberry | c64d30a | 2015-12-26 11:07:27 -0700 | [diff] [blame] | 401 | .setNInNacks(counters.nInNacks) |
| 402 | .setNOutNacks(counters.nOutNacks) |
| 403 | .setNInBytes(counters.nInBytes) |
| 404 | .setNOutBytes(counters.nOutBytes); |
| 405 | |
| 406 | return status; |
| 407 | } |
| 408 | |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 409 | template<typename FaceTraits> |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 410 | void |
| 411 | FaceManager::collectFaceProperties(const Face& face, FaceTraits& traits) |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 412 | { |
| 413 | traits.setFaceId(face.getId()) |
| 414 | .setRemoteUri(face.getRemoteUri().toString()) |
| 415 | .setLocalUri(face.getLocalUri().toString()) |
| 416 | .setFaceScope(face.getScope()) |
| 417 | .setFacePersistency(face.getPersistency()) |
| 418 | .setLinkType(face.getLinkType()); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 419 | |
| 420 | // Set Flag bits |
| 421 | auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService()); |
| 422 | if (linkService != nullptr) { |
| 423 | auto linkServiceOptions = linkService->getOptions(); |
| 424 | traits.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, linkServiceOptions.allowLocalFields); |
| 425 | } |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | void |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 429 | FaceManager::notifyFaceEvent(const Face& face, ndn::nfd::FaceEventKind kind) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 430 | { |
| 431 | ndn::nfd::FaceEventNotification notification; |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 432 | notification.setKind(kind); |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 433 | collectFaceProperties(face, notification); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 434 | |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 435 | m_postNotification(notification.wireEncode()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | void |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 439 | FaceManager::connectFaceStateChangeSignal(const Face& face) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 440 | { |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 441 | FaceId faceId = face.getId(); |
| 442 | m_faceStateChangeConn[faceId] = face.afterStateChange.connect( |
| 443 | [this, faceId] (face::FaceState oldState, face::FaceState newState) { |
| 444 | const Face& face = *m_faceTable.get(faceId); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 445 | |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 446 | if (newState == face::FaceState::UP) { |
| 447 | notifyFaceEvent(face, ndn::nfd::FACE_EVENT_UP); |
| 448 | } |
| 449 | else if (newState == face::FaceState::DOWN) { |
| 450 | notifyFaceEvent(face, ndn::nfd::FACE_EVENT_DOWN); |
| 451 | } |
| 452 | else if (newState == face::FaceState::CLOSED) { |
| 453 | m_faceStateChangeConn.erase(faceId); |
| 454 | } |
| 455 | }); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 458 | } // namespace nfd |