Skip to content

Commit

Permalink
util, transport: add configuration file support and make default unix…
Browse files Browse the repository at this point in the history
… socket configurable

add sample library configuration file

refs: #1364

Change-Id: I3cb36d078aa3f0b0a50d9a83a521e95448df0a93
  • Loading branch information
Steve DiBenedetto committed Mar 23, 2014
1 parent 1c5a1a9 commit c07b3a2
Show file tree
Hide file tree
Showing 17 changed files with 559 additions and 21 deletions.
10 changes: 10 additions & 0 deletions client.conf.sample
@@ -0,0 +1,10 @@
; "unix_socket" specifies the location of the NFD unix socket
unix_socket=/var/run/nfd.sock

; "protocol" deteremines the protocol for prefix registration
; it has a value of:
; nfd-0.1
; nrd-0.1
; ndnd-tlv-0.7
; ndnx-0.7
protocol=nrd-0.1
42 changes: 33 additions & 9 deletions src/face.cpp
Expand Up @@ -13,6 +13,7 @@

#include "util/time.hpp"
#include "util/random.hpp"
#include "util/config-file.hpp"
#include <cstdlib>

#include "management/ndnd-controller.hpp"
Expand All @@ -23,13 +24,15 @@ namespace ndn {

Face::Face()
{
construct(shared_ptr<Transport>(new UnixTransport()),
const std::string socketName = UnixTransport::getDefaultSocketName(m_config);
construct(shared_ptr<Transport>(new UnixTransport(socketName)),
make_shared<boost::asio::io_service>());
}

Face::Face(const shared_ptr<boost::asio::io_service>& ioService)
{
construct(shared_ptr<Transport>(new UnixTransport()),
const std::string socketName = UnixTransport::getDefaultSocketName(m_config);
construct(shared_ptr<Transport>(new UnixTransport(socketName)),
ioService);
}

Expand Down Expand Up @@ -68,18 +71,39 @@ Face::construct(const shared_ptr<Transport>& transport,
m_pitTimeoutCheckTimer = make_shared<monotonic_deadline_timer>(boost::ref(*m_ioService));
m_processEventsTimeoutTimer = make_shared<monotonic_deadline_timer>(boost::ref(*m_ioService));

if (std::getenv("NFD") != 0)
std::string protocol = "nrd-0.1";

try
{
if (std::getenv("NRD") != 0)
m_fwController = make_shared<nrd::Controller>(boost::ref(*this));
else
m_fwController = make_shared<nfd::Controller>(boost::ref(*this));
protocol = m_config.getParsedConfiguration().get<std::string>("protocol");
}
catch (const boost::property_tree::ptree_bad_path& error)
{
// protocol not specified
}
catch (const boost::property_tree::ptree_bad_data& error)
{
throw ConfigFile::Error(error.what());
}

if (isSupportedNrdProtocol(protocol))
{
m_fwController = make_shared<nrd::Controller>(boost::ref(*this));
}
if (isSupportedNfdProtocol(protocol))
{
m_fwController = make_shared<nfd::Controller>(boost::ref(*this));
}
else if (isSupportedNdndProtocol(protocol))
{
m_fwController = make_shared<ndnd::Controller>(boost::ref(*this));
}
else
m_fwController = make_shared<ndnd::Controller>(boost::ref(*this));
{
throw Face::Error("Cannot create controller for unsupported protocol \"" + protocol + "\"");
}
}


const PendingInterestId*
Face::expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout)
{
Expand Down
40 changes: 40 additions & 0 deletions src/face.hpp
Expand Up @@ -58,12 +58,16 @@ class Face : noncopyable

/**
* @brief Create a new Face for communication with an NDN Forwarder using the default UnixTransport.
* @throws ConfigFile::Error on configuration file parse failure
* @throws Face::Error on unsupported protocol
*/
Face();

/**
* @brief Create a new Face for communication with an NDN Forwarder using the default UnixTransport.
* @param ioService A shared pointer to boost::io_service object that should control all IO operations
* @throws ConfigFile::Error on configuration file parse failure
* @throws Face::Error on unsupported protocol
*/
explicit
Face(const shared_ptr<boost::asio::io_service>& ioService);
Expand All @@ -72,13 +76,15 @@ class Face : noncopyable
* Create a new Face for communication with an NDN hub at host:port using the default TcpTransport.
* @param host The host of the NDN hub.
* @param port The port or service name of the NDN hub. If omitted. use 6363.
* @throws Face::Error on unsupported protocol
*/
Face(const std::string& host, const std::string& port = "6363");

/**
* Create a new Face for communication with an NDN hub with the given Transport object and connectionInfo.
* @param transport A shared_ptr to a Transport object used for communication.
* @param transport A shared_ptr to a Transport::ConnectionInfo to be used to connect to the transport.
* @throws Face::Error on unsupported protocol
*/
explicit
Face(const shared_ptr<Transport>& transport);
Expand All @@ -94,6 +100,7 @@ class Face : noncopyable
* // Now the following ensures that events on both faces are processed
* face1.processEvents();
* </code>
* @throws Face::Error on unsupported protocol
*/
Face(const shared_ptr<Transport>& transport,
const shared_ptr<boost::asio::io_service>& ioService);
Expand Down Expand Up @@ -203,9 +210,22 @@ class Face : noncopyable
ioService() { return m_ioService; }

private:

/**
* @throws Face::Error on unsupported protocol
*/
void
construct(const shared_ptr<Transport>& transport,
const shared_ptr<boost::asio::io_service>& ioService);

bool
isSupportedNfdProtocol(const std::string& protocol);

bool
isSupportedNrdProtocol(const std::string& protocol);

bool
isSupportedNdndProtocol(const std::string& protocol);

struct ProcessEventsTimeout {};
typedef std::list<shared_ptr<PendingInterest> > PendingInterestTable;
Expand Down Expand Up @@ -255,8 +275,28 @@ class Face : noncopyable
RegisteredPrefixTable m_registeredPrefixTable;

shared_ptr<Controller> m_fwController;

ConfigFile m_config;
};

inline bool
Face::isSupportedNfdProtocol(const std::string& protocol)
{
return protocol == "nfd-0.1";
}

inline bool
Face::isSupportedNrdProtocol(const std::string& protocol)
{
return protocol == "nrd-0.1";
}

inline bool
Face::isSupportedNdndProtocol(const std::string& protocol)
{
return protocol == "ndnd-tlv-0.7";
}

} // namespace ndn

#endif // NDN_FACE_HPP
16 changes: 14 additions & 2 deletions src/transport/transport.hpp
Expand Up @@ -14,7 +14,12 @@ namespace ndn {

class Transport {
public:
struct Error : public std::runtime_error { inline Error(const boost::system::error_code &code, const std::string &msg); };
class Error : public std::runtime_error
{
public:
inline Error(const boost::system::error_code &code, const std::string &msg);
inline Error(const std::string& msg);
};

typedef ptr_lib::function<void (const Block &wire)> ReceiveCallback;
typedef ptr_lib::function<void ()> ErrorCallback;
Expand Down Expand Up @@ -89,11 +94,18 @@ Transport::Transport()
{
}

inline Transport::Error::Error(const boost::system::error_code& code, const std::string& msg)
inline
Transport::Error::Error(const boost::system::error_code& code, const std::string& msg)
: std::runtime_error(msg + (code.value() ? " (" + code.category().message(code.value()) + ")" : ""))
{
}

inline
Transport::Error::Error(const std::string& msg)
: std::runtime_error(msg)
{
}

inline
Transport::~Transport()
{
Expand Down
50 changes: 42 additions & 8 deletions src/transport/unix-transport.cpp
Expand Up @@ -13,14 +13,6 @@

namespace ndn {

UnixTransport::UnixTransport()
{
if (std::getenv("NFD") != 0)
m_unixSocket = "/var/run/nfd.sock";
else
m_unixSocket = "/tmp/.ndnd.sock";
}

UnixTransport::UnixTransport(const std::string& unixSocket)
: m_unixSocket(unixSocket)
{
Expand All @@ -30,6 +22,48 @@ UnixTransport::~UnixTransport()
{
}

std::string
UnixTransport::getDefaultSocketName(const ConfigFile& config)
{
const ConfigFile::Parsed& parsed = config.getParsedConfiguration();
try
{
return parsed.get<std::string>("unix_socket");
}
catch (const boost::property_tree::ptree_bad_path& error)
{
// unix_socket not present, continue
}
catch (const boost::property_tree::ptree_bad_data& error)
{
throw ConfigFile::Error(error.what());
}

// no unix_socket specified so the default socket name
// depends on the protocol we're using
try
{
const std::string protocol = parsed.get<std::string>("protocol");
if (protocol == "ndnd-tlv-0.7")
{
return "/tmp/.ndnd.sock";
}
}
catch (boost::property_tree::ptree_bad_path& error)
{
return "/var/run/nfd.sock";
}
catch (boost::property_tree::ptree_bad_data& error)
{
throw ConfigFile::Error(error.what());
}

// A we made here, then there's no unix_socket specified in the configuration
// file. A protocol is present, but it's not ndnd.
// Assume the default nfd.sock location.
return "/var/run/nfd.sock";
}

void
UnixTransport::connect(boost::asio::io_service& ioService,
const ReceiveCallback& receiveCallback)
Expand Down
20 changes: 18 additions & 2 deletions src/transport/unix-transport.hpp
Expand Up @@ -9,6 +9,7 @@

#include "../common.hpp"
#include "transport.hpp"
#include "../util/config-file.hpp"

// forward declaration
namespace boost { namespace asio { namespace local { class stream_protocol; } } }
Expand All @@ -22,9 +23,15 @@ class StreamTransportImpl;
class UnixTransport : public Transport
{
public:
UnixTransport();

/**
* Create Unix transport based on the socket specified
* in a well-known configuration file or fallback to /var/run/nfd.sock
*
* @throws Throws UnixTransport::Error on failure to parse a discovered configuration file
*/
UnixTransport(const std::string& unixSocket);

~UnixTransport();

// from Transport
Expand All @@ -46,7 +53,16 @@ class UnixTransport : public Transport

virtual void
send(const Block& header, const Block& payload);


/**
* Determine the default NFD unix socket
*
* @returns unix_socket value if present in config, else /var/run/nfd.sock
* @throws ConfigFile::Error if fail to parse value of a present "unix_socket" field
*/
static std::string
getDefaultSocketName(const ConfigFile& config);

private:
std::string m_unixSocket;

Expand Down

0 comments on commit c07b3a2

Please sign in to comment.