31 #include <boost/filesystem.hpp> 39 : m_endpoint(endpoint)
51 boost::system::error_code error;
52 m_acceptor.close(error);
54 boost::filesystem::remove(m_endpoint.path(), error);
64 NFD_LOG_WARN(
"[" << m_endpoint <<
"] Already listening");
68 namespace fs = boost::filesystem;
70 fs::path socketPath(m_endpoint.path());
71 fs::file_type type = fs::symlink_status(socketPath).type();
73 if (type == fs::socket_file) {
74 boost::system::error_code error;
76 socket.connect(m_endpoint, error);
77 NFD_LOG_TRACE(
"[" << m_endpoint <<
"] connect() on existing socket file returned: " 81 BOOST_THROW_EXCEPTION(
Error(
"Socket file at " + m_endpoint.path()
82 +
" belongs to another NFD process"));
84 else if (error == boost::asio::error::connection_refused ||
85 error == boost::asio::error::timed_out) {
88 NFD_LOG_DEBUG(
"[" << m_endpoint <<
"] Removing stale socket file");
89 fs::remove(socketPath);
92 else if (type != fs::file_not_found) {
93 BOOST_THROW_EXCEPTION(
Error(m_endpoint.path() +
" already exists and is not a socket file"));
97 m_acceptor.bind(m_endpoint);
98 m_acceptor.listen(backlog);
100 if (::chmod(m_endpoint.path().c_str(), 0666) < 0) {
101 BOOST_THROW_EXCEPTION(
Error(
"chmod(" + m_endpoint.path() +
") failed: " +
102 std::strerror(errno)));
106 accept(onFaceCreated, onAcceptFailed);
113 m_acceptor.async_accept(m_socket, bind(&UnixStreamChannel::handleAccept,
this,
114 boost::asio::placeholders::error,
115 onFaceCreated, onAcceptFailed));
119 UnixStreamChannel::handleAccept(
const boost::system::error_code& error,
124 if (error == boost::asio::error::operation_aborted)
127 NFD_LOG_DEBUG(
"[" << m_endpoint <<
"] Accept failed: " << error.message());
129 onAcceptFailed(500,
"Accept failed: " + error.message());
135 auto linkService = make_unique<face::GenericLinkService>();
136 auto transport = make_unique<face::UnixStreamTransport>(std::move(m_socket));
137 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
141 accept(onFaceCreated, onAcceptFailed);
represents the underlying protocol and address used by a Face
UnixStreamChannel-related error.
detail::SimulatorIo & getGlobalIoService()
#define NFD_LOG_DEBUG(expression)
#define NFD_LOG_TRACE(expression)
Copyright (c) 2011-2015 Regents of the University of California.
UnixStreamChannel(const unix_stream::Endpoint &endpoint)
Create UnixStream channel for the specified endpoint.
void setUri(const FaceUri &uri)
function< void(uint32_t status, const std::string &reason)> FaceCreationFailedCallback
Prototype for the callback that is invoked when the face fails to be created.
#define NFD_LOG_WARN(expression)
void listen(const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onAcceptFailed, int backlog=boost::asio::local::stream_protocol::acceptor::max_connections)
Enable listening on the local endpoint, accept connections, and create faces when a connection is mad...
function< void(const shared_ptr< Face > &newFace)> FaceCreatedCallback
Prototype for the callback that is invoked when the face is created (as a response to incoming connec...
boost::asio::local::stream_protocol::endpoint Endpoint
~UnixStreamChannel() override
#define NFD_LOG_INIT(name)