Pass the NDN object to connect. Added TcpTransport::tempReceive()
diff --git a/ndn-cpp/NDN.cpp b/ndn-cpp/NDN.cpp
index c41f1d7..5a3d829 100644
--- a/ndn-cpp/NDN.cpp
+++ b/ndn-cpp/NDN.cpp
@@ -18,7 +18,7 @@
vector<unsigned char> encoding;
interest.encode(encoding);
- transport_->connect(host_.c_str(), port_);
+ transport_->connect(*this);
transport_->send(&encoding[0], encoding.size());
}
diff --git a/ndn-cpp/NDN.hpp b/ndn-cpp/NDN.hpp
index c574b8d..4b8e961 100644
--- a/ndn-cpp/NDN.hpp
+++ b/ndn-cpp/NDN.hpp
@@ -33,7 +33,9 @@
*/
void expressInterest(const Name &name, const ptr_lib::shared_ptr<Closure> &closure, const Interest *interestTemplate);
- Transport &tempGetTransport() { return *transport_; }
+ const char *getHost() const { return host_.c_str(); }
+
+ unsigned short getPort() const { return port_; }
virtual void onReceivedElement(unsigned char *element, unsigned int elementLength);
diff --git a/ndn-cpp/transport/TcpTransport.cpp b/ndn-cpp/transport/TcpTransport.cpp
index c3abab6..5da7baa 100644
--- a/ndn-cpp/transport/TcpTransport.cpp
+++ b/ndn-cpp/transport/TcpTransport.cpp
@@ -4,17 +4,22 @@
*/
#include <stdexcept>
+#include "../NDN.hpp"
+#include "../c/encoding/BinaryXMLElementReader.h"
#include "TcpTransport.hpp"
using namespace std;
namespace ndn {
-void TcpTransport::connect(const char *host, unsigned short port)
+void TcpTransport::connect(NDN &ndn)
{
ndn_Error error;
- if (error = ndn_TcpTransport_connect(&transport_, (char *)host, port))
- throw std::runtime_error(ndn_getErrorString(error));
+ if (error = ndn_TcpTransport_connect(&transport_, (char *)ndn.getHost(), ndn.getPort()))
+ throw std::runtime_error(ndn_getErrorString(error));
+
+ // TODO: Properly indicate connected status.
+ ndn_ = &ndn;
}
void TcpTransport::send(unsigned char *data, unsigned int dataLength)
@@ -24,14 +29,21 @@
throw std::runtime_error(ndn_getErrorString(error));
}
-unsigned int TcpTransport::receive(unsigned char *buffer, unsigned int bufferLength)
+void TcpTransport::tempReceive()
{
+ if (!ndn_)
+ // TODO: Properly check if connected.
+ return;
+ ndn_BinaryXMLElementReader elementReader;
+ // Automaticall cast ndn_ to (struct ndn_ElementListener *)
+ ndn_BinaryXMLElementReader_init(&elementReader, ndn_);
+
+ unsigned char buffer[8000];
ndn_Error error;
unsigned int nBytes;
- if (error = ndn_TcpTransport_receive(&transport_, buffer, bufferLength, &nBytes))
+ if (error = ndn_TcpTransport_receive(&transport_, buffer, sizeof(buffer), &nBytes))
throw std::runtime_error(ndn_getErrorString(error));
-
- return nBytes;
+ ndn_BinaryXMLElementReader_onReceivedData(&elementReader, buffer, nBytes);
}
}
diff --git a/ndn-cpp/transport/TcpTransport.hpp b/ndn-cpp/transport/TcpTransport.hpp
index a5d0ca8..c4fa1c6 100644
--- a/ndn-cpp/transport/TcpTransport.hpp
+++ b/ndn-cpp/transport/TcpTransport.hpp
@@ -16,16 +16,18 @@
TcpTransport()
{
ndn_TcpTransport_init(&transport_);
+ ndn_ = 0;
}
- virtual void connect(const char *host, unsigned short port);
+ virtual void connect(NDN &ndn);
virtual void send(unsigned char *data, unsigned int dataLength);
- virtual unsigned int receive(unsigned char *buffer, unsigned int bufferLength);
+ virtual void tempReceive();
private:
struct ndn_TcpTransport transport_;
+ NDN *ndn_;
};
}
diff --git a/ndn-cpp/transport/Transport.cpp b/ndn-cpp/transport/Transport.cpp
index 84a4e02..4d6024c 100644
--- a/ndn-cpp/transport/Transport.cpp
+++ b/ndn-cpp/transport/Transport.cpp
@@ -10,7 +10,7 @@
namespace ndn {
-void Transport::connect(const char *host, unsigned short port)
+void Transport::connect(NDN &ndn)
{
throw logic_error("unimplemented");
}
@@ -20,9 +20,4 @@
throw logic_error("unimplemented");
}
-unsigned int Transport::receive(unsigned char *buffer, unsigned int bufferLength)
-{
- throw logic_error("unimplemented");
-}
-
}
diff --git a/ndn-cpp/transport/Transport.hpp b/ndn-cpp/transport/Transport.hpp
index 655ce6e..94e60ec 100644
--- a/ndn-cpp/transport/Transport.hpp
+++ b/ndn-cpp/transport/Transport.hpp
@@ -7,14 +7,13 @@
#define NDN_TRANSPORT_HPP
namespace ndn {
-
+
+class NDN;
class Transport {
public:
- virtual void connect(const char *host, unsigned short port);
+ virtual void connect(NDN &ndn);
virtual void send(unsigned char *data, unsigned int dataLength);
-
- virtual unsigned int receive(unsigned char *buffer, unsigned int bufferLength);
};
}