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 | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014 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 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 24 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 25 | #ifndef NFD_DAEMON_MGMT_FACE_MANAGER_HPP |
| 26 | #define NFD_DAEMON_MGMT_FACE_MANAGER_HPP |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 27 | |
| 28 | #include "common.hpp" |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 29 | #include "face/local-face.hpp" |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 30 | #include "mgmt/manager-base.hpp" |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 31 | #include "mgmt/face-status-publisher.hpp" |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 32 | #include "mgmt/notification-stream.hpp" |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 33 | |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 34 | #include <ndn-cxx/management/nfd-control-parameters.hpp> |
| 35 | #include <ndn-cxx/management/nfd-control-response.hpp> |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 36 | |
| 37 | namespace nfd { |
| 38 | |
| 39 | const std::string FACE_MANAGER_PRIVILEGE = "faces"; |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 40 | |
Davide Pesavento | 52a18f9 | 2014-04-10 00:55:01 +0200 | [diff] [blame] | 41 | class ConfigFile; |
| 42 | class Face; |
| 43 | class FaceTable; |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 44 | class LocalFace; |
Davide Pesavento | 52a18f9 | 2014-04-10 00:55:01 +0200 | [diff] [blame] | 45 | class NetworkInterfaceInfo; |
| 46 | class ProtocolFactory; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 47 | |
| 48 | class FaceManager : public ManagerBase |
| 49 | { |
| 50 | public: |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 51 | class Error : public ManagerBase::Error |
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 | public: |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 54 | Error(const std::string& what) : ManagerBase::Error(what) {} |
| 55 | }; |
| 56 | |
| 57 | /** |
| 58 | * \throws FaceManager::Error if localPort is an invalid port number |
| 59 | */ |
| 60 | |
| 61 | FaceManager(FaceTable& faceTable, |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 62 | shared_ptr<InternalFace> face); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 63 | |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 64 | virtual |
| 65 | ~FaceManager(); |
| 66 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 67 | /** \brief Subscribe to a face management section(s) for the config file |
| 68 | */ |
| 69 | void |
| 70 | setConfigFile(ConfigFile& configFile); |
| 71 | |
| 72 | void |
| 73 | onFaceRequest(const Interest& request); |
| 74 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 75 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 76 | void |
| 77 | listFaces(const Interest& request); |
| 78 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 79 | PROTECTED_WITH_TESTS_ELSE_PRIVATE: |
| 80 | |
| 81 | void |
| 82 | onValidatedFaceRequest(const shared_ptr<const Interest>& request); |
| 83 | |
| 84 | VIRTUAL_WITH_TESTS void |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 85 | createFace(const Interest& request, |
| 86 | ControlParameters& parameters); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 87 | |
| 88 | VIRTUAL_WITH_TESTS void |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 89 | destroyFace(const Interest& request, |
| 90 | ControlParameters& parameters); |
| 91 | |
| 92 | VIRTUAL_WITH_TESTS bool |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 93 | extractLocalControlParameters(const Interest& request, |
| 94 | ControlParameters& parameters, |
| 95 | ControlCommand& command, |
| 96 | shared_ptr<LocalFace>& outFace, |
| 97 | LocalControlFeature& outFeature); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 98 | |
| 99 | VIRTUAL_WITH_TESTS void |
| 100 | enableLocalControl(const Interest& request, |
| 101 | ControlParameters& parambeters); |
| 102 | |
| 103 | VIRTUAL_WITH_TESTS void |
| 104 | disableLocalControl(const Interest& request, |
| 105 | ControlParameters& parameters); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 106 | |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 107 | void |
| 108 | ignoreUnsignedVerb(const Interest& request); |
| 109 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 110 | void |
Alexander Afanasyev | bbe3f0c | 2014-03-23 11:44:01 -0700 | [diff] [blame] | 111 | addCreatedFaceToForwarder(const shared_ptr<Face>& newFace); |
| 112 | |
| 113 | void |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 114 | onCreated(const Name& requestName, |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 115 | ControlParameters& parameters, |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 116 | const shared_ptr<Face>& newFace); |
| 117 | |
| 118 | void |
| 119 | onConnectFailed(const Name& requestName, const std::string& reason); |
| 120 | |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 121 | void |
| 122 | onAddFace(shared_ptr<Face> face); |
| 123 | |
| 124 | void |
| 125 | onRemoveFace(shared_ptr<Face> face); |
| 126 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 127 | private: |
| 128 | void |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 129 | onConfig(const ConfigSection& configSection, bool isDryRun, const std::string& filename); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 130 | |
| 131 | void |
| 132 | processSectionUnix(const ConfigSection& configSection, bool isDryRun); |
| 133 | |
| 134 | void |
| 135 | processSectionTcp(const ConfigSection& configSection, bool isDryRun); |
| 136 | |
| 137 | void |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 138 | processSectionUdp(const ConfigSection& configSection, |
| 139 | bool isDryRun, |
| 140 | const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 141 | |
| 142 | void |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 143 | processSectionEther(const ConfigSection& configSection, |
| 144 | bool isDryRun, |
| 145 | const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 146 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 147 | void |
| 148 | processSectionWebSocket(const ConfigSection& configSection, bool isDryRun); |
| 149 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 150 | /** \brief parse a config option that can be either "yes" or "no" |
| 151 | * \throw ConfigFile::Error value is neither "yes" nor "no" |
| 152 | * \return true if "yes", false if "no" |
| 153 | */ |
| 154 | bool |
| 155 | parseYesNo(const ConfigSection::const_iterator& i, |
| 156 | const std::string& optionName, |
| 157 | const std::string& sectionName); |
| 158 | |
| 159 | private: |
| 160 | typedef std::map< std::string/*protocol*/, shared_ptr<ProtocolFactory> > FactoryMap; |
| 161 | FactoryMap m_factories; |
| 162 | FaceTable& m_faceTable; |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 163 | FaceStatusPublisher m_statusPublisher; |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 164 | NotificationStream m_notificationStream; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 165 | |
| 166 | typedef function<void(FaceManager*, |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 167 | const Interest&, |
| 168 | ControlParameters&)> SignedVerbProcessor; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 169 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 170 | typedef std::map<Name::Component, SignedVerbProcessor> SignedVerbDispatchTable; |
| 171 | typedef std::pair<Name::Component, SignedVerbProcessor> SignedVerbAndProcessor; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 172 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 173 | typedef function<void(FaceManager*, const Interest&)> UnsignedVerbProcessor; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 174 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 175 | typedef std::map<Name::Component, UnsignedVerbProcessor> UnsignedVerbDispatchTable; |
| 176 | typedef std::pair<Name::Component, UnsignedVerbProcessor> UnsignedVerbAndProcessor; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 177 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 178 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 179 | const SignedVerbDispatchTable m_signedVerbDispatch; |
| 180 | const UnsignedVerbDispatchTable m_unsignedVerbDispatch; |
| 181 | |
| 182 | static const Name COMMAND_PREFIX; // /localhost/nfd/faces |
| 183 | |
| 184 | // number of components in an invalid signed command (i.e. should be signed, but isn't) |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 185 | // (/localhost/nfd/faces + verb + parameters) = 5 |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 186 | static const size_t COMMAND_UNSIGNED_NCOMPS; |
| 187 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 188 | // number of components in a valid signed command. |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 189 | // (see UNSIGNED_NCOMPS), 9 with signed Interest support. |
| 190 | static const size_t COMMAND_SIGNED_NCOMPS; |
| 191 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 192 | static const SignedVerbAndProcessor SIGNED_COMMAND_VERBS[]; |
| 193 | static const UnsignedVerbAndProcessor UNSIGNED_COMMAND_VERBS[]; |
| 194 | |
| 195 | static const Name LIST_COMMAND_PREFIX; |
| 196 | static const size_t LIST_COMMAND_NCOMPS; |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 197 | |
| 198 | static const Name EVENTS_COMMAND_PREFIX; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | inline bool |
| 202 | FaceManager::parseYesNo(const ConfigSection::const_iterator& i, |
| 203 | const std::string& optionName, |
| 204 | const std::string& sectionName) |
| 205 | { |
| 206 | const std::string value = i->second.get_value<std::string>(); |
| 207 | if (value == "yes") |
| 208 | { |
| 209 | return true; |
| 210 | } |
| 211 | else if (value == "no") |
| 212 | { |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | throw ConfigFile::Error("Invalid value for option \"" + |
| 217 | optionName + "\" in \"" + |
| 218 | sectionName + "\" section"); |
| 219 | |
| 220 | } |
| 221 | |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 222 | inline void |
| 223 | FaceManager::ignoreUnsignedVerb(const Interest& request) |
| 224 | { |
| 225 | // do nothing |
| 226 | } |
| 227 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 228 | } // namespace nfd |
| 229 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 230 | #endif // NFD_DAEMON_MGMT_FACE_MANAGER_HPP |