face: pass addFace and NetworkMonitor to ProtocolFactory constructor
refs #4021
Change-Id: I842515eb044bb1c655b347e6069e63c55c4e2d54
diff --git a/daemon/face/ethernet-factory.cpp b/daemon/face/ethernet-factory.cpp
index 362527f..9ff5fd5 100644
--- a/daemon/face/ethernet-factory.cpp
+++ b/daemon/face/ethernet-factory.cpp
@@ -42,6 +42,10 @@
return id;
}
+EthernetFactory::EthernetFactory(const CtorParams& params)
+ : ProtocolFactory(params)
+{
+}
void
EthernetFactory::processConfig(OptionalConfigSection configSection,
diff --git a/daemon/face/ethernet-factory.hpp b/daemon/face/ethernet-factory.hpp
index 7480728..e4cf7d9 100644
--- a/daemon/face/ethernet-factory.hpp
+++ b/daemon/face/ethernet-factory.hpp
@@ -40,6 +40,9 @@
static const std::string&
getId();
+ explicit
+ EthernetFactory(const CtorParams& params);
+
/** \brief process face_system.ether config section
*/
void
diff --git a/daemon/face/face-system.cpp b/daemon/face/face-system.cpp
index 7877ed0..57b391d 100644
--- a/daemon/face/face-system.cpp
+++ b/daemon/face/face-system.cpp
@@ -33,18 +33,24 @@
NFD_LOG_INIT("FaceSystem");
-FaceSystem::FaceSystem(FaceTable& faceTable, const shared_ptr<ndn::net::NetworkMonitor>& netmon)
+FaceSystem::FaceSystem(FaceTable& faceTable, shared_ptr<ndn::net::NetworkMonitor> netmon)
: m_faceTable(faceTable)
+ , m_netmon(std::move(netmon))
{
- BOOST_ASSERT(netmon != nullptr);
-
- auto addFace = bind(&FaceTable::add, &m_faceTable, _1);
+ auto pfCtorParams = this->makePFCtorParams();
for (const std::string& id : ProtocolFactory::listRegistered()) {
NFD_LOG_TRACE("creating factory " << id);
- m_factories[id] = ProtocolFactory::create(id, netmon, addFace);
+ m_factories[id] = ProtocolFactory::create(id, pfCtorParams);
}
}
+ProtocolFactoryCtorParams
+FaceSystem::makePFCtorParams()
+{
+ auto addFace = bind(&FaceTable::add, &m_faceTable, _1);
+ return {addFace, m_netmon};
+}
+
FaceSystem::FaceSystem(FaceTable& faceTable)
: FaceSystem(faceTable, make_shared<ndn::net::NetworkMonitor>(getGlobalIoService()))
{
diff --git a/daemon/face/face-system.hpp b/daemon/face/face-system.hpp
index 6557c72..039c1c9 100644
--- a/daemon/face/face-system.hpp
+++ b/daemon/face/face-system.hpp
@@ -41,6 +41,7 @@
namespace face {
class ProtocolFactory;
+struct ProtocolFactoryCtorParams;
/** \brief entry point of the face system
*
@@ -50,7 +51,7 @@
class FaceSystem : noncopyable
{
public:
- FaceSystem(FaceTable& faceTable, const shared_ptr<ndn::net::NetworkMonitor>& netmon);
+ FaceSystem(FaceTable& faceTable, shared_ptr<ndn::net::NetworkMonitor> netmon);
DEPRECATED(
explicit
@@ -105,6 +106,10 @@
friend class FaceSystem;
};
+PUBLIC_WITH_TESTS_ELSE_PRIVATE:
+ ProtocolFactoryCtorParams
+ makePFCtorParams();
+
private:
void
processConfig(const ConfigSection& configSection, bool isDryRun,
@@ -123,6 +128,8 @@
std::map<std::string, ProtocolFactory*> m_factoryByScheme;
FaceTable& m_faceTable;
+
+ shared_ptr<ndn::net::NetworkMonitor> m_netmon;
};
} // namespace face
diff --git a/daemon/face/protocol-factory.cpp b/daemon/face/protocol-factory.cpp
index 010190d..0a8a150 100644
--- a/daemon/face/protocol-factory.cpp
+++ b/daemon/face/protocol-factory.cpp
@@ -38,8 +38,7 @@
}
unique_ptr<ProtocolFactory>
-ProtocolFactory::create(const std::string& id, shared_ptr<ndn::net::NetworkMonitor> netmon,
- const FaceCreatedCallback& addFace)
+ProtocolFactory::create(const std::string& id, const CtorParams& params)
{
Registry& registry = getRegistry();
auto found = registry.find(id);
@@ -47,10 +46,7 @@
return nullptr;
}
- auto factory = found->second();
- factory->netmon = std::move(netmon);
- factory->addFace = addFace;
- return factory;
+ return found->second(params);
}
std::set<std::string>
@@ -62,5 +58,13 @@
return factoryIds;
}
+ProtocolFactory::ProtocolFactory(const CtorParams& params)
+ : addFace(params.addFace)
+ , netmon(params.netmon)
+{
+ BOOST_ASSERT(addFace != nullptr);
+ BOOST_ASSERT(netmon != nullptr);
+}
+
} // namespace face
} // namespace nfd
diff --git a/daemon/face/protocol-factory.hpp b/daemon/face/protocol-factory.hpp
index 9d6ad3e..524f901 100644
--- a/daemon/face/protocol-factory.hpp
+++ b/daemon/face/protocol-factory.hpp
@@ -36,6 +36,19 @@
namespace nfd {
namespace face {
+/** \brief Parameters to ProtocolFactory constructor
+ *
+ * Every ProtocolFactory subclass is expected to have a constructor that accepts CtorParams,
+ * which in turn passes it to ProtocolFactory base class constructor. Parameters are passed as a
+ * struct rather than individually, so that a future change in list of parameters does not
+ * require updates to subclass constructors.
+ */
+struct ProtocolFactoryCtorParams
+{
+ FaceCreatedCallback addFace;
+ shared_ptr<ndn::net::NetworkMonitor> netmon;
+};
+
/** \brief Provides support for an underlying protocol
* \sa FaceSystem
*
@@ -46,6 +59,8 @@
class ProtocolFactory : noncopyable
{
public: // registry
+ using CtorParams = ProtocolFactoryCtorParams;
+
/** \brief Register a protocol factory type
* \tparam S subclass of ProtocolFactory
* \param id factory identifier
@@ -56,15 +71,14 @@
{
Registry& registry = getRegistry();
BOOST_ASSERT(registry.count(id) == 0);
- registry[id] = &make_unique<PF>;
+ registry[id] = &make_unique<PF, const CtorParams&>;
}
/** \brief Create a protocol factory instance
* \retval nullptr if factory with \p id is not registered
*/
static unique_ptr<ProtocolFactory>
- create(const std::string& id, shared_ptr<ndn::net::NetworkMonitor> netmon,
- const FaceCreatedCallback& addFace);
+ create(const std::string& id, const CtorParams& params);
/** \brief Get registered protocol factory ids
*/
@@ -72,8 +86,7 @@
listRegistered();
public:
- /**
- * \brief Base class for all exceptions thrown by ProtocolFactory subclasses
+ /** \brief Base class for all exceptions thrown by ProtocolFactory subclasses
*/
class Error : public std::runtime_error
{
@@ -138,6 +151,9 @@
getChannels() const = 0;
protected:
+ explicit
+ ProtocolFactory(const CtorParams& params);
+
template<typename ChannelMap>
static std::vector<shared_ptr<const Channel>>
getChannelsFromMap(const ChannelMap& channelMap)
@@ -148,8 +164,8 @@
}
private: // registry
- typedef std::function<unique_ptr<ProtocolFactory>()> CreateFunc;
- typedef std::map<std::string, CreateFunc> Registry; // indexed by factory id
+ using CreateFunc = std::function<unique_ptr<ProtocolFactory>(const CtorParams&)>;
+ using Registry = std::map<std::string, CreateFunc>; // indexed by factory id
static Registry&
getRegistry();
@@ -157,14 +173,14 @@
protected:
std::set<std::string> providedSchemes; ///< FaceUri schemes provided by this ProtocolFactory
+ FaceCreatedCallback addFace; ///< callback when a new face is created
+
/** \brief NetworkMonitor for listing available network interfaces and monitoring their changes
*
* ProtocolFactory subclass should check the NetworkMonitor has sufficient capabilities prior
* to usage.
*/
shared_ptr<ndn::net::NetworkMonitor> netmon;
-
- FaceCreatedCallback addFace; ///< callback when a new face is created
};
} // namespace face
diff --git a/daemon/face/tcp-factory.cpp b/daemon/face/tcp-factory.cpp
index e2fb3fe..28ac251 100644
--- a/daemon/face/tcp-factory.cpp
+++ b/daemon/face/tcp-factory.cpp
@@ -40,6 +40,11 @@
return id;
}
+TcpFactory::TcpFactory(const CtorParams& params)
+ : ProtocolFactory(params)
+{
+}
+
void
TcpFactory::processConfig(OptionalConfigSection configSection,
FaceSystem::ConfigContext& context)
diff --git a/daemon/face/tcp-factory.hpp b/daemon/face/tcp-factory.hpp
index 4576c65..36ee320 100644
--- a/daemon/face/tcp-factory.hpp
+++ b/daemon/face/tcp-factory.hpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2014-2017, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
@@ -40,6 +40,9 @@
static const std::string&
getId();
+ explicit
+ TcpFactory(const CtorParams& params);
+
/** \brief process face_system.tcp config section
*/
void
diff --git a/daemon/face/udp-factory.cpp b/daemon/face/udp-factory.cpp
index cc02bfc..8303aa8 100644
--- a/daemon/face/udp-factory.cpp
+++ b/daemon/face/udp-factory.cpp
@@ -51,6 +51,10 @@
return id;
}
+UdpFactory::UdpFactory(const CtorParams& params)
+ : ProtocolFactory(params)
+{
+}
void
UdpFactory::processConfig(OptionalConfigSection configSection,
diff --git a/daemon/face/udp-factory.hpp b/daemon/face/udp-factory.hpp
index 3ebf0a3..2f809ac 100644
--- a/daemon/face/udp-factory.hpp
+++ b/daemon/face/udp-factory.hpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2014-2017, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
@@ -56,6 +56,9 @@
static const std::string&
getId();
+ explicit
+ UdpFactory(const CtorParams& params);
+
/** \brief process face_system.udp config section
*/
void
diff --git a/daemon/face/unix-stream-factory.cpp b/daemon/face/unix-stream-factory.cpp
index 2d5fe94..0bba3be 100644
--- a/daemon/face/unix-stream-factory.cpp
+++ b/daemon/face/unix-stream-factory.cpp
@@ -40,6 +40,10 @@
return id;
}
+UnixStreamFactory::UnixStreamFactory(const CtorParams& params)
+ : ProtocolFactory(params)
+{
+}
void
UnixStreamFactory::processConfig(OptionalConfigSection configSection,
diff --git a/daemon/face/unix-stream-factory.hpp b/daemon/face/unix-stream-factory.hpp
index 3061abd..53baadf 100644
--- a/daemon/face/unix-stream-factory.hpp
+++ b/daemon/face/unix-stream-factory.hpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2014-2017, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
@@ -40,6 +40,9 @@
static const std::string&
getId();
+ explicit
+ UnixStreamFactory(const CtorParams& params);
+
/** \brief process face_system.unix config section
*/
void
diff --git a/daemon/face/websocket-factory.cpp b/daemon/face/websocket-factory.cpp
index 2905ae4..21933a4 100644
--- a/daemon/face/websocket-factory.cpp
+++ b/daemon/face/websocket-factory.cpp
@@ -40,6 +40,10 @@
return id;
}
+WebSocketFactory::WebSocketFactory(const CtorParams& params)
+ : ProtocolFactory(params)
+{
+}
void
WebSocketFactory::processConfig(OptionalConfigSection configSection,
diff --git a/daemon/face/websocket-factory.hpp b/daemon/face/websocket-factory.hpp
index b7b7b02..0eaa767 100644
--- a/daemon/face/websocket-factory.hpp
+++ b/daemon/face/websocket-factory.hpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2014-2017, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
@@ -40,6 +40,9 @@
static const std::string&
getId();
+ explicit
+ WebSocketFactory(const CtorParams& params);
+
/** \brief process face_system.websocket config section
*/
void