Added UdpTransport
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