face: When there are no more events to process, pausing transport instead of closing it

Change-Id: I12ad55c6ef3b6b3fce4a5b3c679eb0c6dc3cfa07
Refs: #1314
diff --git a/src/transport/stream-transport.hpp b/src/transport/stream-transport.hpp
index f98b988..2ab4752 100644
--- a/src/transport/stream-transport.hpp
+++ b/src/transport/stream-transport.hpp
@@ -38,10 +38,7 @@
 
     if (!error)
       {
-        m_partialDataSize = 0;
-        m_socket.async_receive(boost::asio::buffer(m_inputBuffer, MAX_LENGTH), 0,
-                               bind(&impl::handle_async_receive, this, _1, _2));
-
+        resume();
         m_transport.m_isConnected = true;
 
         for (std::list<Block>::iterator i = m_sendQueue.begin(); i != m_sendQueue.end(); ++i)
@@ -111,6 +108,28 @@
   }
 
   void
+  pause()
+  {
+    if (m_transport.m_isExpectingData)
+      {
+        m_transport.m_isExpectingData = false;
+        m_socket.cancel();
+      }
+  }
+
+  void
+  resume()
+  {
+    if (!m_transport.m_isExpectingData)
+      {
+        m_transport.m_isExpectingData = true;
+        m_partialDataSize = 0;
+        m_socket.async_receive(boost::asio::buffer(m_inputBuffer, MAX_LENGTH), 0,
+                               bind(&impl::handle_async_receive, this, _1, _2));
+      }
+  }
+
+  void
   send(const Block& wire)
   {
     if (!m_transport.m_isConnected)
diff --git a/src/transport/tcp-transport.cpp b/src/transport/tcp-transport.cpp
index 24ec85c..b0935a4 100644
--- a/src/transport/tcp-transport.cpp
+++ b/src/transport/tcp-transport.cpp
@@ -21,13 +21,13 @@
 {
 }
 
-void 
+void
 TcpTransport::connect(boost::asio::io_service& ioService,
                       const ReceiveCallback& receiveCallback)
 {
   if (!static_cast<bool>(m_impl)) {
     Transport::connect(ioService, receiveCallback);
-  
+
     m_impl = make_shared<Impl> (boost::ref(*this),
                                 boost::ref(ioService));
   }
@@ -36,7 +36,7 @@
   m_impl->connect(query);
 }
 
-void 
+void
 TcpTransport::send(const Block& wire)
 {
   m_impl->send(wire);
@@ -48,10 +48,22 @@
   m_impl->send(header, payload);
 }
 
-void 
+void
 TcpTransport::close()
 {
   m_impl->close();
 }
 
+void
+TcpTransport::pause()
+{
+  m_impl->pause();
+}
+
+void
+TcpTransport::resume()
+{
+  m_impl->resume();
+}
+
 } // namespace ndn
diff --git a/src/transport/tcp-transport.hpp b/src/transport/tcp-transport.hpp
index b97e2a1..6795580 100644
--- a/src/transport/tcp-transport.hpp
+++ b/src/transport/tcp-transport.hpp
@@ -33,6 +33,12 @@
   virtual void 
   close();
 
+  virtual void
+  pause();
+
+  virtual void
+  resume();
+
   virtual void 
   send(const Block& wire);
 
diff --git a/src/transport/transport.hpp b/src/transport/transport.hpp
index c309288..da06da4 100644
--- a/src/transport/transport.hpp
+++ b/src/transport/transport.hpp
@@ -38,7 +38,7 @@
    * Close the connection.
    */
   virtual void 
-  close() =0;
+  close() = 0;
 
   /**
    * @brief Set data to the host
@@ -47,7 +47,7 @@
    * @param dataLength The number of bytes in data.
    */
   virtual void 
-  send(const Block& wire) =0;
+  send(const Block& wire) = 0;
 
   /**
    * @brief Alternative version of sending data, applying scatter/gather I/O concept
@@ -56,11 +56,20 @@
    * same message in datagram-oriented transports.
    */
   virtual void 
-  send(const Block& header, const Block& payload) =0;
+  send(const Block& header, const Block& payload) = 0;
+
+  virtual void
+  pause() = 0;
+
+  virtual void
+  resume() = 0;
   
   inline bool 
   isConnected();
 
+  inline bool
+  isExpectingData();
+
 protected:
   inline void
   receive(const Block& wire);
@@ -68,6 +77,7 @@
 protected:
   boost::asio::io_service* m_ioService;
   bool m_isConnected;
+  bool m_isExpectingData;
   ReceiveCallback m_receiveCallback;
 };
 
@@ -75,6 +85,7 @@
 Transport::Transport()
   : m_ioService(0)
   , m_isConnected(false)
+  , m_isExpectingData(false)
 {
 }
 
@@ -102,6 +113,12 @@
   return m_isConnected;
 }
 
+inline bool
+Transport::isExpectingData()
+{
+  return m_isExpectingData;
+}
+
 inline void
 Transport::receive(const Block& wire)
 {
diff --git a/src/transport/unix-transport.cpp b/src/transport/unix-transport.cpp
index f1e26ec..33fad1c 100644
--- a/src/transport/unix-transport.cpp
+++ b/src/transport/unix-transport.cpp
@@ -62,4 +62,16 @@
   m_impl->close();
 }
 
+void
+UnixTransport::pause()
+{
+  m_impl->pause();
+}
+
+void
+UnixTransport::resume()
+{
+  m_impl->resume();
+}
+
 }
diff --git a/src/transport/unix-transport.hpp b/src/transport/unix-transport.hpp
index 4a89bc8..f8114f5 100644
--- a/src/transport/unix-transport.hpp
+++ b/src/transport/unix-transport.hpp
@@ -36,6 +36,12 @@
   close();
 
   virtual void
+  pause();
+
+  virtual void
+  resume();
+
+  virtual void
   send(const Block& wire);
 
   virtual void