Move the elementReader_ to a member of TcpTransport
diff --git a/ndn-cpp/transport/TcpTransport.cpp b/ndn-cpp/transport/TcpTransport.cpp
index 1db18bb..fe81b42 100644
--- a/ndn-cpp/transport/TcpTransport.cpp
+++ b/ndn-cpp/transport/TcpTransport.cpp
@@ -5,7 +5,6 @@
#include <stdexcept>
#include "../NDN.hpp"
-#include "../c/encoding/BinaryXMLElementReader.h"
#include "TcpTransport.hpp"
using namespace std;
@@ -17,6 +16,10 @@
ndn_Error error;
if (error = ndn_TcpTransport_connect(&transport_, (char *)ndn.getHost(), ndn.getPort()))
throw std::runtime_error(ndn_getErrorString(error));
+
+ // TODO: This belongs in the socket listener.
+ // Automatically cast ndn_ to (struct ndn_ElementListener *)
+ ndn_BinaryXMLElementReader_init(&elementReader_, &ndn);
// TODO: Properly indicate connected status.
ndn_ = &ndn;
@@ -31,22 +34,14 @@
void TcpTransport::tempReceive()
{
- if (!ndn_)
- // TODO: Properly check if connected.
- return;
-
- try {
- ndn_BinaryXMLElementReader elementReader;
- // Automatically cast ndn_ to (struct ndn_ElementListener *)
- ndn_BinaryXMLElementReader_init(&elementReader, ndn_);
-
+ try {
ndn_Error error;
unsigned char buffer[8000];
unsigned int nBytes;
if (error = ndn_TcpTransport_receive(&transport_, buffer, sizeof(buffer), &nBytes))
throw std::runtime_error(ndn_getErrorString(error));
- ndn_BinaryXMLElementReader_onReceivedData(&elementReader, buffer, nBytes);
+ ndn_BinaryXMLElementReader_onReceivedData(&elementReader_, buffer, nBytes);
} catch (...) {
// This function is called by the socket callback, so don't send an exception back to it.
// TODO: Log the exception?
diff --git a/ndn-cpp/transport/TcpTransport.hpp b/ndn-cpp/transport/TcpTransport.hpp
index c4fa1c6..8be6a73 100644
--- a/ndn-cpp/transport/TcpTransport.hpp
+++ b/ndn-cpp/transport/TcpTransport.hpp
@@ -7,6 +7,7 @@
#define NDN_TCPTRANSPORT_HPP
#include "../c/transport/TcpTransport.h"
+#include "../c/encoding/BinaryXMLElementReader.h"
#include "Transport.hpp"
namespace ndn {
@@ -28,6 +29,8 @@
private:
struct ndn_TcpTransport transport_;
NDN *ndn_;
+ // TODO: This belongs in the socket listener.
+ ndn_BinaryXMLElementReader elementReader_;
};
}
diff --git a/tests/test-get-async.cpp b/tests/test-get-async.cpp
index fd2c1cc..b2a797a 100644
--- a/tests/test-get-async.cpp
+++ b/tests/test-get-async.cpp
@@ -17,9 +17,15 @@
class MyClosure : public Closure {
public:
+ MyClosure()
+ : gotContent_(false)
+ {
+ }
+
virtual UpcallResult upcall(UpcallKind kind, UpcallInfo &upcallInfo)
{
if (kind == UPCALL_CONTENT || kind == UPCALL_CONTENT_UNVERIFIED) {
+ gotContent_ = true;
cout << "Got content with name " << upcallInfo.getContentObject()->getName().to_uri() << endl;
for (unsigned int i = 0; i < upcallInfo.getContentObject()->getContent().size(); ++i)
cout << upcallInfo.getContentObject()->getContent()[i];
@@ -30,16 +36,21 @@
else
return CLOSURE_RESULT_OK;
}
+
+ bool gotContent_;
};
int main(int argc, char** argv)
{
try {
shared_ptr<TcpTransport> transport(new TcpTransport());
+ shared_ptr<MyClosure> closure(new MyClosure());
NDN ndn(transport, "E.hub.ndn.ucla.edu", 9695);
- ndn.expressInterest(Name("/ndn/ucla.edu/apps/ndn-js-test/hello.txt/level2/%FD%05%0B%16%7D%95%0E"), make_shared<MyClosure>(), 0);
+ ndn.expressInterest(Name("/ndn/ucla.edu/apps/ndn-js-test/hello.txt/level2/%FD%05%0B%16%7D%95%0E"), closure, 0);
- transport->tempReceive();
+ // Pump the receive process. This should really be done by a socket listener.
+ while (!closure->gotContent_)
+ transport->tempReceive();
} catch (std::exception &e) {
cout << "exception: " << e.what() << endl;
}