Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #ifndef NFD_MGMT_FACE_MANAGER_HPP |
| 8 | #define NFD_MGMT_FACE_MANAGER_HPP |
| 9 | |
| 10 | #include "common.hpp" |
| 11 | #include "face/face.hpp" |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 12 | #include "face/local-face.hpp" |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 13 | #include "mgmt/app-face.hpp" |
| 14 | #include "mgmt/manager-base.hpp" |
| 15 | #include "mgmt/config-file.hpp" |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 16 | #include "mgmt/face-status-publisher.hpp" |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 17 | #include "mgmt/notification-stream.hpp" |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 18 | #include "fw/face-table.hpp" |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 19 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 20 | #include <ndn-cpp-dev/management/nfd-control-parameters.hpp> |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 21 | #include <ndn-cpp-dev/management/nfd-control-response.hpp> |
| 22 | |
| 23 | namespace nfd { |
| 24 | |
| 25 | const std::string FACE_MANAGER_PRIVILEGE = "faces"; |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 26 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 27 | class ProtocolFactory; |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 28 | class NetworkInterfaceInfo; |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 29 | class LocalFace; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 30 | |
| 31 | class FaceManager : public ManagerBase |
| 32 | { |
| 33 | public: |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 34 | class Error : public ManagerBase::Error |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 35 | { |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 36 | public: |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 37 | Error(const std::string& what) : ManagerBase::Error(what) {} |
| 38 | }; |
| 39 | |
| 40 | /** |
| 41 | * \throws FaceManager::Error if localPort is an invalid port number |
| 42 | */ |
| 43 | |
| 44 | FaceManager(FaceTable& faceTable, |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 45 | shared_ptr<InternalFace> face); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 46 | |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 47 | virtual |
| 48 | ~FaceManager(); |
| 49 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 50 | /** \brief Subscribe to a face management section(s) for the config file |
| 51 | */ |
| 52 | void |
| 53 | setConfigFile(ConfigFile& configFile); |
| 54 | |
| 55 | void |
| 56 | onFaceRequest(const Interest& request); |
| 57 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 58 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 59 | void |
| 60 | listFaces(const Interest& request); |
| 61 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 62 | PROTECTED_WITH_TESTS_ELSE_PRIVATE: |
| 63 | |
| 64 | void |
| 65 | onValidatedFaceRequest(const shared_ptr<const Interest>& request); |
| 66 | |
| 67 | VIRTUAL_WITH_TESTS void |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 68 | createFace(const Interest& request, |
| 69 | ControlParameters& parameters); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 70 | |
| 71 | VIRTUAL_WITH_TESTS void |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 72 | destroyFace(const Interest& request, |
| 73 | ControlParameters& parameters); |
| 74 | |
| 75 | VIRTUAL_WITH_TESTS bool |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 76 | extractLocalControlParameters(const Interest& request, |
| 77 | ControlParameters& parameters, |
| 78 | ControlCommand& command, |
| 79 | shared_ptr<LocalFace>& outFace, |
| 80 | LocalControlFeature& outFeature); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 81 | |
| 82 | VIRTUAL_WITH_TESTS void |
| 83 | enableLocalControl(const Interest& request, |
| 84 | ControlParameters& parambeters); |
| 85 | |
| 86 | VIRTUAL_WITH_TESTS void |
| 87 | disableLocalControl(const Interest& request, |
| 88 | ControlParameters& parameters); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 89 | |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 90 | void |
| 91 | ignoreUnsignedVerb(const Interest& request); |
| 92 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 93 | void |
Alexander Afanasyev | bbe3f0c | 2014-03-23 11:44:01 -0700 | [diff] [blame] | 94 | addCreatedFaceToForwarder(const shared_ptr<Face>& newFace); |
| 95 | |
| 96 | void |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 97 | onCreated(const Name& requestName, |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 98 | ControlParameters& parameters, |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 99 | const shared_ptr<Face>& newFace); |
| 100 | |
| 101 | void |
| 102 | onConnectFailed(const Name& requestName, const std::string& reason); |
| 103 | |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 104 | void |
| 105 | onAddFace(shared_ptr<Face> face); |
| 106 | |
| 107 | void |
| 108 | onRemoveFace(shared_ptr<Face> face); |
| 109 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 110 | private: |
| 111 | void |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 112 | onConfig(const ConfigSection& configSection, bool isDryRun, const std::string& filename); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 113 | |
| 114 | void |
| 115 | processSectionUnix(const ConfigSection& configSection, bool isDryRun); |
| 116 | |
| 117 | void |
| 118 | processSectionTcp(const ConfigSection& configSection, bool isDryRun); |
| 119 | |
| 120 | void |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 121 | processSectionUdp(const ConfigSection& configSection, |
| 122 | bool isDryRun, |
| 123 | const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 124 | |
| 125 | void |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 126 | processSectionEther(const ConfigSection& configSection, |
| 127 | bool isDryRun, |
| 128 | const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 129 | |
| 130 | /** \brief parse a config option that can be either "yes" or "no" |
| 131 | * \throw ConfigFile::Error value is neither "yes" nor "no" |
| 132 | * \return true if "yes", false if "no" |
| 133 | */ |
| 134 | bool |
| 135 | parseYesNo(const ConfigSection::const_iterator& i, |
| 136 | const std::string& optionName, |
| 137 | const std::string& sectionName); |
| 138 | |
| 139 | private: |
| 140 | typedef std::map< std::string/*protocol*/, shared_ptr<ProtocolFactory> > FactoryMap; |
| 141 | FactoryMap m_factories; |
| 142 | FaceTable& m_faceTable; |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 143 | FaceStatusPublisher m_statusPublisher; |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 144 | NotificationStream m_notificationStream; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 145 | |
| 146 | typedef function<void(FaceManager*, |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 147 | const Interest&, |
| 148 | ControlParameters&)> SignedVerbProcessor; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 149 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 150 | typedef std::map<Name::Component, SignedVerbProcessor> SignedVerbDispatchTable; |
| 151 | typedef std::pair<Name::Component, SignedVerbProcessor> SignedVerbAndProcessor; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 152 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 153 | typedef function<void(FaceManager*, const Interest&)> UnsignedVerbProcessor; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 154 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 155 | typedef std::map<Name::Component, UnsignedVerbProcessor> UnsignedVerbDispatchTable; |
| 156 | typedef std::pair<Name::Component, UnsignedVerbProcessor> UnsignedVerbAndProcessor; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 157 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 158 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 159 | const SignedVerbDispatchTable m_signedVerbDispatch; |
| 160 | const UnsignedVerbDispatchTable m_unsignedVerbDispatch; |
| 161 | |
| 162 | static const Name COMMAND_PREFIX; // /localhost/nfd/faces |
| 163 | |
| 164 | // 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] | 165 | // (/localhost/nfd/faces + verb + parameters) = 5 |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 166 | static const size_t COMMAND_UNSIGNED_NCOMPS; |
| 167 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 168 | // number of components in a valid signed command. |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 169 | // (see UNSIGNED_NCOMPS), 9 with signed Interest support. |
| 170 | static const size_t COMMAND_SIGNED_NCOMPS; |
| 171 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 172 | static const SignedVerbAndProcessor SIGNED_COMMAND_VERBS[]; |
| 173 | static const UnsignedVerbAndProcessor UNSIGNED_COMMAND_VERBS[]; |
| 174 | |
| 175 | static const Name LIST_COMMAND_PREFIX; |
| 176 | static const size_t LIST_COMMAND_NCOMPS; |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 177 | |
| 178 | static const Name EVENTS_COMMAND_PREFIX; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | inline bool |
| 182 | FaceManager::parseYesNo(const ConfigSection::const_iterator& i, |
| 183 | const std::string& optionName, |
| 184 | const std::string& sectionName) |
| 185 | { |
| 186 | const std::string value = i->second.get_value<std::string>(); |
| 187 | if (value == "yes") |
| 188 | { |
| 189 | return true; |
| 190 | } |
| 191 | else if (value == "no") |
| 192 | { |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | throw ConfigFile::Error("Invalid value for option \"" + |
| 197 | optionName + "\" in \"" + |
| 198 | sectionName + "\" section"); |
| 199 | |
| 200 | } |
| 201 | |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 202 | inline void |
| 203 | FaceManager::ignoreUnsignedVerb(const Interest& request) |
| 204 | { |
| 205 | // do nothing |
| 206 | } |
| 207 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 208 | } // namespace nfd |
| 209 | |
| 210 | #endif // NFD_MGMT_FACE_MANAGER_HPP |