38 const time::seconds& timeout)
39 : m_localEndpoint(localEndpoint)
41 , m_idleFaceTimeout(timeout)
49 return m_channelFaces.size();
58 shared_ptr<Face>
face;
60 face = createFace(remoteEndpoint, persistency).second;
62 catch (
const boost::system::system_error& e) {
63 NFD_LOG_WARN(
"[" << m_localEndpoint <<
"] Connect failed: " << e.what());
65 onConnectFailed(504, std::string(
"Connect failed: ") + e.what());
79 NFD_LOG_WARN(
"[" << m_localEndpoint <<
"] Already listening");
83 m_socket.open(m_localEndpoint.protocol());
84 m_socket.set_option(ip::udp::socket::reuse_address(
true));
85 if (m_localEndpoint.address().is_v6())
86 m_socket.set_option(ip::v6_only(
true));
88 m_socket.bind(m_localEndpoint);
89 this->waitForNewPeer(onFaceCreated, onReceiveFailed);
98 bind(&UdpChannel::handleNewPeer,
this,
99 boost::asio::placeholders::error,
100 boost::asio::placeholders::bytes_transferred,
101 onFaceCreated, onReceiveFailed));
105 UdpChannel::handleNewPeer(
const boost::system::error_code& error,
106 size_t nBytesReceived,
111 if (error == boost::asio::error::operation_aborted)
114 NFD_LOG_DEBUG(
"[" << m_localEndpoint <<
"] Receive failed: " << error.message());
116 onReceiveFailed(500,
"Receive failed: " + error.message());
120 NFD_LOG_DEBUG(
"[" << m_localEndpoint <<
"] New peer " << m_remoteEndpoint);
122 bool isCreated =
false;
123 shared_ptr<Face>
face;
127 catch (
const boost::system::system_error& e) {
128 NFD_LOG_WARN(
"[" << m_localEndpoint <<
"] Failed to create face for peer " 129 << m_remoteEndpoint <<
": " << e.what());
131 onReceiveFailed(504,
"Failed to create face for peer");
141 this->waitForNewPeer(onFaceCreated, onReceiveFailed);
144 std::pair<bool, shared_ptr<Face>>
147 auto it = m_channelFaces.find(remoteEndpoint);
148 if (it != m_channelFaces.end()) {
150 auto face = it->second;
154 bool isTransitionAllowed = persistency !=
face->getPersistency() &&
157 if (isTransitionAllowed) {
158 face->setPersistency(persistency);
160 return {
false,
face};
165 socket.set_option(ip::udp::socket::reuse_address(
true));
166 socket.bind(m_localEndpoint);
167 socket.connect(remoteEndpoint);
169 auto linkService = make_unique<face::GenericLinkService>();
170 auto transport = make_unique<face::UnicastUdpTransport>(std::move(socket), persistency, m_idleFaceTimeout);
171 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
173 face->setPersistency(persistency);
175 m_channelFaces[remoteEndpoint] = face;
177 [
this, remoteEndpoint] {
178 NFD_LOG_TRACE(
"Erasing " << remoteEndpoint <<
" from channel face map");
179 m_channelFaces.erase(remoteEndpoint);
UdpChannel(const udp::Endpoint &localEndpoint, const time::seconds &timeout)
Create UDP channel for the local endpoint.
represents the underlying protocol and address used by a Face
void connectFaceClosedSignal(Face &face, const std::function< void()> &f)
invokes a callback when the face is closed
detail::SimulatorIo & getGlobalIoService()
#define NFD_LOG_DEBUG(expression)
A Transport that communicates on a unicast UDP socket.
void connect(const udp::Endpoint &remoteEndpoint, ndn::nfd::FacePersistency persistency, const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onConnectFailed)
Create a face by establishing connection to remote endpoint.
#define NFD_LOG_TRACE(expression)
Copyright (c) 2011-2015 Regents of the University of California.
size_t size() const
Get number of faces in the channel.
void setUri(const FaceUri &uri)
boost::asio::ip::udp::endpoint Endpoint
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 &onReceiveFailed)
Enable listening on the local endpoint, accept connections, and create faces when remote host makes a...
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...
#define NFD_LOG_INIT(name)
const size_t MAX_NDN_PACKET_SIZE
practical limit of network layer packet size