transport: Implementing TcpTransport

Refs #1156 (http://redmine.named-data.net/issues/1156)

Change-Id: I03322acabc558e7ef220b9da07ce72095e9d8039
diff --git a/.travis.yml b/.travis.yml
index a36b2ca..128be0b 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -2,8 +2,10 @@
 compiler:
   - gcc
 before_install:
+  - travis_retry sudo add-apt-repository -y ppa:named-data/ppa
   - travis_retry sudo apt-get update -qq
   - travis_retry sudo apt-get install -qq autotools-dev
+  - travis_retry sudo apt-get install -qq ndnx-dev
   - travis_retry sudo apt-get install -qq libboost-all-dev
   - travis_retry sudo apt-get install -qq libcrypto++-dev
   - travis_retry sudo apt-get install -qq libsqlite3-dev
@@ -13,4 +15,17 @@
   - make
   - sudo make install
   - sudo ldconfig
+  - #
+  - # Some tests now require daemon to run (and daemon needs the library to be compiled)
+  - travis_retry git clone git://github.com/cawka/ndnd-tlv ndnd-tlv
+  - cd ndnd-tlv
+  - ./waf configure
+  - ./waf -j1
+  - sudo ./waf install
+  - cd ..
+  - #
+  - #
+  - ndnd-tlv-start
+  - sleep 1
+  - #
   - ./tests_boost/unit-tests -l all
diff --git a/Makefile.am b/Makefile.am
index f762073..73d2a9b 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -111,6 +111,7 @@
   src/security/verifier.cpp \
   src/security/sec-policy-no-verify.cpp \
   src/security/sec-policy-self-verify.cpp \
+  src/transport/tcp-transport.cpp \
   src/transport/unix-transport.cpp \
   src/util/blob-stream.hpp \
   src/util/blob.cpp \
diff --git a/include/ndn-cpp-dev/face.hpp b/include/ndn-cpp-dev/face.hpp
index ec4fe27..61d872f 100644
--- a/include/ndn-cpp-dev/face.hpp
+++ b/include/ndn-cpp-dev/face.hpp
@@ -11,6 +11,7 @@
 #include "node.hpp"
 #include "transport/transport.hpp"
 #include "transport/unix-transport.hpp"
+#include "transport/tcp-transport.hpp"
 
 namespace ndn {
 
@@ -58,13 +59,13 @@
   /**
    * Create a new Face for communication with an NDN hub at host:port using the default TcpTransport.
    * @param host The host of the NDN hub.
-   * @param port The port of the NDN hub. If omitted. use 6363.
+   * @param port The port or service name of the NDN hub. If omitted. use 6363.
    */
-  // Face(const char *host, unsigned short port = 6363)
-  // : node_(ptr_lib::shared_ptr<TcpTransport>(new TcpTransport(host, port)))
-  // {
-  // }
-    
+  Face(const std::string &host, const std::string &port = "6363")
+  : node_(ptr_lib::shared_ptr<TcpTransport>(new TcpTransport(host, port)))
+  {
+  }
+  
   /**
    * Send the Interest through the transport, read the entire response and call onData(interest, data).
    * @param interest A reference to the Interest.  This copies the Interest.
diff --git a/include/ndn-cpp-dev/transport/tcp-transport.hpp b/include/ndn-cpp-dev/transport/tcp-transport.hpp
new file mode 100644
index 0000000..00f3fef
--- /dev/null
+++ b/include/ndn-cpp-dev/transport/tcp-transport.hpp
@@ -0,0 +1,43 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/**
+ * Copyright (C) 2013 Regents of the University of California.
+ * @author: Jeff Thompson <jefft0@remap.ucla.edu>
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NDN_TCP_TRANSPORT_HPP
+#define NDN_TCP_TRANSPORT_HPP
+
+#include <string>
+#include "transport.hpp"
+
+namespace ndn {
+  
+class TcpTransport : public Transport
+{
+public:
+  TcpTransport(const std::string& host, const std::string& port = "6363");
+  ~TcpTransport();
+
+  // from Transport
+  virtual void 
+  connect(boost::asio::io_service &ioService,
+          const ReceiveCallback &receiveCallback);
+  
+  virtual void 
+  close();
+
+  virtual void 
+  send(const Block &wire);
+  
+private:
+  std::string host_;
+  std::string port_;
+
+  class Impl;
+  ptr_lib::shared_ptr<Impl> impl_;
+};
+
+}
+
+#endif // NDN_TCP_TRANSPORT_HPP
diff --git a/src/transport/tcp-transport.cpp b/src/transport/tcp-transport.cpp
new file mode 100644
index 0000000..f75dcd2
--- /dev/null
+++ b/src/transport/tcp-transport.cpp
@@ -0,0 +1,303 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/**
+ * Copyright (C) 2013 Regents of the University of California.
+ * @author: Jeff Thompson <jefft0@remap.ucla.edu>
+ * See COPYING for copyright and distribution information.
+ */
+
+#include <stdexcept>
+#include <stdlib.h>
+
+#include <ndn-cpp-dev/face.hpp>
+#include <ndn-cpp-dev/transport/tcp-transport.hpp>
+#include "../c/util/ndn_memory.h"
+
+#include <boost/asio.hpp>
+#if NDN_CPP_HAVE_CXX11
+// In the std library, the placeholders are in a different namespace than boost.
+using namespace ndn::func_lib::placeholders;
+#endif
+
+using namespace std;
+typedef boost::asio::ip::tcp protocol;
+
+namespace ndn {
+
+const size_t MAX_LENGTH = 9000;
+
+class TcpTransport::Impl
+{
+public:
+  Impl(TcpTransport &transport)
+    : transport_(transport)
+    , socket_(*transport_.ioService_)
+    , partialDataSize_(0)
+    , connectionInProgress_(false)
+    , connectTimer_(*transport_.ioService_)
+  {
+  }
+
+  void
+  connectHandler(const boost::system::error_code& error)
+  {
+    connectionInProgress_ = false;
+    connectTimer_.cancel();
+
+    if (!error)
+      {
+        partialDataSize_ = 0;
+        socket_.async_receive(boost::asio::buffer(inputBuffer_, MAX_LENGTH), 0,
+                              func_lib::bind(&Impl::handle_async_receive, this, _1, _2));
+
+        transport_.isConnected_ = true;
+
+        for (std::list<Block>::iterator i = sendQueue_.begin(); i != sendQueue_.end(); ++i)
+          socket_.async_send(boost::asio::buffer(i->wire(), i->size()),
+                             func_lib::bind(&Impl::handle_async_send, this, _1, *i));
+
+        sendQueue_.clear();
+      }
+    else
+      {
+        // may need to throw exception
+        transport_.isConnected_ = false;
+        transport_.close();
+        throw Transport::Error(error, "error while connecting to the forwarder");
+      }
+  }
+
+  void
+  connectTimeoutHandler(const boost::system::error_code& error)
+  {
+    if (error) // e.g., cancelled timer
+      return;
+
+    connectionInProgress_ = false;
+    transport_.isConnected_ = false;
+    socket_.close();
+    throw Transport::Error(error, "error while connecting to the forwarder");
+  }
+
+  void
+  resolveHandler(const boost::system::error_code& error,
+                 boost::asio::ip::tcp::resolver::iterator endpoint,
+                 const ptr_lib::shared_ptr<boost::asio::ip::tcp::resolver>&)
+  {
+    if (error)
+      {
+        if (error == boost::system::errc::operation_canceled)
+          return;
+        
+        throw Transport::Error(error, "Error during resolution of host or port [" + transport_.host_ + ":" + transport_.port_ + "]");
+      }
+    
+    boost::asio::ip::tcp::resolver::iterator end;
+    if (endpoint == end)
+      {
+        connectionInProgress_ = false;
+        transport_.isConnected_ = false;
+        socket_.close();
+        throw Transport::Error(error, "Unable to connect because host or port [" + transport_.host_ + ":" + transport_.port_ + "] cannot be resolved");
+      }
+
+    socket_.async_connect(*endpoint,
+                          func_lib::bind(&Impl::connectHandler, this, _1));
+  }
+  
+  void
+  connect()
+  {
+    if (!connectionInProgress_) {
+      connectionInProgress_ = true;
+
+      // Wait at most 4 seconds to connect
+      /// @todo Decide whether this number should be configurable
+      connectTimer_.expires_from_now(boost::posix_time::seconds(4));
+      connectTimer_.async_wait(func_lib::bind(&Impl::connectTimeoutHandler, this, _1));
+      
+      using boost::asio::ip::tcp;
+      
+      ptr_lib::shared_ptr<tcp::resolver> resolver =
+        ptr_lib::make_shared<tcp::resolver>(boost::ref(*transport_.ioService_));
+
+      tcp::resolver::query query(transport_.host_, transport_.port_);
+
+      resolver->async_resolve(query, func_lib::bind(&Impl::resolveHandler, this, _1, _2, resolver));
+    }
+  }
+
+  void 
+  close()
+  {
+    connectTimer_.cancel();
+    socket_.close();
+    transport_.isConnected_ = false;
+  }
+
+  void 
+  send(const Block &wire)
+  {
+    if (!transport_.isConnected_)
+      sendQueue_.push_back(wire);
+    else
+      socket_.async_send(boost::asio::buffer(wire.wire(), wire.size()),
+                         func_lib::bind(&Impl::handle_async_send, this, _1, wire));
+  }
+
+  inline void
+  processAll(uint8_t *buffer, size_t &offset, size_t availableSize)
+  {
+    while(offset < availableSize)
+      {
+        Block element(buffer + offset, availableSize - offset);
+        transport_.receive(element);
+
+        offset += element.size();
+      }
+  }
+  
+  void
+  handle_async_receive(const boost::system::error_code& error, std::size_t bytes_recvd)
+  {
+    /// @todo The socket is not datagram, so need to have internal buffer to handle partial data reception
+
+    if (error)
+      {
+        if (error == boost::system::errc::operation_canceled) {
+          // async receive has been explicitly cancelled (e.g., socket close)
+          return;
+        }
+        
+        socket_.close(); // closing at this point may not be that necessary
+        transport_.isConnected_ = true;
+        throw Transport::Error(error, "error while receiving data from socket");
+      }
+    
+    if (!error && bytes_recvd > 0)
+      {
+        // inputBuffer_ has bytes_recvd received bytes of data
+        if (partialDataSize_ > 0)
+          {
+            size_t newDataSize = std::min(bytes_recvd, MAX_LENGTH-partialDataSize_);
+            ndn_memcpy(partialData_ + partialDataSize_, inputBuffer_, newDataSize);
+            partialDataSize_ += newDataSize;
+              
+            size_t offset = 0;
+            try
+              {
+                processAll(partialData_, offset, partialDataSize_);
+
+                // no exceptions => processed the whole thing
+                if (bytes_recvd - newDataSize > 0)
+                  {
+                    // there is a little bit more data available
+                        
+                    offset = 0;
+                    partialDataSize_ = bytes_recvd - newDataSize;
+                    ndn_memcpy(partialData_, inputBuffer_ + newDataSize, partialDataSize_);
+
+                    processAll(partialData_, offset, partialDataSize_);
+
+                    // no exceptions => processed the whole thing
+                    partialDataSize_ = 0;
+                  }
+                else
+                  {
+                    // done processing
+                    partialDataSize_ = 0;
+                  }
+              }
+            catch(Tlv::Error &)
+              {
+                if (offset > 0)
+                  {
+                    partialDataSize_ -= offset;
+                    ndn_memcpy(partialData_, partialData_ + offset, partialDataSize_);
+                  }
+                else if (offset == 0 && partialDataSize_ == MAX_LENGTH)
+                  {
+                    // very bad... should close connection
+                    socket_.close();
+                    transport_.isConnected_ = true;
+                    throw Transport::Error(boost::system::error_code(), "input buffer full, but a valid TLV cannot be decoded");
+                  }
+              }
+          }
+        else
+          {
+            size_t offset = 0;
+            try
+              {
+                processAll(inputBuffer_, offset, bytes_recvd);
+              }
+            catch(Tlv::Error &error)
+              {
+                if (offset > 0)
+                  {
+                    partialDataSize_ = bytes_recvd - offset;
+                    ndn_memcpy(partialData_, inputBuffer_ + offset, partialDataSize_);
+                  }
+              }
+          }
+      }
+
+    socket_.async_receive(boost::asio::buffer(inputBuffer_, MAX_LENGTH), 0,
+                          func_lib::bind(&Impl::handle_async_receive, this, _1, _2));
+  }
+
+  void
+  handle_async_send(const boost::system::error_code& error, const Block &wire)
+  {
+    // pass (needed to keep data block alive during the send)
+  }
+  
+private:
+  TcpTransport &transport_;
+  
+  protocol::socket socket_;
+  uint8_t inputBuffer_[MAX_LENGTH];
+
+  uint8_t partialData_[MAX_LENGTH];
+  size_t partialDataSize_;
+
+  std::list< Block > sendQueue_;
+  bool connectionInProgress_;
+
+  boost::asio::deadline_timer connectTimer_;
+};
+
+TcpTransport::TcpTransport(const std::string& host, const std::string& port/* = "6363"*/)
+  : host_(host)
+  , port_(port)
+{
+}
+
+TcpTransport::~TcpTransport()
+{
+}
+
+void 
+TcpTransport::connect(boost::asio::io_service &ioService,
+                      const ReceiveCallback &receiveCallback)
+{
+  if (!static_cast<bool>(impl_)) {
+    Transport::connect(ioService, receiveCallback);
+  
+    impl_ = ptr_lib::make_shared<TcpTransport::Impl> (ptr_lib::ref(*this));
+  }
+  impl_->connect();
+}
+
+void 
+TcpTransport::send(const Block &wire)
+{
+  impl_->send(wire);
+}
+
+void 
+TcpTransport::close()
+{
+  impl_->close();
+}
+
+}
diff --git a/tests_boost/Makefile.am b/tests_boost/Makefile.am
index 8968a60..2e80613 100644
--- a/tests_boost/Makefile.am
+++ b/tests_boost/Makefile.am
@@ -9,6 +9,7 @@
   test-encode-decode-interest.cpp \
   test-encode-decode-forwarding-entry.cpp \
   test-encode-decode-block.cpp \
-  test-sec-tpm-file.cpp
+  test-sec-tpm-file.cpp \
+  test-faces.cpp
 
 unit_tests_LDADD = ../libndn-cpp-dev.la @BOOST_SYSTEM_LIB@ @BOOST_UNIT_TEST_FRAMEWORK_LIB@ @OPENSSL_LIBS@ @CRYPTOPP_LIBS@ @OSX_SECURITY_LIBS@
diff --git a/tests_boost/test-faces.cpp b/tests_boost/test-faces.cpp
new file mode 100644
index 0000000..f43c71e
--- /dev/null
+++ b/tests_boost/test-faces.cpp
@@ -0,0 +1,95 @@
+/**
+ * Copyright (C) 2013 Regents of the University of California.
+ * @author: Jeff Thompson <jefft0@remap.ucla.edu>
+ * See COPYING for copyright and distribution information.
+ */
+
+#include <boost/test/unit_test.hpp>
+#include <ndn-cpp-dev/face.hpp>
+
+using namespace std;
+using namespace ndn;
+
+BOOST_AUTO_TEST_SUITE(TestFaces)
+
+struct FacesFixture
+{
+  FacesFixture()
+    : dataCount(0)
+    , timeoutCount(0)
+  {
+  }
+  
+  void
+  onData()
+  {
+    ++dataCount;
+  }
+
+  void
+  onTimeout()
+  {
+    ++timeoutCount;
+  }
+
+  void
+  onInterest()
+  {
+  }
+
+  void
+  onRegFailed()
+  {
+  }
+
+  uint32_t dataCount;
+  uint32_t timeoutCount;
+};
+
+BOOST_FIXTURE_TEST_CASE (Unix, FacesFixture)
+{
+  Face face;
+
+  face.expressInterest(Interest("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY", 1000),
+                       ptr_lib::bind(&FacesFixture::onData, this),
+                       ptr_lib::bind(&FacesFixture::onTimeout, this));
+
+  BOOST_REQUIRE_NO_THROW(face.processEvents());
+
+  BOOST_CHECK_EQUAL(dataCount, 1);
+  BOOST_CHECK_EQUAL(timeoutCount, 0);
+
+  face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere", 50),
+                       ptr_lib::bind(&FacesFixture::onData, this),
+                       ptr_lib::bind(&FacesFixture::onTimeout, this));
+  
+  BOOST_REQUIRE_NO_THROW(face.processEvents());
+
+  BOOST_CHECK_EQUAL(dataCount, 1);
+  BOOST_CHECK_EQUAL(timeoutCount, 1);
+}
+
+BOOST_FIXTURE_TEST_CASE (Tcp, FacesFixture)
+{
+  Face face("localhost");
+
+  face.expressInterest(Interest("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY", 1000),
+                       ptr_lib::bind(&FacesFixture::onData, this),
+                       ptr_lib::bind(&FacesFixture::onTimeout, this));
+
+  BOOST_REQUIRE_NO_THROW(face.processEvents());
+
+  BOOST_CHECK_EQUAL(dataCount, 1);
+  BOOST_CHECK_EQUAL(timeoutCount, 0);
+
+  face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere", 50),
+                       ptr_lib::bind(&FacesFixture::onData, this),
+                       ptr_lib::bind(&FacesFixture::onTimeout, this));
+
+  BOOST_REQUIRE_NO_THROW(face.processEvents());
+
+  BOOST_CHECK_EQUAL(dataCount, 1);
+  BOOST_CHECK_EQUAL(timeoutCount, 1);
+}
+
+BOOST_AUTO_TEST_SUITE_END()