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