Added UdpTransport
diff --git a/Makefile.am b/Makefile.am
index eec64ed..854cce1 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -31,7 +31,8 @@
ndn-cpp/c/encoding/BinaryXMLName.c ndn-cpp/c/encoding/BinaryXMLName.h \
ndn-cpp/c/encoding/BinaryXMLPublisherPublicKeyDigest.c ndn-cpp/c/encoding/BinaryXMLPublisherPublicKeyDigest.h \
ndn-cpp/c/encoding/BinaryXMLStructureDecoder.c ndn-cpp/c/encoding/BinaryXMLStructureDecoder.h \
- ndn-cpp/c/transport/TcpTransport.c ndn-cpp/c/transport/TcpTransport.c \
+ ndn-cpp/c/transport/TcpTransport.c ndn-cpp/c/transport/TcpTransport.h \
+ ndn-cpp/c/transport/UdpTransport.c ndn-cpp/c/transport/UdpTransport.h \
ndn-cpp/c/util/DynamicUCharArray.c ndn-cpp/c/util/DynamicUCharArray.h \
ndn-cpp/c/util/ndn_memory.c ndn-cpp/c/util/ndn_memory.h \
ndn-cpp/c/util/ndn_realloc.c ndn-cpp/c/util/ndn_realloc.h
@@ -52,8 +53,9 @@
ndn-cpp/c/encoding/BinaryXMLStructureDecoder.h ndn-cpp/encoding/BinaryXMLStructureDecoder.hpp \
ndn-cpp/encoding/BinaryXMLWireFormat.cpp ndn-cpp/c/encoding/BinaryXMLName.h ndn-cpp/encoding/BinaryXMLWireFormat.hpp \
ndn-cpp/encoding/WireFormat.cpp ndn-cpp/encoding/WireFormat.hpp \
- ndn-cpp/transport/TcpTransport.cpp ndn-cpp/transport/TcpTransport.hpp \
- ndn-cpp/transport/Transport.cpp ndn-cpp/transport/Transport.hpp
+ ndn-cpp/c/transport/TcpTransport.h ndn-cpp/transport/TcpTransport.cpp ndn-cpp/transport/TcpTransport.hpp \
+ ndn-cpp/transport/Transport.cpp ndn-cpp/transport/Transport.hpp \
+ ndn-cpp/c/transport/UdpTransport.h ndn-cpp/transport/UdpTransport.cpp ndn-cpp/transport/UdpTransport.hpp
bin_test_encode_decode_Interest_SOURCES = tests/test-encode-decode-Interest.cpp
bin_test_encode_decode_Interest_LDADD = libndn-c.a libndn-cpp.a
diff --git a/Makefile.in b/Makefile.in
index 1fe0fe3..fad2be9 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -147,7 +147,7 @@
ndn-cpp/c/encoding/BinaryXMLPublisherPublicKeyDigest.$(OBJEXT) \
ndn-cpp/c/encoding/BinaryXMLStructureDecoder.$(OBJEXT) \
ndn-cpp/c/transport/TcpTransport.$(OBJEXT) \
- ndn-cpp/c/transport/TcpTransport.$(OBJEXT) \
+ ndn-cpp/c/transport/UdpTransport.$(OBJEXT) \
ndn-cpp/c/util/DynamicUCharArray.$(OBJEXT) \
ndn-cpp/c/util/ndn_memory.$(OBJEXT) \
ndn-cpp/c/util/ndn_realloc.$(OBJEXT)
@@ -161,7 +161,8 @@
ndn-cpp/encoding/BinaryXMLWireFormat.$(OBJEXT) \
ndn-cpp/encoding/WireFormat.$(OBJEXT) \
ndn-cpp/transport/TcpTransport.$(OBJEXT) \
- ndn-cpp/transport/Transport.$(OBJEXT)
+ ndn-cpp/transport/Transport.$(OBJEXT) \
+ ndn-cpp/transport/UdpTransport.$(OBJEXT)
libndn_cpp_a_OBJECTS = $(am_libndn_cpp_a_OBJECTS)
PROGRAMS = $(bin_PROGRAMS)
am_bin_test_encode_decode_ContentObject_OBJECTS = \
@@ -433,7 +434,8 @@
ndn-cpp/c/encoding/BinaryXMLName.c ndn-cpp/c/encoding/BinaryXMLName.h \
ndn-cpp/c/encoding/BinaryXMLPublisherPublicKeyDigest.c ndn-cpp/c/encoding/BinaryXMLPublisherPublicKeyDigest.h \
ndn-cpp/c/encoding/BinaryXMLStructureDecoder.c ndn-cpp/c/encoding/BinaryXMLStructureDecoder.h \
- ndn-cpp/c/transport/TcpTransport.c ndn-cpp/c/transport/TcpTransport.c \
+ ndn-cpp/c/transport/TcpTransport.c ndn-cpp/c/transport/TcpTransport.h \
+ ndn-cpp/c/transport/UdpTransport.c ndn-cpp/c/transport/UdpTransport.h \
ndn-cpp/c/util/DynamicUCharArray.c ndn-cpp/c/util/DynamicUCharArray.h \
ndn-cpp/c/util/ndn_memory.c ndn-cpp/c/util/ndn_memory.h \
ndn-cpp/c/util/ndn_realloc.c ndn-cpp/c/util/ndn_realloc.h
@@ -454,8 +456,9 @@
ndn-cpp/c/encoding/BinaryXMLStructureDecoder.h ndn-cpp/encoding/BinaryXMLStructureDecoder.hpp \
ndn-cpp/encoding/BinaryXMLWireFormat.cpp ndn-cpp/c/encoding/BinaryXMLName.h ndn-cpp/encoding/BinaryXMLWireFormat.hpp \
ndn-cpp/encoding/WireFormat.cpp ndn-cpp/encoding/WireFormat.hpp \
- ndn-cpp/transport/TcpTransport.cpp ndn-cpp/transport/TcpTransport.hpp \
- ndn-cpp/transport/Transport.cpp ndn-cpp/transport/Transport.hpp
+ ndn-cpp/c/transport/TcpTransport.h ndn-cpp/transport/TcpTransport.cpp ndn-cpp/transport/TcpTransport.hpp \
+ ndn-cpp/transport/Transport.cpp ndn-cpp/transport/Transport.hpp \
+ ndn-cpp/c/transport/UdpTransport.h ndn-cpp/transport/UdpTransport.cpp ndn-cpp/transport/UdpTransport.hpp
bin_test_encode_decode_Interest_SOURCES = tests/test-encode-decode-Interest.cpp
bin_test_encode_decode_Interest_LDADD = libndn-c.a libndn-cpp.a
@@ -600,6 +603,9 @@
ndn-cpp/c/transport/TcpTransport.$(OBJEXT): \
ndn-cpp/c/transport/$(am__dirstamp) \
ndn-cpp/c/transport/$(DEPDIR)/$(am__dirstamp)
+ndn-cpp/c/transport/UdpTransport.$(OBJEXT): \
+ ndn-cpp/c/transport/$(am__dirstamp) \
+ ndn-cpp/c/transport/$(DEPDIR)/$(am__dirstamp)
ndn-cpp/c/util/$(am__dirstamp):
@$(MKDIR_P) ndn-cpp/c/util
@: > ndn-cpp/c/util/$(am__dirstamp)
@@ -661,6 +667,9 @@
ndn-cpp/transport/Transport.$(OBJEXT): \
ndn-cpp/transport/$(am__dirstamp) \
ndn-cpp/transport/$(DEPDIR)/$(am__dirstamp)
+ndn-cpp/transport/UdpTransport.$(OBJEXT): \
+ ndn-cpp/transport/$(am__dirstamp) \
+ ndn-cpp/transport/$(DEPDIR)/$(am__dirstamp)
libndn-cpp.a: $(libndn_cpp_a_OBJECTS) $(libndn_cpp_a_DEPENDENCIES) $(EXTRA_libndn_cpp_a_DEPENDENCIES)
$(AM_V_at)-rm -f libndn-cpp.a
@@ -766,6 +775,7 @@
@AMDEP_TRUE@@am__include@ @am__quote@ndn-cpp/c/encoding/$(DEPDIR)/BinaryXMLPublisherPublicKeyDigest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@ndn-cpp/c/encoding/$(DEPDIR)/BinaryXMLStructureDecoder.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@ndn-cpp/c/transport/$(DEPDIR)/TcpTransport.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@ndn-cpp/c/transport/$(DEPDIR)/UdpTransport.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@ndn-cpp/c/util/$(DEPDIR)/DynamicUCharArray.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@ndn-cpp/c/util/$(DEPDIR)/ndn_memory.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@ndn-cpp/c/util/$(DEPDIR)/ndn_realloc.Po@am__quote@
@@ -774,6 +784,7 @@
@AMDEP_TRUE@@am__include@ @am__quote@ndn-cpp/encoding/$(DEPDIR)/WireFormat.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@ndn-cpp/transport/$(DEPDIR)/TcpTransport.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@ndn-cpp/transport/$(DEPDIR)/Transport.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@ndn-cpp/transport/$(DEPDIR)/UdpTransport.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@tests/$(DEPDIR)/test-encode-decode-ContentObject.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@tests/$(DEPDIR)/test-encode-decode-Interest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@tests/$(DEPDIR)/test-get-async.Po@am__quote@
diff --git a/ndn-cpp/c/errors.c b/ndn-cpp/c/errors.c
index 89f30a1..af32008 100644
--- a/ndn-cpp/c/errors.c
+++ b/ndn-cpp/c/errors.c
@@ -62,6 +62,16 @@
return "TcpTransport error in send";
case NDN_ERROR_TcpTransport_error_in_recv:
return "TcpTransport error in recv";
+ case NDN_ERROR_UdpTransport_error_in_getaddrinfo:
+ return "UdpTransport error in getaddrinfo";
+ case NDN_ERROR_UdpTransport_cannot_connect_to_socket:
+ return "UdpTransport cannot connect to socket";
+ case NDN_ERROR_UdpTransport_socket_is_not_open:
+ return "UdpTransport socket is not open";
+ case NDN_ERROR_UdpTransport_error_in_send:
+ return "UdpTransport error in send";
+ case NDN_ERROR_UdpTransport_error_in_recv:
+ return "UdpTransport error in recv";
default:
return "unrecognized ndn_Error code";
}
diff --git a/ndn-cpp/c/errors.h b/ndn-cpp/c/errors.h
index 56f509c..f574b6a 100644
--- a/ndn-cpp/c/errors.h
+++ b/ndn-cpp/c/errors.h
@@ -38,7 +38,12 @@
NDN_ERROR_TcpTransport_cannot_connect_to_socket,
NDN_ERROR_TcpTransport_socket_is_not_open,
NDN_ERROR_TcpTransport_error_in_send,
- NDN_ERROR_TcpTransport_error_in_recv
+ NDN_ERROR_TcpTransport_error_in_recv,
+ NDN_ERROR_UdpTransport_error_in_getaddrinfo,
+ NDN_ERROR_UdpTransport_cannot_connect_to_socket,
+ NDN_ERROR_UdpTransport_socket_is_not_open,
+ NDN_ERROR_UdpTransport_error_in_send,
+ NDN_ERROR_UdpTransport_error_in_recv
} ndn_Error;
/**
diff --git a/ndn-cpp/c/transport/UdpTransport.h b/ndn-cpp/c/transport/UdpTransport.h
new file mode 100644
index 0000000..924e4a7
--- /dev/null
+++ b/ndn-cpp/c/transport/UdpTransport.h
@@ -0,0 +1,35 @@
+/**
+ * @author: Jeff Thompson
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NDN_UDPTRANSPORT_H
+#define NDN_UDPTRANSPORT_H
+
+#include "../errors.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct ndn_UdpTransport {
+ int socketDescriptor; /**< -1 if not connected */
+};
+
+static inline void ndn_UdpTransport_init(struct ndn_UdpTransport *self)
+{
+ self->socketDescriptor = -1;
+}
+
+ndn_Error ndn_UdpTransport_connect(struct ndn_UdpTransport *self, char *host, unsigned short port);
+
+ndn_Error ndn_UdpTransport_send(struct ndn_UdpTransport *self, unsigned char *data, unsigned int dataLength);
+
+ndn_Error ndn_UdpTransport_receive
+ (struct ndn_UdpTransport *self, unsigned char *buffer, unsigned int bufferLength, unsigned int *nBytes);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/ndn-cpp/transport/UdpTransport.cpp b/ndn-cpp/transport/UdpTransport.cpp
new file mode 100644
index 0000000..ae0124f
--- /dev/null
+++ b/ndn-cpp/transport/UdpTransport.cpp
@@ -0,0 +1,54 @@
+/**
+ * @author: Jeff Thompson
+ * See COPYING for copyright and distribution information.
+ */
+
+#include <stdexcept>
+#include "../NDN.hpp"
+#include "../c/util/ndn_realloc.h"
+#include "UdpTransport.hpp"
+
+using namespace std;
+
+namespace ndn {
+
+void UdpTransport::connect(NDN &ndn)
+{
+ ndn_Error error;
+ if (error = ndn_UdpTransport_connect(&transport_, (char *)ndn.getHost(), ndn.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 *)
+ ndn_BinaryXMLElementReader_init
+ (&elementReader_, &ndn, (unsigned char *)malloc(initialLength), initialLength, ndn_realloc);
+
+ // TODO: Properly indicate connected status.
+ ndn_ = &ndn;
+}
+
+void UdpTransport::send(unsigned char *data, unsigned int dataLength)
+{
+ ndn_Error error;
+ if (error = ndn_UdpTransport_send(&transport_, data, dataLength))
+ throw std::runtime_error(ndn_getErrorString(error));
+}
+
+void UdpTransport::tempReceive()
+{
+ try {
+ ndn_Error error;
+ unsigned char buffer[8000];
+ unsigned int nBytes;
+ if (error = ndn_UdpTransport_receive(&transport_, buffer, sizeof(buffer), &nBytes))
+ throw std::runtime_error(ndn_getErrorString(error));
+
+ 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/UdpTransport.hpp b/ndn-cpp/transport/UdpTransport.hpp
new file mode 100644
index 0000000..a24a4dc
--- /dev/null
+++ b/ndn-cpp/transport/UdpTransport.hpp
@@ -0,0 +1,42 @@
+/**
+ * @author: Jeff Thompson
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NDN_TCPTRANSPORT_HPP
+#define NDN_TCPTRANSPORT_HPP
+
+#include "../c/transport/UdpTransport.h"
+#include "../c/encoding/BinaryXMLElementReader.h"
+#include "Transport.hpp"
+
+namespace ndn {
+
+class UdpTransport : public Transport {
+public:
+ UdpTransport()
+ {
+ ndn_UdpTransport_init(&transport_);
+ ndn_ = 0;
+ }
+
+ /**
+ *
+ * @param ndn Not a shared_ptr because we assume that it will remain valid during the life of this Transport object.
+ */
+ virtual void connect(NDN &ndn);
+
+ virtual void send(unsigned char *data, unsigned int dataLength);
+
+ virtual void tempReceive();
+
+private:
+ struct ndn_UdpTransport transport_;
+ NDN *ndn_;
+ // TODO: This belongs in the socket listener.
+ ndn_BinaryXMLElementReader elementReader_;
+};
+
+}
+
+#endif
diff --git a/tests/test-get-async.cpp b/tests/test-get-async.cpp
index b2a797a..f432c25 100644
--- a/tests/test-get-async.cpp
+++ b/tests/test-get-async.cpp
@@ -8,7 +8,7 @@
#include <iostream>
#include <ndn-cpp/Interest.hpp>
#include <ndn-cpp/ContentObject.hpp>
-#include <ndn-cpp/transport/TcpTransport.hpp>
+#include <ndn-cpp/transport/UdpTransport.hpp>
#include <ndn-cpp/NDN.hpp>
using namespace std;
@@ -43,7 +43,7 @@
int main(int argc, char** argv)
{
try {
- shared_ptr<TcpTransport> transport(new TcpTransport());
+ shared_ptr<UdpTransport> transport(new UdpTransport());
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"), closure, 0);