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