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