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 |
| 76 | validateLocalControlParameters(const Interest& request, |
| 77 | ControlParameters& parameters, |
| 78 | shared_ptr<LocalFace>& outFace, |
| 79 | LocalControlFeature& outFeature); |
| 80 | |
| 81 | VIRTUAL_WITH_TESTS void |
| 82 | enableLocalControl(const Interest& request, |
| 83 | ControlParameters& parambeters); |
| 84 | |
| 85 | VIRTUAL_WITH_TESTS void |
| 86 | disableLocalControl(const Interest& request, |
| 87 | ControlParameters& parameters); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 88 | |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 89 | void |
| 90 | ignoreUnsignedVerb(const Interest& request); |
| 91 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 92 | void |
Alexander Afanasyev | bbe3f0c | 2014-03-23 11:44:01 -0700 | [diff] [blame] | 93 | addCreatedFaceToForwarder(const shared_ptr<Face>& newFace); |
| 94 | |
| 95 | void |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 96 | onCreated(const Name& requestName, |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame^] | 97 | ControlParameters& parameters, |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 98 | const shared_ptr<Face>& newFace); |
| 99 | |
| 100 | void |
| 101 | onConnectFailed(const Name& requestName, const std::string& reason); |
| 102 | |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 103 | void |
| 104 | onAddFace(shared_ptr<Face> face); |
| 105 | |
| 106 | void |
| 107 | onRemoveFace(shared_ptr<Face> face); |
| 108 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 109 | private: |
| 110 | void |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 111 | onConfig(const ConfigSection& configSection, bool isDryRun, const std::string& filename); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 112 | |
| 113 | void |
| 114 | processSectionUnix(const ConfigSection& configSection, bool isDryRun); |
| 115 | |
| 116 | void |
| 117 | processSectionTcp(const ConfigSection& configSection, bool isDryRun); |
| 118 | |
| 119 | void |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 120 | processSectionUdp(const ConfigSection& configSection, |
| 121 | bool isDryRun, |
| 122 | const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 123 | |
| 124 | void |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 125 | processSectionEther(const ConfigSection& configSection, |
| 126 | bool isDryRun, |
| 127 | const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 128 | |
| 129 | /** \brief parse a config option that can be either "yes" or "no" |
| 130 | * \throw ConfigFile::Error value is neither "yes" nor "no" |
| 131 | * \return true if "yes", false if "no" |
| 132 | */ |
| 133 | bool |
| 134 | parseYesNo(const ConfigSection::const_iterator& i, |
| 135 | const std::string& optionName, |
| 136 | const std::string& sectionName); |
| 137 | |
| 138 | private: |
| 139 | typedef std::map< std::string/*protocol*/, shared_ptr<ProtocolFactory> > FactoryMap; |
| 140 | FactoryMap m_factories; |
| 141 | FaceTable& m_faceTable; |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 142 | FaceStatusPublisher m_statusPublisher; |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 143 | NotificationStream m_notificationStream; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 144 | |
| 145 | typedef function<void(FaceManager*, |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame^] | 146 | const Interest&, |
| 147 | ControlParameters&)> SignedVerbProcessor; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 148 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 149 | typedef std::map<Name::Component, SignedVerbProcessor> SignedVerbDispatchTable; |
| 150 | typedef std::pair<Name::Component, SignedVerbProcessor> SignedVerbAndProcessor; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 151 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 152 | typedef function<void(FaceManager*, const Interest&)> UnsignedVerbProcessor; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 153 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 154 | typedef std::map<Name::Component, UnsignedVerbProcessor> UnsignedVerbDispatchTable; |
| 155 | typedef std::pair<Name::Component, UnsignedVerbProcessor> UnsignedVerbAndProcessor; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 156 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 157 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 158 | const SignedVerbDispatchTable m_signedVerbDispatch; |
| 159 | const UnsignedVerbDispatchTable m_unsignedVerbDispatch; |
| 160 | |
| 161 | static const Name COMMAND_PREFIX; // /localhost/nfd/faces |
| 162 | |
| 163 | // 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^] | 164 | // (/localhost/nfd/faces + verb + parameters) = 5 |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 165 | static const size_t COMMAND_UNSIGNED_NCOMPS; |
| 166 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 167 | // number of components in a valid signed command. |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 168 | // (see UNSIGNED_NCOMPS), 9 with signed Interest support. |
| 169 | static const size_t COMMAND_SIGNED_NCOMPS; |
| 170 | |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 171 | static const SignedVerbAndProcessor SIGNED_COMMAND_VERBS[]; |
| 172 | static const UnsignedVerbAndProcessor UNSIGNED_COMMAND_VERBS[]; |
| 173 | |
| 174 | static const Name LIST_COMMAND_PREFIX; |
| 175 | static const size_t LIST_COMMAND_NCOMPS; |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 176 | |
| 177 | static const Name EVENTS_COMMAND_PREFIX; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | inline bool |
| 181 | FaceManager::parseYesNo(const ConfigSection::const_iterator& i, |
| 182 | const std::string& optionName, |
| 183 | const std::string& sectionName) |
| 184 | { |
| 185 | const std::string value = i->second.get_value<std::string>(); |
| 186 | if (value == "yes") |
| 187 | { |
| 188 | return true; |
| 189 | } |
| 190 | else if (value == "no") |
| 191 | { |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | throw ConfigFile::Error("Invalid value for option \"" + |
| 196 | optionName + "\" in \"" + |
| 197 | sectionName + "\" section"); |
| 198 | |
| 199 | } |
| 200 | |
Steve DiBenedetto | fbb40a8 | 2014-03-11 19:40:15 -0600 | [diff] [blame] | 201 | inline void |
| 202 | FaceManager::ignoreUnsignedVerb(const Interest& request) |
| 203 | { |
| 204 | // do nothing |
| 205 | } |
| 206 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 207 | } // namespace nfd |
| 208 | |
| 209 | #endif // NFD_MGMT_FACE_MANAGER_HPP |