blob: 55e4df4b3009e603b322bcca5e7f30d2d89dd486 [file] [log] [blame]
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001/* -*- 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"
15
16#include <ndn-cpp-dev/management/nfd-face-management-options.hpp>
17#include <ndn-cpp-dev/management/nfd-control-response.hpp>
18
19namespace nfd {
20
21const std::string FACE_MANAGER_PRIVILEGE = "faces";
22class FaceTable;
23class ProtocolFactory;
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -060024class NetworkInterfaceInfo;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070025
26class FaceManager : public ManagerBase
27{
28public:
29 struct Error : public ManagerBase::Error
30 {
31 Error(const std::string& what) : ManagerBase::Error(what) {}
32 };
33
34 /**
35 * \throws FaceManager::Error if localPort is an invalid port number
36 */
37
38 FaceManager(FaceTable& faceTable,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070039 shared_ptr<InternalFace> face);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070040
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -060041 virtual
42 ~FaceManager();
43
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070044 /** \brief Subscribe to a face management section(s) for the config file
45 */
46 void
47 setConfigFile(ConfigFile& configFile);
48
49 void
50 onFaceRequest(const Interest& request);
51
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070052PROTECTED_WITH_TESTS_ELSE_PRIVATE:
53
54 void
55 onValidatedFaceRequest(const shared_ptr<const Interest>& request);
56
57 VIRTUAL_WITH_TESTS void
58 createFace(const Name& requestName,
59 ndn::nfd::FaceManagementOptions& options);
60
61 VIRTUAL_WITH_TESTS void
62 destroyFace(const Name& requestName,
63 ndn::nfd::FaceManagementOptions& options);
64
65 bool
66 extractOptions(const Interest& request,
67 ndn::nfd::FaceManagementOptions& extractedOptions);
68
69 void
70 onCreated(const Name& requestName,
71 ndn::nfd::FaceManagementOptions& options,
72 const shared_ptr<Face>& newFace);
73
74 void
75 onConnectFailed(const Name& requestName, const std::string& reason);
76
77private:
78 void
79 onConfig(const ConfigSection& configSection, bool isDryRun);
80
81 void
82 processSectionUnix(const ConfigSection& configSection, bool isDryRun);
83
84 void
85 processSectionTcp(const ConfigSection& configSection, bool isDryRun);
86
87 void
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -060088 processSectionUdp(const ConfigSection& configSection,
89 bool isDryRun,
90 const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070091
92 void
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -060093 processSectionEther(const ConfigSection& configSection,
94 bool isDryRun,
95 const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070096
97 /** \brief parse a config option that can be either "yes" or "no"
98 * \throw ConfigFile::Error value is neither "yes" nor "no"
99 * \return true if "yes", false if "no"
100 */
101 bool
102 parseYesNo(const ConfigSection::const_iterator& i,
103 const std::string& optionName,
104 const std::string& sectionName);
105
106private:
107 typedef std::map< std::string/*protocol*/, shared_ptr<ProtocolFactory> > FactoryMap;
108 FactoryMap m_factories;
109 FaceTable& m_faceTable;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700110
111 typedef function<void(FaceManager*,
112 const Name&,
113 ndn::nfd::FaceManagementOptions&)> VerbProcessor;
114
115 typedef std::map<Name::Component, VerbProcessor> VerbDispatchTable;
116
117 typedef std::pair<Name::Component, VerbProcessor> VerbAndProcessor;
118
119 const VerbDispatchTable m_verbDispatch;
120
121 static const Name COMMAND_PREFIX; // /localhost/nfd/fib
122
123 // number of components in an invalid, but not malformed, unsigned command.
124 // (/localhost/nfd/fib + verb + options) = 5
125 static const size_t COMMAND_UNSIGNED_NCOMPS;
126
127 // number of components in a valid signed Interest.
128 // (see UNSIGNED_NCOMPS), 9 with signed Interest support.
129 static const size_t COMMAND_SIGNED_NCOMPS;
130
131 static const VerbAndProcessor COMMAND_VERBS[];
132};
133
134inline bool
135FaceManager::parseYesNo(const ConfigSection::const_iterator& i,
136 const std::string& optionName,
137 const std::string& sectionName)
138{
139 const std::string value = i->second.get_value<std::string>();
140 if (value == "yes")
141 {
142 return true;
143 }
144 else if (value == "no")
145 {
146 return false;
147 }
148
149 throw ConfigFile::Error("Invalid value for option \"" +
150 optionName + "\" in \"" +
151 sectionName + "\" section");
152
153}
154
155} // namespace nfd
156
157#endif // NFD_MGMT_FACE_MANAGER_HPP