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