faces: Channel base class

refs #1261

Change-Id: If17037c4802cb6ccec0201c3bff7fc4b6db41c6a
This commit is contained in:
Junxiao Shi committed 2014-03-05 11:56:10 -07:00
1 parent 2d9bdc8a5f
commit 61e3cc51ed
18 files changed
+317 -158

No files matched your search

+58 -26
View File
@@ -14,49 +14,81 @@ namespace nfd {
FaceUri::FaceUri(const std::string& uri)
{
if (!parse(uri))
{
throw Error("Malformed URI: " + uri);
}
if (!parse(uri)) {
throw Error("Malformed URI: " + uri);
}
}
FaceUri::FaceUri(const char* uri)
{
if (!parse(uri)) {
throw Error("Malformed URI: " + std::string(uri));
}
}
bool
FaceUri::parse(const std::string& uri)
{
m_scheme.clear();
m_domain.clear();
m_host.clear();
m_isV6 = false;
m_port.clear();
boost::regex protocolExp("(\\w+\\d?)://(.+)");
m_path.clear();
boost::regex protocolExp("(\\w+\\d?)://([^/]*)(\\/[^?]*)?");
boost::smatch protocolMatch;
if (!boost::regex_match(uri, protocolMatch, protocolExp))
{
return false;
}
if (!boost::regex_match(uri, protocolMatch, protocolExp)) {
return false;
}
m_scheme = protocolMatch[1];
m_path = protocolMatch[3];
const std::string& remote = protocolMatch[2];
const std::string& authority = protocolMatch[2];
boost::regex v6Exp("^\\[(([a-fA-F0-9:]+))\\](:(\\d+))?$"); // [stuff]:port
boost::regex v6Exp("^\\[(([a-fA-F0-9:]+))\\](:(\\d+))?$"); // [host]:port
boost::regex v4Exp("^((\\d+\\.){3}\\d+)(:(\\d+))?$");
boost::regex hostExp("^(([^:]+))(:(\\d+))?$"); // stuff:port
boost::regex hostExp("^(([^:]+))(:(\\d+))?$"); // host:port
boost::smatch match;
if (boost::regex_match(remote, match, v6Exp) ||
boost::regex_match(remote, match, v4Exp) ||
boost::regex_match(remote, match, hostExp))
{
m_domain = match[1];
m_port = match[4];
}
else
{
m_isV6 = boost::regex_match(authority, match, v6Exp);
if (m_isV6 ||
boost::regex_match(authority, match, v4Exp) ||
boost::regex_match(authority, match, hostExp)) {
m_host = match[1];
m_port = match[4];
}
else {
if (m_path.empty()) {
return false;
}
NFD_LOG_DEBUG("URI [" << uri << "] parsed into: "
<< m_scheme << ", " << m_domain << ", " << m_port);
}
NFD_LOG_DEBUG("URI [" << uri << "] parsed into: " <<
m_scheme << ", " << m_host << ", " << m_port << ", " << m_path);
return true;
}
FaceUri::FaceUri(const boost::asio::ip::tcp::endpoint& endpoint)
{
m_isV6 = endpoint.address().is_v6();
m_scheme = m_isV6 ? "tcp6" : "tcp4";
m_host = endpoint.address().to_string();
m_port = boost::lexical_cast<std::string>(endpoint.port());
}
FaceUri::FaceUri(const boost::asio::ip::udp::endpoint& endpoint)
{
m_isV6 = endpoint.address().is_v6();
m_scheme = m_isV6 ? "udp6" : "udp4";
m_host = endpoint.address().to_string();
m_port = boost::lexical_cast<std::string>(endpoint.port());
}
FaceUri::FaceUri(const boost::asio::local::stream_protocol::endpoint& endpoint)
: m_isV6(false)
{
m_scheme = "unix";
m_path = endpoint.path();
}
} // namespace nfd
+69 -19
View File
@@ -11,6 +11,7 @@
namespace nfd {
/// represents a URI in Face Management protocol
class FaceUri
{
public:
@@ -21,44 +22,62 @@ public:
};
FaceUri();
/** \brief Construct URI object
*
* Expected format: scheme://domain:port/path?query_string#fragment_id
/** \brief construct by parsing
*
* \param uri scheme://domain[:port]/path
* \throw FaceUri::Error if URI cannot be parsed
*/
explicit
FaceUri(const std::string& uri);
/** \brief Exception-safe parsing of URI
*/
// This overload is needed so that calls with string literal won't be
// resolved to boost::asio::local::stream_protocol::endpoint overload.
explicit
FaceUri(const char* uri);
explicit
FaceUri(const boost::asio::ip::tcp::endpoint& endpoint);
explicit
FaceUri(const boost::asio::ip::udp::endpoint& endpoint);
explicit
FaceUri(const boost::asio::local::stream_protocol::endpoint& endpoint);
/// exception-safe parsing
bool
parse(const std::string& uri);
/** \brief Get scheme (protocol) extracted from URI
*/
/// get scheme (protocol)
const std::string&
getScheme() const;
/** \brief Get domain extracted from URI
*/
/// get host (domain)
const std::string&
getDomain() const;
getHost() const;
/** \brief Get port extracted from URI
*
* \return Extracted port or empty string if port wasn't present
*/
/// get port
const std::string&
getPort() const;
// other getters should be added, when necessary
/// get path
const std::string&
getPath() const;
/// write as a string
std::string
toString() const;
private:
std::string m_scheme;
std::string m_domain;
std::string m_host;
/// whether to add [] around host when writing string
bool m_isV6;
std::string m_port;
std::string m_path;
friend std::ostream& operator<<(std::ostream& os, const FaceUri& uri);
};
inline
@@ -73,9 +92,9 @@ FaceUri::getScheme() const
}
inline const std::string&
FaceUri::getDomain() const
FaceUri::getHost() const
{
return m_domain;
return m_host;
}
inline const std::string&
@@ -84,6 +103,37 @@ FaceUri::getPort() const
return m_port;
}
inline const std::string&
FaceUri::getPath() const
{
return m_path;
}
inline std::string
FaceUri::toString() const
{
std::ostringstream os;
os << *this;
return os.str();
}
inline std::ostream&
operator<<(std::ostream& os, const FaceUri& uri)
{
os << uri.m_scheme << "://";
if (uri.m_isV6) {
os << "[" << uri.m_host << "]";
}
else {
os << uri.m_host;
}
if (!uri.m_port.empty()) {
os << ":" << uri.m_port;
}
os << uri.m_path;
return os;
}
} // namespace nfd
#endif // NFD_CORE_FACE_URI_H
+21
View File
@@ -0,0 +1,21 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (C) 2014 Named Data Networking Project
* See COPYING for copyright and distribution information.
*/
#include "channel.hpp"
namespace nfd {
Channel::~Channel()
{
}
void
Channel::setUri(const FaceUri& uri)
{
m_uri = uri;
}
} // namespace nfd
+50
View File
@@ -0,0 +1,50 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (C) 2014 Named Data Networking Project
* See COPYING for copyright and distribution information.
*/
#ifndef NFD_FACE_CHANNEL_HPP
#define NFD_FACE_CHANNEL_HPP
#include "face.hpp"
namespace nfd {
class Channel : noncopyable
{
public:
/** \brief Prototype for the callback called when face is created
* (as a response to incoming connection or after connection
* is established)
*/
typedef function<void(const shared_ptr<Face>& newFace)> FaceCreatedCallback;
/** \brief Prototype for the callback that is called when face is failed to
* get created
*/
typedef function<void(const std::string& reason)> ConnectFailedCallback;
virtual
~Channel();
const FaceUri&
getUri() const;
protected:
void
setUri(const FaceUri& uri);
private:
FaceUri m_uri;
};
inline const FaceUri&
Channel::getUri() const
{
return m_uri;
}
} // namespace nfd
#endif // NFD_FACE_CHANNEL_HPP
+1
View File
@@ -9,6 +9,7 @@
#include "common.hpp"
#include "core/event-emitter.hpp"
#include "core/face-uri.hpp"
#include "face-counter.hpp"
namespace nfd {
+23 -18
View File
@@ -5,6 +5,8 @@
*/
#include "tcp-channel.hpp"
#include "core/global-io.hpp"
#include "core/face-uri.hpp"
namespace nfd {
@@ -12,10 +14,13 @@ NFD_LOG_INIT("TcpChannel");
using namespace boost::asio;
TcpChannel::TcpChannel(io_service& ioService,
const tcp::Endpoint& localEndpoint)
: m_ioService(ioService)
, m_localEndpoint(localEndpoint)
TcpChannel::TcpChannel(const tcp::Endpoint& localEndpoint)
: m_localEndpoint(localEndpoint)
{
this->setUri(FaceUri(localEndpoint));
}
TcpChannel::~TcpChannel()
{
}
@@ -24,14 +29,14 @@ TcpChannel::listen(const FaceCreatedCallback& onFaceCreated,
const ConnectFailedCallback& onAcceptFailed,
int backlog/* = tcp::acceptor::max_connections*/)
{
m_acceptor = make_shared<ip::tcp::acceptor>(boost::ref(m_ioService));
m_acceptor = make_shared<ip::tcp::acceptor>(boost::ref(getGlobalIoService()));
m_acceptor->open(m_localEndpoint.protocol());
m_acceptor->set_option(ip::tcp::acceptor::reuse_address(true));
m_acceptor->bind(m_localEndpoint);
m_acceptor->listen(backlog);
shared_ptr<ip::tcp::socket> clientSocket =
make_shared<ip::tcp::socket>(boost::ref(m_ioService));
make_shared<ip::tcp::socket>(boost::ref(getGlobalIoService()));
m_acceptor->async_accept(*clientSocket,
bind(&TcpChannel::handleSuccessfulAccept, this, _1,
clientSocket,
@@ -51,10 +56,10 @@ TcpChannel::connect(const tcp::Endpoint& remoteEndpoint,
}
shared_ptr<ip::tcp::socket> clientSocket =
make_shared<ip::tcp::socket>(boost::ref(m_ioService));
make_shared<ip::tcp::socket>(boost::ref(getGlobalIoService()));
shared_ptr<monotonic_deadline_timer> connectTimeoutTimer =
make_shared<monotonic_deadline_timer>(boost::ref(m_ioService));
make_shared<monotonic_deadline_timer>(boost::ref(getGlobalIoService()));
clientSocket->open(m_localEndpoint.protocol());
@@ -82,10 +87,10 @@ TcpChannel::connect(const std::string& remoteHost, const std::string& remotePort
const time::Duration& timeout/* = time::seconds(4)*/)
{
shared_ptr<ip::tcp::socket> clientSocket =
make_shared<ip::tcp::socket>(boost::ref(m_ioService));
make_shared<ip::tcp::socket>(boost::ref(getGlobalIoService()));
shared_ptr<monotonic_deadline_timer> connectTimeoutTimer =
make_shared<monotonic_deadline_timer>(boost::ref(m_ioService));
make_shared<monotonic_deadline_timer>(boost::ref(getGlobalIoService()));
clientSocket->open(m_localEndpoint.protocol());
@@ -97,7 +102,7 @@ TcpChannel::connect(const std::string& remoteHost, const std::string& remotePort
ip::tcp::resolver::query query(remoteHost, remotePort);
shared_ptr<ip::tcp::resolver> resolver =
make_shared<ip::tcp::resolver>(boost::ref(m_ioService));
make_shared<ip::tcp::resolver>(boost::ref(getGlobalIoService()));
resolver->async_resolve(query,
bind(&TcpChannel::handleEndpointResolution, this, _1, _2,
@@ -128,7 +133,7 @@ TcpChannel::createFace(const shared_ptr<ip::tcp::socket>& socket,
face = make_shared<TcpLocalFace>(boost::cref(socket));
else
face = make_shared<TcpFace>(boost::cref(socket));
face->onFail += bind(&TcpChannel::afterFaceFailed, this, remoteEndpoint);
onFaceCreated(face);
@@ -163,7 +168,7 @@ TcpChannel::handleSuccessfulAccept(const boost::system::error_code& error,
// prepare accepting the next connection
shared_ptr<ip::tcp::socket> clientSocket =
make_shared<ip::tcp::socket>(boost::ref(m_ioService));
make_shared<ip::tcp::socket>(boost::ref(getGlobalIoService()));
m_acceptor->async_accept(*clientSocket,
bind(&TcpChannel::handleSuccessfulAccept, this, _1,
clientSocket,
@@ -188,10 +193,10 @@ TcpChannel::handleSuccessfulConnect(const boost::system::error_code& error,
return;
socket->close();
NFD_LOG_DEBUG("Connect to remote endpoint failed: "
<< error.category().message(error.value()));
onConnectFailed("Connect to remote endpoint failed: " +
error.category().message(error.value()));
return;
@@ -199,7 +204,7 @@ TcpChannel::handleSuccessfulConnect(const boost::system::error_code& error,
NFD_LOG_DEBUG("[" << m_localEndpoint << "] "
">> Connection to " << socket->remote_endpoint());
createFace(socket, onFaceCreated);
}
@@ -215,7 +220,7 @@ TcpChannel::handleFailedConnect(const boost::system::error_code& error,
NFD_LOG_DEBUG("Connect to remote endpoint timed out: "
<< error.category().message(error.value()));
onConnectFailed("Connect to remote endpoint timed out: " +
error.category().message(error.value()));
socket->close(); // abort the connection
@@ -241,7 +246,7 @@ TcpChannel::handleEndpointResolution(const boost::system::error_code& error,
NFD_LOG_DEBUG("Remote endpoint hostname or port cannot be resolved: "
<< error.category().message(error.value()));
onConnectFailed("Remote endpoint hostname or port cannot be resolved: " +
error.category().message(error.value()));
return;
+12 -23
View File
@@ -7,7 +7,7 @@
#ifndef NFD_FACE_TCP_CHANNEL_HPP
#define NFD_FACE_TCP_CHANNEL_HPP
#include "common.hpp"
#include "channel.hpp"
#include "core/time.hpp"
#include <ndn-cpp-dev/util/monotonic_deadline_timer.hpp>
#include "tcp-face.hpp"
@@ -15,7 +15,7 @@
namespace nfd {
namespace tcp {
typedef boost::asio::ip::tcp::endpoint Endpoint;
typedef boost::asio::ip::tcp::endpoint Endpoint;
} // namespace tcp
/**
@@ -25,30 +25,20 @@ namespace tcp {
* connections (TcpChannel::listen needs to be called for that
* to work) or explicitly after using TcpChannel::connect method.
*/
class TcpChannel // : protected SessionBasedChannel
class TcpChannel : public Channel
{
public:
/**
* \brief Prototype for the callback called when face is created
* (as a response to incoming connection or after connection
* is established)
*/
typedef function<void(const shared_ptr<Face>& newFace)> FaceCreatedCallback;
/**
* \brief Prototype for the callback that is called when face is failed to
* get created
*/
typedef function<void(const std::string& reason)> ConnectFailedCallback;
/**
* \brief Create TCP channel for the local endpoint
*
* To enable creation faces upon incoming connections,
* one needs to explicitly call TcpChannel::listen method.
*/
TcpChannel(boost::asio::io_service& ioService,
const tcp::Endpoint& localEndpoint);
explicit
TcpChannel(const tcp::Endpoint& localEndpoint);
virtual
~TcpChannel();
/**
* \brief Enable listening on the local endpoint, accept connections,
@@ -94,8 +84,8 @@ public:
* \brief Get number of faces in the channel
*/
size_t
size() const;
size() const;
private:
void
createFace(const shared_ptr<boost::asio::ip::tcp::socket>& socket,
@@ -116,7 +106,7 @@ private:
const shared_ptr<boost::asio::monotonic_deadline_timer>& timer,
const FaceCreatedCallback& onFaceCreated,
const ConnectFailedCallback& onConnectFailed);
void
handleFailedConnect(const boost::system::error_code& error,
const shared_ptr<boost::asio::ip::tcp::socket>& socket,
@@ -133,7 +123,6 @@ private:
const shared_ptr<boost::asio::ip::tcp::resolver>& resolver);
private:
boost::asio::io_service& m_ioService;
tcp::Endpoint m_localEndpoint;
typedef std::map< tcp::Endpoint, shared_ptr<Face> > ChannelFaceMap;
@@ -144,5 +133,5 @@ private:
};
} // namespace nfd
#endif // NFD_FACE_TCP_CHANNEL_HPP
+2 -3
View File
@@ -5,7 +5,6 @@
*/
#include "tcp-factory.hpp"
#include "core/global-io.hpp"
#include "core/resolver.hpp"
#include "core/logger.hpp"
@@ -25,7 +24,7 @@ TcpFactory::createChannel(const tcp::Endpoint& endpoint)
if(static_cast<bool>(channel))
return channel;
channel = make_shared<TcpChannel>(boost::ref(getGlobalIoService()), boost::cref(endpoint));
channel = make_shared<TcpChannel>(boost::cref(endpoint));
m_channels[endpoint] = channel;
NFD_LOG_DEBUG("Channel [" << endpoint << "] created");
return channel;
@@ -58,7 +57,7 @@ TcpFactory::createFace(const FaceUri& uri,
else if (uri.getScheme() == "tcp6")
addressSelector = resolver::Ipv6Address();
TcpResolver::asyncResolve(uri.getDomain(),
TcpResolver::asyncResolve(uri.getHost(),
uri.getPort().empty() ? m_defaultPort : uri.getPort(),
bind(&TcpFactory::continueCreateFaceAfterResolve, this, _1,
onCreated, onConnectFailed),
+7
View File
@@ -6,6 +6,7 @@
#include "udp-channel.hpp"
#include "core/global-io.hpp"
#include "core/face-uri.hpp"
namespace nfd {
@@ -35,6 +36,12 @@ UdpChannel::UdpChannel(const udp::Endpoint& localEndpoint,
throw Error("Failed to properly configure the socket. "
"UdpChannel creation aborted, check the address (" + std::string(e.what()) + ")");
}
this->setUri(FaceUri(localEndpoint));
}
UdpChannel::~UdpChannel()
{
}
void
+9 -19
View File
@@ -7,14 +7,15 @@
#ifndef NFD_FACE_UDP_CHANNEL_HPP
#define NFD_FACE_UDP_CHANNEL_HPP
#include "common.hpp"
#include "channel.hpp"
#include "core/time.hpp"
#include "core/global-io.hpp"
#include "udp-face.hpp"
namespace nfd {
namespace udp {
typedef boost::asio::ip::udp::endpoint Endpoint;
typedef boost::asio::ip::udp::endpoint Endpoint;
} // namespace udp
/**
@@ -22,10 +23,9 @@ namespace udp {
*
*
*/
class UdpChannel
class UdpChannel : public Channel
{
public:
/**
* \brief Exception of UdpChannel
*/
@@ -33,19 +33,6 @@ public:
{
Error(const std::string& what) : runtime_error(what) {}
};
/**
* \brief Prototype for the callback called when face is created
* (as a response to new incoming communication not managed
* by any faces yet or after the connect function is created)
*/
typedef function<void(const shared_ptr<UdpFace>& newFace)> FaceCreatedCallback;
/**
* \brief Prototype for the callback that is called when face is failed to
* get created
*/
typedef function<void(const std::string& reason)> ConnectFailedCallback;
/**
* \brief Create UDP channel for the local endpoint
@@ -60,6 +47,9 @@ public:
UdpChannel(const udp::Endpoint& localEndpoint,
const time::Duration& timeout);
virtual
~UdpChannel();
/**
* \brief Enable listening on the local endpoint, accept connections,
* and create faces when remote host makes a connection
@@ -110,7 +100,7 @@ private:
afterFaceFailed(udp::Endpoint& endpoint);
/**
* \brief The UdpChannel has received a new pkt from a remote endpoint not yet
* \brief The UdpChannel has received a new pkt from a remote endpoint not yet
* associated with any UdpFace
*/
void
@@ -138,7 +128,7 @@ private:
* Its handler has a fixed signature. No space for the face callback
*/
FaceCreatedCallback onFaceCreatedNewPeerCallback;
// @todo remove the onConnectFailedNewPeerCallback if it remains unused
ConnectFailedCallback onConnectFailedNewPeerCallback;
+1 -1
View File
@@ -149,7 +149,7 @@ UdpFactory::createFace(const FaceUri& uri,
else if (uri.getScheme() == "udp6")
addressSelector = resolver::Ipv6Address();
UdpResolver::asyncResolve(uri.getDomain(),
UdpResolver::asyncResolve(uri.getHost(),
uri.getPort().empty() ? m_defaultPort : uri.getPort(),
bind(&UdpFactory::continueCreateFaceAfterResolve, this, _1,
onCreated, onConnectFailed),
+7 -7
View File
@@ -5,6 +5,7 @@
*/
#include "unix-stream-channel.hpp"
#include "core/global-io.hpp"
#include <boost/filesystem.hpp>
#include <sys/stat.h> // for chmod()
@@ -15,11 +16,10 @@ NFD_LOG_INIT("UnixStreamChannel");
using namespace boost::asio::local;
UnixStreamChannel::UnixStreamChannel(boost::asio::io_service& ioService,
const unix_stream::Endpoint& endpoint)
: m_ioService(ioService)
, m_endpoint(endpoint)
UnixStreamChannel::UnixStreamChannel(const unix_stream::Endpoint& endpoint)
: m_endpoint(endpoint)
{
this->setUri(FaceUri(endpoint));
}
UnixStreamChannel::~UnixStreamChannel()
@@ -50,7 +50,7 @@ UnixStreamChannel::listen(const FaceCreatedCallback& onFaceCreated,
fs::remove(p);
}
m_acceptor = make_shared<stream_protocol::acceptor>(boost::ref(m_ioService));
m_acceptor = make_shared<stream_protocol::acceptor>(boost::ref(getGlobalIoService()));
m_acceptor->open(m_endpoint.protocol());
m_acceptor->bind(m_endpoint);
m_acceptor->listen(backlog);
@@ -61,7 +61,7 @@ UnixStreamChannel::listen(const FaceCreatedCallback& onFaceCreated,
}
shared_ptr<stream_protocol::socket> clientSocket =
make_shared<stream_protocol::socket>(boost::ref(m_ioService));
make_shared<stream_protocol::socket>(boost::ref(getGlobalIoService()));
m_acceptor->async_accept(*clientSocket,
bind(&UnixStreamChannel::handleSuccessfulAccept, this, _1,
clientSocket, onFaceCreated, onAcceptFailed));
@@ -95,7 +95,7 @@ UnixStreamChannel::handleSuccessfulAccept(const boost::system::error_code& error
// prepare accepting the next connection
shared_ptr<stream_protocol::socket> clientSocket =
make_shared<stream_protocol::socket>(boost::ref(m_ioService));
make_shared<stream_protocol::socket>(boost::ref(getGlobalIoService()));
m_acceptor->async_accept(*clientSocket,
bind(&UnixStreamChannel::handleSuccessfulAccept, this, _1,
clientSocket, onFaceCreated, onAcceptFailed));
+6 -18
View File
@@ -7,13 +7,13 @@
#ifndef NFD_FACE_UNIX_STREAM_CHANNEL_HPP
#define NFD_FACE_UNIX_STREAM_CHANNEL_HPP
#include "common.hpp"
#include "channel.hpp"
#include "unix-stream-face.hpp"
namespace nfd {
namespace unix_stream {
typedef boost::asio::local::stream_protocol::endpoint Endpoint;
typedef boost::asio::local::stream_protocol::endpoint Endpoint;
} // namespace unix_stream
/**
@@ -22,21 +22,9 @@ namespace unix_stream {
* Channel can create faces as a response to incoming IPC connections
* (UnixStreamChannel::listen needs to be called for that to work).
*/
class UnixStreamChannel // : protected SessionBasedChannel
class UnixStreamChannel : public Channel
{
public:
/**
* \brief Prototype for the callback called when a face is created
* (as a response to an incoming connection)
*/
typedef function<void(const shared_ptr<UnixStreamFace>& newFace)> FaceCreatedCallback;
/**
* \brief Prototype for the callback that is called when a face
* fails to be created
*/
typedef function<void(const std::string& reason)> ConnectFailedCallback;
/**
* \brief UnixStreamChannel-related error
*/
@@ -51,9 +39,10 @@ public:
* To enable creation of faces upon incoming connections, one
* needs to explicitly call UnixStreamChannel::listen method.
*/
UnixStreamChannel(boost::asio::io_service& ioService,
const unix_stream::Endpoint& endpoint);
explicit
UnixStreamChannel(const unix_stream::Endpoint& endpoint);
virtual
~UnixStreamChannel();
/**
@@ -82,7 +71,6 @@ private:
const ConnectFailedCallback& onConnectFailed);
private:
boost::asio::io_service& m_ioService;
unix_stream::Endpoint m_endpoint;
shared_ptr<boost::asio::local::stream_protocol::acceptor> m_acceptor;
};
+1 -3
View File
@@ -5,7 +5,6 @@
*/
#include "unix-stream-factory.hpp"
#include "core/global-io.hpp"
#include <boost/filesystem.hpp> // for canonical()
@@ -22,8 +21,7 @@ UnixStreamFactory::createChannel(const std::string& unixSocketPath)
if (channel)
return channel;
channel = make_shared<UnixStreamChannel>(boost::ref(getGlobalIoService()),
boost::cref(endpoint));
channel = make_shared<UnixStreamChannel>(boost::cref(endpoint));
m_channels[endpoint] = channel;
return channel;
}
+17 -5
View File
@@ -24,32 +24,44 @@ BOOST_AUTO_TEST_CASE(Basic)
BOOST_CHECK(uri.parse("udp://hostname:80"));
BOOST_CHECK_EQUAL(uri.getScheme(), "udp");
BOOST_CHECK_EQUAL(uri.getDomain(), "hostname");
BOOST_CHECK_EQUAL(uri.getHost(), "hostname");
BOOST_CHECK_EQUAL(uri.getPort(), "80");
BOOST_CHECK_EQUAL(uri.getPath(), "");
BOOST_CHECK(uri.parse("udp4://192.0.2.1:20"));
BOOST_CHECK_EQUAL(uri.getScheme(), "udp4");
BOOST_CHECK_EQUAL(uri.getDomain(), "192.0.2.1");
BOOST_CHECK_EQUAL(uri.getHost(), "192.0.2.1");
BOOST_CHECK_EQUAL(uri.getPort(), "20");
BOOST_CHECK_EQUAL(uri.getPath(), "");
BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0::1]:6363"));
BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
BOOST_CHECK_EQUAL(uri.getDomain(), "2001:db8:3f9:0::1");
BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0::1");
BOOST_CHECK_EQUAL(uri.getPort(), "6363");
BOOST_CHECK_EQUAL(uri.getPath(), "");
BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86d3]:6363"));
BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
BOOST_CHECK_EQUAL(uri.getDomain(), "2001:db8:3f9:0:3025:ccc5:eeeb:86d3");
BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0:3025:ccc5:eeeb:86d3");
BOOST_CHECK_EQUAL(uri.getPort(), "6363");
BOOST_CHECK_EQUAL(uri.getPath(), "");
BOOST_CHECK(uri.parse("tcp://random.host.name"));
BOOST_CHECK_EQUAL(uri.getScheme(), "tcp");
BOOST_CHECK_EQUAL(uri.getDomain(), "random.host.name");
BOOST_CHECK_EQUAL(uri.getHost(), "random.host.name");
BOOST_CHECK_EQUAL(uri.getPort(), "");
BOOST_CHECK_EQUAL(uri.getPath(), "");
BOOST_CHECK(uri.parse("unix:///var/run/example.sock"));
BOOST_CHECK_EQUAL(uri.getScheme(), "unix");
BOOST_CHECK_EQUAL(uri.getHost(), "");
BOOST_CHECK_EQUAL(uri.getPort(), "");
BOOST_CHECK_EQUAL(uri.getPath(), "/var/run/example.sock");
BOOST_CHECK_EQUAL(uri.parse("tcp://192.0.2.1:"), false);
BOOST_CHECK_EQUAL(uri.parse("tcp://[::zzzz]"), false);
BOOST_CHECK_EQUAL(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86dg]:6363"), false);
BOOST_CHECK_EQUAL(uri.getPath(), "");
}
BOOST_AUTO_TEST_SUITE_END()
+5
View File
@@ -22,9 +22,14 @@ BOOST_AUTO_TEST_CASE(ChannelMap)
shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
shared_ptr<TcpChannel> channel1a = factory.createChannel("127.0.0.1", "20070");
BOOST_CHECK_EQUAL(channel1, channel1a);
BOOST_CHECK_EQUAL(channel1->getUri().toString(), "tcp4://127.0.0.1:20070");
shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
BOOST_CHECK_NE(channel1, channel2);
shared_ptr<TcpChannel> channel3 = factory.createChannel("::1", "20071");
BOOST_CHECK_NE(channel2, channel3);
BOOST_CHECK_EQUAL(channel3->getUri().toString(), "tcp6://[::1]:20071");
}
class EndToEndFixture : protected BaseFixture
+7 -2
View File
@@ -57,12 +57,17 @@ BOOST_FIXTURE_TEST_CASE(ChannelMapUdp, FactoryErrorCheck)
shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
shared_ptr<UdpChannel> channel1a = factory.createChannel("127.0.0.1", "20070");
BOOST_CHECK_EQUAL(channel1, channel1a);
BOOST_CHECK_EQUAL(channel1->getUri().toString(), "udp4://127.0.0.1:20070");
shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
BOOST_CHECK_NE(channel1, channel2);
shared_ptr<UdpChannel> channel3 = factory.createChannel(interfaceIp, "20070");
shared_ptr<UdpChannel> channel4 = factory.createChannel("::1", "20071");
BOOST_CHECK_NE(channel2, channel4);
BOOST_CHECK_EQUAL(channel4->getUri().toString(), "udp6://[::1]:20071");
//same endpoint of a unicast channel
BOOST_CHECK_EXCEPTION(factory.createMulticastFace(interfaceIp,
"224.0.0.1",
@@ -162,7 +167,7 @@ class EndToEndFixture : protected BaseFixture
{
public:
void
channel1_onFaceCreated(const shared_ptr<UdpFace>& newFace)
channel1_onFaceCreated(const shared_ptr<Face>& newFace)
{
BOOST_CHECK(!static_cast<bool>(m_face1));
m_face1 = newFace;
@@ -271,7 +276,7 @@ public:
void
channel_onFaceCreated(const shared_ptr<UdpFace>& newFace)
channel_onFaceCreated(const shared_ptr<Face>& newFace)
{
m_faces.push_back(newFace);
m_limitedIo.afterOp();
+21 -14
View File
@@ -15,17 +15,24 @@ namespace tests {
using namespace boost::asio::local;
#define CHANNEL_PATH1 "unix-stream-test.1.sock"
#define CHANNEL_PATH2 "unix-stream-test.2.sock"
BOOST_FIXTURE_TEST_SUITE(FaceUnixStream, BaseFixture)
BOOST_AUTO_TEST_CASE(ChannelMap)
{
UnixStreamFactory factory;
shared_ptr<UnixStreamChannel> channel1 = factory.createChannel("foo");
shared_ptr<UnixStreamChannel> channel1a = factory.createChannel("foo");
shared_ptr<UnixStreamChannel> channel1 = factory.createChannel(CHANNEL_PATH1);
shared_ptr<UnixStreamChannel> channel1a = factory.createChannel(CHANNEL_PATH1);
BOOST_CHECK_EQUAL(channel1, channel1a);
std::string channel1uri = channel1->getUri().toString();
BOOST_CHECK_EQUAL(channel1uri.find("unix:///"), 0); // third '/' is the path separator
BOOST_CHECK_EQUAL(channel1uri.rfind(CHANNEL_PATH1),
channel1uri.size() - std::string(CHANNEL_PATH1).size());
shared_ptr<UnixStreamChannel> channel2 = factory.createChannel("bar");
shared_ptr<UnixStreamChannel> channel2 = factory.createChannel(CHANNEL_PATH2);
BOOST_CHECK_NE(channel1, channel2);
}
@@ -41,10 +48,10 @@ public:
}
void
channel1_onFaceCreated(const shared_ptr<UnixStreamFace>& newFace)
channel1_onFaceCreated(const shared_ptr<Face>& newFace)
{
BOOST_CHECK(!static_cast<bool>(m_face1));
m_face1 = newFace;
m_face1 = static_pointer_cast<UnixStreamFace>(newFace);
m_face1->onReceiveInterest +=
bind(&EndToEndFixture::face1_onReceiveInterest, this, _1);
m_face1->onReceiveData +=
@@ -94,9 +101,9 @@ public:
}
void
channel_onFaceCreated(const shared_ptr<UnixStreamFace>& newFace)
channel_onFaceCreated(const shared_ptr<Face>& newFace)
{
m_faces.push_back(newFace);
m_faces.push_back(static_pointer_cast<UnixStreamFace>(newFace));
m_limitedIo.afterOp();
}
@@ -127,13 +134,13 @@ BOOST_FIXTURE_TEST_CASE(EndToEnd, EndToEndFixture)
{
UnixStreamFactory factory;
shared_ptr<UnixStreamChannel> channel1 = factory.createChannel("foo");
shared_ptr<UnixStreamChannel> channel1 = factory.createChannel(CHANNEL_PATH1);
channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
shared_ptr<stream_protocol::socket> client =
make_shared<stream_protocol::socket>(boost::ref(g_io));
client->async_connect(stream_protocol::endpoint("foo"),
client->async_connect(stream_protocol::endpoint(CHANNEL_PATH1),
bind(&EndToEndFixture::client_onConnect, this, _1));
BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
@@ -184,13 +191,13 @@ BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
{
UnixStreamFactory factory;
shared_ptr<UnixStreamChannel> channel = factory.createChannel("foo");
shared_ptr<UnixStreamChannel> channel = factory.createChannel(CHANNEL_PATH1);
channel->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
shared_ptr<stream_protocol::socket> client1 =
make_shared<stream_protocol::socket>(boost::ref(g_io));
client1->async_connect(stream_protocol::endpoint("foo"),
client1->async_connect(stream_protocol::endpoint(CHANNEL_PATH1),
bind(&EndToEndFixture::client_onConnect, this, _1));
BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
@@ -200,7 +207,7 @@ BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
shared_ptr<stream_protocol::socket> client2 =
make_shared<stream_protocol::socket>(boost::ref(g_io));
client2->async_connect(stream_protocol::endpoint("foo"),
client2->async_connect(stream_protocol::endpoint(CHANNEL_PATH1),
bind(&EndToEndFixture::client_onConnect, this, _1));
BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
@@ -266,13 +273,13 @@ BOOST_FIXTURE_TEST_CASE(UnixStreamFaceLocalControlHeader, EndToEndFixture)
{
UnixStreamFactory factory;
shared_ptr<UnixStreamChannel> channel1 = factory.createChannel("foo");
shared_ptr<UnixStreamChannel> channel1 = factory.createChannel(CHANNEL_PATH1);
channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
shared_ptr<stream_protocol::socket> client =
make_shared<stream_protocol::socket>(boost::ref(g_io));
client->async_connect(stream_protocol::endpoint("foo"),
client->async_connect(stream_protocol::endpoint(CHANNEL_PATH1),
bind(&EndToEndFixture::client_onConnect, this, _1));
BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,