blob: d489dff5333ff557c4bb3e1988bd9fb892d6a21b [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"
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060015#include "mgmt/face-status-publisher.hpp"
Steve DiBenedettofbb40a82014-03-11 19:40:15 -060016#include "mgmt/notification-stream.hpp"
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060017#include "fw/face-table.hpp"
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070018
19#include <ndn-cpp-dev/management/nfd-face-management-options.hpp>
20#include <ndn-cpp-dev/management/nfd-control-response.hpp>
21
22namespace nfd {
23
24const std::string FACE_MANAGER_PRIVILEGE = "faces";
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060025
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070026class ProtocolFactory;
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -060027class NetworkInterfaceInfo;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070028
29class FaceManager : public ManagerBase
30{
31public:
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060032 class Error : public ManagerBase::Error
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070033 {
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060034 public:
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070035 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 DiBenedetto2c2b8892014-02-27 11:46:48 -070043 shared_ptr<InternalFace> face);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070044
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -060045 virtual
46 ~FaceManager();
47
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070048 /** \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 DiBenedetto9f6c3642014-03-10 17:02:27 -060056PUBLIC_WITH_TESTS_ELSE_PRIVATE:
57 void
58 listFaces(const Interest& request);
59
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070060PROTECTED_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 DiBenedettofbb40a82014-03-11 19:40:15 -060073 void
74 ignoreUnsignedVerb(const Interest& request);
75
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070076 bool
77 extractOptions(const Interest& request,
78 ndn::nfd::FaceManagementOptions& extractedOptions);
79
80 void
Alexander Afanasyevbbe3f0c2014-03-23 11:44:01 -070081 addCreatedFaceToForwarder(const shared_ptr<Face>& newFace);
82
83 void
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070084 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 DiBenedettofbb40a82014-03-11 19:40:15 -060091 void
92 onAddFace(shared_ptr<Face> face);
93
94 void
95 onRemoveFace(shared_ptr<Face> face);
96
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070097private:
98 void
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060099 onConfig(const ConfigSection& configSection, bool isDryRun, const std::string& filename);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700100
101 void
102 processSectionUnix(const ConfigSection& configSection, bool isDryRun);
103
104 void
105 processSectionTcp(const ConfigSection& configSection, bool isDryRun);
106
107 void
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600108 processSectionUdp(const ConfigSection& configSection,
109 bool isDryRun,
110 const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700111
112 void
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600113 processSectionEther(const ConfigSection& configSection,
114 bool isDryRun,
115 const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700116
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
126private:
127 typedef std::map< std::string/*protocol*/, shared_ptr<ProtocolFactory> > FactoryMap;
128 FactoryMap m_factories;
129 FaceTable& m_faceTable;
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600130 FaceStatusPublisher m_statusPublisher;
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600131 NotificationStream m_notificationStream;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700132
133 typedef function<void(FaceManager*,
134 const Name&,
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600135 ndn::nfd::FaceManagementOptions&)> SignedVerbProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700136
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600137 typedef std::map<Name::Component, SignedVerbProcessor> SignedVerbDispatchTable;
138 typedef std::pair<Name::Component, SignedVerbProcessor> SignedVerbAndProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700139
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600140 typedef function<void(FaceManager*, const Interest&)> UnsignedVerbProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700141
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600142 typedef std::map<Name::Component, UnsignedVerbProcessor> UnsignedVerbDispatchTable;
143 typedef std::pair<Name::Component, UnsignedVerbProcessor> UnsignedVerbAndProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700144
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700145
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600146 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 DiBenedettoabe9e972014-02-20 15:37:04 -0700153 static const size_t COMMAND_UNSIGNED_NCOMPS;
154
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600155 // number of components in a valid signed command.
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700156 // (see UNSIGNED_NCOMPS), 9 with signed Interest support.
157 static const size_t COMMAND_SIGNED_NCOMPS;
158
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600159 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 DiBenedettofbb40a82014-03-11 19:40:15 -0600164
165 static const Name EVENTS_COMMAND_PREFIX;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700166};
167
168inline bool
169FaceManager::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 DiBenedettofbb40a82014-03-11 19:40:15 -0600189inline void
190FaceManager::ignoreUnsignedVerb(const Interest& request)
191{
192 // do nothing
193}
194
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700195} // namespace nfd
196
197#endif // NFD_MGMT_FACE_MANAGER_HPP