Implement TcpTransport
diff --git a/ndn-cpp/c/network/TcpTransport.c b/ndn-cpp/c/network/TcpTransport.c
index 30e6828..dad446c 100644
--- a/ndn-cpp/c/network/TcpTransport.c
+++ b/ndn-cpp/c/network/TcpTransport.c
@@ -26,26 +26,28 @@
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
-ndn_Error ndn_TcpTransport_connect(ndn_TcpTransport *self, char *host, int port)
+ndn_Error ndn_TcpTransport_connect(struct ndn_TcpTransport *self, char *host, unsigned short port)
{
+ if (self->socketDescriptor >= 0) {
+ close(self->socketDescriptor);
+ self->socketDescriptor = -1;
+ }
-}
-
-int testTcpTransport(unsigned char *data, unsigned int dataLength)
-{
- struct addrinfo hints, *serverInfo;
-
- printf("starting\n");
-
+ struct addrinfo hints;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
- if (getaddrinfo("E.hub.ndn.ucla.edu", "9695", &hints, &serverInfo) != 0)
- return 1;
+ char portString[10];
+ sprintf(portString, "%d", port);
+
+ struct addrinfo *serverInfo;
+ if (getaddrinfo(host, portString, &hints, &serverInfo) != 0)
+ return NDN_ERROR_TcpTransport_error_in_getaddrinfo;
// loop through all the results and connect to the first we can
struct addrinfo *p;
+ int socketDescriptor;
for(p = serverInfo; p != NULL; p = p->ai_next) {
if ((socketDescriptor = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
continue;
@@ -58,32 +60,47 @@
break;
}
- if (p == NULL)
- return 2;
+ if (p == NULL) {
+ freeaddrinfo(serverInfo);
+ return NDN_ERROR_TcpTransport_cannot_connect_to_socket;
+ }
- freeaddrinfo(serverInfo); // all done with this structure
+ freeaddrinfo(serverInfo);
+ self->socketDescriptor = socketDescriptor;
+ return 0;
+}
+
+ndn_Error ndn_TcpTransport_send(struct ndn_TcpTransport *self, unsigned char *data, unsigned int dataLength)
+{
+ if (self->socketDescriptor < 0)
+ return NDN_ERROR_TcpTransport_socket_is_not_open;
+
int nBytes;
while (1) {
- if ((nBytes = send(socketDescriptor, data, dataLength, 0)) < 0)
- return 1;
+ if ((nBytes = send(self->socketDescriptor, data, dataLength, 0)) < 0)
+ return NDN_ERROR_TcpTransport_error_in_send;
if (nBytes >= dataLength)
break;
+ // Send more.
dataLength -= nBytes;
}
+
+ return 0;
+}
+
+ndn_Error ndn_TcpTransport_receive
+(struct ndn_TcpTransport *self, unsigned char *buffer, unsigned int bufferLength, unsigned int *nBytesOut)
+{
+ if (self->socketDescriptor < 0)
+ return NDN_ERROR_TcpTransport_socket_is_not_open;
+
+ int nBytes;
+ if ((nBytes = recv(self->socketDescriptor, buffer, bufferLength, 0)) == -1)
+ return NDN_ERROR_TcpTransport_error_in_recv;
+
+ *nBytesOut = (unsigned int)nBytes;
- unsigned char buffer[1000];
- if ((nBytes = recv(socketDescriptor, buffer, sizeof(buffer) - 1, 0)) == -1)
- return 1;
-
- printf("received %d bytes\n", nBytes);
- int i;
- for (i = 0; i < nBytes; ++i)
- printf("%02X ", (unsigned int)buffer[i]);
- printf("\n");
-
- close(socketDescriptor);
-
- return 0;
+ return 0;
}
diff --git a/ndn-cpp/c/network/TcpTransport.h b/ndn-cpp/c/network/TcpTransport.h
index 82ef03b..19ed4e2 100644
--- a/ndn-cpp/c/network/TcpTransport.h
+++ b/ndn-cpp/c/network/TcpTransport.h
@@ -8,6 +8,8 @@
#ifndef NDN_TCPTRANSPORT_H
#define NDN_TCPTRANSPORT_H
+#include "../errors.h"
+
#ifdef __cplusplus
extern "C" {
#endif
@@ -16,11 +18,18 @@
int socketDescriptor; /**< -1 if not connected */
};
-static inline void ndn_TcpTransport_init(ndn_TcpTransport *self)
+static inline void ndn_TcpTransport_init(struct ndn_TcpTransport *self)
{
self->socketDescriptor = -1;
}
+ndn_Error ndn_TcpTransport_connect(struct ndn_TcpTransport *self, char *host, unsigned short port);
+
+ndn_Error ndn_TcpTransport_send(struct ndn_TcpTransport *self, unsigned char *data, unsigned int dataLength);
+
+ndn_Error ndn_TcpTransport_receive
+ (struct ndn_TcpTransport *self, unsigned char *buffer, unsigned int bufferLength, unsigned int *nBytes);
+
int testTcpTransport(unsigned char *data, unsigned int dataLength);
#ifdef __cplusplus
diff --git a/tests/test-encode-decode-Interest.cpp b/tests/test-encode-decode-Interest.cpp
index 446a9a4..bba7596 100644
--- a/tests/test-encode-decode-Interest.cpp
+++ b/tests/test-encode-decode-Interest.cpp
@@ -7,6 +7,7 @@
#include <sstream>
#include <iostream>
#include <ndn-cpp/Interest.hpp>
+#include <ndn-cpp/c/network/TcpTransport.h>
using namespace std;
using namespace ndn;
@@ -37,17 +38,36 @@
{
try {
Interest interest;
+#if 0
interest.decode(Interest1, sizeof(Interest1));
cout << "Interest name " << interest.getName().to_uri() << endl;
cout << "Interest minSuffixComponents " << interest.getMinSuffixComponents() << endl;
cout << "Interest publisherPublicKeyDigest length " << interest.getPublisherPublicKeyDigest().getPublisherPublicKeyDigest().size() << endl;
cout << "Interest excludeEntryCount " << interest.getExclude().getEntryCount() << endl;
cout << "InterestLifetimeMilliseconds " << interest.getInterestLifetimeMilliseconds() << endl;
+#endif
vector<unsigned char> encoding;
interest.encode(encoding);
cout << "Interest encoding length " << encoding.size() << " vs. sizeof(Interest1) " << sizeof(Interest1) << endl;
+
+ struct ndn_TcpTransport transport;
+ ndn_TcpTransport_init(&transport);
+ ndn_Error error;
+ if (error = ndn_TcpTransport_connect(&transport, (char *)"E.hub.ndn.ucla.edu", 9695))
+ return error;
+ if (error = ndn_TcpTransport_send(&transport, &encoding[0], encoding.size()))
+ return error;
+ unsigned char buffer[1000];
+ unsigned int nBytes;
+ if (error = ndn_TcpTransport_receive(&transport, buffer, sizeof(buffer), &nBytes))
+ return error;
+
+ for (int i = 0; i < nBytes; ++i)
+ printf("%02X ", (unsigned int)buffer[i]);
+
+#if 0
Interest reDecodedInterest;
reDecodedInterest.decode(encoding);
cout << "Re-decoded Interest name " << reDecodedInterest.getName().to_uri() << endl;
@@ -55,6 +75,7 @@
cout << "Re-decoded Interest publisherPublicKeyDigest length " << reDecodedInterest.getPublisherPublicKeyDigest().getPublisherPublicKeyDigest().size() << endl;
cout << "Re-decoded Interest excludeEntryCount " << reDecodedInterest.getExclude().getEntryCount() << endl;
cout << "Re-decoded InterestLifetimeMilliseconds " << reDecodedInterest.getInterestLifetimeMilliseconds() << endl;
+#endif
} catch (exception &e) {
cout << "exception: " << e.what() << endl;
}