Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 319f2c8 | 2015-01-07 14:56:53 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 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. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 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/>. |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 24 | */ |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 25 | |
| 26 | #include "face-manager.hpp" |
| 27 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 28 | #include "core/logger.hpp" |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 29 | #include "core/config-file.hpp" |
Alexander Afanasyev | dbfedcf | 2014-12-20 19:57:36 -0800 | [diff] [blame] | 30 | #include "face/protocol-factory.hpp" |
| 31 | #include "fw/face-table.hpp" |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 32 | |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 33 | #include <ndn-cxx/management/nfd-face-event-notification.hpp> |
Chengyu Fan | 320d233 | 2014-10-29 16:40:33 -0600 | [diff] [blame] | 34 | #include <ndn-cxx/management/nfd-face-query-filter.hpp> |
Davide Pesavento | 52a18f9 | 2014-04-10 00:55:01 +0200 | [diff] [blame] | 35 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 36 | namespace nfd { |
| 37 | |
| 38 | NFD_LOG_INIT("FaceManager"); |
| 39 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 40 | const Name FaceManager::COMMAND_PREFIX("/localhost/nfd/faces"); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 41 | |
| 42 | const size_t FaceManager::COMMAND_UNSIGNED_NCOMPS = |
| 43 | FaceManager::COMMAND_PREFIX.size() + |
| 44 | 1 + // verb |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 45 | 1; // verb parameters |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 46 | |
| 47 | const size_t FaceManager::COMMAND_SIGNED_NCOMPS = |
| 48 | FaceManager::COMMAND_UNSIGNED_NCOMPS + |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 49 | 4; // (timestamp, nonce, signed info tlv, signature tlv) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 50 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 51 | const FaceManager::SignedVerbAndProcessor FaceManager::SIGNED_COMMAND_VERBS[] = |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 52 | { |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 53 | SignedVerbAndProcessor( |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 54 | Name::Component("create"), |
| 55 | &FaceManager::createFace |
| 56 | ), |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 57 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 58 | SignedVerbAndProcessor( |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 59 | Name::Component("destroy"), |
| 60 | &FaceManager::destroyFace |
| 61 | ), |
| 62 | |
| 63 | SignedVerbAndProcessor( |
| 64 | Name::Component("enable-local-control"), |
| 65 | &FaceManager::enableLocalControl |
| 66 | ), |
| 67 | |
| 68 | SignedVerbAndProcessor( |
| 69 | Name::Component("disable-local-control"), |
| 70 | &FaceManager::disableLocalControl |
| 71 | ), |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 74 | const FaceManager::UnsignedVerbAndProcessor FaceManager::UNSIGNED_COMMAND_VERBS[] = |
| 75 | { |
| 76 | UnsignedVerbAndProcessor( |
| 77 | Name::Component("list"), |
| 78 | &FaceManager::listFaces |
| 79 | ), |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 80 | |
| 81 | UnsignedVerbAndProcessor( |
| 82 | Name::Component("events"), |
| 83 | &FaceManager::ignoreUnsignedVerb |
| 84 | ), |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 85 | |
| 86 | UnsignedVerbAndProcessor( |
| 87 | Name::Component("channels"), |
| 88 | &FaceManager::listChannels |
| 89 | ), |
Chengyu Fan | 320d233 | 2014-10-29 16:40:33 -0600 | [diff] [blame] | 90 | |
| 91 | UnsignedVerbAndProcessor( |
| 92 | Name::Component("query"), |
| 93 | &FaceManager::listQueriedFaces |
| 94 | ), |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 95 | }; |
| 96 | |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 97 | const Name FaceManager::FACES_LIST_DATASET_PREFIX("/localhost/nfd/faces/list"); |
| 98 | const size_t FaceManager::FACES_LIST_DATASET_NCOMPS = FACES_LIST_DATASET_PREFIX.size(); |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 99 | |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 100 | const Name FaceManager::FACE_EVENTS_PREFIX("/localhost/nfd/faces/events"); |
| 101 | |
| 102 | const Name FaceManager::CHANNELS_LIST_DATASET_PREFIX("/localhost/nfd/faces/channels"); |
| 103 | const size_t FaceManager::CHANNELS_LIST_DATASET_NCOMPS = CHANNELS_LIST_DATASET_PREFIX.size(); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 104 | |
Chengyu Fan | 320d233 | 2014-10-29 16:40:33 -0600 | [diff] [blame] | 105 | const Name FaceManager::FACES_QUERY_DATASET_PREFIX("/localhost/nfd/faces/query"); |
| 106 | const size_t FaceManager::FACES_QUERY_DATASET_NCOMPS = FACES_QUERY_DATASET_PREFIX.size() + 1; |
| 107 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 108 | FaceManager::FaceManager(FaceTable& faceTable, |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 109 | shared_ptr<InternalFace> face, |
| 110 | ndn::KeyChain& keyChain) |
| 111 | : ManagerBase(face, FACE_MANAGER_PRIVILEGE, keyChain) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 112 | , m_faceTable(faceTable) |
Junxiao Shi | 1e06417 | 2014-12-14 19:37:46 -0700 | [diff] [blame] | 113 | , m_faceAddConn(m_faceTable.onAdd.connect(bind(&FaceManager::onAddFace, this, _1))) |
| 114 | , m_faceRemoveConn(m_faceTable.onRemove.connect(bind(&FaceManager::onRemoveFace, this, _1))) |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 115 | , m_faceStatusPublisher(m_faceTable, *m_face, FACES_LIST_DATASET_PREFIX, keyChain) |
| 116 | , m_channelStatusPublisher(m_factories, *m_face, CHANNELS_LIST_DATASET_PREFIX, keyChain) |
Junxiao Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame] | 117 | , m_notificationStream(*m_face, FACE_EVENTS_PREFIX, keyChain) |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 118 | , m_signedVerbDispatch(SIGNED_COMMAND_VERBS, |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 119 | SIGNED_COMMAND_VERBS + |
| 120 | (sizeof(SIGNED_COMMAND_VERBS) / sizeof(SignedVerbAndProcessor))) |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 121 | , m_unsignedVerbDispatch(UNSIGNED_COMMAND_VERBS, |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 122 | UNSIGNED_COMMAND_VERBS + |
| 123 | (sizeof(UNSIGNED_COMMAND_VERBS) / sizeof(UnsignedVerbAndProcessor))) |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 124 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 125 | { |
| 126 | face->setInterestFilter("/localhost/nfd/faces", |
| 127 | bind(&FaceManager::onFaceRequest, this, _2)); |
| 128 | } |
| 129 | |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 130 | FaceManager::~FaceManager() |
| 131 | { |
| 132 | |
| 133 | } |
| 134 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 135 | void |
| 136 | FaceManager::setConfigFile(ConfigFile& configFile) |
| 137 | { |
| 138 | configFile.addSectionHandler("face_system", |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 139 | bind(&FaceManager::onConfig, this, _1, _2, _3)); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | |
| 143 | void |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 144 | FaceManager::onConfig(const ConfigSection& configSection, |
| 145 | bool isDryRun, |
| 146 | const std::string& filename) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 147 | { |
Alexander Afanasyev | dbfedcf | 2014-12-20 19:57:36 -0800 | [diff] [blame] | 148 | BOOST_THROW_EXCEPTION(Error("Not supported")); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 152 | FaceManager::onFaceRequest(const Interest& request) |
| 153 | { |
| 154 | const Name& command = request.getName(); |
| 155 | const size_t commandNComps = command.size(); |
| 156 | |
Steve DiBenedetto | cd4ee5f | 2014-12-08 16:09:11 -0700 | [diff] [blame] | 157 | if (commandNComps <= COMMAND_PREFIX.size()) |
| 158 | { |
| 159 | // command is too short to have a verb |
| 160 | NFD_LOG_DEBUG("command result: malformed"); |
| 161 | sendResponse(command, 400, "Malformed command"); |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | const Name::Component& verb = command.at(COMMAND_PREFIX.size()); |
| 166 | |
| 167 | const auto unsignedVerbProcessor = m_unsignedVerbDispatch.find(verb); |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 168 | if (unsignedVerbProcessor != m_unsignedVerbDispatch.end()) |
| 169 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 170 | NFD_LOG_DEBUG("command result: processing verb: " << verb); |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 171 | (unsignedVerbProcessor->second)(this, request); |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 172 | } |
| 173 | else if (COMMAND_UNSIGNED_NCOMPS <= commandNComps && |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 174 | commandNComps < COMMAND_SIGNED_NCOMPS) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 175 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 176 | NFD_LOG_DEBUG("command result: unsigned verb: " << command); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 177 | sendResponse(command, 401, "Signature required"); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 178 | } |
| 179 | else if (commandNComps < COMMAND_SIGNED_NCOMPS || |
| 180 | !COMMAND_PREFIX.isPrefixOf(command)) |
| 181 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 182 | NFD_LOG_DEBUG("command result: malformed"); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 183 | sendResponse(command, 400, "Malformed command"); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 184 | } |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 185 | else |
| 186 | { |
| 187 | validate(request, |
| 188 | bind(&FaceManager::onValidatedFaceRequest, this, _1), |
| 189 | bind(&ManagerBase::onCommandValidationFailed, this, _1, _2)); |
| 190 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | void |
| 194 | FaceManager::onValidatedFaceRequest(const shared_ptr<const Interest>& request) |
| 195 | { |
| 196 | const Name& command = request->getName(); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 197 | const Name::Component& verb = command[COMMAND_PREFIX.size()]; |
| 198 | const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1]; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 199 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 200 | SignedVerbDispatchTable::const_iterator signedVerbProcessor = m_signedVerbDispatch.find(verb); |
| 201 | if (signedVerbProcessor != m_signedVerbDispatch.end()) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 202 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 203 | ControlParameters parameters; |
| 204 | if (!extractParameters(parameterComponent, parameters)) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 205 | { |
| 206 | sendResponse(command, 400, "Malformed command"); |
| 207 | return; |
| 208 | } |
| 209 | |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 210 | NFD_LOG_DEBUG("command result: processing verb: " << verb); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 211 | (signedVerbProcessor->second)(this, *request, parameters); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 212 | } |
| 213 | else |
| 214 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 215 | NFD_LOG_DEBUG("command result: unsupported verb: " << verb); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 216 | sendResponse(command, 501, "Unsupported command"); |
| 217 | } |
| 218 | |
| 219 | } |
| 220 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 221 | void |
Alexander Afanasyev | bbe3f0c | 2014-03-23 11:44:01 -0700 | [diff] [blame] | 222 | FaceManager::addCreatedFaceToForwarder(const shared_ptr<Face>& newFace) |
| 223 | { |
| 224 | m_faceTable.add(newFace); |
| 225 | |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 226 | //NFD_LOG_DEBUG("Created face " << newFace->getRemoteUri() << " ID " << newFace->getId()); |
Alexander Afanasyev | bbe3f0c | 2014-03-23 11:44:01 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | void |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 230 | FaceManager::onCreated(const Name& requestName, |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 231 | ControlParameters& parameters, |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 232 | const shared_ptr<Face>& newFace) |
| 233 | { |
Alexander Afanasyev | bbe3f0c | 2014-03-23 11:44:01 -0700 | [diff] [blame] | 234 | addCreatedFaceToForwarder(newFace); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 235 | parameters.setFaceId(newFace->getId()); |
Steve DiBenedetto | 2599928 | 2014-05-22 15:25:12 -0600 | [diff] [blame] | 236 | parameters.setUri(newFace->getRemoteUri().toString()); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 237 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 238 | sendResponse(requestName, 200, "Success", parameters.wireEncode()); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | void |
| 242 | FaceManager::onConnectFailed(const Name& requestName, const std::string& reason) |
| 243 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 244 | NFD_LOG_DEBUG("Failed to create face: " << reason); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 245 | sendResponse(requestName, 408, reason); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | void |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 249 | FaceManager::createFace(const Interest& request, |
| 250 | ControlParameters& parameters) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 251 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 252 | const Name& requestName = request.getName(); |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 253 | ndn::nfd::FaceCreateCommand command; |
| 254 | |
| 255 | if (!validateParameters(command, parameters)) |
| 256 | { |
| 257 | sendResponse(requestName, 400, "Malformed command"); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 258 | NFD_LOG_TRACE("invalid control parameters URI"); |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 259 | return; |
| 260 | } |
| 261 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 262 | FaceUri uri; |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 263 | if (!uri.parse(parameters.getUri())) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 264 | { |
| 265 | sendResponse(requestName, 400, "Malformed command"); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 266 | NFD_LOG_TRACE("failed to parse URI"); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 267 | return; |
| 268 | } |
| 269 | |
Chengyu Fan | b94af7c | 2014-12-17 11:46:47 +0800 | [diff] [blame] | 270 | if (!uri.isCanonical()) |
| 271 | { |
| 272 | sendResponse(requestName, 400, "Non-canonical URI"); |
| 273 | NFD_LOG_TRACE("received non-canonical URI"); |
| 274 | return; |
| 275 | } |
| 276 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 277 | FactoryMap::iterator factory = m_factories.find(uri.getScheme()); |
| 278 | if (factory == m_factories.end()) |
| 279 | { |
| 280 | sendResponse(requestName, 501, "Unsupported protocol"); |
| 281 | return; |
| 282 | } |
| 283 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 284 | try |
| 285 | { |
| 286 | factory->second->createFace(uri, |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 287 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 288 | bind(&FaceManager::onCreated, |
| 289 | this, requestName, parameters, _1), |
| 290 | bind(&FaceManager::onConnectFailed, |
| 291 | this, requestName, _1)); |
| 292 | } |
| 293 | catch (const std::runtime_error& error) |
| 294 | { |
| 295 | std::string errorMessage = "NFD error: "; |
| 296 | errorMessage += error.what(); |
| 297 | |
| 298 | NFD_LOG_ERROR(errorMessage); |
| 299 | sendResponse(requestName, 500, errorMessage); |
| 300 | } |
| 301 | catch (const std::logic_error& error) |
| 302 | { |
| 303 | std::string errorMessage = "NFD error: "; |
| 304 | errorMessage += error.what(); |
| 305 | |
| 306 | NFD_LOG_ERROR(errorMessage); |
| 307 | sendResponse(requestName, 500, errorMessage); |
| 308 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | |
| 312 | void |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 313 | FaceManager::destroyFace(const Interest& request, |
| 314 | ControlParameters& parameters) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 315 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 316 | const Name& requestName = request.getName(); |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 317 | ndn::nfd::FaceDestroyCommand command; |
| 318 | |
| 319 | if (!validateParameters(command, parameters)) |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 320 | { |
| 321 | sendResponse(requestName, 400, "Malformed command"); |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | shared_ptr<Face> target = m_faceTable.get(parameters.getFaceId()); |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 326 | if (static_cast<bool>(target)) |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 327 | { |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 328 | target->close(); |
| 329 | } |
Steve DiBenedetto | ba74905 | 2014-03-22 19:54:53 -0600 | [diff] [blame] | 330 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 331 | sendResponse(requestName, 200, "Success", parameters.wireEncode()); |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 332 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 335 | void |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 336 | FaceManager::onAddFace(shared_ptr<Face> face) |
| 337 | { |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 338 | ndn::nfd::FaceEventNotification notification; |
Chengyu Fan | f9c2bb1 | 2014-10-06 11:52:44 -0600 | [diff] [blame] | 339 | notification.setKind(ndn::nfd::FACE_EVENT_CREATED); |
| 340 | face->copyStatusTo(notification); |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 341 | |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 342 | m_notificationStream.postNotification(notification); |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | void |
| 346 | FaceManager::onRemoveFace(shared_ptr<Face> face) |
| 347 | { |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 348 | ndn::nfd::FaceEventNotification notification; |
Chengyu Fan | f9c2bb1 | 2014-10-06 11:52:44 -0600 | [diff] [blame] | 349 | notification.setKind(ndn::nfd::FACE_EVENT_DESTROYED); |
| 350 | face->copyStatusTo(notification); |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 351 | |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 352 | m_notificationStream.postNotification(notification); |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 353 | } |
| 354 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 355 | bool |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 356 | FaceManager::extractLocalControlParameters(const Interest& request, |
| 357 | ControlParameters& parameters, |
| 358 | ControlCommand& command, |
| 359 | shared_ptr<LocalFace>& outFace, |
| 360 | LocalControlFeature& outFeature) |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 361 | { |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 362 | if (!validateParameters(command, parameters)) |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 363 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 364 | sendResponse(request.getName(), 400, "Malformed command"); |
| 365 | return false; |
| 366 | } |
| 367 | |
| 368 | shared_ptr<Face> face = m_faceTable.get(request.getIncomingFaceId()); |
| 369 | |
| 370 | if (!static_cast<bool>(face)) |
| 371 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 372 | NFD_LOG_DEBUG("command result: faceid " << request.getIncomingFaceId() << " not found"); |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 373 | sendResponse(request.getName(), 410, "Face not found"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 374 | return false; |
| 375 | } |
| 376 | else if (!face->isLocal()) |
| 377 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 378 | NFD_LOG_DEBUG("command result: cannot enable local control on non-local faceid " << |
| 379 | face->getId()); |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 380 | sendResponse(request.getName(), 412, "Face is non-local"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 381 | return false; |
| 382 | } |
| 383 | |
| 384 | outFace = dynamic_pointer_cast<LocalFace>(face); |
| 385 | outFeature = static_cast<LocalControlFeature>(parameters.getLocalControlFeature()); |
| 386 | |
| 387 | return true; |
| 388 | } |
| 389 | |
| 390 | void |
| 391 | FaceManager::enableLocalControl(const Interest& request, |
| 392 | ControlParameters& parameters) |
| 393 | { |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 394 | ndn::nfd::FaceEnableLocalControlCommand command; |
| 395 | |
| 396 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 397 | shared_ptr<LocalFace> face; |
| 398 | LocalControlFeature feature; |
| 399 | |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 400 | if (extractLocalControlParameters(request, parameters, command, face, feature)) |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 401 | { |
| 402 | face->setLocalControlHeaderFeature(feature, true); |
| 403 | sendResponse(request.getName(), 200, "Success", parameters.wireEncode()); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | void |
| 408 | FaceManager::disableLocalControl(const Interest& request, |
| 409 | ControlParameters& parameters) |
| 410 | { |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 411 | ndn::nfd::FaceDisableLocalControlCommand command; |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 412 | shared_ptr<LocalFace> face; |
| 413 | LocalControlFeature feature; |
| 414 | |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 415 | if (extractLocalControlParameters(request, parameters, command, face, feature)) |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 416 | { |
| 417 | face->setLocalControlHeaderFeature(feature, false); |
| 418 | sendResponse(request.getName(), 200, "Success", parameters.wireEncode()); |
| 419 | } |
| 420 | } |
| 421 | |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 422 | void |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 423 | FaceManager::listFaces(const Interest& request) |
| 424 | { |
| 425 | const Name& command = request.getName(); |
| 426 | const size_t commandNComps = command.size(); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 427 | |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 428 | if (commandNComps < FACES_LIST_DATASET_NCOMPS || |
| 429 | !FACES_LIST_DATASET_PREFIX.isPrefixOf(command)) |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 430 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 431 | NFD_LOG_DEBUG("command result: malformed"); |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 432 | sendResponse(command, 400, "Malformed command"); |
| 433 | return; |
| 434 | } |
| 435 | |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 436 | m_faceStatusPublisher.publish(); |
| 437 | } |
| 438 | |
| 439 | void |
| 440 | FaceManager::listChannels(const Interest& request) |
| 441 | { |
| 442 | NFD_LOG_DEBUG("in listChannels"); |
| 443 | const Name& command = request.getName(); |
| 444 | const size_t commandNComps = command.size(); |
| 445 | |
| 446 | if (commandNComps < CHANNELS_LIST_DATASET_NCOMPS || |
| 447 | !CHANNELS_LIST_DATASET_PREFIX.isPrefixOf(command)) |
| 448 | { |
| 449 | NFD_LOG_DEBUG("command result: malformed"); |
| 450 | sendResponse(command, 400, "Malformed command"); |
| 451 | return; |
| 452 | } |
| 453 | |
| 454 | NFD_LOG_DEBUG("publishing"); |
| 455 | m_channelStatusPublisher.publish(); |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 456 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 457 | |
Chengyu Fan | 320d233 | 2014-10-29 16:40:33 -0600 | [diff] [blame] | 458 | void |
| 459 | FaceManager::listQueriedFaces(const Interest& request) |
| 460 | { |
| 461 | NFD_LOG_DEBUG("in listQueriedFaces"); |
| 462 | const Name& query = request.getName(); |
| 463 | const size_t queryNComps = query.size(); |
| 464 | |
| 465 | if (queryNComps < FACES_QUERY_DATASET_NCOMPS || |
| 466 | !FACES_QUERY_DATASET_PREFIX.isPrefixOf(query)) |
| 467 | { |
| 468 | NFD_LOG_DEBUG("query result: malformed"); |
Chengyu Fan | ab205c2 | 2014-11-18 10:58:41 -0700 | [diff] [blame] | 469 | sendNack(query); |
Chengyu Fan | 320d233 | 2014-10-29 16:40:33 -0600 | [diff] [blame] | 470 | return; |
| 471 | } |
| 472 | |
| 473 | ndn::nfd::FaceQueryFilter faceFilter; |
| 474 | try |
| 475 | { |
| 476 | faceFilter.wireDecode(query[-1].blockFromValue()); |
| 477 | } |
| 478 | catch (tlv::Error&) |
| 479 | { |
| 480 | NFD_LOG_DEBUG("query result: malformed filter"); |
Chengyu Fan | ab205c2 | 2014-11-18 10:58:41 -0700 | [diff] [blame] | 481 | sendNack(query); |
Chengyu Fan | 320d233 | 2014-10-29 16:40:33 -0600 | [diff] [blame] | 482 | return; |
| 483 | } |
| 484 | |
| 485 | FaceQueryStatusPublisher |
| 486 | faceQueryStatusPublisher(m_faceTable, *m_face, query, faceFilter, m_keyChain); |
| 487 | |
| 488 | faceQueryStatusPublisher.publish(); |
| 489 | } |
| 490 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 491 | shared_ptr<ProtocolFactory> |
| 492 | FaceManager::findFactory(const std::string& protocol) |
| 493 | { |
| 494 | FactoryMap::iterator factory = m_factories.find(protocol); |
| 495 | if (factory != m_factories.end()) |
| 496 | return factory->second; |
| 497 | else |
| 498 | return shared_ptr<ProtocolFactory>(); |
| 499 | } |
| 500 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 501 | } // namespace nfd |