Change Transport connect to use the new ConnectionInfo object.
diff --git a/ndn-cpp/transport/tcp-transport.cpp b/ndn-cpp/transport/tcp-transport.cpp
index 248a613..757823d 100644
--- a/ndn-cpp/transport/tcp-transport.cpp
+++ b/ndn-cpp/transport/tcp-transport.cpp
@@ -12,21 +12,26 @@
namespace ndn {
-void TcpTransport::connect(Node &node)
+TcpTransport::ConnectionInfo::~ConnectionInfo()
+{
+}
+
+void TcpTransport::connect(const Transport::ConnectionInfo &connectionInfo, ElementListener &elementListener)
{
+ const TcpTransport::ConnectionInfo &tcpConnectionInfo = dynamic_cast<const TcpTransport::ConnectionInfo &>(connectionInfo);
+
ndn_Error error;
- if ((error = ndn_TcpTransport_connect(&transport_, (char *)node.getHost(), node.getPort())))
+ if ((error = ndn_TcpTransport_connect(&transport_, (char *)tcpConnectionInfo.getHost().c_str(), tcpConnectionInfo.getPort())))
throw std::runtime_error(ndn_getErrorString(error));
// TODO: This belongs in the socket listener.
const unsigned int initialLength = 1000;
- // Automatically cast ndn_ to (struct ndn_ElementListener *)
+ // Automatically cast elementReader_ to (struct ndn_ElementListener *)
ndn_BinaryXmlElementReader_init
- (&elementReader_, &node, (unsigned char *)malloc(initialLength), initialLength, ndn_realloc);
+ (&elementReader_, &elementListener, (unsigned char *)malloc(initialLength), initialLength, ndn_realloc);
- // TODO: Properly indicate connected status.
isConnected_ = true;
- node_ = &node;
+ elementListener_ = &elementListener;
}
void TcpTransport::send(const unsigned char *data, unsigned int dataLength)
diff --git a/ndn-cpp/transport/tcp-transport.hpp b/ndn-cpp/transport/tcp-transport.hpp
index 4190600..0a1e4a7 100644
--- a/ndn-cpp/transport/tcp-transport.hpp
+++ b/ndn-cpp/transport/tcp-transport.hpp
@@ -6,6 +6,7 @@
#ifndef NDN_TCPTRANSPORT_HPP
#define NDN_TCPTRANSPORT_HPP
+#include <string>
#include "../c/transport/tcp-transport.h"
#include "../c/encoding/binary-xml-element-reader.h"
#include "transport.hpp"
@@ -14,18 +15,53 @@
class TcpTransport : public Transport {
public:
+ /**
+ * A UcpTransport::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.
+ */
+ ConnectionInfo(const char *host, unsigned short port)
+ : 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()
- : node_(0), isConnected_(false)
+ : elementListener_(0), isConnected_(false)
{
ndn_TcpTransport_init(&transport_);
elementReader_.partialData.array = 0;
}
/**
- * Connect to the host specified in node.
- * @param node Not a shared_ptr because we assume that it will remain valid during the life of this Transport object.
+ * 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(Node &node);
+ virtual void connect(const Transport::ConnectionInfo &connectionInfo, ElementListener &elementListener);
/**
* Set data to the host
@@ -35,7 +71,7 @@
virtual void send(const unsigned char *data, unsigned int dataLength);
/**
- * Process any data to receive. For each element received, call node.onReceivedElement.
+ * Process any data to receive. For each element received, call elementListener.onReceivedElement.
* This is non-blocking and will return immediately if there is no data to receive.
* You should normally not call this directly since it is called by Face.processEvents.
* @throw This may throw an exception for reading data or in the callback for processing the data. If you
@@ -55,7 +91,7 @@
private:
struct ndn_TcpTransport transport_;
bool isConnected_;
- Node *node_;
+ ElementListener *elementListener_;
// TODO: This belongs in the socket listener.
ndn_BinaryXmlElementReader elementReader_;
};
diff --git a/ndn-cpp/transport/transport.cpp b/ndn-cpp/transport/transport.cpp
index feb1345..1636e4e 100644
--- a/ndn-cpp/transport/transport.cpp
+++ b/ndn-cpp/transport/transport.cpp
@@ -10,7 +10,11 @@
namespace ndn {
-void Transport::connect(Node &node)
+Transport::ConnectionInfo::~ConnectionInfo()
+{
+}
+
+void Transport::connect(const Transport::ConnectionInfo &connectionInfo, ElementListener &elementListener)
{
throw logic_error("unimplemented");
}
diff --git a/ndn-cpp/transport/transport.hpp b/ndn-cpp/transport/transport.hpp
index 6fe4534..b27039a 100644
--- a/ndn-cpp/transport/transport.hpp
+++ b/ndn-cpp/transport/transport.hpp
@@ -10,14 +10,24 @@
namespace ndn {
-class Node;
+class ElementListener;
+
class Transport {
public:
/**
- * Connect to the host specified in node.
- * @param node Not a shared_ptr because we assume that it will remain valid during the life of this Transport object.
+ * A Transport::ConnectionInfo is a base class for connection information used by subclasses of Transport.
*/
- virtual void connect(Node &node);
+ 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);
/**
* Set data to the host
@@ -32,7 +42,7 @@
}
/**
- * Process any data to receive. For each element received, call node.onReceivedElement.
+ * Process any data to receive. For each element received, call elementListener.onReceivedElement.
* This is non-blocking and will silently time out after a brief period if there is no data to receive.
* You should repeatedly call this from an event loop.
* @throw This may throw an exception for reading data or in the callback for processing the data. If you
diff --git a/ndn-cpp/transport/udp-transport.cpp b/ndn-cpp/transport/udp-transport.cpp
index 42551ad..4097b90 100644
--- a/ndn-cpp/transport/udp-transport.cpp
+++ b/ndn-cpp/transport/udp-transport.cpp
@@ -12,21 +12,26 @@
namespace ndn {
-void UdpTransport::connect(Node &node)
+UdpTransport::ConnectionInfo::~ConnectionInfo()
+{
+}
+
+void UdpTransport::connect(const Transport::ConnectionInfo &connectionInfo, ElementListener &elementListener)
{
+ const UdpTransport::ConnectionInfo &udpConnectionInfo = dynamic_cast<const UdpTransport::ConnectionInfo &>(connectionInfo);
+
ndn_Error error;
- if ((error = ndn_UdpTransport_connect(&transport_, (char *)node.getHost(), node.getPort())))
+ if ((error = ndn_UdpTransport_connect(&transport_, (char *)udpConnectionInfo.getHost().c_str(), udpConnectionInfo.getPort())))
throw std::runtime_error(ndn_getErrorString(error));
// TODO: This belongs in the socket listener.
const unsigned int initialLength = 1000;
- // Automatically cast ndn_ to (struct ndn_ElementListener *)
+ // Automatically cast elementReader_ to (struct ndn_ElementListener *)
ndn_BinaryXmlElementReader_init
- (&elementReader_, &node, (unsigned char *)malloc(initialLength), initialLength, ndn_realloc);
+ (&elementReader_, &elementListener, (unsigned char *)malloc(initialLength), initialLength, ndn_realloc);
- // TODO: Properly indicate connected status.
isConnected_ = true;
- node_ = &node;
+ elementListener_ = &elementListener;
}
void UdpTransport::send(const unsigned char *data, unsigned int dataLength)
diff --git a/ndn-cpp/transport/udp-transport.hpp b/ndn-cpp/transport/udp-transport.hpp
index 76c3f36..b720337 100644
--- a/ndn-cpp/transport/udp-transport.hpp
+++ b/ndn-cpp/transport/udp-transport.hpp
@@ -6,6 +6,7 @@
#ifndef NDN_UDPTRANSPORT_HPP
#define NDN_UDPTRANSPORT_HPP
+#include <string>
#include "../c/transport/udp-transport.h"
#include "../c/encoding/binary-xml-element-reader.h"
#include "transport.hpp"
@@ -14,18 +15,53 @@
class UdpTransport : public Transport {
public:
+ /**
+ * A UdpTransport::ConnectionInfo extends Transport::ConnectionInfo to hold the host and port info for the UDP 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.
+ */
+ ConnectionInfo(const char *host, unsigned short port)
+ : 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()
- : node_(0), isConnected_(false)
+ : elementListener_(0), isConnected_(false)
{
ndn_UdpTransport_init(&transport_);
elementReader_.partialData.array = 0;
}
/**
- * Connect to the host specified in node.
- * @param node Not a shared_ptr because we assume that it will remain valid during the life of this Transport object.
+ * 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(Node &node);
+ virtual void connect(const Transport::ConnectionInfo &connectionInfo, ElementListener &elementListener);
/**
* Set data to the host
@@ -35,7 +71,7 @@
virtual void send(const unsigned char *data, unsigned int dataLength);
/**
- * Process any data to receive. For each element received, call node.onReceivedElement.
+ * Process any data to receive. For each element received, call elementListener.onReceivedElement.
* This is non-blocking and will return immediately if there is no data to receive.
* You should normally not call this directly since it is called by Face.processEvents.
* @throw This may throw an exception for reading data or in the callback for processing the data. If you
@@ -55,7 +91,7 @@
private:
struct ndn_UdpTransport transport_;
bool isConnected_;
- Node *node_;
+ ElementListener *elementListener_;
// TODO: This belongs in the socket listener.
ndn_BinaryXmlElementReader elementReader_;
};