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