face+transport: Eliminating concept of ConnectionInfo
The current "transport" is not really a transport, rather than a
connection, which completely handles all connection-specific tasks.
I don't see any reason for not to keep connection information as
inherent property of the "transport".
Change-Id: Ib06697522d1b8f5b22d82fa86994056a9b0b7dec
diff --git a/include/ndn-cpp/face.hpp b/include/ndn-cpp/face.hpp
index 3204ae0..a0b3f0e 100644
--- a/include/ndn-cpp/face.hpp
+++ b/include/ndn-cpp/face.hpp
@@ -23,8 +23,8 @@
* @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.
*/
- Face(const ptr_lib::shared_ptr<Transport>& transport, const ptr_lib::shared_ptr<const Transport::ConnectionInfo>& connectionInfo)
- : node_(transport, connectionInfo)
+ Face(const ptr_lib::shared_ptr<Transport>& transport)
+ : node_(transport)
{
}
@@ -34,8 +34,7 @@
* @param port The port of the NDN hub. If omitted. use 6363.
*/
Face(const char *host, unsigned short port = 6363)
- : node_(ptr_lib::shared_ptr<TcpTransport>(new TcpTransport()),
- ptr_lib::make_shared<TcpTransport::ConnectionInfo>(host, port))
+ : node_(ptr_lib::shared_ptr<TcpTransport>(new TcpTransport(host, port)))
{
}
diff --git a/include/ndn-cpp/node.hpp b/include/ndn-cpp/node.hpp
index 5767be8..c68372d 100644
--- a/include/ndn-cpp/node.hpp
+++ b/include/ndn-cpp/node.hpp
@@ -50,7 +50,7 @@
* @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.
*/
- Node(const ptr_lib::shared_ptr<Transport>& transport, const ptr_lib::shared_ptr<const Transport::ConnectionInfo>& connectionInfo);
+ Node(const ptr_lib::shared_ptr<Transport>& transport);
/**
* Send the Interest through the transport, read the entire response and call onData(interest, data).
@@ -112,9 +112,6 @@
const ptr_lib::shared_ptr<Transport>&
getTransport() { return transport_; }
- const ptr_lib::shared_ptr<const Transport::ConnectionInfo>&
- getConnectionInfo() { return connectionInfo_; }
-
void
onReceivedElement(const uint8_t *element, size_t elementLength);
@@ -332,7 +329,6 @@
const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags, WireFormat& wireFormat);
ptr_lib::shared_ptr<Transport> transport_;
- ptr_lib::shared_ptr<const Transport::ConnectionInfo> connectionInfo_;
std::vector<ptr_lib::shared_ptr<PendingInterest> > pendingInterestTable_;
std::vector<ptr_lib::shared_ptr<RegisteredPrefix> > registeredPrefixTable_;
Interest ndndIdFetcherInterest_;
diff --git a/include/ndn-cpp/transport/tcp-transport.hpp b/include/ndn-cpp/transport/tcp-transport.hpp
index 4be0144..e54516a 100644
--- a/include/ndn-cpp/transport/tcp-transport.hpp
+++ b/include/ndn-cpp/transport/tcp-transport.hpp
@@ -19,51 +19,14 @@
class TcpTransport : public Transport {
public:
- /**
- * A TcpTransport::ConnectionInfo extends Transport::ConnectionInfo to hold the host and port info for the TCP connection.
- */
- class ConnectionInfo : public Transport::ConnectionInfo {
- public:
- /**
- * Create a ConnectionInfo with the given host and port.
- * @param host The host for the connection.
- * @param port The port number for the connection. If omitted, use 6363.
- */
- ConnectionInfo(const char *host, unsigned short port = 6363)
- : host_(host), port_(port)
- {
- }
-
- /**
- * Get the host given to the constructor.
- * @return A string reference for the host.
- */
- const std::string&
- getHost() const { return host_; }
-
- /**
- * Get the port given to the constructor.
- * @return The port number.
- */
- unsigned short
- getPort() const { return port_; }
-
- virtual
- ~ConnectionInfo();
-
- private:
- std::string host_;
- unsigned short port_;
- };
-
- TcpTransport();
+ TcpTransport(const char *host, unsigned short port = 6363);
/**
* Connect according to the info in ConnectionInfo, and processEvents() will use elementListener.
* @param connectionInfo A reference to a TcpTransport::ConnectionInfo.
* @param elementListener Not a shared_ptr because we assume that it will remain valid during the life of this object.
*/
- virtual void connect(const Transport::ConnectionInfo& connectionInfo, ElementListener& elementListener);
+ virtual void connect(ElementListener& elementListener);
/**
* Set data to the host
@@ -91,8 +54,11 @@
~TcpTransport();
private:
- ptr_lib::shared_ptr<struct ndn_TcpTransport> transport_;
+ std::string host_;
+ unsigned short port_;
+
bool isConnected_;
+ ptr_lib::shared_ptr<struct ndn_TcpTransport> transport_;
// TODO: This belongs in the socket listener.
ptr_lib::shared_ptr<struct ndn_BinaryXmlElementReader> elementReader_;
};
diff --git a/include/ndn-cpp/transport/transport.hpp b/include/ndn-cpp/transport/transport.hpp
index 5a23027..80d41b2 100644
--- a/include/ndn-cpp/transport/transport.hpp
+++ b/include/ndn-cpp/transport/transport.hpp
@@ -17,20 +17,12 @@
class Transport {
public:
/**
- * A Transport::ConnectionInfo is a base class for connection information used by subclasses of Transport.
- */
- class ConnectionInfo {
- public:
- virtual ~ConnectionInfo();
- };
-
- /**
* Connect according to the info in ConnectionInfo, and processEvents() will use elementListener.
* @param connectionInfo A reference to an object of a subclass of ConnectionInfo.
* @param elementListener Not a shared_ptr because we assume that it will remain valid during the life of this object.
*/
virtual void
- connect(const Transport::ConnectionInfo& connectionInfo, ElementListener& elementListener);
+ connect(ElementListener& elementListener) = 0;
/**
* Set data to the host
@@ -38,9 +30,9 @@
* @param dataLength The number of bytes in data.
*/
virtual void
- send(const uint8_t *data, size_t dataLength);
+ send(const uint8_t *data, size_t dataLength) = 0;
- void
+ inline void
send(const std::vector<uint8_t>& data)
{
send(&data[0], data.size());
@@ -57,7 +49,7 @@
processEvents() = 0;
virtual bool
- getIsConnected();
+ getIsConnected() = 0;
/**
* Close the connection. This base class implementation does nothing, but your derived class can override.
diff --git a/include/ndn-cpp/transport/udp-transport.hpp b/include/ndn-cpp/transport/udp-transport.hpp
index ea38496..39dfbe2 100644
--- a/include/ndn-cpp/transport/udp-transport.hpp
+++ b/include/ndn-cpp/transport/udp-transport.hpp
@@ -20,43 +20,11 @@
class UdpTransport : public Transport {
public:
/**
- * A UdpTransport::ConnectionInfo extends Transport::ConnectionInfo to hold the host and port info for the UDP connection.
+ * Create a UdpTransport with the given host and port.
+ * @param host The host for the connection.
+ * @param port The port number for the connection. If omitted, use 6363.
*/
- class ConnectionInfo : public Transport::ConnectionInfo {
- public:
- /**
- * Create a ConnectionInfo with the given host and port.
- * @param host The host for the connection.
- * @param port The port number for the connection. If omitted, use 6363.
- */
- ConnectionInfo(const char *host, unsigned short port = 6363)
- : host_(host), port_(port)
- {
- }
-
- /**
- * Get the host given to the constructor.
- * @return A string reference for the host.
- */
- const std::string&
- getHost() const { return host_; }
-
- /**
- * Get the port given to the constructor.
- * @return The port number.
- */
- unsigned short
- getPort() const { return port_; }
-
- virtual
- ~ConnectionInfo();
-
- private:
- std::string host_;
- unsigned short port_;
- };
-
- UdpTransport();
+ UdpTransport(const char *host, unsigned short port = 6363);
/**
* Connect according to the info in ConnectionInfo, and processEvents() will use elementListener.
@@ -64,7 +32,7 @@
* @param elementListener Not a shared_ptr because we assume that it will remain valid during the life of this object.
*/
virtual void
- connect(const Transport::ConnectionInfo& connectionInfo, ElementListener& elementListener);
+ connect(ElementListener& elementListener);
/**
* Set data to the host
@@ -96,8 +64,11 @@
~UdpTransport();
private:
- ptr_lib::shared_ptr<struct ndn_UdpTransport> transport_;
+ std::string host_;
+ unsigned short port_;
+
bool isConnected_;
+ ptr_lib::shared_ptr<struct ndn_UdpTransport> transport_;
// TODO: This belongs in the socket listener.
ptr_lib::shared_ptr<struct ndn_BinaryXmlElementReader> elementReader_;
};
diff --git a/src/node.cpp b/src/node.cpp
index 99db103..a5f3d95 100644
--- a/src/node.cpp
+++ b/src/node.cpp
@@ -110,8 +110,8 @@
signature->setSignature(Blob(signatureBits, (size_t)signatureBitsLength));
}
-Node::Node(const ptr_lib::shared_ptr<Transport>& transport, const ptr_lib::shared_ptr<const Transport::ConnectionInfo>& connectionInfo)
-: transport_(transport), connectionInfo_(connectionInfo),
+Node::Node(const ptr_lib::shared_ptr<Transport>& transport)
+: transport_(transport),
ndndIdFetcherInterest_(Name("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY"), 4000.0)
{
}
@@ -121,7 +121,7 @@
{
// TODO: Properly check if we are already connected to the expected host.
if (!transport_->getIsConnected())
- transport_->connect(*connectionInfo_, *this);
+ transport_->connect(*this);
uint64_t pendingInterestId = PendingInterest::getNextPendingInterestId();
pendingInterestTable_.push_back(ptr_lib::shared_ptr<PendingInterest>(new PendingInterest
diff --git a/src/transport/tcp-transport.cpp b/src/transport/tcp-transport.cpp
index 6c5dc85..cb1c6e3 100644
--- a/src/transport/tcp-transport.cpp
+++ b/src/transport/tcp-transport.cpp
@@ -17,24 +17,19 @@
namespace ndn {
-TcpTransport::ConnectionInfo::~ConnectionInfo()
-{
-}
-
-TcpTransport::TcpTransport()
- : isConnected_(false), transport_(new struct ndn_TcpTransport), elementReader_(new struct ndn_BinaryXmlElementReader)
+TcpTransport::TcpTransport(const char *host, unsigned short port/* = 6363*/)
+ : host_(host), port_(port)
+ , isConnected_(false), transport_(new struct ndn_TcpTransport), elementReader_(new struct ndn_BinaryXmlElementReader)
{
ndn_TcpTransport_initialize(transport_.get());
elementReader_->partialData.array = 0;
}
void
-TcpTransport::connect(const Transport::ConnectionInfo& connectionInfo, ElementListener& elementListener)
+TcpTransport::connect(ElementListener& elementListener)
{
- const TcpTransport::ConnectionInfo& tcpConnectionInfo = dynamic_cast<const TcpTransport::ConnectionInfo&>(connectionInfo);
-
ndn_Error error;
- if ((error = ndn_TcpTransport_connect(transport_.get(), (char *)tcpConnectionInfo.getHost().c_str(), tcpConnectionInfo.getPort())))
+ if ((error = ndn_TcpTransport_connect(transport_.get(), (char *)host_.c_str(), port_)))
throw runtime_error(ndn_getErrorString(error));
// TODO: This belongs in the socket listener.
diff --git a/src/transport/transport.cpp b/src/transport/transport.cpp
index 0dcf518..4d5db21 100644
--- a/src/transport/transport.cpp
+++ b/src/transport/transport.cpp
@@ -12,34 +12,6 @@
namespace ndn {
-Transport::ConnectionInfo::~ConnectionInfo()
-{
-}
-
-void
-Transport::connect(const Transport::ConnectionInfo& connectionInfo, ElementListener& elementListener)
-{
- throw logic_error("unimplemented");
-}
-
-void
-Transport::send(const uint8_t *data, size_t dataLength)
-{
- throw logic_error("unimplemented");
-}
-
-void
-Transport::processEvents()
-{
- throw logic_error("unimplemented");
-}
-
-bool
-Transport::getIsConnected()
-{
- throw logic_error("unimplemented");
-}
-
void
Transport::close()
{
diff --git a/src/transport/udp-transport.cpp b/src/transport/udp-transport.cpp
index c8def39..aaf3d06 100644
--- a/src/transport/udp-transport.cpp
+++ b/src/transport/udp-transport.cpp
@@ -17,24 +17,19 @@
namespace ndn {
-UdpTransport::ConnectionInfo::~ConnectionInfo()
-{
-}
-
-UdpTransport::UdpTransport()
- : isConnected_(false), transport_(new struct ndn_UdpTransport), elementReader_(new struct ndn_BinaryXmlElementReader)
+UdpTransport::UdpTransport(const char *host, unsigned short port/* = 6363*/)
+ : host_(host), port_(port)
+ , isConnected_(false), transport_(new struct ndn_UdpTransport), elementReader_(new struct ndn_BinaryXmlElementReader)
{
ndn_UdpTransport_initialize(transport_.get());
elementReader_->partialData.array = 0;
}
void
-UdpTransport::connect(const Transport::ConnectionInfo& connectionInfo, ElementListener& elementListener)
+UdpTransport::connect(ElementListener& elementListener)
{
- const UdpTransport::ConnectionInfo& udpConnectionInfo = dynamic_cast<const UdpTransport::ConnectionInfo&>(connectionInfo);
-
ndn_Error error;
- if ((error = ndn_UdpTransport_connect(transport_.get(), (char *)udpConnectionInfo.getHost().c_str(), udpConnectionInfo.getPort())))
+ if ((error = ndn_UdpTransport_connect(transport_.get(), (char *)host_.c_str(), port_)))
throw runtime_error(ndn_getErrorString(error));
// TODO: This belongs in the socket listener.